UniSet  2.7.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 IOConfig;
38  //---------------------------------------------------------------------------
128  //---------------------------------------------------------------------------
133  public IOController,
134  public POA_IONotifyController_i
135  {
136  public:
137 
138  IONotifyController(const std::string& name, const std::string& section, std::shared_ptr<IOConfig> ioconf = nullptr );
139  IONotifyController(const uniset::ObjectId id, std::shared_ptr<IOConfig> ioconf = nullptr );
140 
141  virtual ~IONotifyController();
142 
143  virtual uniset::ObjectType getType() override
144  {
145  return uniset::ObjectType("IONotifyController");
146  }
147 
148  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
149 
150  virtual void askSensor(const uniset::ObjectId sid, const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd) override;
151 
152  virtual void askThreshold(const uniset::ObjectId sid, const uniset::ConsumerInfo& ci,
154  CORBA::Long lowLimit, CORBA::Long hiLimit, CORBA::Boolean invert,
155  UniversalIO::UIOCommand cmd ) override;
156 
157  virtual IONotifyController_i::ThresholdInfo getThresholdInfo( const uniset::ObjectId sid, uniset::ThresholdId tid ) override;
158  virtual IONotifyController_i::ThresholdList* getThresholds(const uniset::ObjectId sid ) override;
159  virtual IONotifyController_i::ThresholdsListSeq* getThresholdsList() override;
160 
161  virtual uniset::IDSeq* askSensorsSeq(const uniset::IDSeq& lst,
162  const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd) override;
163 
164  // --------------------------------------------
165 
166 #ifndef DISABLE_REST_API
167  // http API
168  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
169  virtual Poco::JSON::Object::Ptr httpRequest( const std::string& req, const Poco::URI::QueryParameters& p ) override;
170 #endif
171 
172  // --------------------------------------------
175  public uniset::ConsumerInfo
176  {
178  UniSetObject_i_ptr ref = 0, size_t maxAttemtps = 10 ):
180  ref(ref), attempt(maxAttemtps) {}
181 
182  UniSetObject_i_var ref;
183  size_t attempt = { 10 };
184  size_t lostEvents = { 0 }; // количество потерянных сообщений (не смогли послать)
185  size_t smCount = { 0 }; // количество посланных SensorMessage
186 
187  ConsumerInfoExt( const ConsumerInfoExt& ) = default;
188  ConsumerInfoExt& operator=( const ConsumerInfoExt& ) = default;
189  ConsumerInfoExt( ConsumerInfoExt&& ) = default;
190  ConsumerInfoExt& operator=(ConsumerInfoExt&& ) = default;
191  };
192 
193  typedef std::list<ConsumerInfoExt> ConsumerList;
194 
196  {
197  ConsumerListInfo(): mut("ConsumerInfoMutex") {}
198  ConsumerList clst;
200 
201  ConsumerListInfo( const ConsumerListInfo& ) = delete;
202  ConsumerListInfo& operator=( const ConsumerListInfo& ) = delete;
203  ConsumerListInfo( ConsumerListInfo&& ) = default;
204  ConsumerListInfo& operator=(ConsumerListInfo&& ) = default;
205  };
206 
208  typedef std::unordered_map<uniset::ObjectId, ConsumerListInfo> AskMap;
209 
210  // связь: id датчика --> id порога --> список заказчиков
211  // т.к. каждый порог имеет уникальный указатель, используем его в качестве ключа
212  typedef std::unordered_map<UThresholdInfo*, ConsumerListInfo> AskThresholdMap;
213 
214  protected:
216  virtual bool activateObject() override;
217  virtual void sensorsRegistration() override;
218  virtual void initItem( std::shared_ptr<USensorInfo>& usi, IOController* ic );
219 
221  virtual void send( ConsumerListInfo& lst, const uniset::SensorMessage& sm, const uniset::ConsumerInfo* ci = nullptr );
222 
224  virtual void checkThreshold( std::shared_ptr<USensorInfo>& usi, bool send = true );
225  virtual void checkThreshold( IOController::IOStateList::iterator& li, const uniset::ObjectId sid, bool send_msg = true );
226 
228  std::shared_ptr<UThresholdInfo> findThreshold( const uniset::ObjectId sid, const uniset::ThresholdId tid );
229 
231  virtual void readConf();
232 
233  std::shared_ptr<IOConfig> restorer;
234 
235  void onChangeUndefinedState( std::shared_ptr<USensorInfo>& usi, IOController* ic );
236 
237  // функция для работы напрямую с указателем (оптимизация)
238  virtual long localSetValue( std::shared_ptr<USensorInfo>& usi,
239  CORBA::Long value, uniset::ObjectId sup_id ) override;
240 
241 #ifndef DISABLE_REST_API
242  // http api
243  Poco::JSON::Object::Ptr request_consumers( const std::string& req, const Poco::URI::QueryParameters& p );
244  Poco::JSON::Object::Ptr request_lost( const std::string& req, const Poco::URI::QueryParameters& p );
245  Poco::JSON::Object::Ptr getConsumers(uniset::ObjectId sid, ConsumerListInfo& clist, bool ifNotEmpty = true );
246 #endif
247 
248  // статистика
249  void showStatisticsForConsumer( std::ostringstream& inf, const std::string& consumer );
250  void showStatisticsForLostConsumers( std::ostringstream& inf );
251  void showStatisticsForConsusmers( std::ostringstream& inf );
252  void showStatisticsForConsumersWithLostEvent( std::ostringstream& inf );
253  void showStatisticsForSensor( std::ostringstream& inf, const std::string& name );
254 
256  // идентификаторы данных в userdata (см. USensorInfo::userdata)
258  {
259  udataConsumerList = 0
260  };
261 
262  private:
263 
264  friend class NCRestorer;
265 
266  //----------------------
267  bool addConsumer( ConsumerListInfo& lst, const uniset::ConsumerInfo& cons );
268  bool removeConsumer( ConsumerListInfo& lst, const uniset::ConsumerInfo& cons );
269 
271  void ask(AskMap& askLst, const uniset::ObjectId sid,
272  const uniset::ConsumerInfo& ci, UniversalIO::UIOCommand cmd);
273 
275  std::shared_ptr<UThresholdInfo> addThresholdIfNotExist( std::shared_ptr<USensorInfo>& usi, std::shared_ptr<UThresholdInfo>& ti );
276  bool addThresholdConsumer( std::shared_ptr<UThresholdInfo>& ti, const uniset::ConsumerInfo& ci );
277 
279  bool removeThresholdConsumer( std::shared_ptr<USensorInfo>& usi,
280  std::shared_ptr<UThresholdInfo>& ti,
281  const uniset::ConsumerInfo& ci);
282 
283  AskMap askIOList;
284  AskThresholdMap askTMap;
287  uniset::uniset_rwmutex askIOMutex;
289  uniset::uniset_rwmutex trshMutex;
290 
291  sigc::connection conInit;
292  sigc::connection conUndef;
293 
294  int maxAttemtps;
295  int sendAttemtps;
297  std::mutex lostConsumersMutex;
298 
299  struct LostConsumerInfo
300  {
301  size_t count = { 0 }; // количество "пропаданий"
302  bool lost = { false }; // флаг означающий что "заказчик пропал"
303  // lost нужен чтобы в count не увеличивать, на send() по каждому датчику, если заказчик заказывал больше одного датчика)
304  // флаг сбрасывается при перезаказе датчика..
305  };
306 
310  std::unordered_map<uniset::ObjectId, LostConsumerInfo> lostConsumers;
311  };
312  // -------------------------------------------------------------------------
313 } // end of uniset namespace
314 // --------------------------------------------------------------------------
315 #endif
316 // --------------------------------------------------------------------------
virtual void readConf()
Definition: IONotifyController.cc:763
Definition: IONotifyController.h:195
std::shared_ptr< UThresholdInfo > findThreshold(const uniset::ObjectId sid, const uniset::ThresholdId tid)
поиск информации о пороговом датчике
Definition: IONotifyController.cc:1023
UserDataID
Definition: IONotifyController.h:257
Definition: CallbackTimer.h:29
virtual bool activateObject() override
Definition: IONotifyController.cc:747
Definition: IOController_i.idl:235
Definition: IOController_i.idl:203
long ThresholdId
Definition: UniSetTypes_i.idl:31
Definition: UniSetTypes_i.idl:74
sequence< ObjectId > IDSeq
Definition: UniSetTypes_i.idl:82
Definition: IONotifyController.h:132
virtual void checkThreshold(std::shared_ptr< USensorInfo > &usi, bool send=true)
проверка срабатывания пороговых датчиков
Definition: IONotifyController.h:174
Definition: MessageType.h:124
virtual void send(ConsumerListInfo &lst, const uniset::SensorMessage &sm, const uniset::ConsumerInfo *ci=nullptr)
посылка информации об изменении состояния датчика (всем или указанному заказчику) ...
Definition: IONotifyController.cc:653
Definition: Mutex.h:31
virtual uniset::SimpleInfo * getInfo(const char *userparam=0) override
Definition: IONotifyController.cc:367
std::unordered_map< uniset::ObjectId, ConsumerListInfo > AskMap
Definition: IONotifyController.h:208
Definition: IOController.h:45
string< SizeOfObjectType > ObjectType
Definition: UniSetTypes_i.idl:33
Definition: UniSetTypes_i.idl:64
virtual void askSensor(const uniset::ObjectId sid, const uniset::ConsumerInfo &ci, UniversalIO::UIOCommand cmd) override
Definition: IONotifyController.cc:508
long ObjectId
Definition: UniSetTypes_i.idl:30
virtual void sensorsRegistration() override
Definition: IONotifyController.cc:755