UniSet 2.41.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 <atomic>
26#include "IONotifyController.h"
27#include "UniSetObject.h"
28#include "PassiveTimer.h"
29#include "DelayTimer.h"
30#include "Trigger.h"
31#include "Mutex.h"
32#include "Calibration.h"
33#include "SMInterface.h"
34#include "SharedMemory.h"
35#include "ThreadCreator.h"
36#include "IOBase.h"
37#include "VTypes.h"
38#include "MTR.h"
39#include "RTUStorage.h"
40#include "modbus/ModbusClient.h"
41#include "LogAgregator.h"
42#include "LogServer.h"
43#include "LogAgregator.h"
44#include "VMonitor.h"
45#include "MBConfig.h"
46// -----------------------------------------------------------------------------
47#ifndef vmonit
48#define vmonit( var ) vmon.add( #var, var )
49#endif
50// -------------------------------------------------------------------------
51namespace uniset
52{
53 // -----------------------------------------------------------------------------
57 class MBExchange:
58 private USingleProcess,
59 public UniSetObject
60 {
61 public:
62 MBExchange( uniset::ObjectId objId, xmlNode* confnode,
63 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
64 const std::string& prefix = "mb" );
65 virtual ~MBExchange();
66
68 static void help_print( int argc, const char* const* argv );
69
70 // ----------------------------------
71 enum Timer
72 {
73 tmExchange
74 };
75
76 void execute();
77
78 inline std::shared_ptr<LogAgregator> getLogAggregator()
79 {
80 return loga;
81 }
82 inline std::shared_ptr<DebugStream> log()
83 {
84 return mblog;
85 }
86
87 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
88
89 bool reload( const std::string& confile );
90
91 static uint8_t firstBit( uint16_t mask );
92
93 // offset = firstBit(mask)
94 static uint16_t getBits( uint16_t value, uint16_t mask, uint8_t offset );
95 // if mask = 0 return value
96 static uint16_t setBits( uint16_t value, uint16_t set, uint16_t mask, uint8_t offset );
97 // if mask=0 return set
98 static uint16_t forceSetBits( uint16_t value, uint16_t set, uint16_t mask, uint8_t offset );
99
100 protected:
101 virtual void step();
102 virtual void sysCommand( const uniset::SystemMessage* msg ) override;
103 virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
104 virtual void timerInfo( const uniset::TimerMessage* tm ) override;
105 virtual void askSensors( UniversalIO::UIOCommand cmd );
106 virtual void initOutput();
107 virtual bool deactivateObject() override;
108 virtual bool activateObject() override;
109 virtual void initIterators();
110 virtual void initValues();
111 virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf );
112#ifndef DISABLE_REST_API
113 // http API
114 virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
115 virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p ) override;
116
117 // control helpers
118 Poco::JSON::Object::Ptr httpModeGet(const Poco::URI::QueryParameters& p );
119 Poco::JSON::Object::Ptr httpModeSet(const Poco::URI::QueryParameters& p );
120 Poco::JSON::Object::Ptr httpModeSupported(const Poco::URI::QueryParameters&);
121 Poco::JSON::Object::Ptr httpMode( const Poco::URI::QueryParameters& p );
122 Poco::JSON::Object::Ptr httpReload( const Poco::URI::QueryParameters& p );
123 virtual Poco::JSON::Object::Ptr httpGetParam( const Poco::URI::QueryParameters& p );
124 virtual Poco::JSON::Object::Ptr httpSetParam( const Poco::URI::QueryParameters& p );
125 virtual Poco::JSON::Object::Ptr httpStatus();
126
127 bool httpEnabledSetParams = { false };
128#endif
129 void firstInitRegisters();
130 bool preInitRead( MBConfig::InitList::iterator& p );
131 bool initSMValue( ModbusRTU::ModbusData* data, int count, MBConfig::RSProperty* p );
132 bool allInitOK;
133
134 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) = 0;
135
136 virtual bool poll();
137 bool pollRTU( std::shared_ptr<MBConfig::RTUDevice>& dev, MBConfig::RegMap::iterator& it );
138
139 void updateSM();
140
141 // в функции передаётся итератор,
142 // т.к. в них идёт итерирование в случае если запрос в несколько регистров
143 void updateRTU(MBConfig::RegMap::iterator& it);
144 void updateMTR(MBConfig::RegMap::iterator& it);
145 void updateRTU188(MBConfig::RegMap::iterator& it);
146 void updateRSProperty( MBConfig::RSProperty* p, bool write_only = false );
147 virtual void updateRespondSensors();
148
149 bool isUpdateSM( bool wrFunc, long devMode ) const noexcept;
150 bool isPollEnabled( bool wrFunc ) const noexcept;
151 bool isSafeMode( std::shared_ptr<MBConfig::RTUDevice>& dev ) const noexcept;
152
153 bool isProcActive() const;
154 void setProcActive( bool st );
155 bool waitSMReady();
156
157 bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
158 bool initItem( UniXML::iterator& it );
159 void initOffsetList();
160 std::string initPropPrefix( const std::string& def_prop_prefix );
161
162 xmlNode* cnode = { 0 };
163 std::shared_ptr<SMInterface> shm;
164
165 timeout_t initPause = { 3000 };
166 uniset::uniset_rwmutex mutex_start;
167
168 bool force = { false };
169 bool force_out = { false };
170
171 PassiveTimer ptHeartBeat;
172 uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
173 long maxHeartBeat = { 10 };
174 IOController::IOStateList::iterator itHeartBeat;
175 uniset::ObjectId sidTestSMReady = {uniset::DefaultObjectId };
176
178 IOController::IOStateList::iterator itExchangeMode;
179 std::atomic<MBConfig::ExchangeMode> exchangeMode = { MBConfig::emNone };
180
181 std::atomic_bool activated = { false };
182 std::atomic_bool canceled = { false };
183 timeout_t activateTimeout = { 20000 }; // msec
184 bool notUseExchangeTimer = { false };
185
186 timeout_t stat_time = { 0 };
187 size_t poll_count = { 0 };
189 std::string statInfo = { "" };
190
191 std::shared_ptr<ModbusClient> mb;
192
194 Trigger trReopen;
195
197
198 std::shared_ptr<LogAgregator> loga;
199 std::shared_ptr<DebugStream> mblog;
200 std::shared_ptr<LogServer> logserv;
201 std::string logserv_host = {""};
202 int logserv_port = {0};
203 const std::shared_ptr<SharedMemory> ic;
204
205 VMonitor vmon;
206
207 size_t ncycle = { 0 };
208
209 std::shared_ptr<uniset::MBConfig> mbconf;
210 uniset::uniset_rwmutex mutex_conf;
211
212 private:
213 MBExchange();
214
215 };
216 // --------------------------------------------------------------------------
217} // end of namespace uniset
218// -----------------------------------------------------------------------------
219#endif // _MBExchange_H_
220// -----------------------------------------------------------------------------
@ emNone
Определения MBConfig.h:53
Определения MBExchange.h:60
size_t ncycle
Определения MBExchange.h:207
std::atomic< MBConfig::ExchangeMode > exchangeMode
Определения MBExchange.h:179
uniset::ObjectId sidExchangeMode
Определения MBExchange.h:177
PassiveTimer ptReopen
Определения MBExchange.h:193
bool force_out
Определения MBExchange.h:169
PassiveTimer ptInitChannel
Определения MBExchange.h:196
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы).
Определения MBExchange.cc:314
void updateRSProperty(MBConfig::RSProperty *p, bool write_only=false)
Определения MBExchange.cc:1173
void updateMTR(MBConfig::RegMap::iterator &it)
Определения MBExchange.cc:1661
static void help_print(int argc, const char *const *argv)
Определения MBExchange.cc:204
timeout_t stat_time
Определения MBExchange.h:186
PassiveTimer ptStatistic
Определения MBExchange.h:188
bool force
Определения MBExchange.h:168
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации).
Определения MBExchange.cc:2073
Пассивный таймер
Определения PassiveTimer.h:94
Определения MessageType.h:127
Определения MessageType.h:171
Определения MessageType.h:214
Определения Trigger.h:31
UniSetObject(const std::string &name, const std::string &section)
Определения UniSetObject.cc:89
Определения Mutex.h:32
Определения Calibration.h:27
const ObjectId DefaultObjectId
Определения UniSetTypes.h:71
long ObjectId
Определения UniSetTypes_i.idl:30
Определения MBConfig.h:92
Определения UniSetTypes_i.idl:65