Important Update:
- DataTreeCore:
- Rename enum EDataType -> EJsonDataType
- DeviceCore:
- Rename enum EMBDataType -> EDeviceDataType
- Add method GetNextTypeDevice() method
- Bug fix: Add Type Name: 0 -> "none"
- Added permanent Event Channel to TDevice
- Add param Type to TDevice
- Removed UpdateInterval/Timeout params & methods from TDevice
- Separated DeviceInit() method from Init()
- Add param DeviceInit to force/avoid DeviceInit() in Init()
- Renamed and restructured SetUpdate() & SetParamScan() methods to:
SetParamAccess() & SetParamEvent()
- Converted DestroyDevice() and DestroyDeviceParam() to inline methods
- Moved methods GetCmdParam() and HandleCommand() from CModbusInterface
- Renamed TDeviceParam field:
UpdateInterval/Timeout -> EventInterval/Timeout
- Renamed TDeviceParam field: Scan -> Read
- Added TDeviceParam field: Write
This commit is contained in:
103
DeviceCore.h
103
DeviceCore.h
@@ -17,11 +17,11 @@
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Enumerated types
|
||||
typedef enum { dtNone = 0, dtUnsigned16 = 1, dtSigned16 = 2, dtUnsigned32 = 3, dtSigned32 = 4, dtFloat32 = 5, dtString = 6 } EMBDataType;
|
||||
typedef enum { dtNone = 0, dtUnsigned16 = 1, dtSigned16 = 2, dtUnsigned32 = 3, dtSigned32 = 4, dtFloat32 = 5, dtString = 6 } EDeviceDataType;
|
||||
|
||||
// Constants
|
||||
const char DataTypeCount = 6;
|
||||
const char DataTypeName[][20] = { "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" };
|
||||
const char DataTypeName[][20] = { "None", "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" };
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -31,32 +31,44 @@ typedef struct SDeviceParam TDeviceParam;
|
||||
|
||||
// Devices with are polled
|
||||
struct SDevice {
|
||||
// Device definition
|
||||
char * Name;
|
||||
char * Type;
|
||||
|
||||
// Device status
|
||||
bool Online;
|
||||
|
||||
// Device parameters
|
||||
TDeviceParam * FirstParam;
|
||||
|
||||
// Device peer list
|
||||
TDevice * Next;
|
||||
};
|
||||
|
||||
// Data parameters of devices
|
||||
struct SDeviceParam {
|
||||
// Parameter definition
|
||||
char * Name;
|
||||
EMBDataType DataType;
|
||||
EDeviceDataType DataType;
|
||||
|
||||
bool Scan;
|
||||
// Polling/Event parameters
|
||||
bool Read;
|
||||
TChannel * EventChannel;
|
||||
long UpdateInterval;
|
||||
timeval UpdateTimeout;
|
||||
long EventInterval;
|
||||
timeval EventTimeout;
|
||||
|
||||
// Last value received from device
|
||||
void * Value;
|
||||
int Len;
|
||||
bool Changed;
|
||||
|
||||
// Value to on device
|
||||
bool Write;
|
||||
void * SetValue;
|
||||
int SetLen;
|
||||
bool SetChanged;
|
||||
|
||||
// Parameter peer list
|
||||
TDeviceParam * Next;
|
||||
};
|
||||
|
||||
@@ -73,10 +85,7 @@ class CDeviceCore : public CFunctionCore
|
||||
protected:
|
||||
// Configuration
|
||||
CDataMember * Config;
|
||||
|
||||
// Update
|
||||
int UpdateInterval;
|
||||
timeval UpdateTimeout;
|
||||
bool DeviceInit;
|
||||
|
||||
// Parameters
|
||||
TDevice * FirstDevice;
|
||||
@@ -85,6 +94,7 @@ protected:
|
||||
// Standard channels
|
||||
TChannel * DeviceChannel;
|
||||
TChannel * CmdChannel;
|
||||
TChannel * EventChannel;
|
||||
|
||||
// Poll
|
||||
int PollStep; // Position in polling sequence
|
||||
@@ -115,8 +125,14 @@ protected:
|
||||
Device = &((*Device)->Next);
|
||||
return Device;
|
||||
}
|
||||
inline TDevice * GetNextOnlineDevice( TDevice * LastDevice ) {
|
||||
TDevice * Device = LastDevice;
|
||||
inline TDevice * GetNextTypeDevice( const char * Type, TDevice * PrevDevice = NULL ) {
|
||||
TDevice * Device = (PrevDevice)? PrevDevice->Next : FirstDevice;
|
||||
while (Device && ((!Type && Device->Type) || (Type && (!Device->Type || !strcasecmp( Type, Device->Type )))))
|
||||
Device = Device->Next;
|
||||
return Device;
|
||||
}
|
||||
inline TDevice * GetNextOnlineDevice( TDevice * PrevDevice = NULL ) {
|
||||
TDevice * Device = (PrevDevice)? PrevDevice->Next : FirstDevice;
|
||||
while (Device && !Device->Online)
|
||||
Device = Device->Next;
|
||||
return Device;
|
||||
@@ -126,24 +142,24 @@ protected:
|
||||
bool DestroyDeviceParam( TDeviceParam ** Param );
|
||||
|
||||
// Find Params
|
||||
inline TDeviceParam * GetDeviceParam( TDevice * Device, const char * Name ) {
|
||||
if (!Device) return NULL;
|
||||
inline TDeviceParam * GetDeviceParam( TDevice * Device, const char * pName ) {
|
||||
if (!Device || !pName) return NULL;
|
||||
TDeviceParam * Param = Device->FirstParam;
|
||||
while (Param && strcasecmp( Param->Name, Name ))
|
||||
while (Param && strcasecmp( Param->Name, pName ))
|
||||
Param = Param->Next;
|
||||
return Param;
|
||||
}
|
||||
inline TDeviceParam ** GetDeviceParamPtr( TDevice * Device, const char * Name ) {
|
||||
if (!Device) return NULL;
|
||||
TDeviceParam ** Param = &Device->FirstParam;
|
||||
while (*Param && strcasecmp( (*Param)->Name, Name ))
|
||||
inline TDeviceParam ** GetDeviceParamPtr( TDevice * Device, const char * pName ) {
|
||||
if (!Device || !pName) return NULL;
|
||||
TDeviceParam ** Param = &(Device->FirstParam);
|
||||
while (*Param && strcasecmp( (*Param)->Name, pName ))
|
||||
Param = &((*Param)->Next);
|
||||
return Param;
|
||||
}
|
||||
inline TDeviceParam * GetNextScanParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
||||
inline TDeviceParam * GetNextReadParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
||||
if (!Device) return NULL;
|
||||
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
||||
while (Param && (!Param->Scan))
|
||||
while (Param && (!Param->Read))
|
||||
Param = Param->Next;
|
||||
return Param;
|
||||
}
|
||||
@@ -163,11 +179,11 @@ protected:
|
||||
}
|
||||
|
||||
// Tools
|
||||
inline EMBDataType GetDataType( const char * TypeName ) {
|
||||
inline EDeviceDataType GetDataType( const char * TypeName ) {
|
||||
int Type;
|
||||
for (Type = 0; Type < DataTypeCount; Type++)
|
||||
if (!strcasecmp( TypeName, DataTypeName[Type])) break;
|
||||
return (Type == DataTypeCount)? dtNone : (EMBDataType)Type;
|
||||
return (Type == DataTypeCount)? dtNone : (EDeviceDataType)Type;
|
||||
}
|
||||
|
||||
bool CompareParamString( const char * ParamValue, const int ParamLen, const char * Value, const int Len );
|
||||
@@ -189,39 +205,54 @@ public:
|
||||
virtual ~CDeviceCore();
|
||||
|
||||
// Configuration
|
||||
bool Init( CDataMember * FunctionConfig );
|
||||
virtual bool Init( CDataMember * FunctionConfig );
|
||||
virtual bool InitDevices( CDataMember * FunctionConfig );
|
||||
|
||||
// Generate events
|
||||
// Polling parameters
|
||||
bool SetPollParam( int pPollInterval );
|
||||
bool SetReplyParam( int pReplyTimeout, int pMaxRetries );
|
||||
bool SetUpdate( int pUpdateInterval );
|
||||
bool SetParamScan( TDeviceParam * Param, bool Scan, const char * ChannelName, long pUpdateInterval );
|
||||
|
||||
bool SetParamAccess( TDeviceParam * Param, bool Read, bool Write );
|
||||
bool SetParamEvent( TDeviceParam * Param, const char * ChannelName, long pEventInterval );
|
||||
|
||||
// Manage Devices
|
||||
TDevice * AddDevice( const char * DeviceName );
|
||||
bool DestroyDevice( const char * DeviceName );
|
||||
TDevice * AddDevice( const char * DeviceName, const char * Type = NULL );
|
||||
inline bool DestroyDevice( const char * DeviceName ) {
|
||||
TDevice ** Device = GetDevicePtr(DeviceName);
|
||||
return (Device)? DestroyDevice( Device ) : false;
|
||||
}
|
||||
|
||||
// Manage Params
|
||||
TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EMBDataType DataType, int ParamLen = 1 );
|
||||
bool DestroyDeviceParam( TDevice * Device, const char * ParamName );
|
||||
TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EDeviceDataType DataType, int ParamLen = 1 );
|
||||
inline bool DestroyDeviceParam( TDevice * Device, const char * ParamName ) {
|
||||
TDeviceParam ** Param = GetDeviceParamPtr( Device, ParamName );
|
||||
return (Param)? DestroyDeviceParam( Param ) : false;
|
||||
};
|
||||
|
||||
// Update/Init Param values
|
||||
bool UpdateUnsignedValue( TDeviceParam * Param, const u_int32_t Value, bool Init );
|
||||
bool UpdateSignedValue( TDeviceParam * Param, const int32_t Value, bool Init );
|
||||
bool UpdateFloatValue( TDeviceParam * Param, const float Value, bool Init );
|
||||
bool UpdateStringValue( TDeviceParam * Param, const char * Value, const int Len, bool Init );
|
||||
bool UpdateStringValue( TDeviceParam * Param, const char * Value, bool Init )
|
||||
{ return UpdateStringValue( Param, Value, strlen(Value), Init ); };
|
||||
inline bool UpdateStringValue( TDeviceParam * Param, const char * Value, bool Init ) {
|
||||
return UpdateStringValue( Param, Value, strlen(Value), Init );
|
||||
};
|
||||
|
||||
// Change Param Update instruction
|
||||
bool SetUnsignedValue( TDeviceParam * Param, const u_int32_t Value, bool Force );
|
||||
bool SetSignedValue( TDeviceParam * Param, const int32_t Value, bool Force );
|
||||
bool SetFloatValue( TDeviceParam * Param, const float Value, bool Force );
|
||||
bool SetStringValue( TDeviceParam * Param, const char * Value, const int Len, bool Force );
|
||||
bool SetStringValue( TDeviceParam * Param, const char * Value, bool Force )
|
||||
{ return SetStringValue( Param, Value, strlen(Value), Force ); };
|
||||
inline bool SetStringValue( TDeviceParam * Param, const char * Value, bool Force ) {
|
||||
return SetStringValue( Param, Value, strlen(Value), Force );
|
||||
};
|
||||
|
||||
// Text Interfaces
|
||||
|
||||
// Handle Interface Commands
|
||||
bool GetCmdParam( const char * Start, char * Param, char ** NextParam );
|
||||
int HandleCommand( const char *ChannelName, const char * Data, const int MaxLen );
|
||||
|
||||
// Command Text Interfaces
|
||||
bool SetValue( TDeviceParam * Param, const char * Value, const int Len, bool Force );
|
||||
bool GetValue( TDeviceParam * Param, char * Value, int &Len );
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user