Important Update:
- Update Command port: - Simplified error handling - Provide error reply - Added method GetNextParamGroup()
This commit is contained in:
235
DeviceCore.cpp
235
DeviceCore.cpp
@@ -1404,8 +1404,10 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int
|
|||||||
bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len )
|
bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len )
|
||||||
{
|
{
|
||||||
// Validate
|
// Validate
|
||||||
if (!Param || !Value)
|
if (!Param || !Value) {
|
||||||
|
Value[0] = 0;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if return value longer than actual value
|
// Check if return value longer than actual value
|
||||||
switch (Param->DataType)
|
switch (Param->DataType)
|
||||||
@@ -1448,6 +1450,7 @@ bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len )
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default :
|
default :
|
||||||
|
Value[0] = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -1512,7 +1515,12 @@ bool CDeviceCore::GetCmdParam( const char * Start, char * Param, char ** NextPar
|
|||||||
int ParamLen;
|
int ParamLen;
|
||||||
|
|
||||||
// Get length of param
|
// Get length of param
|
||||||
if ((NextDelimeter = strchr( (char*)Start, ',' ))) {
|
if (!Start || !*Start) {
|
||||||
|
Param[0] = 0;
|
||||||
|
NextParam = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if ((NextDelimeter = strchr( (char*)Start, ',' ))) {
|
||||||
ParamLen = NextDelimeter - Start;
|
ParamLen = NextDelimeter - Start;
|
||||||
*NextParam = NextDelimeter + 1;
|
*NextParam = NextDelimeter + 1;
|
||||||
}
|
}
|
||||||
@@ -1537,162 +1545,147 @@ bool CDeviceCore::GetCmdParam( const char * Start, char * Param, char ** NextPar
|
|||||||
int CDeviceCore::HandleCommand( const char *ChannelName, const char * Data, const int MaxLen )
|
int CDeviceCore::HandleCommand( const char *ChannelName, const char * Data, const int MaxLen )
|
||||||
{
|
{
|
||||||
int Len;
|
int Len;
|
||||||
char ParamName[50];
|
char Value[50];
|
||||||
char * NextParam = NULL;
|
char * NextParam = NULL;
|
||||||
TDevice * Device = NULL;
|
TDevice * Device = NULL;
|
||||||
TDeviceParam * Param = NULL;
|
TDeviceParam * Param = NULL;
|
||||||
char OutputStr[250];
|
char OutputStr[250];
|
||||||
|
bool Error = false;
|
||||||
|
|
||||||
// Show accepted input
|
// Show accepted input
|
||||||
Log->Output( LogLevel, dlHigh, loNormal, Data, MaxLen, "%s/%s: Channel '%s' - IN:",
|
Log->Output( LogLevel, dlHigh, loNormal, Data, MaxLen, "%s/%s: Channel '%s' - IN:",
|
||||||
ProcessName, Name, ChannelName );
|
ProcessName, Name, ChannelName );
|
||||||
|
|
||||||
// Get command command
|
// Get command command
|
||||||
GetCmdParam( Data, ParamName, &NextParam );
|
GetCmdParam( Data, Value, &NextParam );
|
||||||
if (!strcasecmp( "get", ParamName ))
|
if (!strcasecmp( "get", Value ))
|
||||||
{
|
{
|
||||||
// Check for additional parameters
|
// Validate parameters
|
||||||
if (!NextParam) {
|
if (!GetCmdParam( NextParam, Value, &NextParam)) {
|
||||||
// No Parameters
|
strcpy( OutputStr, "- Missing device name" );
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Missing 'get' parameters",
|
Error = true;
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!(Device = GetDeviceByName( Value ))) {
|
||||||
// Get device name
|
strcpy( OutputStr, "- Device not found");
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (!(Device = GetDeviceByName( ParamName ))) {
|
|
||||||
// Unknown Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unknown 'get' Device: '%s'",
|
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!GetCmdParam( NextParam, Value, &NextParam )) {
|
||||||
// Get parameter name
|
strcpy( OutputStr, "- Missing parameter name" );
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (!(Param = GetDeviceParam( Device, ParamName ))) {
|
|
||||||
// Unknown Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unknown 'get' Param: '%s'",
|
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!(Param = GetDeviceParam( Device, Value ))) {
|
||||||
// Check for additional parameters
|
strcpy( OutputStr, "- Device parameter not found");
|
||||||
if (NextParam) {
|
Error = true;
|
||||||
// Unused Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unnecessary 'get' parameters",
|
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (NextParam) {
|
||||||
|
strcpy( OutputStr, "- Too many parameters");
|
||||||
|
Error = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sprintf( OutputStr, "> %s,%s = ", Device->Name, Param->Name );
|
||||||
|
Len = MaxLen - strlen(OutputStr) - 1;
|
||||||
|
|
||||||
// Build reply
|
if (!GetValue( (TDeviceParam*)Param, &OutputStr[ strlen(OutputStr) ], Len )) {
|
||||||
sprintf( OutputStr, "get,%s,", Param->Name );
|
strcpy( OutputStr, "- Error getting parameter");
|
||||||
Len = MaxLen - strlen(OutputStr);
|
Error = true;
|
||||||
|
}
|
||||||
// Insert value
|
else {
|
||||||
if (!GetValue( (TDeviceParam*)Param, &OutputStr[ strlen(OutputStr) ], Len ))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Send reply
|
|
||||||
strcat( OutputStr, "\n" );
|
strcat( OutputStr, "\n" );
|
||||||
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else if (!strcasecmp( "set", ParamName ))
|
}
|
||||||
|
|
||||||
|
// Report error
|
||||||
|
if (Error) {
|
||||||
|
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' Get",
|
||||||
|
ProcessName, Name, ChannelName, OutputStr );
|
||||||
|
strcat( OutputStr, "\n" );
|
||||||
|
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!strcasecmp( "set", Value ))
|
||||||
{
|
{
|
||||||
// Check for additional parameters
|
// Validate parameters
|
||||||
if (!NextParam) {
|
if (!GetCmdParam( NextParam, Value, &NextParam)) {
|
||||||
// No Parameters
|
strcpy( OutputStr, "- Missing device name" );
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Missing 'set' parameters",
|
Error = true;
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!(Device = GetDeviceByName( Value ))) {
|
||||||
// Get Device name
|
strcpy( OutputStr, "- Device not found");
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (!(Device = GetDeviceByName( ParamName ))) {
|
|
||||||
// Unknown Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unknown 'set' Device: '%s'",
|
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!GetCmdParam( NextParam, Value, &NextParam )) {
|
||||||
// Get parameter name
|
strcpy( OutputStr, "- Missing parameter name" );
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (!(Param = GetDeviceParam( Device, ParamName ))) {
|
|
||||||
// Unknown Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unknown 'set' Param: '%s'",
|
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!(Param = GetDeviceParam( Device, Value ))) {
|
||||||
// Get parameter value
|
strcpy( OutputStr, "- Device parameter not found");
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (strlen(ParamName) == 0) {
|
|
||||||
// No Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - No 'set' value parameter",
|
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!GetCmdParam( NextParam, Value, &NextParam )) {
|
||||||
// Check for additional parameters
|
strcpy( OutputStr, "- Missing set value");
|
||||||
if (NextParam) {
|
Error = true;
|
||||||
// Unused Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unnecessary 'set' parameters",
|
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (NextParam) {
|
||||||
// Set value
|
strcpy( OutputStr, "- Too many parameters");
|
||||||
Len = MaxLen;
|
Error = true;
|
||||||
if (!SetValue( Param, ParamName, Len, true ))
|
}
|
||||||
return false;
|
else {
|
||||||
|
if (!SetValue( Param, Value, strlen(Value), true )) {
|
||||||
// Build & send reply
|
strcpy( OutputStr, "- Error seting parameter");
|
||||||
sprintf( OutputStr, "set,%s,%s\n", Param->Name, ParamName );
|
Error = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sprintf( OutputStr, "> set %s,%s = %s\n", Device->Name, Param->Name, Value );
|
||||||
Output( ChannelName, OutputStr );
|
Output( ChannelName, OutputStr );
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else if (!strcasecmp( "status", ParamName ))
|
}
|
||||||
|
|
||||||
|
// Report error
|
||||||
|
if (Error) {
|
||||||
|
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' Set",
|
||||||
|
ProcessName, Name, ChannelName, OutputStr );
|
||||||
|
strcat( OutputStr, "\n" );
|
||||||
|
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!strcasecmp( "status", Value ))
|
||||||
{
|
{
|
||||||
// Check for additional parameters
|
// Validate parameters
|
||||||
if (!NextParam) {
|
if (!GetCmdParam( NextParam, Value, &NextParam)) {
|
||||||
// No Parameters
|
strcpy( OutputStr, "- Missing device name" );
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Missing 'status' parameters",
|
Error = true;
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (!(Device = GetDeviceByName( Value ))) {
|
||||||
// Get Device name
|
strcpy( OutputStr, "- Device not found");
|
||||||
GetCmdParam( NextParam, ParamName, &NextParam );
|
Error = true;
|
||||||
if (!(Device = GetDeviceByName( ParamName ))) {
|
|
||||||
// Unknown Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unknown 'status' Device: '%s'",
|
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else if (NextParam) {
|
||||||
// Check for additional parameters
|
strcpy( OutputStr, "- Too many parameters");
|
||||||
if (NextParam) {
|
Error = true;
|
||||||
// Unused Parameters
|
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unnecessary 'status' parameters",
|
|
||||||
ProcessName, Name, ChannelName );
|
|
||||||
return MaxLen;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
// Build & send reply
|
sprintf( OutputStr, "> %s - %s\n", Device->Name, ((Device->Online)? "online" : "offline") );
|
||||||
sprintf( OutputStr, "status,%s,%d\n", Device->Name, Device->Online );
|
|
||||||
Output( ChannelName, OutputStr );
|
Output( ChannelName, OutputStr );
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
// Report error
|
||||||
|
if (Error) {
|
||||||
|
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' Status",
|
||||||
|
ProcessName, Name, ChannelName, OutputStr );
|
||||||
|
strcat( OutputStr, "\n" );
|
||||||
|
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Unrecognised command
|
// Unrecognised command
|
||||||
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s' - Unrecognized command: '%s'",
|
strcpy( OutputStr, "- Unknown command");
|
||||||
ProcessName, Name, ChannelName, ParamName );
|
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s', %s%s",
|
||||||
return MaxLen;
|
ProcessName, Name, ChannelName, Value, OutputStr );
|
||||||
|
strcat( OutputStr, " (comma separated values)\n" );
|
||||||
|
Output( ChannelName, OutputStr, strlen(OutputStr) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return MaxLen;
|
return MaxLen;
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -231,6 +231,11 @@ protected:
|
|||||||
Param = &((*Param)->Next);
|
Param = &((*Param)->Next);
|
||||||
return Param;
|
return Param;
|
||||||
}
|
}
|
||||||
|
inline TDeviceParamGroup * GetNextParamGroup( TDevice * Device, TDeviceParamGroup * LastParamGroup = NULL ) {
|
||||||
|
if (!Device) return NULL;
|
||||||
|
TDeviceParamGroup * ParamGroup = (LastParamGroup)? LastParamGroup->NextGroup : Device->FirstParamGroup;
|
||||||
|
return ParamGroup;
|
||||||
|
}
|
||||||
inline TDeviceParam * GetNextReadParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
inline TDeviceParam * GetNextReadParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
||||||
if (!Device) return NULL;
|
if (!Device) return NULL;
|
||||||
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
||||||
|
|||||||
Reference in New Issue
Block a user