forked from opendata-stuttgart/sensors-software
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.h
187 lines (152 loc) · 6.72 KB
/
utils.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/************************************************************************
* *
* airRohr firmware *
* Copyright (C) 2016-2020 Code for Stuttgart a.o. *
* Copyright (C) 2019-2020 Dirk Mueller *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
************************************************************************
*/
#ifndef utils_h
#define utils_h
#include <WString.h>
#include <map>
#include <vector>
#if defined(ESP8266)
#include <Hash.h>
#include <coredecls.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#endif
#if defined(ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <HardwareSerial.h>
#include "sha/sha_parallel_engine.h"
#include <freertos/queue.h>
#endif
const char DBG_TXT_TEMPERATURE[] PROGMEM = "Temperature (°C): ";
const char DBG_TXT_DECIBEL[] PROGMEM = "Noise Level (DB): ";
const char DBG_TXT_HUMIDITY[] PROGMEM = "Humidity (%): ";
const char DBG_TXT_PRESSURE[] PROGMEM = "Pressure (hPa): ";
const char DBG_TXT_START_READING[] PROGMEM = "R/ ";
const char DBG_TXT_END_READING[] PROGMEM = "/R ";
const char DBG_TXT_CHECKSUM_IS[] PROGMEM = "Checksum is: ";
const char DBG_TXT_CHECKSUM_SHOULD[] PROGMEM = "Checksum should: ";
const char DBG_TXT_DATA_READ_FAILED[] PROGMEM = "Data read failed";
const char DBG_TXT_UPDATE[] PROGMEM = "[update] ";
const char DBG_TXT_UPDATE_FAILED[] PROGMEM = "Update failed.";
const char DBG_TXT_UPDATE_NO_UPDATE[] PROGMEM = "No update.";
const char DBG_TXT_SENDING_TO[] PROGMEM = "## Sending to ";
const char DBG_TXT_SDS011_VERSION_DATE[] PROGMEM = "SDS011 version date";
const char DBG_TXT_CONNECTING_TO[] PROGMEM = "Connecting to ";
const char DBG_TXT_FOUND[] PROGMEM = " ... found";
const char DBG_TXT_NOT_FOUND[] PROGMEM = " ... not found";
const char DBG_TXT_SEP[] PROGMEM = "----";
const char JSON_DATA_VALUES[] PROGMEM = "values";
constexpr unsigned SMALL_STR = 64-1;
constexpr unsigned MED_STR = 256-1;
constexpr unsigned LARGE_STR = 512-1;
constexpr unsigned XLARGE_STR = 1024-1;
#define msSince(timestamp_before) (millis() - (timestamp_before))
#define RESERVE_STRING(name, size) String name((const char*)nullptr); name.reserve(size)
#define UPDATE_MIN(MIN, SAMPLE) if (SAMPLE < MIN) { MIN = SAMPLE; }
#define UPDATE_MAX(MAX, SAMPLE) if (SAMPLE > MAX) { MAX = SAMPLE; }
#define UPDATE_MIN_MAX(MIN, MAX, SAMPLE) { UPDATE_MIN(MIN, SAMPLE); UPDATE_MAX(MAX, SAMPLE); }
struct api_status_t {
bool is_ok = true;
unsigned long count_sends = 0;
time_t last_send_time;
};
struct device_status_t {
unsigned long last_update_attempt;
unsigned long time_point_device_start_ms;
int last_update_returncode;
unsigned long count_sends = 0;
std::map<std::string, api_status_t> apis_status;
std::vector<std::string> sensor_names;
};
String tmpl(const __FlashStringHelper* patt, const String& value);
String wlan_ssid_to_table_row(const String& ssid, const String& encryption, int32_t rssi);
String delayToString(unsigned time_ms);
void sensor_restart();
extern String check_display_value(double value, double undef, uint8_t len, uint8_t str_len);
extern void add_Value2Json(String& res, const __FlashStringHelper* type, const String& value);
extern void add_Value2Json(String& res, const __FlashStringHelper* type, const __FlashStringHelper* debug_type, const float& value);
#if defined(ESP8266)
extern bool launchUpdateLoader(const String& md5);
#endif
extern float readCorrectionOffset(const char* correction);
namespace cfg {
extern unsigned debug;
}
#if defined(ESP8266)
extern SoftwareSerial serialSDS;
#endif
#if defined(ESP32)
#define serialSDS (Serial1)
#endif
// enum class PmSensorCmd {
// Start,
// Stop,
// ContinuousMode
// };
enum class PmSensorCmd2 { // for NPM
State,
Change,
Concentration
};
/*****************************************************************
* Debug output *
*****************************************************************/
class LoggingSerial : public HardwareSerial {
public:
LoggingSerial();
size_t write(uint8_t c) override;
size_t write(const uint8_t *buffer, size_t size) override;
String popLines();
private:
#if defined(ESP8266)
std::unique_ptr<circular_queue<uint8_t> > m_buffer;
#endif
#if defined(ESP32)
QueueHandle_t m_buffer;
#endif
};
extern class LoggingSerial Debug;
extern void debug_out(const String& text, unsigned int level);
extern void debug_out(const __FlashStringHelper* text, unsigned int level);
extern void debug_outln(const String& text, unsigned int level);
extern void debug_outln_info(const String& text);
extern void debug_outln_verbose(const String& text);
extern void debug_outln_error(const __FlashStringHelper* text);
extern void debug_outln_info(const __FlashStringHelper* text);
extern void debug_outln_verbose(const __FlashStringHelper* text);
extern void debug_outln_info(const __FlashStringHelper* text, const String& option);
extern void debug_outln_info(const __FlashStringHelper* text, float value);
extern void debug_outln_verbose(const __FlashStringHelper* text, const String& option);
extern void debug_outln_info_bool(const __FlashStringHelper* text, const bool option);
extern bool SDS_checksum_valid(const uint8_t (&data)[8]);
extern void SDS_rawcmd(const uint8_t cmd_head1, const uint8_t cmd_head2, const uint8_t cmd_head3);
// extern bool SDS_cmd(PmSensorCmd cmd);
// extern bool PMS_cmd(PmSensorCmd cmd);
// extern bool HPM_cmd(PmSensorCmd cmd);
// extern void NPM_cmd(PmSensorCmd2 cmd);
extern bool NPM_checksum_valid_4(const uint8_t (&data)[4]);
extern bool NPM_checksum_valid_16(const uint8_t (&data)[16]);
extern bool isNumeric(const String& str);
extern const __FlashStringHelper* loggerDescription(unsigned i);
#endif