-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.h
247 lines (228 loc) · 10.4 KB
/
helpers.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
#####################################################################################
# File: helpers.h #
# Project: tinyUPS #
# File Created: Thursday, 19th May 2022 2:36:45 am #
# Author: Sergey Ko #
# Last Modified: Wednesday, 19th March 2025 4:13:24 pm #
# Modified By: Sergey Ko #
# License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.txt) #
#####################################################################################
# CHANGELOG: #
#####################################################################################
*/
/* NOTE:
******** DEVELOPERs' MEMO
1. Debug levels:
1 - flog
2 - ntpc
3 - httpd
4 - monitor
5 - snmp
6 - snmp OID detection via serial
*/
#ifndef HELPERS_H
#define HELPERS_H
#include <WiFi.h>
#include <sys/param.h>
// #define DEBUG 3
#define _STRING(x) #x
#define STRING(x) _STRING(x)
#define VERSION_UI STRING(VERSION_WEBUI)
#define VERSION_FW STRING(VERSION_FIRMWARE)
typedef enum
{
OKAY = 1,
ERR = 0,
NOT = 2,
WAIT = 3,
NTP_SYNC_INTL = -1,
NTP_SYNC_ERR = -2,
} status_t;
#define _CHB(b, s) \
do \
{ \
b = new char[s]; \
memset(b, '\0', s); \
} while (0)
#define _CHBC(b) \
do \
{ \
memset(b, '\0', strlen(b)); \
} while (0)
#define _CHBD(b) \
do \
{ \
delete[] b; \
b = NULL; \
} while (0)
#if defined(DEBUG_ESP_PORT)
#define __DF(F, ...) \
do \
{ \
DEBUG_ESP_PORT.printf(F, ##__VA_ARGS__); \
} while (0)
#define __DL(F) \
do \
{ \
DEBUG_ESP_PORT.println(F); \
} while (0)
#define __D(F) \
do \
{ \
DEBUG_ESP_PORT.print(F); \
} while (0)
#else
#define __DF(F...) (void)0
#define __DL(F) (void)0
#define __D(F) (void)0
#endif
// config
typedef struct
{ // Configurable, Units
// - WiFi (both 32 octets max)
char ssid[32] = ""; // V
char ssidkey[32] = ""; // V
char apkey[32] = "Ya9geiqu6AiThiel"; // V
// - NTP
char ntpServer[20] = "pool.ntp.org"; // V
char ntpServerFB[20] = "time.google.com"; // V
uint16_t ntpSyncInterval = 21600; // V, second
// variable defines the offset in seconds for daylight
// saving time. It is generally one hour, that corresponds to 3600 seconds
int32_t ntpDaylightOffset = 0; // X, sec
// (GMT -1 = -3600, GMT 0 = 0, GMT +1 = 3600)
int8_t ntpTimeOffset = 0; // V, hrs
// - Auth (both 16 octets max)
char admLogin[16] = ""; // V
char admPassw[16] = ""; // V
uint16_t authTimeoutMax = 3600; // V, second
// - SYS: DIGIT
float batteryTempUT = 55.00; // V, degree C
float batteryTempLT = 45.00; // V, degree C
float deviceTempUT = 65.00; // V, degree C
float deviceTempLT = 50.00; // V, degree C
// - SNMP: STRING
char snmpGetCN[20] = "public"; // V
char snmpSetCN[20] = "secret"; // V
char snmpTrapCN[20] = "";
char sysLocation[20] = ""; // V
char sysContact[30] = ""; // V
// SNMP: DIGIT
uint16_t snmpPort = 161; // V
uint16_t snmpTrapPort = 162; // V
// - OIDs
char BatteryLastReplaceDate[7] = "010120"; // V, mm/dd/yy
// The delay in seconds the UPS remains on after being told to turn off
uint16_t upsAdvConfigShutoffDelay = 50; // V, seconds
// The delay in seconds after utility line power returns before the UPS will turn on
uint16_t upsAdvConfigReturnDelay = 20; // V, seconds
// The desired run time of the UPS, in seconds, once the low battery condition is reached
uint16_t upsAdvConfigLowBatteryRunTime = 40; // V, seconds
// unique UPS serial number
char upsSerialNumber[16] = ""; // X
} config_t;
extern config_t config;
// session for web server
typedef struct
{
char authToken[64] = "";
unsigned long authTimeout = 0;
} session_t;
extern session_t session;
// system events
typedef struct
{
volatile bool upsBatteryStatusChange = false;
volatile bool upsOutputStateChange = true;
volatile bool upsBatteryCapacityChange = true;
// when failed to connect to config.ssid
volatile bool isActiveFilesystem = false;
volatile bool wifiIsInAPMode = false;
volatile bool wifiAPConnectSuccess = false;
volatile bool isActiveSnmpAgent = false;
volatile bool isActiveHttpd = false;
volatile bool isActiveMonitor = false;
volatile bool updateInProgress = false;
} common_event_t;
extern common_event_t systemEvent;
// monitored parameters
typedef struct
{
// rw
uint8_t upsAdvControlUpsOff = 1;
uint8_t upsAdvControlTurnOnUPS = 2;
uint8_t upsAdvControlSimulatePowerFail = 1;
uint8_t upsAdvControlBypassSwitch = 1;
uint8_t upsAdvControlFlashAndBeep = 1;
uint8_t upsAdvTestDiagnostics = 1;
uint8_t upsAdvTestRuntimeCalibration = 1;
uint8_t upsPhaseResetMaxMinValues = 1;
uint8_t upsBasicOutputStatus = 2;
uint8_t upsBasicBatteryStatus = 2;
float upsAdvBatteryTemperature = 0; // , tenths of C°
float upsAdvSystemTemperature = 0; // , tenths of C°
// The current utility line voltage in VAC.
uint16_t upsAdvInputLineVoltage = UPS_RATED_INPUT_VOLTAGE;
// The current utility line voltage in tenths of VAC
uint16_t upsHighPrecInputLineVoltage = UPS_RATED_INPUT_VOLTAGE * 10;
// The current input frequency to the UPS system in tenths of Hz
uint16_t upsHighPrecInputFrequency = UPS_RATED_INPUT_FREQ * 10;
// The output voltage of the UPS system in VAC
uint16_t upsAdvOutputVoltage = UPS_RATED_OUTPUT_VOLTAGE;
// The output voltage of the UPS system in tenths of VAC
uint16_t upsHighPrecOutputVoltage = UPS_RATED_OUTPUT_VOLTAGE * 10;
// The output frequency in 0.1 Hertz, or -1 if it's unsupported by this UPS
uint8_t upsAdvOutputFrequency = UPS_RATED_OUTPUT_FREQ;
// The nominal output voltage from the UPS in VAC
uint8_t upsAdvConfigRatedOutputVoltage = UPS_RATED_OUTPUT_VOLTAGE;
// The minimum line voltage in VAC allowed before the UPS system transfers to battery backup
uint8_t upsAdvConfigLowTransferVolt = UPS_RATED_LOW_INPUT_VOLTAGE_THRESHOLD;
// The maximum line voltage in VAC allowed before the UPS system transfers to battery backup
uint8_t upsAdvConfigHighTransferVolt = UPS_RATED_HIGH_INPUT_VOLTAGE_THRESHOLD;
// The reason for the occurrence of the last transfer to UPS battery power
uint8_t upsAdvInputLineFailCause = 1;
// The sensitivity of the UPS to utility line abnormalities or noises
uint8_t upsAdvConfigSensitivity = 1;
// The results of the last runtime calibration
uint8_t upsAdvTestCalibrationResults = 1;
// Indicates whether the UPS batteries need replacing
// uint8_t upsAdvBatteryReplaceIndicator = 1; // Always NOT
// The current UPS load expressed in (?) tenths of percent of rated capacity
uint8_t upsAdvOutputLoad = 0;
// The remaining battery capacity expressed in percent of full capacity
uint8_t upsAdvBatteryCapacity = 99;
// The minimum battery capacity required before the UPS will return from a low battery shutdown condition
uint8_t getAdvConfigMinReturnCapacity = 5;
// Setting this variable to turnUpsOffToConserveBattery(2) causes a UPS on battery to be put into 'sleep' mode
uint8_t upsBasicControlConserveBattery = 1;
// The UPS battery run time remaining before battery exhaustion (seconds)
// const uint16_t upsAdvBatteryRunTimeRemaining = UPS_RATED_BATTERY_RUNTIME_SEC;
// The battery current in Amps
uint8_t upsAdvBatteryCurrent = UPS_RATED_BATTERY_AMPS_MAX;
// The results of the last UPS diagnostics test performed
uint8_t upsAdvTestDiagnosticsResults = 1;
// Current battery status determined during the last diagnostic test
uint8_t upsDiagBatteryStatus = 3;
} monitor_data_t;
extern monitor_data_t monitorData;
extern void scheduleReboot(bool now = false);
const char _logDirPath[] = "/logs";
const char _dataDirPath[] = "/data";
const char _sysLogPath[] = "/logs/sys";
const char _snmpLogPath[] = "/logs/snmp";
const char _logTempMonPath[] = "/logs/montmp";
const char _logDataMonPath[] = "/logs/monbdta";
void val2str(float val, char *buffer, uint8_t prec = 2);
void val2str(int val, char *buffer);
void val2str(unsigned int val, char *buffer);
void val2str(long val, char *buffer);
void val2str(unsigned long val, char *buffer);
void str2val(char *buffer, long *val);
void str2val(char *buffer, unsigned long *val);
void str2val(char *buffer, int *val);
void str2val(char *buffer, unsigned int *val);
void str2dt(const char *str, char *buffer);
void str2snmpdt(const char *str, char *buffer);
void dt2str(const char *dt, char *buffer);
#endif // HELPERS_H