UniSet  2.6.0
UniSetTypes.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00021 // --------------------------------------------------------------------------
00022 #ifndef UniSetTypes_H_
00023 #define UniSetTypes_H_
00024 // --------------------------------------------------------------------------
00025 #include <memory>
00026 #include <cstdlib>
00027 #include <cstdio>
00028 #include <string>
00029 #include <list>
00030 #include <vector>
00031 #include <limits>
00032 #include <ostream>
00033 #include <chrono>
00034 #include <thread>
00035 
00036 #include <omniORB4/CORBA.h>
00037 #include "UniSetTypes_i.hh"
00038 #include "IOController_i.hh"
00039 #include "Mutex.h"
00040 #include "UniXML.h"
00041 // -----------------------------------------------------------------------------------------
00043 inline void msleep( unsigned int m )
00044 {
00045     std::this_thread::sleep_for(std::chrono::milliseconds(m));
00046 }
00047 
00049 namespace uniset
00050 {
00051 class Configuration;
00052 // ---------------------------------------------------------------
00053 // Вспомогательные типы данных и константы
00054 
00055 const ObjectId DefaultObjectId = -1;    
00056 const ThresholdId DefaultThresholdId = -1;      
00057 const ThresholdId DefaultTimerId = -1;      
00059 const ObjectId AdminID = -2; 
00061 typedef size_t KeyType;    
00071 inline static KeyType key( const uniset::ObjectId id, const uniset::ObjectId node )
00072 {
00073     return KeyType((id * node) + (id + 2 * node));
00074 }
00075 
00076 inline static KeyType key( const IOController_i::SensorInfo& si )
00077 {
00078     return key(si.id, si.node);
00079 }
00080 
00081 typedef std::list<std::string> ListObjectName;    
00083 typedef ObjectId SysId;
00084 typedef CORBA::Object_ptr ObjectPtr;    
00085 typedef CORBA::Object_var ObjectVar;    
00087 UniversalIO::IOType getIOType( const std::string& s ) noexcept;
00088 std::string iotype2str( const UniversalIO::IOType& t ) noexcept;
00089 std::ostream& operator<<( std::ostream& os, const UniversalIO::IOType t );
00090 std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdInfo& ti );
00091 std::ostream& operator<<( std::ostream& os, const IOController_i::ShortIOInfo& s );
00092 std::ostream& operator<<( std::ostream& os, const IONotifyController_i::ThresholdState& s);
00093 
00095 enum LampCommand
00096 {
00097     lmpOFF      = 0,    
00098     lmpON       = 1,    
00099     lmpBLINK    = 2,    
00100     lmpBLINK2   = 3,    
00101     lmpBLINK3   = 4     
00102 };
00103 
00104 static const long ChannelBreakValue = std::numeric_limits<long>::max();
00105 
00106 class IDList
00107 {
00108     public:
00109 
00110         IDList( std::vector<std::string>& v );
00111         IDList( std::vector<std::string>&& v );
00112         IDList();
00113         ~IDList();
00114 
00115         void add( ObjectId id );
00116         void del( ObjectId id );
00117 
00118         inline int size() const noexcept
00119         {
00120             return lst.size();
00121         }
00122         inline bool empty() const noexcept
00123         {
00124             return lst.empty();
00125         }
00126 
00127         std::list<ObjectId> getList() noexcept;
00128 
00129         // за освобождение выделеной памяти
00130         // отвечает вызывающий!
00131         IDSeq* getIDSeq() const;
00132 
00133         //
00134         ObjectId getFirst() const noexcept;
00135         ObjectId node;    // узел, на котором находятся датчики
00136 
00137     private:
00138         std::list<ObjectId> lst;
00139 };
00140 
00142 struct ObjectInfo
00143 {
00144     ObjectInfo() noexcept:
00145         id(DefaultObjectId),
00146         repName(""), textName(""), data(0) {}
00147 
00148     ObjectId id;        
00149     std::string repName;      
00150     std::string textName;     
00151     void* data;
00152 
00153     inline bool operator < ( const ObjectInfo& o ) const
00154     {
00155         return (id < o.id);
00156     }
00157 };
00158 
00159 typedef std::list<NodeInfo> ListOfNode;
00160 
00162 const char BadSymbols[] = {'.', '/'};
00163 
00164 // ---------------------------------------------------------------
00165 // Различные преобразования
00166 
00168 int uni_atoi( const char* str ) noexcept;
00169 inline int uni_atoi( const std::string& str ) noexcept
00170 {
00171     return uni_atoi(str.c_str());
00172 }
00173 
00174 char* uni_strdup( const std::string& src );
00175 
00176 std::string timeToString(time_t tm = time(0), const std::string& brk = ":") noexcept; 
00177 std::string dateToString(time_t tm = time(0), const std::string& brk = "/") noexcept; 
00179 struct timeval to_timeval( const std::chrono::system_clock::duration& d ); 
00180 struct timespec to_timespec( const std::chrono::system_clock::duration& d ); 
00181 struct timespec now_to_timespec(); 
00183 inline bool operator==( const struct timespec& r1,  const struct timespec& r2 )
00184 {
00185     return ( r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec );
00186 }
00187 
00188 inline bool operator!=( const struct timespec& r1,  const struct timespec& r2 )
00189 {
00190     return !(operator==(r1, r2));
00191 }
00192 
00194 IDList explode( const std::string& str, char sep = ',' );
00195 std::vector<std::string> explode_str( const std::string& str, char sep = ',' );
00196 
00197 struct ParamSInfo
00198 {
00199     IOController_i::SensorInfo si;
00200     long val;
00201     std::string fname; // fullname id@node or id
00202 };
00203 
00207 std::list<ParamSInfo> getSInfoList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
00208 
00209 
00212 std::list<uniset::ConsumerInfo> getObjectsList( const std::string& s, std::shared_ptr<uniset::Configuration> conf = nullptr );
00213 
00215 bool is_digit( const std::string& s ) noexcept;
00216 
00222 std::string replace_all( const std::string& src, const std::string& from, const std::string& to );
00223 // ---------------------------------------------------------------
00224 // Работа с командной строкой
00225 
00230 inline std::string getArgParam( const std::string& name,
00231                                 int _argc, const char* const* _argv,
00232                                 const std::string& defval = "" ) noexcept
00233 {
00234     for( int i = 1; i < (_argc - 1) ; i++ )
00235     {
00236         if( name == _argv[i] )
00237             return _argv[i + 1];
00238     }
00239 
00240     return defval;
00241 }
00242 
00243 inline int getArgInt( const std::string& name,
00244                       int _argc, const char* const* _argv,
00245                       const std::string& defval = "" ) noexcept
00246 {
00247     return uni_atoi(getArgParam(name, _argc, _argv, defval));
00248 }
00249 
00257 inline int findArgParam( const std::string& name, int _argc, const char* const* _argv )
00258 {
00259     for( int i = 1; i < _argc; i++ )
00260     {
00261         if( name == _argv[i] )
00262             return i;
00263     }
00264 
00265     return -1;
00266 }
00267 
00268 // ---------------------------------------------------------------
00269 // Калибровка
00270 
00271 std::ostream& operator<<( std::ostream& os, const IOController_i::CalibrateInfo& c );
00272 
00273 // Функции калибровки значений
00274 // raw      - преобразуемое значение
00275 // rawMin   - минимальная граница исходного диапазона
00276 // rawMax   - максимальная граница исходного диапазона
00277 // calMin   - минимальная граница калиброванного диапазона
00278 // calMin   - минимальная граница калиброванного диапазона
00279 // limit    - обрезать итоговое значение по границам
00280 float fcalibrate(float raw, float rawMin, float rawMax, float calMin, float calMax, bool limit = true );
00281 long lcalibrate(long raw, long rawMin, long rawMax, long calMin, long calMax, bool limit = true );
00282 
00283 // установка значения в нужный диапазон
00284 long setinregion(long raw, long rawMin, long rawMax);
00285 // установка значения вне диапазона
00286 long setoutregion(long raw, long rawMin, long rawMax);
00287 
00288 // ---------------------------------------------------------------
00289 // Всякие helper-ы
00290 
00291 bool file_exist( const std::string& filename );
00292 
00293 // Проверка xml-узла на соответствие <...f_prop="f_val">,
00294 // если не задано f_val, то проверяется, что просто f_prop!=""
00295 bool check_filter( UniXML::iterator& it, const std::string& f_prop, const std::string& f_val = "" ) noexcept;
00296 
00298 template<typename InputIterator,
00299          typename OutputIterator,
00300          typename Predicate>
00301 OutputIterator copy_if(InputIterator begin,
00302                        InputIterator end,
00303                        OutputIterator destBegin,
00304                        Predicate p)
00305 {
00306     while( begin != end)
00307     {
00308         if( p(*begin) ) &destBegin++ = *begin;
00309 
00310         ++begin;
00311     }
00312 
00313     return destBegin;
00314 }
00315 // -----------------------------------------------------------------------------------------
00316 } // end of namespace uniset
00317 // -----------------------------------------------------------------------------------------
00318 #endif