|
UniSet
2.2.1
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2002 Free Software Foundation, Inc. 00003 * Copyright (c) 2002 Pavel Vainerman 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00024 // -------------------------------------------------------------------------- 00025 #ifndef IOController_H_ 00026 #define IOController_H_ 00027 //--------------------------------------------------------------------------- 00028 #include <unordered_map> 00029 #include <list> 00030 #include <sigc++/sigc++.h> 00031 #include "IOController_i.hh" 00032 #include "UniSetTypes.h" 00033 #include "UniSetManager.h" 00034 #include "Configuration.h" 00035 #include "Mutex.h" 00036 //--------------------------------------------------------------------------- 00038 class IOController: 00039 public UniSetManager, 00040 public POA_IOController_i 00041 { 00042 public: 00043 00044 IOController( const std::string& name, const std::string& section ); 00045 IOController( const UniSetTypes::ObjectId id ); 00046 virtual ~IOController(); 00047 00048 virtual UniSetTypes::ObjectType getType() override 00049 { 00050 return UniSetTypes::ObjectType("IOController"); 00051 } 00052 00053 virtual UniSetTypes::SimpleInfo* getInfo( ::CORBA::Long userparam = 0 ) override; 00054 00055 virtual CORBA::Long getValue( UniSetTypes::ObjectId sid ) override; 00056 00057 // -------------------- !!!!!!!!! --------------------------------- 00058 // Реализуются конкретным i/o контроллером 00059 // Не забывайте писать реализацию этих функций 00060 virtual void setValue( UniSetTypes::ObjectId sid, CORBA::Long value, 00061 UniSetTypes::ObjectId sup_id = UniSetTypes::DefaultObjectId ) override; 00062 00063 virtual void fastSetValue( UniSetTypes::ObjectId sid, CORBA::Long value, 00064 UniSetTypes::ObjectId sup_id = UniSetTypes::DefaultObjectId ) override; 00065 00066 // ---------------------------------------------------------------- 00067 virtual void setUndefinedState( UniSetTypes::ObjectId sid, 00068 CORBA::Boolean undefined, 00069 UniSetTypes::ObjectId sup_id = UniSetTypes::DefaultObjectId ) override; 00070 00071 00072 virtual IOController_i::SensorInfoSeq* getSensorSeq( const UniSetTypes::IDSeq& lst ) override; 00073 virtual UniSetTypes::IDSeq* setOutputSeq( const IOController_i::OutSeq& lst, UniSetTypes::ObjectId sup_id ) override; 00074 00075 // ---------------------------------------------------------------- 00076 virtual UniversalIO::IOType getIOType( UniSetTypes::ObjectId sid ) override; 00077 00078 virtual IOController_i::SensorInfoSeq* getSensorsMap() override; 00079 virtual IOController_i::SensorIOInfo getSensorIOInfo( UniSetTypes::ObjectId sid ) override; 00080 00081 virtual CORBA::Long getRawValue( UniSetTypes::ObjectId sid ) override; 00082 virtual void calibrate( UniSetTypes::ObjectId sid, 00083 const IOController_i::CalibrateInfo& ci, 00084 UniSetTypes::ObjectId adminId ) override; 00085 00086 IOController_i::CalibrateInfo getCalibrateInfo( UniSetTypes::ObjectId sid ) override; 00087 00088 inline IOController_i::SensorInfo SensorInfo( const UniSetTypes::ObjectId sid, 00089 const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode()) 00090 { 00091 IOController_i::SensorInfo si; 00092 si.id = sid; 00093 si.node = node; 00094 return si; 00095 }; 00096 00097 UniSetTypes::Message::Priority getPriority( const UniSetTypes::ObjectId id ); 00098 00099 virtual IOController_i::ShortIOInfo getChangedTime( const UniSetTypes::ObjectId id ) override; 00100 00101 virtual IOController_i::ShortMapSeq* getSensors() override; 00102 00103 public: 00104 00105 // предварительное объявление.. 00106 struct USensorInfo; 00107 typedef std::unordered_map<UniSetTypes::ObjectId, std::shared_ptr<USensorInfo>> IOStateList; 00108 00109 // ================== Достпуные сигналы ================= 00115 typedef sigc::signal<void, std::shared_ptr<USensorInfo>&, IOController*> ChangeSignal; 00116 typedef sigc::signal<void, std::shared_ptr<USensorInfo>&, IOController*> ChangeUndefinedStateSignal; 00117 00118 // signal по изменению определённого датчика 00119 ChangeSignal signal_change_value( UniSetTypes::ObjectId sid ); 00120 00121 // signal по изменению любого датчика 00122 ChangeSignal signal_change_value(); 00123 00124 // сигналы по изменению флага "неопределённое состояние" (обрыв датчика например) 00125 ChangeUndefinedStateSignal signal_change_undefined_state( UniSetTypes::ObjectId sid ); 00126 ChangeUndefinedStateSignal signal_change_undefined_state(); 00127 // ----------------------------------------------------------------------------------------- 00128 inline IOStateList::iterator ioBegin() 00129 { 00130 return ioList.begin(); 00131 } 00132 inline IOStateList::iterator ioEnd() 00133 { 00134 return ioList.end(); 00135 } 00136 inline IOStateList::iterator find(UniSetTypes::KeyType k) 00137 { 00138 return ioList.find(k); 00139 } 00140 inline int ioCount() 00141 { 00142 return ioList.size(); 00143 } 00144 00145 // доступ к элементам через итератор 00146 virtual void localSetValue( IOStateList::iterator& it, const UniSetTypes::ObjectId sid, 00147 CORBA::Long value, UniSetTypes::ObjectId sup_id ); 00148 00149 virtual long localGetValue( IOStateList::iterator& it, const UniSetTypes::ObjectId sid ); 00150 00151 // вариант с указателем 00152 void localSetValue( std::shared_ptr<USensorInfo>& usi, UniSetTypes::ObjectId sid, 00153 CORBA::Long value, UniSetTypes::ObjectId sup_id ); 00154 00155 long localGetValue( std::shared_ptr<USensorInfo>& it, const UniSetTypes::ObjectId sid ); 00156 00157 00162 virtual void localSetUndefinedState( IOStateList::iterator& it, bool undefined, 00163 const UniSetTypes::ObjectId sid ); 00164 00165 protected: 00166 // переопределяем для добавления вызова регистрации датчиков 00167 virtual bool deactivateObject() override; 00168 virtual bool activateObject() override; 00169 00171 virtual void activateInit(); 00172 00174 virtual void sensorsRegistration() {}; 00176 virtual void sensorsUnregistration(); 00177 00178 typedef sigc::signal<void, IOStateList::iterator&, IOController*> InitSignal; 00179 // signal по изменению определённого датчика 00180 inline InitSignal signal_init() 00181 { 00182 return sigInit; 00183 } 00184 00188 void ioRegistration( std::shared_ptr<USensorInfo>&, bool force = false ); 00189 00191 void ioUnRegistration( const UniSetTypes::ObjectId sid ); 00192 00193 // ------------------------------ 00194 inline IOController_i::SensorIOInfo 00195 SensorIOInfo(long v, UniversalIO::IOType t, const IOController_i::SensorInfo& si, 00196 UniSetTypes::Message::Priority p = UniSetTypes::Message::Medium, 00197 long defval = 0, IOController_i::CalibrateInfo* ci = 0, 00198 UniSetTypes::ObjectId sup_id = UniSetTypes::DefaultObjectId ) 00199 { 00200 IOController_i::SensorIOInfo ai; 00201 ai.si = si; 00202 ai.type = t; 00203 ai.value = v; 00204 ai.priority = p; 00205 ai.default_val = defval; 00206 ai.real_value = v; 00207 ai.blocked = false; 00208 ai.supplier = sup_id; 00209 00210 if( ci != 0 ) 00211 ai.ci = *ci; 00212 else 00213 { 00214 ai.ci.minRaw = 0; 00215 ai.ci.maxRaw = 0; 00216 ai.ci.minCal = 0; 00217 ai.ci.maxCal = 0; 00218 ai.ci.precision = 0; 00219 } 00220 00221 return ai; 00222 }; 00223 00225 virtual void logging( UniSetTypes::SensorMessage& sm ); 00226 00228 virtual void dumpToDB(); 00229 00230 IOController(); 00231 00232 // доступ к списку c изменением только для своих 00233 IOStateList::iterator myioBegin(); 00234 IOStateList::iterator myioEnd(); 00235 IOStateList::iterator myiofind( UniSetTypes::ObjectId id ); 00236 // -------------------------- 00237 // ФИЛЬТРОВАНИЕ 00238 // 00239 typedef sigc::slot<bool, std::shared_ptr<USensorInfo>&, CORBA::Long, UniSetTypes::ObjectId> IOFilterSlot; 00240 typedef std::list<IOFilterSlot> IOFilterSlotList; 00241 00242 /* 00243 Фильтрующая функция должна возвращать: 00244 TRUE - если значение 'нормальное' 00245 FALSE - если значение не подходит (отбрасывается) 00246 00247 Пример использования: 00248 addIOFilter( sigc::mem_fun(my,&MyClass::my_filter) ); 00249 */ 00250 IOFilterSlotList::iterator addIOFilter( IOFilterSlot sl, bool push_front = false ); 00251 void eraseIOFilter(IOFilterSlotList::iterator& it); 00252 00253 // функии проверки текущего значения 00254 bool checkIOFilters( std::shared_ptr<USensorInfo>& ai, CORBA::Long& newvalue, UniSetTypes::ObjectId sup_id ); 00255 00256 inline bool iofiltersEmpty() 00257 { 00258 return iofilters.empty(); 00259 } 00260 inline int iodiltersSize() 00261 { 00262 return iofilters.size(); 00263 } 00264 00265 private: 00266 friend class NCRestorer; 00267 00268 UniSetTypes::uniset_mutex siganyMutex; 00269 ChangeSignal sigAnyChange; 00270 00271 UniSetTypes::uniset_mutex siganyundefMutex; 00272 ChangeSignal sigAnyUndefChange; 00273 InitSignal sigInit; 00274 00275 IOStateList ioList; 00276 UniSetTypes::uniset_rwmutex ioMutex; 00278 bool isPingDBServer; // флаг связи с DBServer-ом 00279 00280 IOFilterSlotList iofilters; 00282 UniSetTypes::uniset_rwmutex loggingMutex; 00284 public: 00285 struct USensorInfo: 00286 public IOController_i::SensorIOInfo 00287 { 00288 USensorInfo( const USensorInfo& ) = delete; 00289 const USensorInfo& operator=(const USensorInfo& ) = delete; 00290 USensorInfo( USensorInfo&& ) = default; 00291 USensorInfo& operator=(USensorInfo&& ) = default; 00292 00293 USensorInfo(): any(0), d_value(0), d_off_value(0) 00294 { 00295 d_si.id = UniSetTypes::DefaultObjectId; 00296 d_si.node = UniSetTypes::DefaultObjectId; 00297 default_val = 0; 00298 value = default_val; 00299 real_value = default_val; 00300 dbignore = false; 00301 undefined = false; 00302 blocked = false; 00303 supplier = UniSetTypes::DefaultObjectId; 00304 } 00305 00306 virtual ~USensorInfo() {} 00307 00308 USensorInfo(IOController_i::SensorIOInfo& r); 00309 USensorInfo(IOController_i::SensorIOInfo* r); 00310 USensorInfo(const IOController_i::SensorIOInfo& r); 00311 00312 USensorInfo& operator=(IOController_i::SensorIOInfo& r); 00313 const USensorInfo& operator=(const IOController_i::SensorIOInfo& r); 00314 USensorInfo& operator=(IOController_i::SensorIOInfo* r); 00315 00316 // Дополнительные (вспомогательные поля) 00317 UniSetTypes::uniset_rwmutex val_lock; 00319 // IOStateList::iterator it; 00320 std::shared_ptr<USensorInfo> it; 00321 00322 void* any; 00324 // сигнал для реализации механизма зависимостией.. 00325 // (все зависимые датчики подключаются к нему (см. NCRestorer::init_depends_signals) 00326 UniSetTypes::uniset_rwmutex changeMutex; 00327 ChangeSignal sigChange; 00328 00329 UniSetTypes::uniset_rwmutex undefMutex; 00330 ChangeUndefinedStateSignal sigUndefChange; 00331 00332 IOController_i::SensorInfo d_si; 00333 long d_value; 00334 long d_off_value; 00336 // функция обработки информации об изменении состояния датчика, от которого зависит данный 00337 void checkDepend( std::shared_ptr<USensorInfo>& it, IOController* ); 00338 00339 void init( const IOController_i::SensorIOInfo& s ); 00340 00341 inline IOController_i::SensorIOInfo makeSensorIOInfo() 00342 { 00343 UniSetTypes::uniset_rwmutex_rlock lock(val_lock); 00344 IOController_i::SensorIOInfo s(*this); 00345 return std::move(s); 00346 } 00347 00348 inline UniSetTypes::SensorMessage makeSensorMessage() 00349 { 00350 UniSetTypes::SensorMessage sm; 00351 00352 UniSetTypes::uniset_rwmutex_rlock lock(val_lock); 00353 sm.id = si.id; 00354 sm.node = si.node; // uniset_conf()->getLocalNode()? 00355 sm.sensor_type = type; 00356 sm.value = value; 00357 sm.undefined = undefined; 00358 sm.priority = (UniSetTypes::Message::Priority)priority; 00359 sm.sm_tv_sec = tv_sec; 00360 sm.sm_tv_usec = tv_usec; 00361 sm.ci = ci; 00362 sm.supplier = supplier; 00363 return std::move(sm); 00364 } 00365 }; 00366 }; 00367 // -------------------------------------------------------------------------- 00368 #endif 00369 // --------------------------------------------------------------------------
1.7.6.1