UniSet  2.24.2
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 
57  const char BadSymbols[] = {'.', '/'};
58 
63  char checkBadSymbols(const std::string& str);
64 
66  std::string BadSymbolsToStr();
67 
68 
69  /* hash32("DefaultObjectId") = 122387491 */
74  const ObjectId AdminID = -2;
76  typedef uint64_t KeyType;
83  KeyType key( const uniset::ObjectId id, const uniset::ObjectId node );
85 
86  uint64_t hash64( const std::string& str ) noexcept;
87  uint64_t hash64( const char* buf, size_t sz ) noexcept;
88  uint32_t hash32( const std::string& str ) noexcept;
89  uint32_t hash32( const char* buf, size_t sz ) noexcept;
90 
91  typedef std::list<std::string> ListObjectName;
93  typedef CORBA::Object_ptr ObjectPtr;
94  typedef CORBA::Object_var ObjectVar;
96  UniversalIO::IOType getIOType( const std::string& s ) noexcept;
97  std::string iotype2str( const UniversalIO::IOType& t ) noexcept;
98  std::ostream& operator<<( std::ostream& os, const UniversalIO::IOType t );
99  std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdInfo& ti );
100  std::ostream& operator<<( std::ostream& os, const IOController_i::ShortIOInfo& s );
101  std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdState& s);
102 
105  {
106  lmpOFF = 0,
107  lmpON = 1,
108  lmpBLINK = 2,
109  lmpBLINK2 = 3,
110  lmpBLINK3 = 4
111  };
112 
113  const long ChannelBreakValue = std::numeric_limits<long>::max();
114 
115  class IDList
116  {
117  public:
118 
119  IDList( const std::vector<std::string>& v );
120  IDList();
121  ~IDList();
122 
123  void add( ObjectId id );
124  void del( ObjectId id );
125 
126  inline size_t size() const noexcept
127  {
128  return lst.size();
129  }
130  inline bool empty() const noexcept
131  {
132  return lst.empty();
133  }
134 
135  std::list<ObjectId> getList() const noexcept;
136 
137  // за освобождение выделенной памяти
138  // отвечает вызывающий!
139  IDSeq* getIDSeq() const;
140 
141  //
142  ObjectId getFirst() const noexcept;
143  ObjectId node; // узел, на котором находятся датчики
144 
145  private:
146  std::list<ObjectId> lst;
147  };
148 
150  struct ObjectInfo
151  {
153  std::string repName = { "" };
154  std::string textName = { "" };
155  xmlNode* xmlnode = { nullptr };
156 
157  inline bool operator < ( const ObjectInfo& o ) const
158  {
159  return (id < o.id);
160  }
161  };
162 
163  typedef std::list<NodeInfo> ListOfNode;
164 
165  // ---------------------------------------------------------------
166  // Различные преобразования
167 
169  int uni_atoi( const char* str ) noexcept;
170  inline int uni_atoi( const std::string& str ) noexcept
171  {
172  return uni_atoi(str.c_str());
173  }
174 
175  char* uni_strdup( const std::string& src );
176 
177  std::string timeToString(time_t tm = time(0), const std::string& brk = ":") noexcept;
178  std::string dateToString(time_t tm = time(0), const std::string& brk = "/") noexcept;
180  struct timeval to_timeval( const std::chrono::system_clock::duration& d );
181  struct timespec to_timespec( const std::chrono::system_clock::duration& d );
182  struct timespec now_to_timespec();
184  uniset::Timespec_var to_uniset_timespec( const std::chrono::system_clock::duration& d );
185  uniset::Timespec_var now_to_uniset_timespec();
188  IDList explode( const std::string& str, char sep = ',' );
189  std::vector<std::string> explode_str( const std::string& str, char sep = ',' );
190 
191  struct ParamSInfo
192  {
194  long val;
195  std::string fname; // fullname id@node or id
196  };
197 
201  std::list<ParamSInfo> getSInfoList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
202 
203 
206  std::list<uniset::ConsumerInfo> getObjectsList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
207 
212  bool is_digit( const std::string& s ) noexcept;
213 
219  std::string replace_all( const std::string& src, const std::string& from, const std::string& to );
220  // ---------------------------------------------------------------
221  // Работа с командной строкой
222 
228  inline std::string getArgParam( const std::string& name,
229  int _argc, const char* const* _argv,
230  const std::string& defval = "" ) noexcept
231  {
232  for( int i = 1; i < (_argc - 1) ; i++ )
233  {
234  if( name == _argv[i] )
235  return _argv[i + 1];
236  }
237 
238  return defval;
239  }
240 
242  inline std::string getArg2Param(const std::string& name,
243  int _argc, const char* const* _argv,
244  const std::string& defval, const std::string& defval2 = "") noexcept
245  {
246  std::string s(uniset::getArgParam(name, _argc, _argv, ""));
247 
248  if( !s.empty() )
249  return s;
250 
251  if( !defval.empty() )
252  return defval;
253 
254  return defval2;
255  }
256 
257  inline int getArgInt( const std::string& name,
258  int _argc, const char* const* _argv,
259  const std::string& defval = "" ) noexcept
260  {
261  return uni_atoi(getArgParam(name, _argc, _argv, defval));
262  }
263 
264  inline int getArgPInt( const std::string& name,
265  int _argc, const char* const* _argv,
266  const std::string& strdefval, int defval ) noexcept
267  {
268  std::string param = uniset::getArgParam(name, _argc, _argv, strdefval);
269 
270  if( param.empty() && strdefval.empty() )
271  return defval;
272 
273  return uniset::uni_atoi(param);
274  }
275 
276 
286  inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
287  {
288  for( int i = 1; i < _argc; i++ )
289  {
290  if( name == _argv[i] )
291  return i;
292  }
293 
294  return -1;
295  }
296 
297  // ---------------------------------------------------------------
298  // Калибровка
299 
300  std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo& c );
301 
302  // Функции калибровки значений
303  // raw - преобразуемое значение
304  // rawMin - минимальная граница исходного диапазона
305  // rawMax - максимальная граница исходного диапазона
306  // calMin - минимальная граница калиброванного диапазона
307  // calMin - минимальная граница калиброванного диапазона
308  // limit - обрезать итоговое значение по границам
309  float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit = true );
310  long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit = true );
311  double dcalibrate(double raw, double rawMin, double rawMax, double calMin, double calMax, bool limit = true );
312 
313  // установка значения в нужный диапазон
314  long setinregion(long raw, long rawMin, long rawMax);
315  // установка значения вне диапазона
316  long setoutregion(long raw, long rawMin, long rawMax);
317 
318  // ---------------------------------------------------------------
319  // Всякие helper-ы
320 
321  bool file_exist( const std::string& filename );
322  bool directory_exist( const std::string& path );
323 
324  // Проверка xml-узла на соответствие <...f_prop="f_val">,
325  // если не задано f_val, то проверяется, что просто f_prop!=""
326  bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val = "" ) noexcept;
327 
328  // RAII для флагов форматирования ostream..
330  {
331  public:
332  ios_fmt_restorer( std::ostream& s ):
333  os(s), f(nullptr)
334  {
335  f.copyfmt(s);
336  }
337 
339  {
340  os.copyfmt(f);
341  }
342 
343  ios_fmt_restorer( const ios_fmt_restorer& ) = delete;
344  ios_fmt_restorer& operator=( const ios_fmt_restorer& ) = delete;
345 
346  private:
347  std::ostream& os;
348  std::ios f;
349  };
350 
351  // -----------------------------------------------------------------------------------------
352 } // end of namespace uniset
353 // -----------------------------------------------------------------------------------------
354  inline bool operator==(const struct timespec &r1, const struct timespec &r2)
355  {
356  return (r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec);
357  }
358  inline bool operator!=(const struct timespec &r1, const struct timespec &r2)
359  {
360  return !(operator==(r1, r2));
361  }
362 #endif
Definition: UniSetTypes.h:116
Definition: UniSetTypes.h:330
ThresholdState
Definition: IOController_i.idl:203
Definition: CommonEventLoop.h:15
struct timeval to_timeval(const std::chrono::system_clock::duration &d)
const ObjectId AdminID
Definition: UniSetTypes.h:74
std::list< ParamSInfo > getSInfoList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition: UniSetTypes.cc:324
std::string dateToString(time_t tm=time(0), const std::string &brk="/") noexcept
Definition: UniSetTypes.cc:530
int uni_atoi(const char *str) noexcept
Преобразование строки в число (воспринимает префикс 0, как 8-ное, префикс 0x, как 16-ное,...
Definition: UniSetTypes.cc:542
int findArgParam(const std::string &name, int _argc, const char *const *_argv)
Definition: UniSetTypes.h:286
std::list< std::string > ListObjectName
Definition: UniSetTypes.h:91
bool is_digit(const std::string &s) noexcept
Definition: UniSetTypes.cc:309
CORBA::Object_ptr ObjectPtr
Definition: UniSetTypes.h:93
std::string getArgParam(const std::string &name, int _argc, const char *const *_argv, const std::string &defval="") noexcept
Definition: UniSetTypes.h:228
LampCommand
Definition: UniSetTypes.h:105
@ lmpBLINK2
Definition: UniSetTypes.h:109
@ lmpBLINK
Definition: UniSetTypes.h:108
@ lmpON
Definition: UniSetTypes.h:107
@ lmpOFF
Definition: UniSetTypes.h:106
@ lmpBLINK3
Definition: UniSetTypes.h:110
KeyType key(const uniset::ObjectId id, const uniset::ObjectId node)
Definition: UniSetTypes.cc:746
CORBA::Object_var ObjectVar
Definition: UniSetTypes.h:94
uint64_t KeyType
Definition: UniSetTypes.h:76
uniset::Timespec_var now_to_uniset_timespec()
Definition: UniSetTypes.cc:684
long ThresholdId
Definition: UniSetTypes_i.idl:31
std::string replace_all(const std::string &src, const std::string &from, const std::string &to)
Definition: UniSetTypes.cc:627
const ThresholdId DefaultThresholdId
Definition: UniSetTypes.h:71
std::string timeToString(time_t tm=time(0), const std::string &brk=":") noexcept
Definition: UniSetTypes.cc:519
struct timespec now_to_timespec()
Definition: UniSetTypes.cc:678
sequence< ObjectId > IDSeq
Definition: UniSetTypes_i.idl:89
const char BadSymbols[]
Definition: UniSetTypes.h:57
struct timespec to_timespec(const std::chrono::system_clock::duration &d)
const ThresholdId DefaultTimerId
Definition: UniSetTypes.h:72
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
IDList explode(const std::string &str, char sep=',')
Definition: UniSetTypes.cc:259
std::list< uniset::ConsumerInfo > getObjectsList(const std::string &s, std::shared_ptr< uniset::Configuration > conf=nullptr)
Definition: UniSetTypes.cc:394
char checkBadSymbols(const std::string &str)
Definition: UniSetTypes.cc:706
std::string BadSymbolsToStr()
Definition: UniSetTypes.cc:721
std::string getArg2Param(const std::string &name, int _argc, const char *const *_argv, const std::string &defval, const std::string &defval2="") noexcept
Definition: UniSetTypes.h:242
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: IOController_i.idl:83
Definition: IOController_i.idl:58
Definition: IOController_i.idl:144
Definition: IOController_i.idl:210
Definition: UniSetTypes.h:151
std::string repName
Definition: UniSetTypes.h:153
std::string textName
Definition: UniSetTypes.h:154
Definition: UniSetTypes.h:192