UniSet  2.6.0
UniSetTypes.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 UniSetTypes_H_
23 #define UniSetTypes_H_
24 // --------------------------------------------------------------------------
25 #include <memory>
26 #include <cstdlib>
27 #include <cstdio>
28 #include <string>
29 #include <list>
30 #include <vector>
31 #include <limits>
32 #include <ostream>
33 #include <chrono>
34 #include <thread>
35 
36 #include <omniORB4/CORBA.h>
37 #include "UniSetTypes_i.hh"
38 #include "IOController_i.hh"
39 #include "Mutex.h"
40 #include "UniXML.h"
41 #include "PassiveTimer.h" // for typedef timeout_t
42 // -----------------------------------------------------------------------------------------
44 inline void msleep( uniset::timeout_t m )
45 {
46  std::this_thread::sleep_for(std::chrono::milliseconds(m));
47 }
48 
50 namespace uniset
51 {
52  class Configuration;
53  // ---------------------------------------------------------------
54  // Вспомогательные типы данных и константы
55 
60  const ObjectId AdminID = -2;
62  typedef size_t KeyType;
72  inline static KeyType key( const uniset::ObjectId id, const uniset::ObjectId node )
73  {
74  return KeyType((id * node) + (id + 2 * node));
75  }
76 
77  inline static KeyType key( const IOController_i::SensorInfo& si )
78  {
79  return key(si.id, si.node);
80  }
81 
82  typedef std::list<std::string> ListObjectName;
84  typedef CORBA::Object_ptr ObjectPtr;
85  typedef CORBA::Object_var ObjectVar;
87  UniversalIO::IOType getIOType( const std::string& s ) noexcept;
88  std::string iotype2str( const UniversalIO::IOType& t ) noexcept;
89  std::ostream& operator<<( std::ostream& os, const UniversalIO::IOType t );
90  std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdInfo& ti );
91  std::ostream& operator<<( std::ostream& os, const IOController_i::ShortIOInfo& s );
92  std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdState& s);
93 
96  {
97  lmpOFF = 0,
98  lmpON = 1,
99  lmpBLINK = 2,
100  lmpBLINK2 = 3,
102  };
103 
104  static const long ChannelBreakValue = std::numeric_limits<long>::max();
105 
106  class IDList
107  {
108  public:
109 
110  IDList( const std::vector<std::string>& v );
111  IDList();
112  ~IDList();
113 
114  void add( ObjectId id );
115  void del( ObjectId id );
116 
117  inline size_t size() const noexcept
118  {
119  return lst.size();
120  }
121  inline bool empty() const noexcept
122  {
123  return lst.empty();
124  }
125 
126  std::list<ObjectId> getList() noexcept;
127 
128  // за освобождение выделеной памяти
129  // отвечает вызывающий!
130  IDSeq* getIDSeq() const;
131 
132  //
133  ObjectId getFirst() const noexcept;
134  ObjectId node; // узел, на котором находятся датчики
135 
136  private:
137  std::list<ObjectId> lst;
138  };
139 
141  struct ObjectInfo
142  {
143  ObjectInfo() noexcept:
144  id(DefaultObjectId),
145  repName(""), textName(""), xmlnode(0) {}
146 
148  std::string repName;
149  std::string textName;
150  xmlNode* xmlnode;
151 
152  inline bool operator < ( const ObjectInfo& o ) const
153  {
154  return (id < o.id);
155  }
156  };
157 
158  typedef std::list<NodeInfo> ListOfNode;
159 
161  const char BadSymbols[] = {'.', '/'};
162 
163  // ---------------------------------------------------------------
164  // Различные преобразования
165 
167  int uni_atoi( const char* str ) noexcept;
168  inline int uni_atoi( const std::string& str ) noexcept
169  {
170  return uni_atoi(str.c_str());
171  }
172 
173  char* uni_strdup( const std::string& src );
174 
175  std::string timeToString(time_t tm = time(0), const std::string& brk = ":") noexcept;
176  std::string dateToString(time_t tm = time(0), const std::string& brk = "/") noexcept;
178  struct timeval to_timeval( const std::chrono::system_clock::duration& d );
179  struct timespec to_timespec( const std::chrono::system_clock::duration& d );
180  struct timespec now_to_timespec();
182  inline bool operator==( const struct timespec& r1, const struct timespec& r2 )
183  {
184  return ( r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec );
185  }
186 
187  inline bool operator!=( const struct timespec& r1, const struct timespec& r2 )
188  {
189  return !(operator==(r1, r2));
190  }
191 
193  IDList explode( const std::string& str, char sep = ',' );
194  std::vector<std::string> explode_str( const std::string& str, char sep = ',' );
195 
196  struct ParamSInfo
197  {
199  long val;
200  std::string fname; // fullname id@node or id
201  };
202 
206  std::list<ParamSInfo> getSInfoList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
207 
208 
211  std::list<uniset::ConsumerInfo> getObjectsList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
212 
214  bool is_digit( const std::string& s ) noexcept;
215 
221  std::string replace_all( const std::string& src, const std::string& from, const std::string& to );
222  // ---------------------------------------------------------------
223  // Работа с командной строкой
224 
229  inline std::string getArgParam( const std::string& name,
230  int _argc, const char* const* _argv,
231  const std::string& defval = "" ) noexcept
232  {
233  for( int i = 1; i < (_argc - 1) ; i++ )
234  {
235  if( name == _argv[i] )
236  return _argv[i + 1];
237  }
238 
239  return defval;
240  }
241 
242  inline int getArgInt( const std::string& name,
243  int _argc, const char* const* _argv,
244  const std::string& defval = "" ) noexcept
245  {
246  return uni_atoi(getArgParam(name, _argc, _argv, defval));
247  }
248 
256  inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
257  {
258  for( int i = 1; i < _argc; i++ )
259  {
260  if( name == _argv[i] )
261  return i;
262  }
263 
264  return -1;
265  }
266 
267  // ---------------------------------------------------------------
268  // Калибровка
269 
270  std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo& c );
271 
272  // Функции калибровки значений
273  // raw - преобразуемое значение
274  // rawMin - минимальная граница исходного диапазона
275  // rawMax - максимальная граница исходного диапазона
276  // calMin - минимальная граница калиброванного диапазона
277  // calMin - минимальная граница калиброванного диапазона
278  // limit - обрезать итоговое значение по границам
279  float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit = true );
280  long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit = true );
281 
282  // установка значения в нужный диапазон
283  long setinregion(long raw, long rawMin, long rawMax);
284  // установка значения вне диапазона
285  long setoutregion(long raw, long rawMin, long rawMax);
286 
287  // ---------------------------------------------------------------
288  // Всякие helper-ы
289 
290  bool file_exist( const std::string& filename );
291 
292  // Проверка xml-узла на соответствие <...f_prop="f_val">,
293  // если не задано f_val, то проверяется, что просто f_prop!=""
294  bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val = "" ) noexcept;
295 
297  template<typename InputIterator,
298  typename OutputIterator,
299  typename Predicate>
300  OutputIterator copy_if(InputIterator begin,
301  InputIterator end,
302  OutputIterator destBegin,
303  Predicate p)
304  {
305  while( begin != end)
306  {
307  if( p(*begin) ) &(destBegin++) = *begin;
308 
309  ++begin;
310  }
311 
312  return destBegin;
313  }
314  // -----------------------------------------------------------------------------------------
315 } // end of namespace uniset
316 // -----------------------------------------------------------------------------------------
317 #endif
Definition: UniSetTypes.h:101
int uni_atoi(const char *str) noexcept
Преобразование строки в число (воспринимает префикс 0, как 8-ное, префикс 0x, как 16-ное...
Definition: UniSetTypes.cc:484
std::string repName
Definition: UniSetTypes.h:148
std::string dateToString(time_t tm=time(0), const std::string &brk="/") noexcept
Definition: UniSetTypes.cc:473
Definition: CallbackTimer.h:29
std::string timeToString(time_t tm=time(0), const std::string &brk=":") noexcept
Definition: UniSetTypes.cc:463
std::list< ParamSInfo > getSInfoList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
IDList explode(const std::string &str, char sep= ',')
Definition: UniSetTypes.cc:212
Definition: IOController_i.idl:203
long ThresholdId
Definition: UniSetTypes_i.idl:31
Definition: UniSetTypes.h:196
STL namespace.
std::list< uniset::ConsumerInfo > getObjectsList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
const ObjectId AdminID
Definition: UniSetTypes.h:60
ThresholdState
Definition: IOController_i.idl:196
sequence< ObjectId > IDSeq
Definition: UniSetTypes_i.idl:82
struct timespec now_to_timespec()
Definition: UniSetTypes.cc:620
ObjectId id
Definition: UniSetTypes.h:147
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:56
CORBA::Object_ptr ObjectPtr
Definition: UniSetTypes.h:84
Definition: UniSetTypes.h:100
bool is_digit(const std::string &s) noexcept
Definition: UniSetTypes.cc:262
const ThresholdId DefaultThresholdId
Definition: UniSetTypes.h:57
std::list< std::string > ListObjectName
Definition: UniSetTypes.h:82
const ThresholdId DefaultTimerId
Definition: UniSetTypes.h:58
uniset::ObjectId node
Definition: IOController_i.idl:59
std::string textName
Definition: UniSetTypes.h:149
size_t KeyType
Definition: UniSetTypes.h:62
Definition: IOController_i.idl:137
std::string replace_all(const std::string &src, const std::string &from, const std::string &to)
Definition: UniSetTypes.cc:569
Definition: UniSetTypes.h:141
Definition: UniSetTypes.h:106
Definition: UniSetTypes.h:97
int findArgParam(const std::string &name, int _argc, const char *const *_argv)
Definition: UniSetTypes.h:256
Definition: UniSetTypes.h:99
Definition: IOController_i.idl:78
CORBA::Object_var ObjectVar
Definition: UniSetTypes.h:85
Definition: UniSetTypes.h:98
Definition: IOController_i.idl:56
OutputIterator copy_if(InputIterator begin, InputIterator end, OutputIterator destBegin, Predicate p)
Definition: UniSetTypes.h:300
static KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.h:72
std::string getArgParam(const std::string &name, int _argc, const char *const *_argv, const std::string &defval="") noexcept
Definition: UniSetTypes.h:229
const char BadSymbols[]
Definition: UniSetTypes.h:161
struct timespec to_timespec(const std::chrono::system_clock::duration &d)
LampCommand
Definition: UniSetTypes.h:95
long ObjectId
Definition: UniSetTypes_i.idl:30
uniset::ObjectId id
Definition: IOController_i.idl:58
struct timeval to_timeval(const std::chrono::system_clock::duration &d)