forked from opendata-stuttgart/sensors-software
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.h
More file actions
182 lines (149 loc) · 6.84 KB
/
utils.h
File metadata and controls
182 lines (149 loc) · 6.84 KB
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
/************************************************************************
* *
* 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>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <HardwareSerial.h>
#include "sha/sha_parallel_engine.h"
#include <freertos/queue.h>
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;
unsigned long count_sends_success = 0;
time_t last_send_time = 0;
};
struct device_status_t {
unsigned long last_update_attempt;
unsigned long time_point_device_start_ms;
int last_update_returncode;
bool sd_card_connected = false;
bool ota_in_progress = false; // When true, display shows "Updating firmware" screen
bool ota_failed = false; // When true, display shows "Update failed" screen before returning
bool ota_success = false; // When true, display shows "Update successful" screen before restart
int ota_progress_percent = -1; // 0-100 during download, -1 when not applicable
volatile bool ota_update_requested = false; // Set by webserver to trigger manual OTA check
String ip_address;
std::map<std::string, api_status_t> apis_status;
std::vector<std::string> sensor_names;
};
// UART metrics structure for status logging
struct metrics_t {
unsigned long boot_counter = 0;
unsigned long uptime_sec = 0;
unsigned long tx_counter = 0;
time_t last_telemetry_timestamp = 0;
unsigned long err_wifi_reconnects = 0;
unsigned long err_sensor = 0;
unsigned long err_sd_write = 0;
float esp_temperature = 0.0;
bool has_error = false;
};
String get_chipid();
String tmpl(const __FlashStringHelper* patt, const String& value);
enum CustomRestartReason : uint32_t {
RESTART_REASON_NONE = 0,
RESTART_REASON_OTA = 1,
RESTART_REASON_CONFIG = 2,
RESTART_REASON_USER = 3,
};
void set_restart_reason(CustomRestartReason reason);
const char* get_reset_reason_text();
String wlan_ssid_to_table_row(const String& ssid, const String& encryption, int32_t rssi);
String delayToString(unsigned time_ms);
void sensor_restart();
extern float readCorrectionOffset(const char* correction);
namespace cfg {
extern unsigned debug;
}
#define serialSDS (Serial1)
/*****************************************************************
* 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:
QueueHandle_t m_buffer;
};
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 isNumeric(const String& str);
extern const __FlashStringHelper* loggerDescription(unsigned i);
// UART metrics logging functions
extern metrics_t system_metrics;
void initMetrics();
void updateMetrics();
void logMetrics();
void incrementWiFiReconnectError();
void incrementSensorError();
void incrementSDWriteError();
void incrementTXCounter();
void initESPTemperatureSensor();
float getESPTemperature();
#endif