-
Notifications
You must be signed in to change notification settings - Fork 0
/
mup_util.h
172 lines (161 loc) · 4.14 KB
/
mup_util.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
// i2c_pwm.h
// Adafruit PWM Servo Driver Library,
// https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library
#pragma once
//#ifdef __USE_SPIFFS_FS__
//#include <FS.h>
//#ifdef __ESP32__
//#include "SPIFFS.h"
//#endif
//#endif
#include "scheduler.h"
namespace ustd {
double parseUnitLevel(String msg) {
char buff[32];
int l;
int len = msg.length();
double br = 0.0;
memset(buff, 0, 32);
if (len > 31)
l = 31;
else
l = len;
strncpy(buff, (const char *)msg.c_str(), l);
if ((!strcmp((const char *)buff, "on")) || (!strcmp((const char *)buff, "true"))) {
br = 1.0;
} else {
if ((!strcmp((const char *)buff, "off")) || (!strcmp((const char *)buff, "false"))) {
br = 0.0;
} else {
if ((strlen(buff) > 4) && (!strncmp((const char *)buff, "pct ", 4))) {
br = atoi((char *)(buff + 4)) / 100.0;
} else {
if (strlen(buff) > 1 && buff[strlen(buff) - 1] == '%') {
buff[strlen(buff) - 1] = 0;
br = atoi((char *)buff) / 100.0;
} else {
if (strchr((char *)buff, '.')) {
br = atof((char *)buff);
} else {
br = atoi((char *)buff) / 100.0;
}
}
}
}
}
if (br < 0.0)
br = 0.0;
if (br > 1.0)
br = 1.0;
return br;
}
#ifdef __ESP__
bool fsBeginDone = false;
bool writeJson(String filename, JSONVar jsonobj) {
if (!fsBeginDone) {
#ifdef __USE_SPIFFS_FS__
if (!SPIFFS.begin(false)) {
#ifdef USE_SERIAL_DBG
Serial.println("SPIFFS.begin() failed.");
#endif
return false;
}
#else
if (!LittleFS.begin()) {
#ifdef USE_SERIAL_DBG
Serial.println("LittleFS.begin() failed.");
#endif
return false;
}
#endif
fsBeginDone = true;
}
#ifdef __USE_SPIFFS_FS__
fs::File f = SPIFFS.open(filename, "w");
#else
fs::File f = LittleFS.open(filename, "w");
#endif
if (!f) {
return false;
}
String jsonstr = JSON.stringify(jsonobj);
f.println(jsonstr);
f.close();
return true;
}
bool readJson(String filename, String &content) {
if (!fsBeginDone) {
#ifdef __USE_SPIFFS_FS__
if (!SPIFFS.begin(false)) {
#ifdef USE_SERIAL_DBG
Serial.println("SPIFFS.begin() failed.");
#endif
return false;
}
#else
if (!LittleFS.begin()) {
#ifdef USE_SERIAL_DBG
Serial.println("LittleFS.begin() failed.");
#endif
return false;
}
#endif
fsBeginDone = true;
}
content = "";
#ifdef __USE_SPIFFS_FS__
fs::File f = SPIFFS.open(filename, "r");
#else
fs::File f = LittleFS.open(filename, "r");
#endif
if (!f) {
return false;
} else {
while (f.available()) {
String lin = f.readStringUntil('\n');
content = content + lin;
}
f.close();
}
return true;
}
bool readNetJsonString(String key, String &value) {
/*
SPIFFS.begin();
fs::File f = SPIFFS.open("/net.json", "r");
if (!f) {
return false;s
} else {
String jsonstr = "";
while (f.available()) {
// Lets read line by line from the file
String lin = f.readStringUntil('\n');
jsonstr = jsonstr + lin;
}
f.close();
*/
String jsonstr;
if (readJson("/net.json", jsonstr)) {
JSONVar configObj = JSON.parse(jsonstr);
if (JSON.typeof(configObj) == "undefined") {
return false;
}
if (configObj.hasOwnProperty(key.c_str())) {
value = (const char *)configObj[key.c_str()];
} else {
return false;
}
return true;
} else {
return false;
}
}
bool readFriendlyName(String &friendlyName) {
if (readNetJsonString("friendlyname", friendlyName))
return true;
if (readNetJsonString("hostname", friendlyName))
return true;
return false;
}
#endif // __ESP__
} // namespace ustd