- 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
262 lines
9.1 KiB
C++
262 lines
9.1 KiB
C++
/*
|
|
* FunctionCore.h
|
|
*
|
|
* Created on: 18 May 2016
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
#ifndef REDACORE_DEVICECORE_H_
|
|
#define REDACORE_DEVICECORE_H_
|
|
|
|
// Standard C/C++ Libraries
|
|
/* none */
|
|
|
|
// redA Libraries
|
|
#include "FunctionCore.h"
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Enumerated types
|
|
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] = { "None", "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" };
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Structure prototypes
|
|
typedef struct SDevice TDevice;
|
|
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;
|
|
EDeviceDataType DataType;
|
|
|
|
// Polling/Event parameters
|
|
bool Read;
|
|
TChannel * EventChannel;
|
|
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;
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Function Constructor List
|
|
#define TYPE_DEVICE "Device"
|
|
//CFunctionCore * NewDeviceCore( const char * Name );
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
class CDeviceCore : public CFunctionCore
|
|
{
|
|
protected:
|
|
// Configuration
|
|
CDataMember * Config;
|
|
bool DeviceInit;
|
|
|
|
// Parameters
|
|
TDevice * FirstDevice;
|
|
TDevice * ActiveDevice;
|
|
|
|
// Standard channels
|
|
TChannel * DeviceChannel;
|
|
TChannel * CmdChannel;
|
|
TChannel * EventChannel;
|
|
|
|
// Poll
|
|
int PollStep; // Position in polling sequence
|
|
timeval PollWait; // Time at which last poll was done
|
|
long PollInterval; // Minimum delay between polls
|
|
|
|
// Reply
|
|
bool WaitingForReply; // Command sent, waiting for reply
|
|
long ReplyTimeout; // Max waiting time for reply
|
|
|
|
// Retry
|
|
int PollRetry; // No of polling retries that has timed out
|
|
int MaxRetries; // Max allowed retries
|
|
|
|
// Manage Devices
|
|
bool DestroyDevice( TDevice ** Device );
|
|
|
|
// Find Devices
|
|
inline TDevice * GetDevice( const char * Name ) {
|
|
TDevice * Device = FirstDevice;
|
|
while (Device && strcasecmp( Device->Name, Name ))
|
|
Device = Device->Next;
|
|
return Device;
|
|
}
|
|
inline TDevice ** GetDevicePtr( const char * Name ) {
|
|
TDevice ** Device = &FirstDevice;
|
|
while (*Device && strcasecmp( (*Device)->Name, Name ))
|
|
Device = &((*Device)->Next);
|
|
return Device;
|
|
}
|
|
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;
|
|
}
|
|
|
|
// Manage Params
|
|
bool DestroyDeviceParam( TDeviceParam ** Param );
|
|
|
|
// Find Params
|
|
inline TDeviceParam * GetDeviceParam( 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 ** 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 * GetNextReadParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
|
if (!Device) return NULL;
|
|
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
|
while (Param && (!Param->Read))
|
|
Param = Param->Next;
|
|
return Param;
|
|
}
|
|
inline TDeviceParam * GetNextChangedParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
|
if (!Device) return NULL;
|
|
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
|
while (Param && (!Param->Changed))
|
|
Param = Param->Next;
|
|
return Param;
|
|
}
|
|
inline TDeviceParam * GetNextSetChangedParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
|
|
if (!Device) return NULL;
|
|
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
|
|
while (Param && (!Param->SetChanged))
|
|
Param = Param->Next;
|
|
return Param;
|
|
}
|
|
|
|
// Tools
|
|
inline EDeviceDataType GetDataType( const char * TypeName ) {
|
|
int Type;
|
|
for (Type = 0; Type < DataTypeCount; Type++)
|
|
if (!strcasecmp( TypeName, DataTypeName[Type])) break;
|
|
return (Type == DataTypeCount)? dtNone : (EDeviceDataType)Type;
|
|
}
|
|
|
|
bool CompareParamString( const char * ParamValue, const int ParamLen, const char * Value, const int Len );
|
|
|
|
// Generate events
|
|
bool EventOutput( TDeviceParam * Param, bool Force );
|
|
bool TimedParamEvents();
|
|
|
|
// Manage device
|
|
virtual bool DeviceOnline( TDevice * Device, bool Online );
|
|
|
|
// Handle Reply Timing
|
|
virtual void SetWaitForReply();
|
|
virtual bool CheckReplyTimeout( int TimeoutPollStep );
|
|
|
|
public:
|
|
// Life cycle
|
|
CDeviceCore( const char * pName, const char * pType = TYPE_DEVICE );
|
|
virtual ~CDeviceCore();
|
|
|
|
// Configuration
|
|
virtual bool Init( CDataMember * FunctionConfig );
|
|
virtual bool InitDevices( CDataMember * FunctionConfig );
|
|
|
|
// Polling parameters
|
|
bool SetPollParam( int pPollInterval );
|
|
bool SetReplyParam( int pReplyTimeout, int pMaxRetries );
|
|
|
|
bool SetParamAccess( TDeviceParam * Param, bool Read, bool Write );
|
|
bool SetParamEvent( TDeviceParam * Param, const char * ChannelName, long pEventInterval );
|
|
|
|
// Manage Devices
|
|
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, 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 );
|
|
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 );
|
|
inline bool SetStringValue( TDeviceParam * Param, const char * Value, bool Force ) {
|
|
return SetStringValue( Param, Value, strlen(Value), Force );
|
|
};
|
|
|
|
|
|
// 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 );
|
|
};
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif /* REDACORE_DEVICECORE_H_ */
|