-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigHandler.h
150 lines (119 loc) · 3.63 KB
/
ConfigHandler.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
#pragma once
#include <map>
#include <string>
#include <LittleFS.h>
#include <time.h>
//1,触发方式,1,高电平(默认)2,低电平 triggerType: 1:高电平 (默认)2:低电平
//2,是否物理开关连续5次进入配网模式。wifiConfig[1,2] 1:连续开、关5次物理开关,进入配网模式。 2、长按物理开关10秒进入配置模式(默认)(点动模式有效)
//3,点动模式,自锁模式 EventType 1:点动模式(默认) ,2:自锁模式
//4,通电时开关状态开还是关,initState 1:开 ,2:关 (默认)
enum EventType{
ELECTRONIC =1,//1:点动模式(默认)
SELF_LOCK = 2//自锁模式
};
enum WifiConfig{
FIVE_TIMES = 1,//连续开、关5次物理开关,进入配网模式
LONG_TOUCH = 2,//长按物理开关10秒进入配置模式(默认)(点动模式有效)
WifiConfig_NONE = 3 //不执行
};
enum TriggerType{
TriggerType_HIGH = 1,//高电平
TriggerType_LOW = 2//低电平
};
enum InitState{
INIT_ON = 1,//开
INIT_OFF = 2//关 (默认) int
};
typedef struct{
int EventType = 1;
int wifiConfig = 2;
int triggerType = 1;
int initState = 2;
}LoginConfig;
static LoginConfig loginConfig;
static int getEventType(){
return loginConfig.EventType;
}
static void setEventType(int EventType){
loginConfig.EventType = EventType;
}
static int getWifiConfig(){
return loginConfig.wifiConfig;
}
static void setWifiConfig(int wifiConfig){
loginConfig.wifiConfig = wifiConfig;
}
static int getTriggerType(){
return loginConfig.triggerType;
}
static void setTriggerType(int triggerType){
loginConfig.triggerType = triggerType;
}
static int getInitState(){
return loginConfig.initState;
}
static void setInitState(int initState){
loginConfig.initState = initState;
}
bool getLocalTime(struct tm * info, uint32_t ms) {
uint32_t count = ms / 10;
time_t now;
time(&now);
localtime_r(&now, info);
if (info->tm_year > (2016 - 1900)) {
return true;
}
while (count--) {
delay(10);
time(&now);
localtime_r(&now, info);
if (info->tm_year > (2016 - 1900)) {
return true;
}
}
return false;
}
bool saveDeviceConfig(){
LittleFS.end();
if (!LittleFS.begin()) {
Serial.println("Unable to begin(), aborting");
return false;
}
Serial.println("Save file, may take a while...");
long start = millis();
File f = LittleFS.open("/deviceconfig.bin", "w");
if (!f) {
Serial.println("Unable to open deviceconfig file for writing");
return false;
}
f.write((char*)&loginConfig.EventType, sizeof(uint16_t));
f.write((char*)&loginConfig.wifiConfig, sizeof(uint16_t));
f.write((char*)&loginConfig.triggerType, sizeof(uint16_t));
f.write((char*)&loginConfig.initState, sizeof(uint16_t));
f.close();
long stop = millis();
Serial.printf("==> Time to write chunks = %ld milliseconds\n", stop - start);
return true;
}
bool loadDeviceConfig(){
LittleFS.end();
if (!LittleFS.begin()) {
Serial.println("Unable to begin(), aborting");
return false;
}
Serial.println("Reading file, may take a while...");
long start = millis();
File f = LittleFS.open("/deviceconfig.bin", "r");
if (!f) {
Serial.println("Unable to open file for reading, aborting");
return false;
}
f.read((uint8_t*)&loginConfig.EventType, sizeof(uint16_t));
f.read((uint8_t*)&loginConfig.wifiConfig, sizeof(uint16_t));
f.read((uint8_t*)&loginConfig.triggerType, sizeof(uint16_t));
f.read((uint8_t*)&loginConfig.initState, sizeof(uint16_t));
f.close();
long stop = millis();
Serial.printf("==> Time to write chunks = %ld milliseconds\n", stop - start);
return true;
}