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