UniSet  2.7.0
IOControl.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 IOControl_H_
18 #define IOControl_H_
19 // -----------------------------------------------------------------------------
20 #include <vector>
21 #include <memory>
22 #include <deque>
23 #include <string>
24 #include "UniXML.h"
25 #include "ThreadCreator.h"
26 #include "PassiveTimer.h"
27 #include "Trigger.h"
28 #include "IONotifyController.h"
29 #include "UniSetObject.h"
30 #include "Mutex.h"
31 #include "MessageType.h"
32 #include "ComediInterface.h"
33 #include "DigitalFilter.h"
34 #include "Calibration.h"
35 #include "SMInterface.h"
36 #include "IOController.h"
37 #include "IOBase.h"
38 #include "SharedMemory.h"
39 #include "LogServer.h"
40 #include "DebugStream.h"
41 #include "LogAgregator.h"
42 // -------------------------------------------------------------------------
43 #ifndef vmonit
44 #define vmonit( var ) vmon.add( #var, var )
45 #endif
46 // -------------------------------------------------------------------------
47 namespace uniset
48 {
49  // ---------------------------------------------------------------------
188  // -----------------------------------------------------------------------------
191  class CardList:
192  public std::vector<ComediInterface*>
193  {
194  public:
195 
196  explicit CardList( size_t size ) : std::vector<ComediInterface * >(size) { }
197 
198  ~CardList()
199  {
200  for( size_t i = 0; i < size(); i++ )
201  delete (*this)[i];
202  }
203 
204  inline ComediInterface* getCard( size_t ncard ) const
205  {
206  if( ncard < size() )
207  return (*this)[ncard];
208 
209  return nullptr;
210  }
211 
212  };
213 
229  class IOControl:
230  public UniSetObject
231  {
232  public:
233  IOControl( uniset::ObjectId id, uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& shm = nullptr, size_t numcards = 2, const std::string& prefix = "io" );
234  virtual ~IOControl();
235 
237  static std::shared_ptr<IOControl> init_iocontrol( int argc, const char* const* argv,
238  uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& ic = nullptr,
239  const std::string& prefix = "io" );
241  static void help_print( int argc, const char* const* argv );
242 
243  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
244 
246  struct IOInfo:
247  public IOBase
248  {
249  // т.к. IOBase содержит rwmutex с запрещённым конструктором копирования
250  // приходится здесь тоже объявлять разрешенными только операции "перемещения"
251  IOInfo( const IOInfo& r ) = delete;
252  IOInfo& operator=(const IOInfo& r) = delete;
253  IOInfo( IOInfo&& r ) = default;
254  IOInfo& operator=(IOInfo&& r) = default;
255 
256  IOInfo():
257  subdev(DefaultSubdev), channel(DefaultChannel),
258  ncard(-1),
259  aref(0),
260  range(0),
261  lamp(false),
262  no_testlamp(false),
263  enable_testmode(false),
264  disable_testmode(false)
265  {}
266 
267 
268  int subdev;
269  int channel;
270  int ncard;
278  int aref;
279 
280  int adelay;
288  int range;
289 
290  bool lamp;
291  bool no_testlamp;
295  friend std::ostream& operator<<(std::ostream& os, const IOInfo& inf );
296  friend std::ostream& operator<<(std::ostream& os, const std::shared_ptr<IOInfo>& inf );
297  };
298 
299  struct IOPriority
300  {
301  IOPriority(size_t p, size_t i):
302  priority(p), index(i) {}
303 
304  size_t priority;
305  size_t index;
306  };
307 
309  {
310  tmNone = 0,
311  tmOffPoll = 1,
312  tmConfigEnable = 2,
313  tmConfigDisable = 3,
314  tmOnlyInputs = 4,
315  tmOnlyOutputs = 5
316  };
317 
318  protected:
319 
320  void iopoll();
321  void ioread( std::shared_ptr<IOInfo>& it );
322  void check_testlamp();
323  void check_testmode();
324  void blink();
325  void iothread();
326 
327  // действия при завершении работы
328  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
329  virtual void askSensors( UniversalIO::UIOCommand cmd );
330  virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
331  virtual void timerInfo( const uniset::TimerMessage* tm ) override;
332  virtual bool activateObject() override;
333  virtual bool deactivateObject() override;
334 
335  // начальная инициализация выходов
336  void initOutputs();
337 
338  // инициализация карты (каналов в/в)
339  void initIOCard();
340 
341  // чтение файла конфигурации
342  void readConfiguration();
343  bool initIOItem( UniXML::iterator& it );
344  bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
345  void buildCardsList();
346 
347  bool waitSM();
348 
349  xmlNode* confnode = { 0 };
351  int polltime = { 150 };
353  bool noCards = { false };
354 
355  typedef std::vector< std::shared_ptr<IOInfo> > IOMap;
356  IOMap iomap;
358  typedef std::deque<IOPriority> PIOMap;
359  PIOMap pmap;
361  size_t maxItem = { 0 };
362  size_t maxHalf = { 0 };
363  int filtersize = { 0 };
364  float filterT = { 0.0 };
365 
366  std::string s_field;
367  std::string s_fvalue;
368 
369  std::shared_ptr<SMInterface> shm;
371  std::string prefix;
372 
373  typedef std::list<std::shared_ptr<IOInfo>> BlinkList;
374 
375  void addBlink( std::shared_ptr<IOInfo>& it, BlinkList& lst );
376  void delBlink( std::shared_ptr<IOInfo>& it, BlinkList& lst );
377  void blink( BlinkList& lst, bool& bstate );
378 
379  // обычное мигание
380  BlinkList lstBlink;
381  PassiveTimer ptBlink;
382  bool blink_state = { false };
383 
384  // мигание с двойной частотой
385  BlinkList lstBlink2;
386  PassiveTimer ptBlink2;
387  bool blink2_state = { false };
388 
389  // мигание с тройной частотой
390  BlinkList lstBlink3;
391  PassiveTimer ptBlink3;
392  bool blink3_state = { false };
393 
394  uniset::ObjectId testLamp_s = { uniset::DefaultObjectId };
395  Trigger trTestLamp;
396  bool isTestLamp = { false };
397  IOController::IOStateList::iterator itTestLamp;
398 
399  PassiveTimer ptHeartBeat;
400  uniset::ObjectId sidHeartBeat;
401  int maxHeartBeat = { 10 };
402  IOController::IOStateList::iterator itHeartBeat;
403 
404  bool force = { false };
405  bool force_out = { false };
406  timeout_t smReadyTimeout = { 15000 };
407  ssize_t defCardNum = { -1 };
408  size_t maxCardNum = { 10 };
410  std::mutex iopollMutex;
411  std::atomic_bool activated = { false };
412  std::atomic_bool cancelled = { false };
413  bool readconf_ok = { false };
414  int activateTimeout;
415  uniset::ObjectId sidTestSMReady = { uniset::DefaultObjectId };
416 
417  uniset::ObjectId testMode_as = { uniset::DefaultObjectId };
418  IOController::IOStateList::iterator itTestMode;
419  long testmode = { false };
420  long prev_testmode = { false };
421 
422  std::shared_ptr<LogAgregator> loga;
423  std::shared_ptr<DebugStream> iolog;
424  std::shared_ptr<LogServer> logserv;
425  std::string logserv_host = {""};
426  int logserv_port = {0};
427 
428  std::shared_ptr< ThreadCreator<IOControl> > ioThread;
429 
430  VMonitor vmon;
431 
432  private:
433  };
434  // --------------------------------------------------------------------------
435 } // end of namespace uniset
436 // -----------------------------------------------------------------------------
437 #endif // IOControl_H_
438 // -----------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:92
Definition: IOControl.h:246
Definition: CallbackTimer.h:29
int ncard
Definition: IOControl.h:270
Definition: IOControl.h:299
int aref
Definition: IOControl.h:278
Definition: UniXML.h:43
IOMap iomap
Definition: IOControl.h:356
Definition: ComediInterface.h:28
Definition: MessageType.h:168
Definition: UniSetObject.h:73
Definition: VMonitor.h:116
TestModeID
Definition: IOControl.h:308
std::mutex iopollMutex
Definition: IOControl.h:410
bool disable_testmode
Definition: IOControl.h:293
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:69
Definition: Trigger.h:29
CardList cards
Definition: IOControl.h:352
bool no_testlamp
Definition: IOControl.h:291
Definition: IOControl.h:229
int subdev
Definition: IOControl.h:268
bool enable_testmode
Definition: IOControl.h:292
Definition: MessageType.h:124
bool lamp
Definition: IOControl.h:290
int range
Definition: IOControl.h:288
Definition: IOBase.h:34
Definition: MessageType.h:211
PIOMap pmap
Definition: IOControl.h:359
int channel
Definition: IOControl.h:269
Definition: UniSetTypes_i.idl:64
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: IOControl.h:191