UniSet  2.6.0
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 <sstream>
28 #include <unordered_map>
29 #include <functional>
30 #include <omniORB4/CORBA.h>
31 #include "Exceptions.h"
32 #include "UniSetTypes.h"
33 #include "ObjectIndex.h"
34 #include "ObjectRepository.h"
35 #include "IOController_i.hh"
36 #include "MessageType.h"
37 #include "Configuration.h"
38 
39 // -----------------------------------------------------------------------------------------
40 #define UI_THROW_EXCEPTIONS uniset::TimeOut,uniset::IOBadParam,uniset::ORepFailed
41 // -----------------------------------------------------------------------------------------
42 namespace uniset
43 {
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 throw(UI_THROW_EXCEPTIONS);
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 throw(UI_THROW_EXCEPTIONS);
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  uniset::IDSeq_var setOutputSeq( const IOController_i::OutSeq& lst, uniset::ObjectId sup_id );
85 
86  // ---------------------------------------------------------------
87  // Заказ датчиков
88 
90  void askSensor( const uniset::ObjectId id, UniversalIO::UIOCommand cmd,
92 
93  void askRemoteSensor( const uniset::ObjectId id, UniversalIO::UIOCommand cmd, const uniset::ObjectId node,
94  uniset::ObjectId backid = uniset::DefaultObjectId ) const throw(UI_THROW_EXCEPTIONS);
95 
97  uniset::IDSeq_var askSensorsSeq( const uniset::IDList& lst, UniversalIO::UIOCommand cmd,
99  // ------------------------------------------------------
100 
101  // установка неопределённого состояния
102  void setUndefinedState( const IOController_i::SensorInfo& si, bool undefined, uniset::ObjectId supplier );
103 
104  // ---------------------------------------------------------------
105  // Калибровка... пороги...
106 
108  void calibrate(const IOController_i::SensorInfo& si,
111 
112  IOController_i::CalibrateInfo getCalibrateInfo( const IOController_i::SensorInfo& si );
113 
115  void askThreshold( const uniset::ObjectId sensorId, const uniset::ThresholdId tid,
116  UniversalIO::UIOCommand cmd,
117  long lowLimit, long hiLimit, bool invert = false,
118  uniset::ObjectId backid = uniset::DefaultObjectId ) const;
119 
120  void askRemoteThreshold( const uniset::ObjectId sensorId, const uniset::ObjectId node,
121  const uniset::ThresholdId thresholdId, UniversalIO::UIOCommand cmd,
122  long lowLimit, long hiLimit, bool invert = false,
123  uniset::ObjectId backid = uniset::DefaultObjectId ) const;
124 
125  IONotifyController_i::ThresholdInfo getThresholdInfo( const IOController_i::SensorInfo& si, const uniset::ThresholdId tid ) const;
126  IONotifyController_i::ThresholdInfo getThresholdInfo( const uniset::ObjectId sid, const uniset::ThresholdId tid ) const;
127 
128  // ---------------------------------------------------------------
129  // Вспомогательные функции
130 
131  UniversalIO::IOType getIOType(const uniset::ObjectId id, uniset::ObjectId node) const throw(UI_THROW_EXCEPTIONS);
132  UniversalIO::IOType getIOType(const uniset::ObjectId id) const;
133 
134  // read from xml (only for xml!) т.е. без удалённого запроса
135  UniversalIO::IOType getConfIOType( const uniset::ObjectId id ) const noexcept;
136 
137  // Получение типа объекта..
138  uniset::ObjectType getType(const uniset::ObjectId id, const uniset::ObjectId node) const throw(UI_THROW_EXCEPTIONS);
140 
143 
145  std::string getObjectInfo( const uniset::ObjectId id, const std::string& params, const uniset::ObjectId node ) const;
146  std::string apiRequest( const uniset::ObjectId id, const std::string& query, const uniset::ObjectId node ) const;
147 
149  IOController_i::ShortMapSeq* getSensors( const uniset::ObjectId id,
150  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
151 
152  IOController_i::SensorInfoSeq* getSensorsMap( const uniset::ObjectId id,
153  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
154 
155  IONotifyController_i::ThresholdsListSeq* getThresholdsList( const uniset::ObjectId id,
156  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() );
157  // ---------------------------------------------------------------
158  // Работа с репозиторием
159 
160  // /*! регистрация объекта в репозитории */
161  void registered(const uniset::ObjectId id, const uniset::ObjectPtr oRef, bool force = false)const throw(uniset::ORepFailed);
162 
163  // /*! разрегистрация объекта */
164  void unregister(const uniset::ObjectId id)throw(uniset::ORepFailed);
165 
167  inline uniset::ObjectPtr resolve( const std::string& name ) const
168  {
169  return rep.resolve(name);
170  }
171 
172  inline uniset::ObjectPtr resolve( const uniset::ObjectId id ) const
173  {
174  return rep.resolve( oind->getNameById(id) );
175  }
176 
177  uniset::ObjectPtr resolve(const uniset::ObjectId id, const uniset::ObjectId nodeName) const
179 
180 
181  // Проверка доступности объекта или датчика
182  bool isExist( const uniset::ObjectId id ) const noexcept;
183  bool isExist( const uniset::ObjectId id, const uniset::ObjectId node ) const noexcept;
184 
185  // used for check 'isExist'
186  bool waitReady( const uniset::ObjectId id, int msec, int pause = 5000,
187  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() ) noexcept;
188 
189  // used for check 'getValue'
190  bool waitWorking( const uniset::ObjectId id, int msec, int pause = 3000,
191  const uniset::ObjectId node = uniset::uniset_conf()->getLocalNode() ) noexcept;
192 
193  // ---------------------------------------------------------------
194  // Работа с ID, Name
195 
197  inline uniset::ObjectId getIdByName( const std::string& name ) const noexcept
198  {
199  return oind->getIdByName(name);
200  }
201 
203  inline std::string getNameById( const uniset::ObjectId id ) const noexcept
204  {
205  return oind->getNameById(id);
206  }
207 
208  inline uniset::ObjectId getNodeId( const std::string& fullname ) const noexcept
209  {
210  return oind->getNodeId(fullname);
211  }
212 
213  inline std::string getTextName( const uniset::ObjectId id ) const noexcept
214  {
215  return oind->getTextName(id);
216  }
217 
218  // ---------------------------------------------------------------
219  // Получение указателей на вспомогательные классы.
220  inline const std::shared_ptr<uniset::ObjectIndex> getObjectIndex() noexcept
221  {
222  return oind;
223  }
224  inline const std::shared_ptr<uniset::Configuration> getConf() noexcept
225  {
226  return uconf;
227  }
228  // ---------------------------------------------------------------
229  // Посылка сообщений
230 
232  void send( const uniset::ObjectId name, const uniset::TransportMessage& msg, uniset::ObjectId node ) throw(UI_THROW_EXCEPTIONS);
233  void send( const uniset::ObjectId name, const uniset::TransportMessage& msg);
234 
235  // ---------------------------------------------------------------
236  // Вспомогательный класс для кэширования ссылок на удалённые объекты
237 
238  inline void setCacheMaxSize( size_t newsize ) noexcept
239  {
240  rcache.setMaxSize(newsize);
241  }
242 
245  {
246  public:
247  CacheOfResolve( size_t maxsize, size_t cleancount = 20 ):
248  MaxSize(maxsize), minCallCount(cleancount) {};
249  ~CacheOfResolve() {};
250 
251  uniset::ObjectPtr resolve( const uniset::ObjectId id, const uniset::ObjectId node ) const throw(uniset::NameNotFound);
252  void cache(const uniset::ObjectId id, const uniset::ObjectId node, uniset::ObjectVar& ptr ) const;
253  void erase( const uniset::ObjectId id, const uniset::ObjectId node ) const noexcept;
254 
255  inline void setMaxSize( size_t ms ) noexcept
256  {
257  MaxSize = ms;
258  };
259 
260  protected:
261  CacheOfResolve() {};
262 
263  private:
264 
265  bool clean() noexcept;
266  inline void clear() noexcept
267  {
269  mcache.clear();
270  };
271 
272  struct Item
273  {
274  Item( const uniset::ObjectVar& ptr ): ptr(ptr), ncall(0) {}
275  Item(): ptr(NULL), ncall(0) {}
276 
277  uniset::ObjectVar ptr;
278  size_t ncall; // счётчик обращений
279 
280  bool operator<( const CacheOfResolve::Item& rhs ) const
281  {
282  return this->ncall > rhs.ncall;
283  }
284  };
285 
286  typedef std::unordered_map<uniset::KeyType, Item> CacheMap;
287  mutable CacheMap mcache;
288  mutable uniset::uniset_rwmutex cmutex;
289  size_t MaxSize = { 20 };
290  size_t minCallCount = { 20 };
291  };
292 
293  void initBackId( uniset::ObjectId backid );
294 
295  protected:
296  std::string set_err(const std::string& pre, const uniset::ObjectId id, const uniset::ObjectId node) const;
297 
298  private:
299  void init();
300 
301  ObjectRepository rep;
302  uniset::ObjectId myid;
303  mutable CosNaming::NamingContext_var localctx;
304  mutable CORBA::ORB_var orb;
305  CacheOfResolve rcache;
306  std::shared_ptr<uniset::ObjectIndex> oind;
307  std::shared_ptr<uniset::Configuration> uconf;
308  };
309  // -------------------------------------------------------------------------
310 } // end of uniset namespace
311 // --------------------------------------------------------------------------
312 #endif
313 // --------------------------------------------------------------------------