UniSet 2.32.1
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// -------------------------------------------------------------------------
50namespace uniset
51{
52 // -----------------------------------------------------------------------------
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 static uint8_t firstBit( uint16_t mask );
89
90 // offset = firstBit(mask)
91 static uint16_t getBits( uint16_t value, uint16_t mask, uint8_t offset );
92 // if mask = 0 return value
93 static uint16_t setBits( uint16_t value, uint16_t set, uint16_t mask, uint8_t offset );
94 // if mask=0 return set
95 static uint16_t forceSetBits( uint16_t value, uint16_t set, uint16_t mask, uint8_t offset );
96
97 protected:
98 virtual void step();
99 virtual void sysCommand( const uniset::SystemMessage* msg ) override;
100 virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
101 virtual void timerInfo( const uniset::TimerMessage* tm ) override;
102 virtual void askSensors( UniversalIO::UIOCommand cmd );
103 virtual void initOutput();
104 virtual bool deactivateObject() override;
105 virtual bool activateObject() override;
106 virtual void initIterators();
107 virtual void initValues();
108 virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf );
109#ifndef DISABLE_REST_API
110 // http API
111 virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
112 virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p ) override;
113#endif
114 void firstInitRegisters();
115 bool preInitRead( MBConfig::InitList::iterator& p );
116 bool initSMValue( ModbusRTU::ModbusData* data, int count, MBConfig::RSProperty* p );
117 bool allInitOK;
118
119 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) = 0;
120
121 virtual bool poll();
122 bool pollRTU( std::shared_ptr<MBConfig::RTUDevice>& dev, MBConfig::RegMap::iterator& it );
123
124 void updateSM();
125
126 // в функции передаётся итератор,
127 // т.к. в них идёт итерирование в случае если запрос в несколько регистров
128 void updateRTU(MBConfig::RegMap::iterator& it);
129 void updateMTR(MBConfig::RegMap::iterator& it);
130 void updateRTU188(MBConfig::RegMap::iterator& it);
131 void updateRSProperty( MBConfig::RSProperty* p, bool write_only = false );
132 virtual void updateRespondSensors();
133
134 bool isUpdateSM( bool wrFunc, long devMode ) const noexcept;
135 bool isPollEnabled( bool wrFunc ) const noexcept;
136 bool isSafeMode( std::shared_ptr<MBConfig::RTUDevice>& dev ) const noexcept;
137
138 bool isProcActive() const;
139 void setProcActive( bool st );
140 bool waitSMReady();
141
142 bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
143 bool initItem( UniXML::iterator& it );
144 void initOffsetList();
145 std::string initPropPrefix( const std::string& def_prop_prefix );
146
147 xmlNode* cnode = { 0 };
148 std::shared_ptr<SMInterface> shm;
149
150 timeout_t initPause = { 3000 };
151 uniset::uniset_rwmutex mutex_start;
152
153 bool force = { false };
154 bool force_out = { false };
156 PassiveTimer ptHeartBeat;
157 uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
158 long maxHeartBeat = { 10 };
159 IOController::IOStateList::iterator itHeartBeat;
160 uniset::ObjectId sidTestSMReady = {uniset::DefaultObjectId };
161
163 IOController::IOStateList::iterator itExchangeMode;
166 std::atomic_bool activated = { false };
167 std::atomic_bool canceled = { false };
168 timeout_t activateTimeout = { 20000 }; // msec
169 bool notUseExchangeTimer = { false };
170
171 timeout_t stat_time = { 0 };
172 size_t poll_count = { 0 };
174 std::string statInfo = { "" };
175
176 std::shared_ptr<ModbusClient> mb;
177
179 Trigger trReopen;
180
183 std::shared_ptr<LogAgregator> loga;
184 std::shared_ptr<DebugStream> mblog;
185 std::shared_ptr<LogServer> logserv;
186 std::string logserv_host = {""};
187 int logserv_port = {0};
188 const std::shared_ptr<SharedMemory> ic;
189
190 VMonitor vmon;
191
192 size_t ncycle = { 0 };
194 std::shared_ptr<uniset::MBConfig> mbconf;
195 uniset::uniset_rwmutex mutex_conf;
196
197 private:
198 MBExchange();
199
200 };
201 // --------------------------------------------------------------------------
202} // end of namespace uniset
203// -----------------------------------------------------------------------------
204#endif // _MBExchange_H_
205// -----------------------------------------------------------------------------
@ emNone
Definition MBConfig.h:53
Definition MBExchange.h:58
size_t ncycle
Definition MBExchange.h:192
uniset::ObjectId sidExchangeMode
Definition MBExchange.h:162
PassiveTimer ptReopen
Definition MBExchange.h:178
long exchangeMode
Definition MBExchange.h:164
bool force_out
Definition MBExchange.h:154
PassiveTimer ptInitChannel
Definition MBExchange.h:181
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition MBExchange.cc:310
void updateRSProperty(MBConfig::RSProperty *p, bool write_only=false)
Definition MBExchange.cc:1162
void updateMTR(MBConfig::RegMap::iterator &it)
Definition MBExchange.cc:1646
static void help_print(int argc, const char *const *argv)
Definition MBExchange.cc:203
timeout_t stat_time
Definition MBExchange.h:171
PassiveTimer ptStatistic
Definition MBExchange.h:173
bool force
Definition MBExchange.h:153
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition MBExchange.cc:2050
Пассивный таймер
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 Calibration.h:27
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition MBConfig.h:89
Definition UniSetTypes_i.idl:65