Minor update: code compacting

This commit is contained in:
2022-07-22 11:56:19 +02:00
parent fd3738567a
commit 7dfdfdb4ce
10 changed files with 129 additions and 248 deletions

View File

@@ -141,8 +141,7 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig )
// Load Device Types
DeviceConfig = Config->GetChFirstChild( "DeviceTypes", true );
while (DeviceConfig)
{
while (DeviceConfig) {
DeviceType = (char*)DeviceConfig->GetName();
if (!DeviceConfig->isObject()) {
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Invalid device type config for '%s'",
@@ -177,8 +176,7 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig )
// Load Actual Devices
DeviceConfig = Config->GetChFirstChild( "Devices", true );
while (DeviceConfig)
{
while (DeviceConfig) {
DeviceName = (char*)DeviceConfig->GetName();
if (!DeviceConfig->isObject()) {
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Invalid device config for '%s'",
@@ -193,9 +191,8 @@ bool CDeviceCore::InitDevices( CDataMember * FunctionConfig )
DataPath = (char*)DeviceConfig->GetChStr( "DataPath", NULL );
Device = AddDevice( DeviceName, DeviceType, DeviceID, DeviceAddr, DataPath );
if (Device) {
if (Device)
InitDeviceParams( Device, DeviceConfig, Device->DataNode );
}
DeviceConfig = DeviceConfig->GetNextPeer();
}
return true;
@@ -222,7 +219,6 @@ bool CDeviceCore::InitDeviceParams( TDevice * Device, CDataMember * DeviceConfi
InitDeviceParam( Device, ParamConfig, NULL, ((isType)? NULL : Device->DataPath), ParentNode, NULL );
ParamConfig = ParamConfig->GetNextPeer();
}
return true;
}
//---------------------------------------------------------------------------
@@ -233,8 +229,7 @@ bool CDeviceCore::CopyTemplateParam( TDevice * Device, TDeviceParam * Template,
CDataMember * DataNode = NULL;
char * DataPath = NULL;
if ((Param = AddDeviceParam( Device, Template->Name, Template->DataType, Template->Len )))
{
if ((Param = AddDeviceParam( Device, Template->Name, Template->DataType, Template->Len ))) {
SetParamAccess( Param, Template->Read, Template->Write );
if (Template->DataPath) {
@@ -332,7 +327,8 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig,
if (ParentName && *ParentName) {
FullName = (char*)malloc( strlen(ParentName) + strlen(NodeName) + 2 );
sprintf( FullName, "%s_%s", ParentName, NodeName );
} else {
}
else {
FullName = strdup( NodeName );
}
@@ -341,17 +337,17 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig,
if (ParentPath && *ParentPath) {
DataPath = (char*)malloc( strlen(ParentPath) + strlen(NodeName) + 2 );
sprintf( DataPath, "%s/%s", ParentPath, NodeName );
} else {
}
else {
DataPath = strdup( NodeName );
}
if ((Children = ParamConfig->GetChild( "Children", false ))) {
// Data Branch
if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) {
if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false )))
ParamGroup = AddParamGroup( Device, ParamGroupName );
} else {
else
ParamGroup = ParentParamGroup;
}
if (Children->isString()) {
// Load from file
@@ -414,11 +410,11 @@ bool CDeviceCore::InitDeviceParam( TDevice * Device, CDataMember * ParamConfig,
}
}
if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false ))) {
if ((ParamGroupName = (char*)ParamConfig->GetChStr( "ParamGroup", NULL, false )))
ParamGroup = AddParamGroup( Device, ParamGroupName );
} else {
else
ParamGroup = ParentParamGroup;
}
if (ParamGroup)
AddParamItem( ParamGroup, Param );
@@ -492,10 +488,8 @@ void CDeviceCore::ValidReplyReceived()
bool CDeviceCore::CheckReplyTimeout()
{
// Check for Reply timeout
if (!WaitingForReply || !Timeout( PollWait, ReplyTimeout )) {
// No timeout
if (!WaitingForReply || !Timeout( PollWait, ReplyTimeout ))
return false;
}
// Update flags
WaitingForReply = false;
@@ -503,8 +497,7 @@ bool CDeviceCore::CheckReplyTimeout()
PollRetry++;
// Handle No Reply / Retry
if (PollRetry > MaxRetries)
{
if (PollRetry > MaxRetries) {
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s' - Device '%s' %s - Max retries",
ProcessName, Name, DeviceChannel->Name, ActiveDevice->Name,
((InvalidReply)? "invalid reply" : "timeout"), PollRetry );
@@ -587,9 +580,8 @@ TDevice * CDeviceCore::AddDevice( const char * DeviceName, const char * DeviceTy
(*Device)->Name = strdup( DeviceName );
(*Device)->ID = DeviceID;
if (DeviceAddress) {
if (DeviceAddress)
(*Device)->Address = strdup( DeviceAddress );
}
if (DeviceType && *DeviceType) {
(*Device)->Type = strdup( DeviceType );
@@ -638,12 +630,10 @@ bool CDeviceCore::DestroyDevice( TDevice ** Device )
free( (*Device)->DataPath );
// Destroy parameters & groups
while ((*Device)->FirstParam) {
while ((*Device)->FirstParam)
DestroyDeviceParam( &((*Device)->FirstParam) );
}
while ((*Device)->FirstParamGroup) {
while ((*Device)->FirstParamGroup)
DestroyParamGroup( &((*Device)->FirstParamGroup) );
}
// Destroy Device
delete *Device;
@@ -684,8 +674,7 @@ TDeviceParam * CDeviceCore::AddDeviceParam( TDevice * Device, const char * Param
(*Param)->Device = Device;
// Init values
switch (DataType)
{
switch (DataType) {
case dtBool :
// Create Value pointer
(*Param)->Value = (bool*)malloc( sizeof(bool) );
@@ -861,9 +850,8 @@ bool CDeviceCore::DestroyParamGroup( TDeviceParamGroup ** ParamGroup )
free( (*ParamGroup)->Name );
// Destroy items
while ((*ParamGroup)->FirstParam) {
while ((*ParamGroup)->FirstParam)
DestroyParamItem( &((*ParamGroup)->FirstParam) );
}
// Destroy ParamGroup
delete *ParamGroup;
@@ -986,11 +974,9 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
if (Init || (*((bool*)Param->Value) != Value))
{
if (Init || (*((bool*)Param->Value) != Value)) {
// Set new value & mark change
*((bool*)Param->Value) = Value;
@@ -1008,8 +994,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val
break;
case dtUnsigned16 :
if (Init || (*((u_int16_t*)Param->Value) != Value))
{
if (Init || (*((u_int16_t*)Param->Value) != Value)) {
// Set new value & mark change
*((u_int16_t*)Param->Value) = Value;
@@ -1028,8 +1013,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val
case dtUnsigned32_HL :
case dtUnsigned32_LH :
if (Init || (*((u_int32_t*)Param->Value) != Value))
{
if (Init || (*((u_int32_t*)Param->Value) != Value)) {
// Set new value & mark change
*((u_int32_t*)Param->Value) = Value;
@@ -1049,8 +1033,7 @@ bool CDeviceCore::UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Val
case dtFloat32_L :
case dtFloat32_B :
// Special case, where float value is stored in Integer memory (e.g. modbus register)
if (Init || (*((u_int32_t*)Param->Value) != Value))
{
if (Init || (*((u_int32_t*)Param->Value) != Value)) {
// Set new value & mark change
*((u_int32_t*)Param->Value) = Value;
@@ -1088,11 +1071,9 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value,
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
if (Init || (*((bool*)Param->Value) != Value))
{
if (Init || (*((bool*)Param->Value) != Value)) {
// Set new value & mark change
*((bool*)Param->Value) = Value;
@@ -1110,8 +1091,7 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value,
break;
case dtSigned16 :
if (Init || (*((int16_t*)Param->Value) != Value))
{
if (Init || (*((int16_t*)Param->Value) != Value)) {
// Set new value & mark change
*((int16_t*)Param->Value) = Value;
@@ -1130,8 +1110,7 @@ bool CDeviceCore::UpdateSignedValue( TDeviceParam * Param, const int32_t Value,
case dtSigned32_HL :
case dtSigned32_LH :
if (Init || (*((int32_t*)Param->Value) != Value))
{
if (Init || (*((int32_t*)Param->Value) != Value)) {
// Set new value & mark change
*((int32_t*)Param->Value) = Value;
@@ -1169,12 +1148,10 @@ bool CDeviceCore::UpdateFloatValue( TDeviceParam * Param, const float Value, boo
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtFloat32_L :
case dtFloat32_B :
if (Init || (*((float*)Param->Value) != Value))
{
if (Init || (*((float*)Param->Value) != Value)) {
// Set new value & mark change
*((float*)Param->Value) = Value;
@@ -1212,12 +1189,10 @@ bool CDeviceCore::UpdateStringValue( TDeviceParam * Param, const char * Value, c
if (!Param || (Param->DataType != dtString))
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtString :
// Update register
if (Init || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len))
{
if (Init || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) {
if (Len >= Param->Len) {
// Copy full register length (ignore additional bytes)
memcpy( Param->Value, Value, Param->Len );
@@ -1262,11 +1237,9 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value,
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
if (Force || (*((bool*)Param->SetValue) != Value))
{
if (Force || (*((bool*)Param->SetValue) != Value)) {
// Set new value
*((bool*)Param->SetValue) = Value;
@@ -1276,8 +1249,7 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value,
break;
case dtUnsigned16 :
if (Force || (*((u_int16_t*)Param->SetValue) != Value))
{
if (Force || (*((u_int16_t*)Param->SetValue) != Value)) {
// Set new value
*((u_int16_t*)Param->SetValue) = Value;
@@ -1288,8 +1260,7 @@ bool CDeviceCore::SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value,
case dtUnsigned32_HL :
case dtUnsigned32_LH :
if (Force || (*((u_int32_t*)Param->SetValue) != Value))
{
if (Force || (*((u_int32_t*)Param->SetValue) != Value)) {
// Set new value
*((u_int32_t*)Param->SetValue) = Value;
@@ -1313,11 +1284,9 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
if (Force || (*((bool*)Param->SetValue) != Value))
{
if (Force || (*((bool*)Param->SetValue) != Value)) {
// Set new value
*((bool*)Param->SetValue) = Value;
@@ -1327,8 +1296,7 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo
break;
case dtSigned16 :
if (Force || (*((int16_t*)Param->SetValue) != Value))
{
if (Force || (*((int16_t*)Param->SetValue) != Value)) {
// Set new value
*((int16_t*)Param->SetValue) = Value;
@@ -1339,8 +1307,7 @@ bool CDeviceCore::SetSignedValue( TDeviceParam * Param, const int32_t Value, boo
case dtSigned32_HL :
case dtSigned32_LH :
if (Force || (*((int32_t*)Param->SetValue) != Value))
{
if (Force || (*((int32_t*)Param->SetValue) != Value)) {
// Set new value
*((int32_t*)Param->SetValue) = Value;
@@ -1364,12 +1331,10 @@ bool CDeviceCore::SetFloatValue( TDeviceParam * Param, const float Value, bool F
if (!Param)
return false;
switch (Param->DataType)
{
switch (Param->DataType) {
case dtFloat32_L :
case dtFloat32_B :
if (Force || (*((float*)Param->SetValue) != Value))
{
if (Force || (*((float*)Param->SetValue) != Value)) {
// Set new value
*((float*)Param->SetValue) = Value;
@@ -1394,8 +1359,7 @@ bool CDeviceCore::SetStringValue( TDeviceParam * Param, const char * Value, cons
return false;
// Update register
if (Force || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len))
{
if (Force || !CompareParamString( (char*)Param->Value, Param->Len, Value, Len)) {
if (Len >= Param->SetLen) {
// Copy full register length (ignore additional bytes)
memcpy( Param->SetValue, Value, Param->SetLen );
@@ -1429,13 +1393,11 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int
return false;
// Check if string
if (Param->DataType == dtString)
{
if (Param->DataType == dtString) {
// Simply set string
SetStringValue( Param, Value, Len, Force );
}
else
{
else {
// Ensure string is zero terminated
if (Value[Len] != 0) {
TempStr = strndup( Value, Len );
@@ -1444,8 +1406,7 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int
}
// Convert to correct type
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
case dtUnsigned16 :
case dtUnsigned32_HL :
@@ -1472,9 +1433,8 @@ bool CDeviceCore::SetValue( TDeviceParam * Param, const char * Value, const int
}
// Clear memory
if (UseTempStr) {
if (UseTempStr)
free( TempStr );
}
}
return true;
}
@@ -1489,8 +1449,7 @@ bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len )
}
// Check if return value longer than actual value
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool :
sprintf( Value, "%u", (*((bool*)Param->Value)) );
break;
@@ -1519,14 +1478,12 @@ bool CDeviceCore::GetValue( TDeviceParam * Param, char * Value, int &Len )
break;
case dtString :
if (Len < Param->Len)
{
if (Len < Param->Len) {
// Only copy requested no of chars
memcpy( Value, Param->Value, Len );
Value[ Len ] = 0;
}
else
{
else {
memcpy( Value, Param->Value, Param->Len );
Value[ Param->Len ] = 0;
}
@@ -1548,15 +1505,14 @@ bool CDeviceCore::CompareParamString( const char * ParamValue, const int ParamLe
// Compare Value against current value
if (Len >= ParamLen) {
// Compare full register length (ignore additional bytes)
if (memcmp( Value, ParamValue, ParamLen)) {
if (memcmp( Value, ParamValue, ParamLen))
Match = false;
}
}
else {
// Compare length of new value only
if (memcmp( ParamValue, Value, Len)) {
if (memcmp( ParamValue, Value, Len))
Match = false;
}
// Remaining bytes must be null
else {
for (Pos = (char*)ParamValue; Pos < (char*)ParamValue + ParamLen; Pos++) {
@@ -1580,12 +1536,10 @@ bool CDeviceCore::TimedParamEvents()
// Loop through all devices
Device = FirstDevice;
while (Device)
{
while (Device) {
// Loop through all Read parameters
while ((Param = GetNextReadParam( Device, Param ))) {
while ((Param = GetNextReadParam( Device, Param )))
EventOutput( Param, false );
}
Device = Device->Next;
}
return true;
@@ -1641,8 +1595,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef,
// Get command command
GetCmdParam( Data, Value, &NextParam );
if (!strcasecmp( "get", Value ))
{
if (!strcasecmp( "get", Value )) {
// Validate parameters
if (!GetCmdParam( NextParam, Value, &NextParam)) {
strcpy( OutputStr, "- Missing device name" );
@@ -1686,8 +1639,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef,
Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) );
}
}
else if (!strcasecmp( "set", Value ))
{
else if (!strcasecmp( "set", Value )) {
// Validate parameters
if (!GetCmdParam( NextParam, Value, &NextParam)) {
strcpy( OutputStr, "- Missing device name" );
@@ -1732,8 +1684,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef,
Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) );
}
}
else if (!strcasecmp( "status", Value ))
{
else if (!strcasecmp( "status", Value )) {
// Validate parameters
if (!GetCmdParam( NextParam, Value, &NextParam)) {
strcpy( OutputStr, "- Missing device name" );
@@ -1760,8 +1711,7 @@ int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef,
Output( ChannelName, NULL, true, OutputStr, strlen(OutputStr) );
}
}
else
{
else {
// Unrecognised command
strcpy( OutputStr, "- Unknown command");
Log->Message( LogLevel, dlMedium, "%s/%s: Channel '%s', %s%s",
@@ -1786,10 +1736,7 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force )
return false;
// Check Timer or force
if (Force ||
(Param->EventInterval && Timeout( Param->EventTimeout, Param->EventInterval )) )
{
// Create message
if (Force || (Param->EventInterval && Timeout( Param->EventTimeout, Param->EventInterval ))) {
// Post event
if (true) {
// JSON output
@@ -1851,8 +1798,7 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force )
JSONparse->WriteToString( NULL, EventMsg, EventLen, 0 );
}
else {
switch (Param->DataType)
{
switch (Param->DataType) {
case dtBool : sprintf( EventMsg, "%s: %u\n", Param->DataPath, *((bool*)Param->Value) );
break;
@@ -1886,9 +1832,8 @@ bool CDeviceCore::EventOutput( TDeviceParam * Param, bool Force )
Output( Param->EventChannel, NULL, true, EventMsg, strlen(EventMsg) );
// Reset timer
if (Param->EventInterval) {
if (Param->EventInterval)
SetStartTime( &(Param->EventTimeout) );
}
}
return true;
}