-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.hpp
executable file
·164 lines (125 loc) · 4.81 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
The MIT License (MIT)
Copyright (c) 2017 Wassim Filali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
___________________________________________________________________________________
dependencies :
- json.hpp
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
___________________________________________________________________________________
start date : 21.01.2017 port from 2016
utils :
* time measures
* text string processing
* generic sensors data maps
* json stringify of sensors data
* application arguments and file into map
* time and date routines
*/
#ifndef __UTILS__
#define __UTILS__
#include <string>
#include <map>
#include <vector>
#include <ctime>
#include <ctime>
#include <chrono>
#include "json.hpp"
typedef std::map<std::string,std::string> strmap;
typedef std::vector<std::string> strvect;
// temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC.
// humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits).
// Output value of “47445” represents 47445/1024 = 46.333 %RH
typedef struct RGB_data
{
int sendCount;
int R,G,B;
int NodeId;
}RGB_data_t;
struct color_t
{
uint8_t R;
uint8_t G;
uint8_t B;
};
struct sensor_line_t
{
std::time_t time;
std::string line;
};
struct sensor_measure_t
{
std::time_t time;
float value;
};
typedef std::vector<sensor_measure_t> sensor_measures_table_t;
typedef std::map<std::string,sensor_measures_table_t> sensors_tables_t;
//Nodes[NodeId][SensorName].time
//Nodes[NodeId][SensorName].value
typedef std::map<int,sensors_tables_t> NodeMap_t;
namespace utl
{
typedef std::chrono::time_point<std::chrono::system_clock> time_u;
//not thread safe, non sense for multithreading due to locking that spoils the measures !!!!!!
void start();
std::string stop();
//this is thread safe as variables are kept on user thread only
time_u get_start();
std::string get_stop(time_u& start_time);
//command line to map including check in configfile.txt
std::string args2map( int argc, char** argv ,strmap ¶ms);
void str2map(const std::string &str ,strmap ¶ms);
bool compare(std::string& str1, std::string str2);
bool exists(const strmap ¶ms,const std::string param);
std::string ParseRemTill(std::string &str,char sep,bool &found);
std::string TakeParseTo(std::string &str,char sep);
strvect split(std::string NodesList,char sep);
std::string getTime(std::time_t rawtime);
std::string getTime();
std::string getDay(std::time_t rawtime);
std::string getDay();
void getYearMonthDay(std::time_t rawtime,std::string &text_year,std::string &text_month,std::string &text_day);
void printTime(std::time_t rawtime);
std::string remove_spaces(std::string &str);
void replace(std::string &str,char what,char withwhat);
std::string remove_0x(std::string &str);
void remove(std::string substr, std::string &str);
void hextext2data(const std::string &str, uint8_t *data,const int MaxData);
std::string data2hextext(const uint8_t *data,int data_size);
//json routines
std::string stringify(NodeMap_t &measures,const std::string &type);
std::string stringify2(NodeMap_t &measures,const std::string &type);
void make_json(NodeMap_t &measures,nlohmann::json &jRes,const std::string &type);
void make_json_resp( int NodeId,
const std::string &SensorName,
NodeMap_t &measures,
nlohmann::json &jRes,
const std::string &type
);
void crc_set(uint8_t *data);
uint16_t crc_Fletcher16( uint8_t const *data, uint8_t count );
std::string getRequestType(std::string &request);
}
namespace crc
{
uint16_t calculate( uint8_t const *data, uint8_t count );
// size(size+data) : data : crc
bool check(uint8_t const *data);
void set(uint8_t *data);
}
#endif /*__UTILS__*/