UniSet @VERSION@
MBTCPMultiMaster.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 _MBTCPMultiMaster_H_
18#define _MBTCPMultiMaster_H_
19// -----------------------------------------------------------------------------
20#include <ostream>
21#include <string>
22#include <map>
23#include <vector>
24#include "MBExchange.h"
25#include "modbus/ModbusTCPMaster.h"
26// -------------------------------------------------------------------------
27namespace uniset
28{
29 // -----------------------------------------------------------------------------
258 // -----------------------------------------------------------------------------
269 public MBExchange
270 {
271 public:
272 MBTCPMultiMaster( uniset::ObjectId objId, xmlNode* cnode,
273 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
274 const std::string& prefix = "mbtcp" );
275 virtual ~MBTCPMultiMaster();
276
278 static std::shared_ptr<MBTCPMultiMaster> init_mbmaster(int argc, const char* const* argv,
279 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
280 const std::string& prefix = "mbtcp" );
281
282 static void help_print( int argc, const char* const* argv );
283
284 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
285
286 protected:
287 virtual void sysCommand( const uniset::SystemMessage* sm ) override;
288 virtual void initIterators() override;
289 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override;
290#ifndef DISABLE_REST_API
291 virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root ) override;
292#endif
293 virtual bool deactivateObject() override;
294 virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf ) override;
295 void initCheckConnectionParameters();
296 void initGateList( uniset::UniXML::iterator it, const std::shared_ptr<uniset::MBConfig>& mbconf );
297
298 void poll_thread();
299 void check_thread();
300 void final_thread();
301
303 bool force_disconnect;
304 timeout_t checktime;
305 timeout_t defaultIgnoreTimeout;
306 timeout_t channelTimeout;
307
308 private:
310
311 struct MBSlaveInfo
312 {
313 MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0),
314 respond(false), respond_id(uniset::DefaultObjectId), respond_invert(false),
315 recv_timeout(200), aftersend_pause(0), sleepPause_usec(100),
316 force_disconnect(true),
317 myname(""), use(false), initOK(false), ignore(false) {}
318
319 std::string ip;
320 int port;
321 std::shared_ptr<ModbusTCPMaster> mbtcp;
322 int priority;
323
324 // параметры для проверки соединения..
325 ModbusRTU::SlaveFunctionCode checkFunc = { ModbusRTU::fnUnknown };
326 ModbusRTU::ModbusAddr checkAddr = { 0x00 };
327 ModbusRTU::ModbusData checkReg = { 0 };
328
329 bool respond;
330 uniset::ObjectId respond_id;
331 IOController::IOStateList::iterator respond_it;
332 bool respond_invert;
333 bool respond_init = { false };
334 bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки
335 DelayTimer respondDelay;
336 timeout_t channel_timeout = { 0 };
337
338 inline bool operator < ( const MBSlaveInfo& mbs ) const noexcept
339 {
340 return priority < mbs.priority;
341 }
342
343 bool init( std::shared_ptr<DebugStream>& mblog );
344 bool check();
345 void setUse( bool st );
346
347 timeout_t recv_timeout;
348 timeout_t aftersend_pause;
349 timeout_t sleepPause_usec;
350 bool force_disconnect;
351
352 std::string myname;
353
354 bool use = { false }; // флаг используется ли в данный момент этот канал
355 bool initOK = { false };
356 bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть.
357 PassiveTimer ptIgnoreTimeout;
358
359 const std::string getShortInfo() const;
360
361 std::mutex mutInit;
362 };
363
364 typedef std::list<std::shared_ptr<MBSlaveInfo>> MBGateList;
365
366 MBGateList mblist;
367 MBGateList::reverse_iterator mbi;
368
369 // т.к. TCP может "зависнуть" на подключении к недоступному узлу
370 // делаем опрос в отдельном потоке
371 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread;
372 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread;
373 };
374 // --------------------------------------------------------------------------
375} // end of namespace uniset
376// -----------------------------------------------------------------------------
377#endif // _MBTCPMultiMaster_H_
378// -----------------------------------------------------------------------------
Definition DelayTimer.h:30
Definition MBExchange.h:60
Definition MBTCPMultiMaster.h:270
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition MBTCPMultiMaster.cc:574
static std::shared_ptr< MBTCPMultiMaster > init_mbmaster(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="mbtcp")
Definition MBTCPMultiMaster.cc:698
Пассивный таймер
Definition PassiveTimer.h:94
Definition MessageType.h:171
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 UniSetTypes_i.idl:65