Files
redAcore/DeviceCore.h
Charl Wentzel a972fb9101 Major Update:
- Implement consistent Function addition to application
  - Use TYPE_XXX constants to declare function type
  - Use NewXXXX() methods to call constructor correctly
  - Add FunctionType list (with constructor) to Application
  - Create Function by comparison to FunctionType list
- Simplify LoadConfig() and Init() methods for functions
  - Combine methods into Init() method
  - Pass relevant data member to Init() method
  - Remove all CDataMember references on functions
- ApplicationCore:
  - Split ReadParam() method from LoadConfig() method
  - Split main configuration into separate files:
    - config/ - main config file, general application settings
    - definition/ - application definition, e.g. function blocks
  - Definition and Address List files specified in config file
  - Load address file in address/ branch
  - Made DataTree & JSONparser private
  - Made Config, Definition & Address branches public
  - Removed unnecessary branch references
  - Improved event logging
- DataTreeCore:
  - Allow GetChFirstChild & GetChElement to create parent branches
    with correct type, ie. Object/Array
  - Remove unnecessary Create param from GetXxx functions
  - Bug fix: Print empty objects/arrays correct, ie. empty brackets
  - Bug fix: Adding element at specific index
  - Bug fix: Error when get/create string value with "null"
- FunctionCore:
  - Type param now set as constant via constructor
  - Create empty Handles & Channels objects if none in Config
- SelectableCore:
  - Add Queue length parameter to handles for UNIX and TCP sockets
- DeviceCore:
  - Bug fix: missing Process() method
2018-11-24 13:35:23 +02:00

213 lines
7.0 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 { dtUnsigned16 = 0, dtSigned16 = 1, dtUnsigned32 = 2, dtSigned32 = 3, dtFloat32 = 4, dtString = 5 } EMBDataType;
// Constants
const char DataTypeName[][20] = { "Unsigned16", "Signed16", "Unsigned32", "Signed32", "Float32", "String" };
//---------------------------------------------------------------------------
// Structure prototypes
typedef struct SDevice TDevice;
typedef struct SDeviceParam TDeviceParam;
// Devices with are polled
struct SDevice {
char * Name;
bool Online;
TDeviceParam * FirstParam;
TDevice * Next;
};
// Data parameters of devices
struct SDeviceParam {
char * Name;
EMBDataType DataType;
bool Scan;
TChannel * EventChannel;
long UpdateInterval;
timeval UpdateTimeout;
void * Value;
int Len;
bool Changed;
void * SetValue;
int SetLen;
bool SetChanged;
TDeviceParam * Next;
};
//---------------------------------------------------------------------------
// Function Constructor List
#define TYPE_DEVICE "Device"
//CFunctionCore * NewDeviceCore( const char * Name );
//---------------------------------------------------------------------------
class CDeviceCore : public CFunctionCore
{
protected:
// Update
int UpdateInterval;
timeval UpdateTimeout;
// Parameters
TDevice * FirstDevice;
TDevice * ActiveDevice;
// Standard channels
TChannel * DeviceChannel;
TChannel * CmdChannel;
// 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 * GetNextOnlineDevice( TDevice * LastDevice ) {
TDevice * Device = LastDevice;
while (Device && !Device->Online)
Device = Device->Next;
return Device;
}
// Manage Params
bool DestroyDeviceParam( TDeviceParam ** Param );
// Find Params
inline TDeviceParam * GetDeviceParam( TDevice * Device, const char * Name ) {
if (!Device) return NULL;
TDeviceParam * Param = Device->FirstParam;
while (Param && strcasecmp( Param->Name, Name ))
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 ))
Param = &((*Param)->Next);
return Param;
}
inline TDeviceParam * GetNextScanParam( TDevice * Device, TDeviceParam * LastParam = NULL ) {
if (!Device) return NULL;
TDeviceParam * Param = (LastParam)? LastParam->Next : Device->FirstParam;
while (Param && (!Param->Scan))
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
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();
// Generate events
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 );
// Manage Devices
TDevice * AddDevice( const char * DeviceName );
bool DestroyDevice( const char * DeviceName );
// Manage Params
TDeviceParam * AddDeviceParam( TDevice * Device, const char * ParamName, EMBDataType DataType, int ParamLen = 1 );
bool DestroyDeviceParam( TDevice * Device, const char * ParamName );
// 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 );
// 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 );
// 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_ */