UniSet  2.6.0
IOControl.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // -----------------------------------------------------------------------------
00017 #ifndef IOControl_H_
00018 #define IOControl_H_
00019 // -----------------------------------------------------------------------------
00020 #include <vector>
00021 #include <memory>
00022 #include <deque>
00023 #include <string>
00024 #include "UniXML.h"
00025 #include "PassiveTimer.h"
00026 #include "Trigger.h"
00027 #include "IONotifyController.h"
00028 #include "UniSetObject.h"
00029 #include "Mutex.h"
00030 #include "MessageType.h"
00031 #include "ComediInterface.h"
00032 #include "DigitalFilter.h"
00033 #include "Calibration.h"
00034 #include "SMInterface.h"
00035 #include "SingleProcess.h"
00036 #include "IOController.h"
00037 #include "IOBase.h"
00038 #include "SharedMemory.h"
00039 #include "LogServer.h"
00040 #include "DebugStream.h"
00041 #include "LogAgregator.h"
00042 // -------------------------------------------------------------------------
00043 namespace uniset
00044 {
00045 // -----------------------------------------------------------------------------
00184 // -----------------------------------------------------------------------------
00187 class CardList:
00188     public std::vector<ComediInterface*>
00189 {
00190     public:
00191 
00192         explicit CardList( size_t size ) : std::vector<ComediInterface * >(size) { }
00193 
00194         ~CardList()
00195         {
00196             for( size_t i = 0; i < size(); i++ )
00197                 delete (*this)[i];
00198         }
00199 
00200         inline ComediInterface* getCard(int ncard) const
00201         {
00202             if( ncard >= 0 && ncard < (int)size() )
00203                 return (*this)[ncard];
00204 
00205             return NULL;
00206         }
00207 
00208 };
00209 
00225 class IOControl:
00226     public UniSetObject
00227 {
00228     public:
00229         IOControl( uniset::ObjectId id, uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& shm = nullptr, int numcards = 2, const std::string& prefix = "io" );
00230         virtual ~IOControl();
00231 
00233         static std::shared_ptr<IOControl> init_iocontrol( int argc, const char* const* argv,
00234                                                           uniset::ObjectId icID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00235                                                           const std::string& prefix = "io" );
00237         static void help_print( int argc, const char* const* argv );
00238 
00240         struct IOInfo:
00241             public IOBase
00242         {
00243             // т.к. IOBase содержит rwmutex с запрещённым конструктором копирования
00244             // приходится здесь тоже объявлять разрешенными только операции "перемещения"
00245             IOInfo( const IOInfo& r ) = delete;
00246             IOInfo& operator=(const IOInfo& r) = delete;
00247             IOInfo( IOInfo&& r ) = default;
00248             IOInfo& operator=(IOInfo&& r) = default;
00249 
00250             IOInfo():
00251                 subdev(DefaultSubdev), channel(DefaultChannel),
00252                 ncard(-1),
00253                 aref(0),
00254                 range(0),
00255                 lamp(false),
00256                 no_testlamp(false),
00257                 enable_testmode(false),
00258                 disable_testmode(false)
00259             {}
00260 
00261 
00262             int subdev;     
00263             int channel;    
00264             int ncard;      
00272             int aref;
00273 
00280             int range;
00281 
00282             bool lamp;             
00283             bool no_testlamp;      
00284             bool enable_testmode;  
00285             bool disable_testmode; 
00287             friend std::ostream& operator<<(std::ostream& os, IOInfo& inf );
00288         };
00289 
00290         struct IOPriority
00291         {
00292             IOPriority(size_t  p, size_t i):
00293                 priority(p), index(i) {}
00294 
00295             size_t  priority;
00296             size_t index;
00297         };
00298 
00299         enum TestModeID
00300         {
00301             tmNone       = 0,     
00302             tmOffPoll    = 1,     
00303             tmConfigEnable  = 2,  
00304             tmConfigDisable = 3,  
00305             tmOnlyInputs    = 4,  
00306             tmOnlyOutputs   = 5   
00307         };
00308 
00309         void execute();
00310 
00311     protected:
00312 
00313         void iopoll(); 
00314         void ioread( IOInfo* it );
00315         void check_testlamp();
00316         void check_testmode();
00317         void blink();
00318 
00319         // действия при завершении работы
00320         virtual void sysCommand( const uniset::SystemMessage* sm ) override;
00321         virtual void askSensors( UniversalIO::UIOCommand cmd );
00322         virtual void sensorInfo( const uniset::SensorMessage* sm ) override;
00323         virtual void timerInfo( const uniset::TimerMessage* tm ) override;
00324         virtual void sigterm( int signo ) override;
00325         virtual bool activateObject() override;
00326 
00327         // начальная инициализация выходов
00328         void initOutputs();
00329 
00330         // инициализация карты (каналов в/в)
00331         void initIOCard();
00332 
00333         // чтение файла конфигурации
00334         void readConfiguration();
00335         bool initIOItem( UniXML::iterator& it );
00336         bool readItem( const std::shared_ptr<UniXML>& xml, UniXML::iterator& it, xmlNode* sec );
00337         void buildCardsList();
00338 
00339         void waitSM();
00340 
00341         xmlNode* confnode = { 0 }; 
00343         int polltime = { 150 };   
00344         CardList cards; 
00345         bool noCards = { false };
00346 
00347         typedef std::vector<IOInfo> IOMap;
00348         IOMap iomap;    
00350         typedef std::deque<IOPriority> PIOMap;
00351         PIOMap pmap;    
00353         unsigned int maxItem = { 0 };    
00354         unsigned int maxHalf = { 0 };
00355         int filtersize = { 0 };
00356         float filterT = { 0.0 };
00357 
00358         std::string s_field;
00359         std::string s_fvalue;
00360 
00361         std::shared_ptr<SMInterface> shm;
00362         uniset::ObjectId myid = { uniset::DefaultObjectId };
00363         std::string prefix;
00364 
00365         typedef std::list<IOInfo*> BlinkList;
00366 
00367         void addBlink( IOInfo* it, BlinkList& lst );
00368         void delBlink( IOInfo* it, BlinkList& lst );
00369         void blink( BlinkList& lst, bool& bstate );
00370 
00371         // обычное мигание
00372         BlinkList lstBlink;
00373         PassiveTimer ptBlink;
00374         bool blink_state = { false };
00375 
00376         // мигание с двойной частотой
00377         BlinkList lstBlink2;
00378         PassiveTimer ptBlink2;
00379         bool blink2_state = { false };
00380 
00381         // мигание с тройной частотой
00382         BlinkList lstBlink3;
00383         PassiveTimer ptBlink3;
00384         bool blink3_state = { false };
00385 
00386         uniset::ObjectId testLamp_s = { uniset::DefaultObjectId };
00387         Trigger trTestLamp;
00388         bool isTestLamp = { false };
00389         IOController::IOStateList::iterator itTestLamp;
00390 
00391         PassiveTimer ptHeartBeat;
00392         uniset::ObjectId sidHeartBeat;
00393         int maxHeartBeat = { 10 };
00394         IOController::IOStateList::iterator itHeartBeat;
00395 
00396         bool force = { false };            
00397         bool force_out = { false };        
00398         timeout_t smReadyTimeout = { 15000 };    
00399         int defCardNum = { -1 };        
00400         int maxCardNum = { 10 };        
00402         std::mutex iopollMutex;
00403         std::atomic_bool activated = { false };
00404         bool readconf_ok = { false };
00405         int activateTimeout;
00406         uniset::ObjectId sidTestSMReady = { uniset::DefaultObjectId };
00407         std::atomic_bool term = { false };
00408 
00409         uniset::ObjectId testMode_as = { uniset::DefaultObjectId };
00410         IOController::IOStateList::iterator itTestMode;
00411         long testmode = { false };
00412         long prev_testmode = { false };
00413 
00414         std::shared_ptr<LogAgregator> loga;
00415         std::shared_ptr<DebugStream> iolog;
00416         std::shared_ptr<LogServer> logserv;
00417         std::string logserv_host = {""};
00418         int logserv_port = {0};
00419 
00420     private:
00421 };
00422 // --------------------------------------------------------------------------
00423 } // end of namespace uniset
00424 // -----------------------------------------------------------------------------
00425 #endif // IOControl_H_
00426 // -----------------------------------------------------------------------------