UniSet 2.41.2
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 // -----------------------------------------------------------------------------
268 class MBTCPMultiMaster:
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 virtual bool deactivateObject() override;
291 virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf ) override;
292 void initCheckConnectionParameters();
293 void initGateList( uniset::UniXML::iterator it, const std::shared_ptr<uniset::MBConfig>& mbconf );
294
295 void poll_thread();
296 void check_thread();
297 void final_thread();
298
300 bool force_disconnect;
301 timeout_t checktime;
302 timeout_t defaultIgnoreTimeout;
303 timeout_t channelTimeout;
304
305 private:
306 MBTCPMultiMaster();
307
308 struct MBSlaveInfo
309 {
310 MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0),
311 respond(false), respond_id(uniset::DefaultObjectId), respond_invert(false),
312 recv_timeout(200), aftersend_pause(0), sleepPause_usec(100),
313 force_disconnect(true),
314 myname(""), use(false), initOK(false), ignore(false) {}
315
316 std::string ip;
317 int port;
318 std::shared_ptr<ModbusTCPMaster> mbtcp;
319 int priority;
320
321 // параметры для проверки соединения..
322 ModbusRTU::SlaveFunctionCode checkFunc = { ModbusRTU::fnUnknown };
323 ModbusRTU::ModbusAddr checkAddr = { 0x00 };
324 ModbusRTU::ModbusData checkReg = { 0 };
325
326 bool respond;
327 uniset::ObjectId respond_id;
328 IOController::IOStateList::iterator respond_it;
329 bool respond_invert;
330 bool respond_init = { false };
331 bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки
332 DelayTimer respondDelay;
333 timeout_t channel_timeout = { 0 };
334
335 inline bool operator < ( const MBSlaveInfo& mbs ) const noexcept
336 {
337 return priority < mbs.priority;
338 }
339
340 bool init( std::shared_ptr<DebugStream>& mblog );
341 bool check();
342 void setUse( bool st );
343
344 timeout_t recv_timeout;
345 timeout_t aftersend_pause;
346 timeout_t sleepPause_usec;
347 bool force_disconnect;
348
349 std::string myname;
350
351 bool use = { false }; // флаг используется ли в данный момент этот канал
352 bool initOK = { false };
353 bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть.
354 PassiveTimer ptIgnoreTimeout;
355
356 const std::string getShortInfo() const;
357
358 std::mutex mutInit;
359 };
360
361 typedef std::list<std::shared_ptr<MBSlaveInfo>> MBGateList;
362
363 MBGateList mblist;
364 MBGateList::reverse_iterator mbi;
365
366 // т.к. TCP может "зависнуть" на подключении к недоступному узлу
367 // делаем опрос в отдельном потоке
368 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread;
369 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread;
370 };
371 // --------------------------------------------------------------------------
372} // end of namespace uniset
373// -----------------------------------------------------------------------------
374#endif // _MBTCPMultiMaster_H_
375// -----------------------------------------------------------------------------
Определения DelayTimer.h:30
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы).
Определения MBTCPMultiMaster.cc:575
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")
Определения MBTCPMultiMaster.cc:699
Пассивный таймер
Определения PassiveTimer.h:94
Определения MessageType.h:171
Определения Mutex.h:32
Определения Calibration.h:27
const ObjectId DefaultObjectId
Определения UniSetTypes.h:71
long ObjectId
Определения UniSetTypes_i.idl:30
Определения UniSetTypes_i.idl:65