17 #ifndef _MBExchange_H_
18 #define _MBExchange_H_
23 #include <unordered_map>
25 #include "IONotifyController.h"
26 #include "UniSetObject.h"
27 #include "PassiveTimer.h"
28 #include "DelayTimer.h"
31 #include "Calibration.h"
32 #include "SMInterface.h"
33 #include "SharedMemory.h"
34 #include "ThreadCreator.h"
38 #include "RTUStorage.h"
39 #include "modbus/ModbusClient.h"
40 #include "LogAgregator.h"
41 #include "LogServer.h"
42 #include "LogAgregator.h"
46 #define vmonit( var ) vmon.add( #var, var )
60 const std::string& prefix =
"mb" );
64 static void help_print(
int argc,
const char*
const* argv );
66 static const int NoSafetyState = -1;
78 friend std::ostream& operator<<( std::ostream& os,
const ExchangeMode& em );
88 static DeviceType getDeviceType(
const std::string& dtype );
89 friend std::ostream& operator<<( std::ostream& os,
const DeviceType& dt );
105 rnum(VTypes::wsize(VTypes::vtUnknown)),
116 std::shared_ptr<RegInfo> reg;
119 friend std::ostream& operator<<( std::ostream& os,
const RSProperty& p );
121 typedef std::list<RSProperty> PList;
122 static std::ostream& print_plist( std::ostream& os,
const PList& p );
124 typedef std::map<ModbusRTU::RegID, std::shared_ptr<RegInfo>> RegMap;
137 rtuJack(RTUStorage::nUnknown), rtuChan(0),
142 ModbusRTU::ModbusData mbval;
148 std::shared_ptr<RTUDevice> dev;
151 RTUStorage::RTUJack rtuJack;
161 RegMap::iterator rit;
175 friend std::ostream& operator<<( std::ostream& os,
RegInfo& r );
176 friend std::ostream& operator<<( std::ostream& os,
RegInfo* r );
191 speed(ComPort::ComSpeed38400),
197 std::unordered_map<unsigned int, std::shared_ptr<RegMap>> pollmap;
203 IOController::IOStateList::iterator resp_it;
208 bool resp_force = {
false };
210 std::atomic<size_t> numreply;
211 std::atomic<size_t> prev_numreply;
218 IOController::IOStateList::iterator mode_it;
222 bool checkRespond( std::shared_ptr<DebugStream>& log );
225 ComPort::Speed speed;
226 std::shared_ptr<RTUStorage> rtu188;
228 std::string getShortInfo()
const;
231 friend std::ostream& operator<<( std::ostream& os,
RTUDevice& d );
233 typedef std::unordered_map<ModbusRTU::ModbusAddr, std::shared_ptr<RTUDevice>> RTUDeviceMap;
235 friend std::ostream& operator<<( std::ostream& os, RTUDeviceMap& d );
236 void printMap(RTUDeviceMap& d);
246 inline std::shared_ptr<LogAgregator> getLogAggregator()
250 inline std::shared_ptr<DebugStream> log()
262 virtual void askSensors( UniversalIO::UIOCommand cmd );
263 virtual void initOutput();
264 virtual void sigterm(
int signo )
override;
266 virtual void initIterators();
267 virtual void initValues();
273 mbfunc(ModbusRTU::fnUnknown),
277 std::shared_ptr<RTUDevice> dev;
278 ModbusRTU::ModbusData mbreg;
279 ModbusRTU::SlaveFunctionCode mbfunc;
281 std::shared_ptr<RegInfo> ri;
283 typedef std::list<InitRegInfo> InitList;
285 void firstInitRegisters();
286 bool preInitRead( InitList::iterator& p );
287 bool initSMValue( ModbusRTU::ModbusData* data,
int count,
RSProperty* p );
290 RTUDeviceMap devices;
294 virtual std::shared_ptr<ModbusClient> initMB(
bool reopen =
false ) = 0;
297 bool pollRTU( std::shared_ptr<RTUDevice>& dev, RegMap::iterator& it );
300 void updateRTU(RegMap::iterator& it);
301 void updateMTR(RegMap::iterator& it);
302 void updateRTU188(RegMap::iterator& it);
303 void updateRSProperty(
RSProperty* p,
bool write_only =
false );
304 virtual void updateRespondSensors();
306 bool checkUpdateSM(
bool wrFunc,
long devMode );
307 bool checkPoll(
bool wrFunc )
const;
309 bool checkProcActive()
const;
310 void setProcActive(
bool st );
313 void readConfiguration();
314 bool readItem(
const std::shared_ptr<UniXML>& xml,
UniXML::iterator& it, xmlNode* sec );
316 void initDeviceList();
317 void initOffsetList();
319 std::shared_ptr<RTUDevice> addDev( RTUDeviceMap& dmap, ModbusRTU::ModbusAddr a,
UniXML::iterator& it );
320 std::shared_ptr<RegInfo> addReg(std::shared_ptr<RegMap>& devices, ModbusRTU::RegID
id, ModbusRTU::ModbusData r,
UniXML::iterator& it, std::shared_ptr<RTUDevice> dev );
326 bool initRegInfo(std::shared_ptr<RegInfo>& r,
UniXML::iterator& it, std::shared_ptr<RTUDevice>& dev );
328 virtual bool initDeviceInfo( RTUDeviceMap& m, ModbusRTU::ModbusAddr a,
UniXML::iterator& it );
330 std::string initPropPrefix(
const std::string& def_prop_prefix =
"" );
332 void rtuQueryOptimization( RTUDeviceMap& m );
334 xmlNode* cnode = { 0 };
336 std::string s_fvalue;
338 std::shared_ptr<SMInterface> shm;
340 timeout_t initPause = { 3000 };
345 bool mbregFromID = {
false };
347 timeout_t sleepPause_msec;
352 long maxHeartBeat = { 10 };
353 IOController::IOStateList::iterator itHeartBeat;
357 IOController::IOStateList::iterator itExchangeMode;
360 std::atomic_bool activated = {
false };
361 timeout_t activateTimeout = { 20000 };
362 bool noQueryOptimization = {
false };
363 bool no_extimer = {
false };
368 size_t poll_count = { 0 };
370 std::string statInfo = {
"" };
374 std::shared_ptr<ModbusClient> mb;
377 timeout_t recv_timeout = { 500 };
378 timeout_t default_timeout = { 5000 };
380 timeout_t aftersend_pause = { 0 };
389 typedef std::list<IOBase> ThresholdList;
390 ThresholdList thrlist;
392 std::string defaultMBtype;
393 std::string defaultMBaddr;
394 bool defaultMBinitOK = {
false };
396 std::shared_ptr<LogAgregator> loga;
397 std::shared_ptr<DebugStream> mblog;
398 std::shared_ptr<LogServer> logserv;
399 std::string logserv_host = {
""};
400 int logserv_port = {0};
401 const std::shared_ptr<SharedMemory> ic;
414 #endif // _MBExchange_H_