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 );
75 friend std::ostream& operator<<( std::ostream& os,
const ExchangeMode& em );
86 friend std::string to_string(
const SafeMode& m );
87 friend std::ostream& operator<<( std::ostream& os,
const SafeMode& m );
97 static DeviceType getDeviceType(
const std::string& dtype ) noexcept;
98 friend std::ostream& operator<<( std::ostream& os,
const DeviceType& dt );
114 rnum(VTypes::wsize(VTypes::vtUnknown)),
125 std::shared_ptr<RegInfo> reg;
128 friend std::ostream& operator<<( std::ostream& os,
const RSProperty& p );
130 typedef std::list<RSProperty> PList;
131 static std::ostream& print_plist( std::ostream& os,
const PList& p );
133 typedef std::map<ModbusRTU::RegID, std::shared_ptr<RegInfo>> RegMap;
144 ModbusRTU::ModbusData mbval = { 0 };
145 ModbusRTU::ModbusData
mbreg = { 0 };
146 ModbusRTU::SlaveFunctionCode
mbfunc = { ModbusRTU::fnUnknown };
148 ModbusRTU::RegID regID = { 0 };
150 std::shared_ptr<RTUDevice> dev;
153 RTUStorage::RTUJack rtuJack = { RTUStorage::nUnknown };
163 RegMap::iterator rit;
177 friend std::ostream& operator<<( std::ostream& os,
const RegInfo& r );
178 friend std::ostream& operator<<( std::ostream& os,
const RegInfo* r );
183 std::unordered_map<size_t, std::shared_ptr<RegMap>> pollmap;
189 IOController::IOStateList::iterator resp_it;
192 bool resp_state = {
false };
193 bool resp_invert = {
false };
194 bool resp_force = {
false };
196 std::atomic<size_t> numreply = { 0 };
197 std::atomic<size_t> prev_numreply = { 0 };
204 IOController::IOStateList::iterator mode_it;
210 IOController::IOStateList::iterator safemode_it;
211 long safemode_value = { 1 };
214 bool checkRespond( std::shared_ptr<DebugStream>& log );
217 ComPort::Speed speed = { ComPort::ComSpeed38400 };
218 std::shared_ptr<RTUStorage> rtu188;
220 std::string getShortInfo()
const;
223 friend std::ostream& operator<<( std::ostream& os, RTUDevice& d );
225 typedef std::unordered_map<ModbusRTU::ModbusAddr, std::shared_ptr<RTUDevice>> RTUDeviceMap;
227 friend std::ostream& operator<<( std::ostream& os, RTUDeviceMap& d );
228 void printMap(RTUDeviceMap& d);
238 inline std::shared_ptr<LogAgregator> getLogAggregator()
242 inline std::shared_ptr<DebugStream> log()
254 virtual void askSensors( UniversalIO::UIOCommand cmd );
255 virtual void initOutput();
258 virtual void initIterators();
259 virtual void initValues();
265 mbfunc(ModbusRTU::fnUnknown),
269 std::shared_ptr<RTUDevice> dev;
270 ModbusRTU::ModbusData mbreg;
271 ModbusRTU::SlaveFunctionCode mbfunc;
273 std::shared_ptr<RegInfo> ri;
275 typedef std::list<InitRegInfo> InitList;
277 void firstInitRegisters();
278 bool preInitRead( InitList::iterator& p );
279 bool initSMValue( ModbusRTU::ModbusData* data,
int count,
RSProperty* p );
282 RTUDeviceMap devices;
285 virtual std::shared_ptr<ModbusClient> initMB(
bool reopen =
false ) = 0;
288 bool pollRTU( std::shared_ptr<RTUDevice>& dev, RegMap::iterator& it );
294 void updateRTU(RegMap::iterator& it);
296 void updateRTU188(RegMap::iterator& it);
298 virtual void updateRespondSensors();
300 bool isUpdateSM(
bool wrFunc,
long devMode )
const noexcept;
301 bool isPollEnabled(
bool wrFunc )
const noexcept;
302 bool isSafeMode( std::shared_ptr<RTUDevice>& dev )
const noexcept;
304 bool isProcActive()
const;
305 void setProcActive(
bool st );
308 void readConfiguration();
309 bool readItem(
const std::shared_ptr<UniXML>& xml,
UniXML::iterator& it, xmlNode* sec );
311 void initDeviceList();
312 void initOffsetList();
314 std::shared_ptr<RTUDevice> addDev( RTUDeviceMap& dmap, ModbusRTU::ModbusAddr a,
UniXML::iterator& it );
315 std::shared_ptr<RegInfo> addReg(std::shared_ptr<RegMap>& devices, ModbusRTU::RegID
id, ModbusRTU::ModbusData r,
UniXML::iterator& it, std::shared_ptr<RTUDevice> dev );
321 bool initRegInfo(std::shared_ptr<RegInfo>& r,
UniXML::iterator& it, std::shared_ptr<RTUDevice>& dev );
323 virtual bool initDeviceInfo( RTUDeviceMap& m, ModbusRTU::ModbusAddr a,
UniXML::iterator& it );
325 std::string initPropPrefix(
const std::string& def_prop_prefix =
"" );
327 void rtuQueryOptimization( RTUDeviceMap& m );
328 void rtuQueryOptimizationForDevice(
const std::shared_ptr<RTUDevice>& d );
329 void rtuQueryOptimizationForRegMap(
const std::shared_ptr<RegMap>& regmap );
331 xmlNode* cnode = { 0 };
333 std::string s_fvalue;
335 std::shared_ptr<SMInterface> shm;
337 timeout_t initPause = { 3000 };
342 bool mbregFromID = {
false };
344 timeout_t sleepPause_msec = { 10 };
349 long maxHeartBeat = { 10 };
350 IOController::IOStateList::iterator itHeartBeat;
354 IOController::IOStateList::iterator itExchangeMode;
357 std::atomic_bool activated = {
false };
358 std::atomic_bool canceled = {
false };
359 timeout_t activateTimeout = { 20000 };
360 bool noQueryOptimization = {
false };
361 bool notUseExchangeTimer = {
false };
366 size_t poll_count = { 0 };
368 std::string statInfo = {
"" };
372 std::shared_ptr<ModbusClient> mb;
375 timeout_t recv_timeout = { 500 };
376 timeout_t default_timeout = { 5000 };
378 timeout_t aftersend_pause = { 0 };
387 typedef std::list<IOBase> ThresholdList;
388 ThresholdList thrlist;
390 std::string defaultMBtype;
391 std::string defaultMBaddr;
392 bool defaultMBinitOK = {
false };
394 std::shared_ptr<LogAgregator> loga;
395 std::shared_ptr<DebugStream> mblog;
396 std::shared_ptr<LogServer> logserv;
397 std::string logserv_host = {
""};
398 int logserv_port = {0};
399 const std::shared_ptr<SharedMemory> ic;
412 #endif // _MBExchange_H_