UniSet  2.24.2
MBExchange.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // -----------------------------------------------------------------------------
17 #ifndef _MBExchange_H_
18 #define _MBExchange_H_
19 // -----------------------------------------------------------------------------
20 #include <ostream>
21 #include <string>
22 #include <map>
23 #include <unordered_map>
24 #include <memory>
25 #include "IONotifyController.h"
26 #include "UniSetObject.h"
27 #include "PassiveTimer.h"
28 #include "DelayTimer.h"
29 #include "Trigger.h"
30 #include "Mutex.h"
31 #include "Calibration.h"
32 #include "SMInterface.h"
33 #include "SharedMemory.h"
34 #include "ThreadCreator.h"
35 #include "IOBase.h"
36 #include "VTypes.h"
37 #include "MTR.h"
38 #include "RTUStorage.h"
39 #include "modbus/ModbusClient.h"
40 #include "LogAgregator.h"
41 #include "LogServer.h"
42 #include "LogAgregator.h"
43 #include "VMonitor.h"
44 #include "MBConfig.h"
45 // -----------------------------------------------------------------------------
46 #ifndef vmonit
47 #define vmonit( var ) vmon.add( #var, var )
48 #endif
49 // -------------------------------------------------------------------------
50 namespace uniset
51 {
52  // -----------------------------------------------------------------------------
56  class MBExchange:
57  public UniSetObject
58  {
59  public:
60  MBExchange( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
61  const std::string& prefix = "mb" );
62  virtual ~MBExchange();
63 
65  static void help_print( int argc, const char* const* argv );
66 
67  // ----------------------------------
68  enum Timer
69  {
70  tmExchange
71  };
72 
73  void execute();
74 
75  inline std::shared_ptr<LogAgregator> getLogAggregator()
76  {
77  return loga;
78  }
79  inline std::shared_ptr<DebugStream> log()
80  {
81  return mblog;
82  }
83 
84  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
85 
86  bool reload( const std::string& confile );
87 
88  protected:
89  virtual void step();
90  virtual void sysCommand( const uniset::SystemMessage* msg ) override;
91  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
92  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
93  virtual void askSensors( UniversalIO::UIOCommand cmd );
94  virtual void initOutput();
95  virtual bool deactivateObject() override;
96  virtual bool activateObject() override;
97  virtual void initIterators();
98  virtual void initValues();
99  virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf );
100 #ifndef DISABLE_REST_API
101  // http API
102  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
103  virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p ) override;
104 #endif
105  void firstInitRegisters();
106  bool preInitRead( MBConfig::InitList::iterator& p );
107  bool initSMValue( ModbusRTU::ModbusData* data, int count, MBConfig::RSProperty* p );
108  bool allInitOK;
109 
110  virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) = 0;
111 
112  virtual bool poll();
113  bool pollRTU( std::shared_ptr<MBConfig::RTUDevice>& dev, MBConfig::RegMap::iterator& it );
114 
115  void updateSM();
116 
117  // в функции передаётся итератор,
118  // т.к. в них идёт итерирование в случае если запрос в несколько регистров
119  void updateRTU(MBConfig::RegMap::iterator& it);
120  void updateMTR(MBConfig::RegMap::iterator& it);
121  void updateRTU188(MBConfig::RegMap::iterator& it);
122  void updateRSProperty( MBConfig::RSProperty* p, bool write_only = false );
123  virtual void updateRespondSensors();
124 
125  bool isUpdateSM( bool wrFunc, long devMode ) const noexcept;
126  bool isPollEnabled( bool wrFunc ) const noexcept;
127  bool isSafeMode( std::shared_ptr<MBConfig::RTUDevice>& dev ) const noexcept;
128 
129  bool isProcActive() const;
130  void setProcActive( bool st );
131  bool waitSMReady();
132 
133  bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
134  bool initItem( UniXML::iterator& it );
135  void initOffsetList();
136  std::string initPropPrefix( const std::string& s_filed, const std::string& def_prop_prefix );
137 
138  xmlNode* cnode = { 0 };
139  std::shared_ptr<SMInterface> shm;
140 
141  timeout_t initPause = { 3000 };
142  uniset::uniset_rwmutex mutex_start;
143 
144  bool force = { false };
145  bool force_out = { false };
147  PassiveTimer ptHeartBeat;
148  uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
149  long maxHeartBeat = { 10 };
150  IOController::IOStateList::iterator itHeartBeat;
152 
154  IOController::IOStateList::iterator itExchangeMode;
157  std::atomic_bool activated = { false };
158  std::atomic_bool canceled = { false };
159  timeout_t activateTimeout = { 20000 }; // msec
160  bool notUseExchangeTimer = { false };
161 
162  timeout_t stat_time = { 0 };
163  size_t poll_count = { 0 };
165  std::string statInfo = { "" };
166 
167  std::shared_ptr<ModbusClient> mb;
168 
170  Trigger trReopen;
171 
174  std::shared_ptr<LogAgregator> loga;
175  std::shared_ptr<DebugStream> mblog;
176  std::shared_ptr<LogServer> logserv;
177  std::string logserv_host = {""};
178  int logserv_port = {0};
179  const std::shared_ptr<SharedMemory> ic;
180 
181  VMonitor vmon;
182 
183  size_t ncycle = { 0 };
185  std::shared_ptr<uniset::MBConfig> mbconf;
186  uniset::uniset_rwmutex mutex_conf;
187 
188  private:
189  MBExchange();
190 
191  };
192  // --------------------------------------------------------------------------
193 } // end of namespace uniset
194 // -----------------------------------------------------------------------------
195 #endif // _MBExchange_H_
196 // -----------------------------------------------------------------------------
@ emNone
Definition: MBConfig.h:53
Definition: MBExchange.h:58
size_t ncycle
Definition: MBExchange.h:183
uniset::ObjectId sidExchangeMode
Definition: MBExchange.h:153
PassiveTimer ptReopen
Definition: MBExchange.h:169
long exchangeMode
Definition: MBExchange.h:155
bool force_out
Definition: MBExchange.h:145
PassiveTimer ptInitChannel
Definition: MBExchange.h:172
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: MBExchange.cc:323
void updateRSProperty(MBConfig::RSProperty *p, bool write_only=false)
Definition: MBExchange.cc:1111
void updateMTR(MBConfig::RegMap::iterator &it)
Definition: MBExchange.cc:1563
static void help_print(int argc, const char *const *argv)
Definition: MBExchange.cc:217
timeout_t stat_time
Definition: MBExchange.h:162
PassiveTimer ptStatistic
Definition: MBExchange.h:164
bool force
Definition: MBExchange.h:144
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition: MBExchange.cc:1967
Пассивный таймер
Definition: PassiveTimer.h:94
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: MessageType.h:214
Definition: Trigger.h:31
Definition: UniSetObject.h:80
Definition: UniXML.h:44
Definition: Mutex.h:32
Definition: CommonEventLoop.h:15
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: MBConfig.h:89
Definition: UniSetTypes_i.idl:65