forked from Hopperpop/Sattrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.h
201 lines (151 loc) · 8.07 KB
/
Config.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
#ifndef config_h
#define config_h
#include "Pixels.h"
#define CONFIGVERSION 8
void closeAllConnections();
void sendconfig(AsyncWebServerRequest *request);
boolean checkRange(String Value);
void setDefaultConfig();
bool getTle(int ide, bool forceupdate);
//////////////////////////
// Config definition //
//////////////////////////
struct ConfigStruct {
boolean daylight;
boolean dhcp;
uint8_t IP[4];
uint8_t Netmask[4];
uint8_t Gateway[4];
double lon;
double lat;
double alt;
double offset;
double sunoffset;
int8_t timezone;
uint8_t version;
uint16_t satnum;
RgbColor ColorDayL;
RgbColor ColorDayH;
RgbColor ColorVisL;
RgbColor ColorVisH;
RgbColor ColorEclL;
RgbColor ColorEclH;
char ntpServerName[128];
char password[64];
char ssid[32];
char host[32];
char ap_password[32];
};
ConfigStruct* config;
void setDefaultConfig(){
strcpy(config->ssid,"SSID");
config->password[0] = '\0';
strcpy(config->host,"Sattrack");
strcpy(config->ap_password,"123456789");
strcpy(config->ntpServerName, "0.be.pool.ntp.org");
config->daylight = true;
config->timezone = 1;
config->dhcp = true;
config->IP[0]=192;config->IP[1]=168;config->IP[2]=1;config->IP[3]=100;
config->Netmask[0]=255;config->Netmask[1]=255;config->Netmask[2]=255;config->Netmask[3]=0;
config->Gateway[0]=192;config->Gateway[1]=168;config->Gateway[2]=1;config->Gateway[3]=1;
config->lat = 0.0;
config->lon = 0.0;
config->alt = 0.0;
config->offset = 0.0;
config->sunoffset = -6.0;
config->satnum = 25544; //ISS
config->ColorDayL = RgbColor(0,255,0); //green
config->ColorDayH = RgbColor(0,0,255); //blue
config->ColorVisL = RgbColor(255,255,0); //yellow
config->ColorVisH = RgbColor(255,255,255); //white
config->ColorEclL = RgbColor(0,0,255); //blue
config->ColorEclH = RgbColor(255,0,0); //red
config->version = CONFIGVERSION;
}
void initConfig(){
EEPROM.begin(sizeof(ConfigStruct));
config = reinterpret_cast<ConfigStruct*>(EEPROM.getDataPtr());
if(config->version != CONFIGVERSION){
setDefaultConfig();
}
}
////////////////////////////////////
// Post handlers //
////////////////////////////////////
RgbColor HexColorToRgb(String hexstring){
long number = strtol( &hexstring[1], NULL, 16);
// Split them up into r, g, b values
uint8_t r = number >> 16;
uint8_t g = number >> 8 & 0xFF;
uint8_t b = number & 0xFF;
return RgbColor(r,g,b);
}
bool saveNetworkSettings(AsyncWebServerRequest *request){
int params = request->params();
if (params > 0 ){
int part = request->getParam(0)->value().toInt();
if (part == 1){
config->dhcp = false;
}else if (part == 3){
config->daylight = false;
}
for ( uint8_t i = 1; i < params; i++ ) {
if (part == 1){
if (request->getParam(i)->name() == "SSID") {strlcpy(config->ssid,request->getParam(i)->value().c_str(),sizeof(ConfigStruct::ssid));state = RESTART;}
else if (request->getParam(i)->name() == "PSK") {strlcpy(config->password,request->getParam(i)->value().c_str(),sizeof(ConfigStruct::password));}
else if (request->getParam(i)->name() == "HOST") {strlcpy(config->host,request->getParam(i)->value().c_str(),sizeof(ConfigStruct::host));}
else if (request->getParam(i)->name() == "PASS") {strlcpy(config->ap_password,request->getParam(i)->value().c_str(),sizeof(ConfigStruct::ap_password));}
else if (request->getParam(i)->name() == "ip_0") {if (checkRange(request->getParam(i)->value())) config->IP[0] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "ip_1") {if (checkRange(request->getParam(i)->value())) config->IP[1] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "ip_2") {if (checkRange(request->getParam(i)->value())) config->IP[2] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "ip_3") {if (checkRange(request->getParam(i)->value())) config->IP[3] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "nm_0") {if (checkRange(request->getParam(i)->value())) config->Netmask[0] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "nm_1") {if (checkRange(request->getParam(i)->value())) config->Netmask[1] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "nm_2") {if (checkRange(request->getParam(i)->value())) config->Netmask[2] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "nm_3") {if (checkRange(request->getParam(i)->value())) config->Netmask[3] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "gw_0") {if (checkRange(request->getParam(i)->value())) config->Gateway[0] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "gw_1") {if (checkRange(request->getParam(i)->value())) config->Gateway[1] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "gw_2") {if (checkRange(request->getParam(i)->value())) config->Gateway[2] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "gw_3") {if (checkRange(request->getParam(i)->value())) config->Gateway[3] = (uint8_t)request->getParam(i)->value().toInt();}
else if (request->getParam(i)->name() == "DHCP") {config->dhcp = true;}
}
if (part == 2){
if (request->getParam(i)->name() == "lon") {config->lon = atof(request->getParam(i)->value().c_str());state = RECALC;}
else if (request->getParam(i)->name() == "lat") {config->lat = atof(request->getParam(i)->value().c_str());}
else if (request->getParam(i)->name() == "alt") {config->alt = atof(request->getParam(i)->value().c_str());}
else if (request->getParam(i)->name() == "off") {config->offset = atof(request->getParam(i)->value().c_str());}
else if (request->getParam(i)->name() == "sun") {config->sunoffset = atof(request->getParam(i)->value().c_str());}
else if (request->getParam(i)->name() == "sat") {config->satnum = atoi(request->getParam(i)->value().c_str());}
}
if (part == 3){
if (request->getParam(i)->name() == "VisL") {config->ColorVisL = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "VisH") {config->ColorVisH = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "DayL") {config->ColorDayL = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "DayH") {config->ColorDayH = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "EclL") {config->ColorEclL = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "EclH") {config->ColorEclH = HexColorToRgb(request->getParam(i)->value());}
else if (request->getParam(i)->name() == "ds") {config->daylight = true;}
else if (request->getParam(i)->name() == "ts") {strlcpy(config->ntpServerName,request->getParam(i)->value().c_str(),sizeof(ConfigStruct::ntpServerName));}
else if (request->getParam(i)->name() == "tz") {config->timezone = (int8_t)request->getParam(i)->value().toInt();state = RESEND;}
}
}
EEPROM.getDataPtr();
EEPROM.commit();
return true;
}else{
return false;
}
}
boolean checkRange(String Value)
{
if (Value.toInt() < 0 || Value.toInt() > 255)
{
return false;
}
else
{
return true;
}
}
#endif