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