UniSet  2.0.0
IOControl/IOControl.h
00001 // -----------------------------------------------------------------------------
00002 #ifndef IOControl_H_
00003 #define IOControl_H_
00004 // -----------------------------------------------------------------------------
00005 #include <vector>
00006 #include <deque>
00007 #include <string>
00008 #include "UniXML.h"
00009 #include "PassiveTimer.h"
00010 #include "Trigger.h"
00011 #include "IONotifyController.h"
00012 #include "UniSetObject_LT.h"
00013 #include "Mutex.h"
00014 #include "MessageType.h"
00015 #include "ComediInterface.h" 
00016 #include "DigitalFilter.h" 
00017 #include "Calibration.h" 
00018 #include "SMInterface.h" 
00019 #include "SingleProcess.h"
00020 #include "IOController.h" 
00021 #include "IOBase.h" 
00022 #include "SharedMemory.h" 
00023 // -----------------------------------------------------------------------------
00161 // -----------------------------------------------------------------------------
00164 class CardList:
00165     public std::vector<ComediInterface*>
00166 {
00167     public:
00168 
00169         CardList(int size) : std::vector<ComediInterface*>(size) { }
00170 
00171         ~CardList() {
00172             for( unsigned int i=0; i<size(); i++ )
00173                 delete (*this)[i];
00174         }
00175 
00176         inline ComediInterface* getCard(int ncard) {
00177             if( ncard >= 0 && ncard < (int)size() )
00178                 return (*this)[ncard];
00179             return NULL;
00180     }
00181 
00182 };
00183 
00199 class IOControl:
00200     public UniSetObject
00201 {
00202     public:
00203         IOControl( UniSetTypes::ObjectId id, UniSetTypes::ObjectId icID, SharedMemory* ic=0, int numcards=2, const std::string& prefix="io" );
00204         virtual ~IOControl();
00205 
00207         static IOControl* init_iocontrol( int argc, const char* const* argv,
00208                                             UniSetTypes::ObjectId icID, SharedMemory* ic=0,
00209                                             const std::string& prefix="io" );
00211         static void help_print( int argc, const char* const* argv );
00212 
00214         struct IOInfo:
00215             public IOBase
00216         {
00217             // т.к. IOBase содержит rwmutex с запрещённым конструктором копирования
00218             // приходится здесь тоже объявлять разрешенными только операции "перемещения"
00219             IOInfo( const IOInfo& r ) = delete;
00220             IOInfo& operator=(const IOInfo& r) = delete;
00221             IOInfo( IOInfo&& r ) = default;
00222             IOInfo& operator=(IOInfo&& r) = default;
00223 
00224             IOInfo():
00225                 subdev(DefaultSubdev),channel(DefaultChannel),
00226                 ncard(-1),
00227                 aref(0),
00228                 range(0),
00229                 lamp(false),
00230                 no_testlamp(false),
00231                 enable_testmode(false),
00232                 disable_testmode(false)
00233             {}
00234 
00235 
00236             int subdev;     
00237             int channel;    
00238             int ncard;      
00246             int aref;
00247 
00254             int range;
00255 
00256             bool lamp;             
00257             bool no_testlamp;      
00258             bool enable_testmode;  
00259             bool disable_testmode; 
00261             friend std::ostream& operator<<(std::ostream& os, IOInfo& inf );
00262         };
00263 
00264         struct IOPriority
00265         {
00266             IOPriority(int p, int i):
00267                 priority(p),index(i){}
00268 
00269             int priority;
00270             int index;
00271         };
00272 
00273         enum TestModeID
00274         {
00275             tmNone        = 0,       
00276             tmOffPoll    = 1,        
00277             tmConfigEnable    = 2,   
00278             tmConfigDisable    = 3,  
00279             tmOnlyInputs    = 4,     
00280             tmOnlyOutputs    = 5     
00281         };
00282 
00283         void execute();
00284 
00285     protected:
00286 
00287         void iopoll(); 
00288         void ioread( IOInfo* it );
00289         void check_testlamp();
00290         void check_testmode();
00291         void blink();
00292 
00293         // действия при завершении работы
00294         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) override;
00295         virtual void askSensors( UniversalIO::UIOCommand cmd );
00296         virtual void sensorInfo( const UniSetTypes::SensorMessage* sm ) override;
00297         virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
00298         virtual void sigterm( int signo ) override;
00299         virtual bool activateObject() override;
00300 
00301         // начальная инициализация выходов
00302         void initOutputs();
00303 
00304         // инициализация карты (каналов в/в)
00305         void initIOCard();
00306 
00307         // чтение файла конфигурации
00308         void readConfiguration();
00309         bool initIOItem( UniXML::iterator& it );
00310         bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
00311         void buildCardsList();
00312 
00313         void waitSM();
00314 
00315         bool checkCards( const std::string& func="" );
00316 
00317         xmlNode* cnode; 
00319         int polltime;   
00320         CardList cards; 
00321         bool noCards;
00322 
00323         typedef std::vector<IOInfo> IOMap;
00324         IOMap iomap;    
00326         typedef std::deque<IOPriority> PIOMap;
00327         PIOMap pmap;    
00329         unsigned int maxItem;    
00330         unsigned int maxHalf;
00331         int filtersize;
00332         float filterT;
00333 
00334         std::string s_field;
00335         std::string s_fvalue;
00336 
00337         SMInterface* shm;
00338         UInterface ui;
00339         UniSetTypes::ObjectId myid;
00340         std::string prefix;
00341 
00342         typedef std::list<IOInfo*> BlinkList;
00343 
00344         void addBlink( IOInfo* it, BlinkList& lst );
00345         void delBlink( IOInfo* it, BlinkList& lst );
00346         void blink( BlinkList& lst, bool& bstate );
00347 
00348         // обычное мигание
00349         BlinkList lstBlink;
00350         PassiveTimer ptBlink;
00351         bool blink_state;
00352 
00353         // мигание с двойной частотой
00354         BlinkList lstBlink2;
00355         PassiveTimer ptBlink2;
00356         bool blink2_state;
00357 
00358         // мигание с тройной частотой
00359         BlinkList lstBlink3;
00360         PassiveTimer ptBlink3;
00361         bool blink3_state;
00362 
00363         UniSetTypes::ObjectId testLamp_S;
00364         Trigger trTestLamp;
00365         bool isTestLamp;
00366         IOController::IOStateList::iterator itTestLamp;
00367 
00368         PassiveTimer ptHeartBeat;
00369         UniSetTypes::ObjectId sidHeartBeat;
00370         int maxHeartBeat;
00371         IOController::IOStateList::iterator itHeartBeat;
00372 
00373         bool force;            
00374         bool force_out;        
00375         int smReadyTimeout;    
00376         int defCardNum;        
00377         int maxCardNum;        
00379         UniSetTypes::uniset_mutex iopollMutex;
00380         std::atomic_bool activated;
00381         bool readconf_ok;
00382         int activateTimeout;
00383         UniSetTypes::ObjectId sidTestSMReady;
00384         bool term;
00385 
00386 
00387         UniSetTypes::ObjectId testMode_as;
00388         IOController::IOStateList::iterator itTestMode;
00389         long testmode;
00390         long prev_testmode;
00391 
00392     private:
00393 };
00394 // -----------------------------------------------------------------------------
00395 #endif // IOControl_H_
00396 // -----------------------------------------------------------------------------