UniSet  2.6.0
IONotifyController.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 // --------------------------------------------------------------------------
21 // --------------------------------------------------------------------------
22 #ifndef IONotifyController_H_
23 #define IONotifyController_H_
24 //---------------------------------------------------------------------------
25 #include <memory>
26 #include <unordered_map>
27 #include <list>
28 #include <string>
29 
30 #include "UniSetTypes.h"
31 #include "IOController_i.hh"
32 #include "IOController.h"
33 
34 //---------------------------------------------------------------------------
35 namespace uniset
36 {
37  class NCRestorer;
38  //---------------------------------------------------------------------------
121  //---------------------------------------------------------------------------
132  public IOController,
133  public POA_IONotifyController_i
134  {
135  public:
136 
137  IONotifyController(const std::string& name, const std::string& section, std::shared_ptr<NCRestorer> dumper = nullptr );
138  IONotifyController(const uniset::ObjectId id, std::shared_ptr<NCRestorer> dumper = nullptr );
139 
140  virtual ~IONotifyController();
141 
142  virtual uniset::ObjectType getType() override
143  {
144  return uniset::ObjectType("IONotifyController");
145  }
146 
147  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
148 
149  virtual void askSensor(const uniset::ObjectId sid, const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd) override;
150 
151  virtual void askThreshold(const uniset::ObjectId sid, const uniset::ConsumerInfo& ci,
153  CORBA::Long lowLimit, CORBA::Long hiLimit, CORBA::Boolean invert,
154  UniversalIO::UIOCommand cmd ) override;
155 
157  virtual IONotifyController_i::ThresholdList* getThresholds(const uniset::ObjectId sid ) override;
158  virtual IONotifyController_i::ThresholdsListSeq* getThresholdsList() override;
159 
160  virtual uniset::IDSeq* askSensorsSeq(const uniset::IDSeq& lst,
161  const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd) override;
162 
163  // --------------------------------------------
164 
165 #ifndef DISABLE_REST_API
166  // http API
167  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
168  virtual Poco::JSON::Object::Ptr httpRequest( const string& req, const Poco::URI::QueryParameters& p ) override;
169 #endif
170 
171  // --------------------------------------------
174  public uniset::ConsumerInfo
175  {
177  UniSetObject_i_ptr ref = 0, size_t maxAttemtps = 10 ):
179  ref(ref), attempt(maxAttemtps) {}
180 
181  UniSetObject_i_var ref;
182  size_t attempt;
183  size_t lostEvents = { 0 }; // количество потерянных сообщений (не смогли послать)
184  size_t smCount = { 0 }; // количество посланных SensorMessage
185 
186  ConsumerInfoExt( const ConsumerInfoExt& ) = default;
187  ConsumerInfoExt& operator=( const ConsumerInfoExt& ) = default;
188  ConsumerInfoExt( ConsumerInfoExt&& ) = default;
189  ConsumerInfoExt& operator=(ConsumerInfoExt&& ) = default;
190  };
191 
192  typedef std::list<ConsumerInfoExt> ConsumerList;
193 
195  {
196  ConsumerListInfo(): mut("ConsumerInfoMutex") {}
197  ConsumerList clst;
199 
200  ConsumerListInfo( const ConsumerListInfo& ) = delete;
201  ConsumerListInfo& operator=( const ConsumerListInfo& ) = delete;
202  ConsumerListInfo( ConsumerListInfo&& ) = default;
203  ConsumerListInfo& operator=(ConsumerListInfo&& ) = default;
204  };
205 
207  typedef std::unordered_map<uniset::ObjectId, ConsumerListInfo> AskMap;
208 
212  {
213  ThresholdInfoExt( uniset::ThresholdId tid, CORBA::Long low, CORBA::Long hi, bool inv,
215  sid(_sid),
216  invert(inv)
217  {
218  id = tid;
219  hilimit = hi;
220  lowlimit = low;
222  }
223 
228 
230  IOController::IOStateList::iterator sit;
231 
233  bool invert;
234 
235  inline bool operator== ( const ThresholdInfo& r ) const
236  {
237  return ((id == r.id) &&
238  (hilimit == r.hilimit) &&
239  (lowlimit == r.lowlimit) &&
240  (invert == r.invert) );
241  }
242 
244  {
246  r.id = id;
247  r.hilimit = hilimit;
248  r.lowlimit = lowlimit;
249  r.invert = invert;
250  r.tv_sec = tv_sec;
251  r.tv_nsec = tv_nsec;
252  r.state = state;
253  return r;
254  }
255 
256  ThresholdInfoExt( const ThresholdInfoExt& ) = delete;
257  ThresholdInfoExt& operator=( const ThresholdInfoExt& ) = delete;
258  ThresholdInfoExt( ThresholdInfoExt&& ) = default;
259  ThresholdInfoExt& operator=(ThresholdInfoExt&& ) = default;
260  };
261 
263  typedef std::list<ThresholdInfoExt> ThresholdExtList;
264 
266  {
267  ThresholdsListInfo() {}
268  ThresholdsListInfo( const IOController_i::SensorInfo& si, ThresholdExtList&& list,
269  UniversalIO::IOType t = UniversalIO::AI ):
270  si(si), type(t), list( std::move(list) ) {}
271 
273 
275  std::shared_ptr<USensorInfo> usi;
276  UniversalIO::IOType type = { UniversalIO::AI };
277  ThresholdExtList list;
278  };
279 
281  typedef std::unordered_map<uniset::ObjectId, ThresholdsListInfo> AskThresholdMap;
282 
283  protected:
285  virtual bool activateObject() override;
286  virtual void initItem(std::shared_ptr<USensorInfo>& usi, IOController* ic );
287 
289  virtual void send( ConsumerListInfo& lst, const uniset::SensorMessage& sm );
290 
292  virtual void checkThreshold( std::shared_ptr<USensorInfo>& usi, bool send = true );
293  virtual void checkThreshold(IOController::IOStateList::iterator& li, const uniset::ObjectId sid, bool send_msg = true );
294 
296  ThresholdExtList::iterator findThreshold( const uniset::ObjectId sid, const uniset::ThresholdId tid );
297 
301  virtual void dumpOrdersList( const uniset::ObjectId sid, const IONotifyController::ConsumerListInfo& lst );
302 
306  virtual void dumpThresholdList( const uniset::ObjectId sid, const IONotifyController::ThresholdExtList& lst );
307 
309  virtual void readDump();
310 
311  std::shared_ptr<NCRestorer> restorer;
312 
313  void onChangeUndefinedState( std::shared_ptr<USensorInfo>& usi, IOController* ic );
314 
315  // функция для работы напрямую с указателем (оптимизация)
316  virtual long localSetValue( std::shared_ptr<USensorInfo>& usi,
317  CORBA::Long value, uniset::ObjectId sup_id ) override;
318 
320  // идентификаторы данные в userdata (см. USensorInfo::userdata)
322  {
323  udataConsumerList = 0,
324  udataThresholdList = 1
325  };
326 
327 #ifndef DISABLE_REST_API
328  // http api
329  Poco::JSON::Object::Ptr request_consumers( const std::string& req, const Poco::URI::QueryParameters& p );
330  Poco::JSON::Object::Ptr request_lost( const string& req, const Poco::URI::QueryParameters& p );
331  Poco::JSON::Object::Ptr getConsumers(uniset::ObjectId sid, ConsumerListInfo& clist, bool ifNotEmpty = true );
332 #endif
333 
334  private:
335  friend class NCRestorer;
336 
337  //----------------------
338  bool addConsumer(ConsumerListInfo& lst, const uniset::ConsumerInfo& cons );
339  bool removeConsumer(ConsumerListInfo& lst, const uniset::ConsumerInfo& cons );
340 
342  void ask(AskMap& askLst, const uniset::ObjectId sid,
343  const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd);
344 
346  bool addThreshold(ThresholdExtList& lst, ThresholdInfoExt&& ti, const uniset::ConsumerInfo& ci);
348  bool removeThreshold(ThresholdExtList& lst, ThresholdInfoExt& ti, const uniset::ConsumerInfo& ci);
349 
350  AskMap askIOList;
351  AskThresholdMap askTMap;
354  uniset::uniset_rwmutex askIOMutex;
356  uniset::uniset_rwmutex trshMutex;
357 
358  sigc::connection conInit;
359  sigc::connection conUndef;
360 
361  int maxAttemtps;
362  int sendAttemtps;
364  std::mutex lostConsumersMutex;
365 
366  struct LostConsumerInfo
367  {
368  size_t count = { 0 }; // количество "пропаданий"
369  bool lost = { false }; // флаг означающий что "заказчик пропал"
370  // lost нужен чтобы в count не увеличивать, на send() по каждому датчику, если заказчик заказывал больше одного датчика)
371  // флаг сбрасывается при перезаказе датчика..
372  };
373 
379  std::unordered_map<uniset::ObjectId, LostConsumerInfo> lostConsumers;
380  };
381  // -------------------------------------------------------------------------
382 } // end of uniset namespace
383 // --------------------------------------------------------------------------
384 #endif
385 // --------------------------------------------------------------------------
Definition: IONotifyController.h:194
unsigned long tv_nsec
Definition: IOController_i.idl:210
UserDataID
Definition: IONotifyController.h:321
ThresholdExtList::iterator findThreshold(const uniset::ObjectId sid, const uniset::ThresholdId tid)
поиск информации о пороговом датчике
Definition: IONotifyController.cc:962
Definition: CallbackTimer.h:29
virtual bool activateObject() override
Definition: IONotifyController.cc:584
virtual IONotifyController_i::ThresholdInfo getThresholdInfo(const uniset::ObjectId sid, uniset::ThresholdId tid) override
Definition: IONotifyController.cc:983
Definition: IOController_i.idl:235
unsigned long tv_sec
Definition: IOController_i.idl:209
Definition: IOController_i.idl:203
long ThresholdId
Definition: UniSetTypes_i.idl:31
std::unordered_map< uniset::ObjectId, ThresholdsListInfo > AskThresholdMap
Definition: IONotifyController.h:281
Definition: IONotifyController.h:210
virtual void dumpOrdersList(const uniset::ObjectId sid, const IONotifyController::ConsumerListInfo &lst)
Definition: IONotifyController.cc:616
Definition: UniSetTypes_i.idl:74
sequence< ObjectId > IDSeq
Definition: UniSetTypes_i.idl:82
virtual void readDump()
Definition: IONotifyController.cc:592
Definition: IONotifyController.h:265
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:56
Definition: IONotifyController.h:131
uniset::ObjectId sid
Definition: IONotifyController.h:227
virtual void checkThreshold(std::shared_ptr< USensorInfo > &usi, bool send=true)
проверка срабатывания пороговых датчиков
virtual long localSetValue(std::shared_ptr< USensorInfo > &usi, CORBA::Long value, uniset::ObjectId sup_id) override
Definition: IONotifyController.cc:424
Definition: IONotifyController.h:173
Definition: MessageType.h:122
long lowlimit
Definition: IOController_i.idl:207
bool invert
Definition: IONotifyController.h:233
ThresholdExtList list
Definition: IONotifyController.h:277
std::list< ThresholdInfoExt > ThresholdExtList
Definition: IONotifyController.h:263
virtual void dumpThresholdList(const uniset::ObjectId sid, const IONotifyController::ThresholdExtList &lst)
Definition: IONotifyController.cc:637
Definition: Mutex.h:32
virtual uniset::SimpleInfo * getInfo(const char *userparam=0) override
Definition: IONotifyController.cc:79
long hilimit
Definition: IOController_i.idl:206
std::unordered_map< uniset::ObjectId, ConsumerListInfo > AskMap
Definition: IONotifyController.h:207
virtual void send(ConsumerListInfo &lst, const uniset::SensorMessage &sm)
посылка информации об изменении состояния датчика
Definition: IONotifyController.cc:496
IOController::IOStateList::iterator sit
Definition: IONotifyController.h:230
Definition: IOController_i.idl:56
Definition: IOController.h:37
ConsumerListInfo clst
Definition: IONotifyController.h:224
Definition: IOController_i.idl:199
string< SizeOfObjectType > ObjectType
Definition: UniSetTypes_i.idl:33
boolean invert
Definition: IOController_i.idl:211
Definition: UniSetTypes_i.idl:64
virtual void askSensor(const uniset::ObjectId sid, const uniset::ConsumerInfo &ci, UniversalIO::UIOCommand cmd) override
Definition: IONotifyController.cc:273
Definition: NCRestorer.h:41
void onChangeUndefinedState(std::shared_ptr< USensorInfo > &usi, IOController *ic)
Definition: IONotifyController.cc:1131
long ObjectId
Definition: UniSetTypes_i.idl:30
virtual void askThreshold(const uniset::ObjectId sid, const uniset::ConsumerInfo &ci, uniset::ThresholdId tid, CORBA::Long lowLimit, CORBA::Long hiLimit, CORBA::Boolean invert, UniversalIO::UIOCommand cmd) override
Definition: IONotifyController.cc:655