UniSet @VERSION@
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 // -----------------------------------------------------------------------------
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 UHttp::HttpRequestContext& ctx ) override;
116 virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root ) override;
117
118 // control helpers
119 Poco::JSON::Object::Ptr httpModeGet(const Poco::URI::QueryParameters& p );
120 Poco::JSON::Object::Ptr httpModeSet(const Poco::URI::QueryParameters& p );
121 Poco::JSON::Object::Ptr httpModeSupported(const Poco::URI::QueryParameters&);
122 Poco::JSON::Object::Ptr httpMode( const Poco::URI::QueryParameters& p );
123 Poco::JSON::Object::Ptr httpReload( const Poco::URI::QueryParameters& p );
124 virtual Poco::JSON::Object::Ptr httpGetParam( const Poco::URI::QueryParameters& p );
125 virtual Poco::JSON::Object::Ptr httpSetParam( const Poco::URI::QueryParameters& p );
126 virtual Poco::JSON::Object::Ptr httpStatus();
127 Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p );
128 Poco::JSON::Object::Ptr httpRegisters( const Poco::URI::QueryParameters& p );
129 Poco::JSON::Object::Ptr httpDevices( const Poco::URI::QueryParameters& p );
130 Poco::JSON::Object::Ptr httpTakeControl( Poco::Net::HTTPServerResponse& resp, const Poco::URI::QueryParameters& p );
131 Poco::JSON::Object::Ptr httpReleaseControl( const Poco::URI::QueryParameters& p );
132
133 bool httpControlAllow = { false };
134 std::atomic_bool httpControlActive = { false };
135 bool httpEnabledSetParams = { false };
136#endif
137 void firstInitRegisters();
138 bool preInitRead( MBConfig::InitList::iterator& p );
139 bool initSMValue( ModbusRTU::ModbusData* data, int count, MBConfig::RSProperty* p );
140 bool allInitOK;
141
142 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) = 0;
143
144 virtual bool poll();
145 bool pollRTU( std::shared_ptr<MBConfig::RTUDevice>& dev, MBConfig::RegMap::iterator& it );
146
147 void updateSM();
148
149 // в функции передаётся итератор,
150 // т.к. в них идёт итерирование в случае если запрос в несколько регистров
151 void updateRTU(MBConfig::RegMap::iterator& it);
152 void updateMTR(MBConfig::RegMap::iterator& it);
153 void updateRTU188(MBConfig::RegMap::iterator& it);
154 void updateRSProperty( MBConfig::RSProperty* p, bool write_only = false );
155 virtual void updateRespondSensors();
156
157 bool isUpdateSM( bool wrFunc, long devMode ) const noexcept;
158 bool isPollEnabled( bool wrFunc ) const noexcept;
159 bool isSafeMode( std::shared_ptr<MBConfig::RTUDevice>& dev ) const noexcept;
160
161 bool isProcActive() const;
162 void setProcActive( bool st );
163 bool waitSMReady();
164
165 bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
166 bool initItem( UniXML::iterator& it );
167 void initOffsetList();
168 std::string initPropPrefix( const std::string& def_prop_prefix );
169
170 xmlNode* cnode = { 0 };
171 std::shared_ptr<SMInterface> shm;
172
173 timeout_t initPause = { 3000 };
174 uniset::uniset_rwmutex mutex_start;
175
176 bool force = { false };
177 bool force_out = { false };
179 PassiveTimer ptHeartBeat;
180 uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
181 long maxHeartBeat = { 10 };
182 IOController::IOStateList::iterator itHeartBeat;
183 uniset::ObjectId sidTestSMReady = {uniset::DefaultObjectId };
184
186 IOController::IOStateList::iterator itExchangeMode;
187 std::atomic<MBConfig::ExchangeMode> exchangeMode = { MBConfig::emNone };
188 std::atomic<MBConfig::ExchangeMode> sensorExchangeMode = { MBConfig::emNone };
190 std::atomic_bool activated = { false };
191 std::atomic_bool canceled = { false };
192 timeout_t activateTimeout = { 20000 }; // msec
193 bool notUseExchangeTimer = { false };
194
195 timeout_t stat_time = { 0 };
196 size_t poll_count = { 0 };
198 std::string statInfo = { "" };
199
200 std::shared_ptr<ModbusClient> mb;
201
203 Trigger trReopen;
204
207 std::shared_ptr<LogAgregator> loga;
208 std::shared_ptr<DebugStream> mblog;
209 std::shared_ptr<LogServer> logserv;
210 std::string logserv_host = {""};
211 int logserv_port = {0};
212 const std::shared_ptr<SharedMemory> ic;
213
214 VMonitor vmon;
215
216 size_t ncycle = { 0 };
218 std::shared_ptr<uniset::MBConfig> mbconf;
219 uniset::uniset_rwmutex mutex_conf;
220
221 private:
222 MBExchange();
223
224 };
225 // --------------------------------------------------------------------------
226} // end of namespace uniset
227// -----------------------------------------------------------------------------
228#endif // _MBExchange_H_
229// -----------------------------------------------------------------------------
@ emNone
Definition MBConfig.h:53
Definition MBExchange.h:60
size_t ncycle
Definition MBExchange.h:216
std::atomic< MBConfig::ExchangeMode > exchangeMode
Definition MBExchange.h:187
uniset::ObjectId sidExchangeMode
Definition MBExchange.h:185
PassiveTimer ptReopen
Definition MBExchange.h:202
bool force_out
Definition MBExchange.h:177
PassiveTimer ptInitChannel
Definition MBExchange.h:205
bool httpEnabledSetParams
Definition MBExchange.h:135
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition MBExchange.cc:337
void updateRSProperty(MBConfig::RSProperty *p, bool write_only=false)
Definition MBExchange.cc:1196
void updateMTR(MBConfig::RegMap::iterator &it)
Definition MBExchange.cc:1684
static void help_print(int argc, const char *const *argv)
Definition MBExchange.cc:208
std::atomic_bool httpControlActive
Definition MBExchange.h:134
std::atomic< MBConfig::ExchangeMode > sensorExchangeMode
Definition MBExchange.h:188
timeout_t stat_time
Definition MBExchange.h:195
bool httpControlAllow
Definition MBExchange.h:133
PassiveTimer ptStatistic
Definition MBExchange.h:197
bool force
Definition MBExchange.h:176
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition MBExchange.cc:2096
Пассивный таймер
Definition PassiveTimer.h:94
Definition MessageType.h:127
Definition MessageType.h:171
Definition MessageType.h:214
Definition Trigger.h:31
Definition USingleProcess.h:28
Definition UniSetObject.h:80
Definition UniXML.h:44
Definition Mutex.h:32
Definition AccessConfig.h:30
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition MBConfig.h:92
Definition UniSetTypes_i.idl:65
Definition UHttpRequestHandler.h:87