UniSet  2.24.2
UInterface.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 UInterface_H_
23 #define UInterface_H_
24 // ---------------------------------------------------------------------------
25 #include <memory>
26 #include <string>
27 #include <atomic>
28 #include <sstream>
29 #include <unordered_map>
30 #include <functional>
31 #include <omniORB4/CORBA.h>
32 #include "Exceptions.h"
33 #include "UniSetTypes.h"
34 #include "ObjectIndex.h"
35 #include "ObjectRepository.h"
36 #include "IOController_i.hh"
37 #include "MessageType.h"
38 #include "Configuration.h"
39 #ifndef DISABLE_REST_API
40 #include "UHttpClient.h"
41 #endif
42 // -----------------------------------------------------------------------------------------
43 namespace uniset
44 {
54  class UInterface
55  {
56  public:
57 
58  UInterface( const uniset::ObjectId backid, CORBA::ORB_var orb = NULL, const std::shared_ptr<uniset::ObjectIndex> oind = nullptr );
59  UInterface( const std::shared_ptr<uniset::Configuration>& uconf = uniset::uniset_conf() );
60  ~UInterface();
61 
62  // ---------------------------------------------------------------
63  // Работа с датчиками
64 
66  long getValue (const uniset::ObjectId id, const uniset::ObjectId node) const;
67  long getValue ( const uniset::ObjectId id ) const;
68  long getRawValue( const IOController_i::SensorInfo& si );
69 
71  void setValue ( const uniset::ObjectId id, long value, const uniset::ObjectId node, uniset::ObjectId sup_id = uniset::DefaultObjectId ) const;
72  void setValue ( const uniset::ObjectId id, long value ) const;
73  void setValue ( const IOController_i::SensorInfo& si, long value, const uniset::ObjectId supplier ) const;
74 
75  // fast - это удалённый вызов "без подтверждения", он быстрее, но менее надёжен
76  // т.к. вызывающий никогда не узнает об ошибке, если она была (датчик такой не найден и т.п.)
77  void fastSetValue( const IOController_i::SensorInfo& si, long value, uniset::ObjectId supplier ) const;
78 
80  IOController_i::SensorInfoSeq_var getSensorSeq( const uniset::IDList& lst );
81 
84 
87  uniset::IDSeq_var setOutputSeq( const IOController_i::OutSeq& lst, uniset::ObjectId sup_id );
88 
89  // ---------------------------------------------------------------
90  // Заказ датчиков
91 
93  void askSensor( const uniset::ObjectId id, UniversalIO::UIOCommand cmd,
95 
96  void askRemoteSensor( const uniset::ObjectId id, UniversalIO::UIOCommand cmd, const uniset::ObjectId node,
98 
100  uniset::IDSeq_var askSensorsSeq( const uniset::IDList& lst, UniversalIO::UIOCommand cmd,
102  // ------------------------------------------------------
103 
104  // установка неопределённого состояния
105  void setUndefinedState( const IOController_i::SensorInfo& si, bool undefined, uniset::ObjectId supplier );
106 
107  // заморозка значения (выставить указанный value и не менять)
108  void freezeValue( const IOController_i::SensorInfo& si, bool set, long value, uniset::ObjectId supplier = uniset::DefaultObjectId );
109  // ---------------------------------------------------------------
110  // Калибровка... пороги...
111 
113  void calibrate(const IOController_i::SensorInfo& si,
116 
117  IOController_i::CalibrateInfo getCalibrateInfo( const IOController_i::SensorInfo& si );
118 
120  void askThreshold( const uniset::ObjectId sensorId, const uniset::ThresholdId tid,
121  UniversalIO::UIOCommand cmd,
122  long lowLimit, long hiLimit, bool invert = false,
123  uniset::ObjectId backid = uniset::DefaultObjectId ) const;
124 
125  void askRemoteThreshold( const uniset::ObjectId sensorId, const uniset::ObjectId node,
126  const uniset::ThresholdId thresholdId, UniversalIO::UIOCommand cmd,
127  long lowLimit, long hiLimit, bool invert = false,
128  uniset::ObjectId backid = uniset::DefaultObjectId ) const;
129 
130  IONotifyController_i::ThresholdInfo getThresholdInfo( const IOController_i::SensorInfo& si, const uniset::ThresholdId tid ) const;
131  IONotifyController_i::ThresholdInfo getThresholdInfo( const uniset::ObjectId sid, const uniset::ThresholdId tid ) const;
132 
133  // ---------------------------------------------------------------
134  // Вспомогательные функции
135 
136  UniversalIO::IOType getIOType(const uniset::ObjectId id, uniset::ObjectId node) const;
137  UniversalIO::IOType getIOType(const uniset::ObjectId id) const;
138 
139  // read from xml (only for xml!) т.е. без удалённого запроса
140  UniversalIO::IOType getConfIOType( const uniset::ObjectId id ) const noexcept;
141 
142  // Получение типа объекта..
143  uniset::ObjectType getType(const uniset::ObjectId id, const uniset::ObjectId node) const;
145 
148 
150  std::string getObjectInfo( const uniset::ObjectId id, const std::string& params, const uniset::ObjectId node ) const;
151  std::string apiRequest( const uniset::ObjectId id, const std::string& query, const uniset::ObjectId node ) const;
152 
154  IOController_i::ShortMapSeq* getSensors( const uniset::ObjectId id,
155  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
156 
157  IOController_i::SensorInfoSeq* getSensorsMap( const uniset::ObjectId id,
158  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
159 
160  IONotifyController_i::ThresholdsListSeq* getThresholdsList( const uniset::ObjectId id,
161  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
162  // ---------------------------------------------------------------
163  // Работа с репозиторием
164 
168  void registered(const uniset::ObjectId id, const uniset::ObjectPtr oRef, bool force = false) const;
169 
170  // throw(uniset::ORepFailed)
171  void unregister(const uniset::ObjectId id);
172 
173  uniset::ObjectPtr resolve( const std::string& name ) const;
174  uniset::ObjectPtr resolve( const uniset::ObjectId id ) const;
175 
176  // throw(uniset::ResolveNameError, uniset::TimeOut);
177  uniset::ObjectPtr resolve(const uniset::ObjectId id, const uniset::ObjectId nodeName) const;
178 
179  // Проверка доступности объекта или датчика
180  bool isExist( const uniset::ObjectId id ) const noexcept;
181  bool isExist( const uniset::ObjectId id, const uniset::ObjectId node ) const noexcept;
182 
184  bool waitReady( const uniset::ObjectId id, int msec, int pause = 5000,
185  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() ) noexcept;
186 
188  bool waitWorking( const uniset::ObjectId id, int msec, int pause = 3000,
189  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() ) noexcept;
190 
191  bool waitReadyWithCancellation( const uniset::ObjectId id, int msec, std::atomic_bool& cancelFlag, int pause = 5000,
192  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() ) noexcept;
193 
194  // ---------------------------------------------------------------
195  // Работа с ID, Name
196 
198  inline uniset::ObjectId getIdByName( const std::string& name ) const noexcept
199  {
200  return oind->getIdByName(name);
201  }
202 
204  inline std::string getNameById( const uniset::ObjectId id ) const noexcept
205  {
206  return oind->getNameById(id);
207  }
208 
209  inline uniset::ObjectId getNodeId( const std::string& fullname ) const noexcept
210  {
211  return oind->getNodeId(fullname);
212  }
213 
214  inline std::string getTextName( const uniset::ObjectId id ) const noexcept
215  {
216  return oind->getTextName(id);
217  }
218 
219  // ---------------------------------------------------------------
220  // Получение указателей на вспомогательные классы.
221  inline const std::shared_ptr<uniset::ObjectIndex> getObjectIndex() noexcept
222  {
223  return oind;
224  }
225  inline const std::shared_ptr<uniset::Configuration> getConf() noexcept
226  {
227  return uconf;
228  }
229  // ---------------------------------------------------------------
230  // Посылка сообщений
231 
233  void send( const uniset::ObjectId name, const uniset::TransportMessage& msg, uniset::ObjectId node );
234  void send( const uniset::ObjectId name, const uniset::TransportMessage& msg);
235  void sendText(const uniset::ObjectId name, const std::string& text, int mtype, const uniset::ObjectId node = uniset::DefaultObjectId );
236  void sendText(const uniset::ObjectId name, const uniset::TextMessage& msg, const uniset::ObjectId node = uniset::DefaultObjectId );
237 
238  // ---------------------------------------------------------------
239  // Вспомогательный класс для кэширования ссылок на удалённые объекты
240 
241  inline void setCacheMaxSize( size_t newsize ) noexcept
242  {
243  rcache.setMaxSize(newsize);
244  }
245 
248  {
249  public:
250  CacheOfResolve( size_t maxsize, size_t cleancount = 20 ):
251  MaxSize(maxsize), minCallCount(cleancount) {};
252  ~CacheOfResolve() {};
253 
254  // throw(uniset::NameNotFound, uniset::SystemError)
255  uniset::ObjectPtr resolve( const uniset::ObjectId id, const uniset::ObjectId node ) const;
256 
257  void cache(const uniset::ObjectId id, const uniset::ObjectId node, uniset::ObjectVar& ptr ) const;
258  void erase( const uniset::ObjectId id, const uniset::ObjectId node ) const noexcept;
259 
260  inline void setMaxSize( size_t ms ) noexcept
261  {
262  MaxSize = ms;
263  };
264 
265  protected:
266  CacheOfResolve() {};
267 
268  private:
269 
270  bool clean() noexcept;
271  inline void clear() noexcept
272  {
274  mcache.clear();
275  };
276 
277  struct Item
278  {
279  Item( const uniset::ObjectVar& ptr ): ptr(ptr), ncall(0) {}
280  Item(): ptr(NULL), ncall(0) {}
281 
282  uniset::ObjectVar ptr;
283  size_t ncall; // счётчик обращений
284 
285  bool operator<( const CacheOfResolve::Item& rhs ) const
286  {
287  return this->ncall > rhs.ncall;
288  }
289  };
290 
291  typedef std::unordered_map<uniset::KeyType, Item> CacheMap;
292  mutable CacheMap mcache;
293  mutable uniset::uniset_rwmutex cmutex;
294  size_t MaxSize = { 20 };
295  size_t minCallCount = { 20 };
296  };
297 
298  void initBackId( uniset::ObjectId backid );
299 
300  protected:
301  std::string set_err(const std::string& pre, const uniset::ObjectId id, const uniset::ObjectId node) const;
302  std::string httpResolve( const uniset::ObjectId id, const uniset::ObjectId node ) const;
303 
304  private:
305  void init();
306 
307  ObjectRepository rep;
308  uniset::ObjectId myid;
309  mutable CosNaming::NamingContext_var localctx;
310  mutable CORBA::ORB_var orb;
311  CacheOfResolve rcache;
312  std::shared_ptr<uniset::ObjectIndex> oind;
313  std::shared_ptr<uniset::Configuration> uconf;
314 #ifndef DISABLE_REST_API
315  mutable UHttp::UHttpClient resolver;
316 #endif
317  };
318  // -------------------------------------------------------------------------
319 } // end of uniset namespace
320 // --------------------------------------------------------------------------
321 #endif
322 // --------------------------------------------------------------------------
Definition: UniSetTypes.h:116
Definition: ObjectRepository.h:46
Definition: MessageType.h:280
Definition: UHttpClient.h:32
Definition: UInterface.h:248
Definition: UInterface.h:55
void setValue(const uniset::ObjectId id, long value, const uniset::ObjectId node, uniset::ObjectId sup_id=uniset::DefaultObjectId) const
Выставление состояния датчика
Definition: UInterface.cc:361
UniversalIO::IOType getIOType(const uniset::ObjectId id, uniset::ObjectId node) const
Definition: UInterface.cc:656
IOController_i::ShortIOInfo getTimeChange(const uniset::ObjectId id, const uniset::ObjectId node) const
Время последнего изменения датчика
Definition: UInterface.cc:1278
std::string getObjectInfo(const uniset::ObjectId id, const std::string &params, const uniset::ObjectId node) const
Информация об объекте
Definition: UInterface.cc:1359
std::string getNameById(const uniset::ObjectId id) const noexcept
Definition: UInterface.h:204
void registered(const uniset::ObjectId id, const uniset::ObjectPtr oRef, bool force=false) const
Definition: UInterface.cc:834
void askRemoteSensor(const uniset::ObjectId id, UniversalIO::UIOCommand cmd, const uniset::ObjectId node, uniset::ObjectId backid=uniset::DefaultObjectId) const
Definition: UInterface.cc:552
bool waitWorking(const uniset::ObjectId id, int msec, int pause=3000, const uniset::ObjectId node=uniset::uniset_conf() ->getLocalNode()) noexcept
used for check 'getValue'
Definition: UInterface.cc:2734
uniset::ObjectId getIdByName(const std::string &name) const noexcept
Definition: UInterface.h:198
void askThreshold(const uniset::ObjectId sensorId, const uniset::ThresholdId tid, UniversalIO::UIOCommand cmd, long lowLimit, long hiLimit, bool invert=false, uniset::ObjectId backid=uniset::DefaultObjectId) const
Заказ информации об изменении порога
Definition: UInterface.cc:1712
void askSensor(const uniset::ObjectId id, UniversalIO::UIOCommand cmd, uniset::ObjectId backid=uniset::DefaultObjectId) const
Универсальный заказ информации об изменении датчика
Definition: UInterface.cc:646
uniset::IDSeq_var askSensorsSeq(const uniset::IDList &lst, UniversalIO::UIOCommand cmd, uniset::ObjectId backid=uniset::DefaultObjectId)
Заказ по списку
Definition: UInterface.cc:2395
void send(const uniset::ObjectId name, const uniset::TransportMessage &msg, uniset::ObjectId node)
Definition: UInterface.cc:1055
uniset::IDSeq_var setOutputSeq(const IOController_i::OutSeq &lst, uniset::ObjectId sup_id)
Definition: UInterface.cc:2317
IOController_i::SensorInfoSeq_var getSensorSeq(const uniset::IDList &lst)
Получение состояния для списка указанных датчиков
Definition: UInterface.cc:2159
long getValue(const uniset::ObjectId id, const uniset::ObjectId node) const
Получение состояния датчика
Definition: UInterface.cc:117
bool waitReady(const uniset::ObjectId id, int msec, int pause=5000, const uniset::ObjectId node=uniset::uniset_conf() ->getLocalNode()) noexcept
used for check 'isExist'
Definition: UInterface.cc:2728
uniset::ObjectType getType(const uniset::ObjectId id, const uniset::ObjectId node) const
Definition: UInterface.cc:746
void calibrate(const IOController_i::SensorInfo &si, const IOController_i::CalibrateInfo &ci, uniset::ObjectId adminId=uniset::DefaultObjectId)
калибровка
Definition: UInterface.cc:1988
IOController_i::ShortMapSeq * getSensors(const uniset::ObjectId id, const uniset::ObjectId node=uniset::uniset_conf() ->getLocalNode())
Получить список датчиков
Definition: UInterface.cc:2487
IOController_i::SensorIOInfo getSensorIOInfo(const IOController_i::SensorInfo &si)
Получение состояния информации о датчике
Definition: UInterface.cc:2241
Definition: Mutex.h:73
Definition: Mutex.h:32
Definition: CommonEventLoop.h:15
CORBA::Object_ptr ObjectPtr
Definition: UniSetTypes.h:93
CORBA::Object_var ObjectVar
Definition: UniSetTypes.h:94
string< SizeOfObjectType > ObjectType
Definition: UniSetTypes_i.idl:33
long ThresholdId
Definition: UniSetTypes_i.idl:31
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
std::shared_ptr< Configuration > uniset_conf() noexcept
Definition: Configuration.cc:90
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: IOController_i.idl:83
Definition: IOController_i.idl:97
Definition: IOController_i.idl:58
Definition: IOController_i.idl:144
Definition: IOController_i.idl:210
Definition: UniSetTypes_i.idl:45