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