This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandling.ino
124 lines (117 loc) · 5.64 KB
/
FileHandling.ino
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
#define JSONCONFIG_IP "ip"
#define JSONCONFIG_NETMASK "netmask"
#define JSONCONFIG_GW "gw"
#define JSONCONFIG_CCUIP "ccuip"
#define JSONCONFIG_SONOFF "sonoff"
#define JSONCONFIG_LOXUDPPORT "loxudpport"
//#define JSONCONFIG_LOXUSERNAME "loxusername"
//#define JSONCONFIG_LOXPASSWORD "loxpassword"
#define JSONCONFIG_BACKENDTYPE "backendtype"
#define JSONCONFIG_MODEL "model"
#define JSONCONFIG_SHUTTER_MOTORSWITCHINGTIME "sh_mst"
#define JSONCONFIG_SHUTTER_DRIVETIMEUP "sh_dtup"
#define JSONCONFIG_SHUTTER_DRIVETIMEDOWN "sh_dtdown"
#define JSONCONFIG_SHUTTER_DRIVESUNTILCALIB "sh_cal"
bool loadSystemConfig() {
DEBUG(F("loadSystemConfig mounting FS..."), "loadSystemConfig()", _slInformational);
if (SPIFFS.begin()) {
DEBUG(F("loadSystemConfig mounted file system"), "loadSystemConfig()", _slInformational);
if (SPIFFS.exists("/" + configJsonFile)) {
DEBUG(F("loadSystemConfig reading config file"), "loadSystemConfig()", _slInformational);
File configFile = SPIFFS.open("/" + configJsonFile, "r");
if (configFile) {
DEBUG(F("loadSystemConfig opened config file"), "loadSystemConfig()", _slInformational);
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
DEBUG("Content of JSON Config-File: /" + configJsonFile, "loadSystemConfig()", _slInformational);
#ifdef SERIALDEBUG
json.printTo(Serial);
Serial.println();
#endif
if (json.success()) {
DEBUG("\nJSON OK", "loadSystemConfig()", _slInformational);
((json[JSONCONFIG_IP]).as<String>()).toCharArray(SonoffNetConfig.ip, IPSIZE);
((json[JSONCONFIG_NETMASK]).as<String>()).toCharArray(SonoffNetConfig.netmask, IPSIZE);
((json[JSONCONFIG_GW]).as<String>()).toCharArray(SonoffNetConfig.gw, IPSIZE);
((json[JSONCONFIG_CCUIP]).as<String>()).toCharArray(GlobalConfig.ccuIP, IPSIZE);
((json[JSONCONFIG_SONOFF]).as<String>()).toCharArray(GlobalConfig.DeviceName, VARIABLESIZE);
//((json[JSONCONFIG_LOXUSERNAME]).as<String>()).toCharArray(LoxoneConfig.Username, VARIABLESIZE);
//((json[JSONCONFIG_LOXPASSWORD]).as<String>()).toCharArray(LoxoneConfig.Password, VARIABLESIZE);
((json[JSONCONFIG_LOXUDPPORT]).as<String>()).toCharArray(LoxoneConfig.UDPPort, 10);
GlobalConfig.BackendType = json[JSONCONFIG_BACKENDTYPE];
GlobalConfig.Model = json[JSONCONFIG_MODEL];
GlobalConfig.Hostname = "Sonoff-" + String(GlobalConfig.DeviceName);
ShutterConfig.MotorSwitchingTime = json[JSONCONFIG_SHUTTER_MOTORSWITCHINGTIME].as<float>();
ShutterConfig.DriveTimeUp = json[JSONCONFIG_SHUTTER_DRIVETIMEUP].as<float>();
ShutterConfig.DriveTimeDown = json[JSONCONFIG_SHUTTER_DRIVETIMEDOWN].as<float>();
ShutterConfig.DrivesUntilCalib = json[JSONCONFIG_SHUTTER_DRIVESUNTILCALIB];
} else {
DEBUG(F("\nloadSystemConfig ERROR loading config"), "loadSystemConfig()", _slInformational);
}
}
return true;
} else {
DEBUG("/" + configJsonFile + " not found.", "loadSystemConfig()", _slInformational);
return false;
}
SPIFFS.end();
} else {
DEBUG(F("loadSystemConfig failed to mount FS"), "loadSystemConfig()", _slCritical);
return false;
}
}
bool saveSystemConfig() {
SPIFFS.begin();
DEBUG(F("saving config"), "saveSystemConfig()", _slInformational);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json[JSONCONFIG_IP] = SonoffNetConfig.ip;
json[JSONCONFIG_NETMASK] = SonoffNetConfig.netmask;
json[JSONCONFIG_GW] = SonoffNetConfig.gw;
json[JSONCONFIG_CCUIP] = GlobalConfig.ccuIP;
json[JSONCONFIG_SONOFF] = GlobalConfig.DeviceName;
json[JSONCONFIG_BACKENDTYPE] = GlobalConfig.BackendType;
//json[JSONCONFIG_LOXUSERNAME] = LoxoneConfig.Username;
//json[JSONCONFIG_LOXPASSWORD] = LoxoneConfig.Password;
json[JSONCONFIG_LOXUDPPORT] = LoxoneConfig.UDPPort;
json[JSONCONFIG_MODEL] = GlobalConfig.Model;
json[JSONCONFIG_SHUTTER_MOTORSWITCHINGTIME] = ShutterConfig.MotorSwitchingTime;
json[JSONCONFIG_SHUTTER_DRIVETIMEUP] = ShutterConfig.DriveTimeUp;
json[JSONCONFIG_SHUTTER_DRIVETIMEDOWN] = ShutterConfig.DriveTimeDown;
json[JSONCONFIG_SHUTTER_DRIVESUNTILCALIB] = ShutterConfig.DrivesUntilCalib;
SPIFFS.remove("/" + configJsonFile);
File configFile = SPIFFS.open("/" + configJsonFile, "w");
if (!configFile) {
DEBUG(F("failed to open config file for writing"), "saveSystemConfig()", _slCritical);
return false;
}
#ifdef SERIALDEBUG
json.printTo(Serial);
Serial.println();
#endif
json.printTo(configFile);
configFile.close();
SPIFFS.end();
return true;
}
void setBootConfigMode() {
if (SPIFFS.begin()) {
DEBUG(F("setBootConfigMode mounted file system"), "setBootConfigMode()", _slInformational);
if (!SPIFFS.exists("/" + bootConfigModeFilename)) {
File bootConfigModeFile = SPIFFS.open("/" + bootConfigModeFilename, "w");
bootConfigModeFile.print("0");
bootConfigModeFile.close();
SPIFFS.end();
DEBUG(F("Boot to ConfigMode requested. Restarting..."), "setBootConfigMode()", _slInformational);
WebServer.send(200, "text/plain", F("<state>enableBootConfigMode - Rebooting</state>"));
delay(500);
ESP.restart();
} else {
WebServer.send(200, "text/plain", F("<state>enableBootConfigMode - FAILED!</state>"));
SPIFFS.end();
}
}
}