-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDeviceHandler.cpp
More file actions
265 lines (178 loc) · 5.42 KB
/
DeviceHandler.cpp
File metadata and controls
265 lines (178 loc) · 5.42 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
DeviceHandler.cpp - Work as device loader and save device configuration
Saul bertuccio 9 feb 2017
Released into the public domain.
*/
#include "DeviceHandler.h"
const String DeviceHandler::DATA_DIR = "/data/";
DeviceHandler::DeviceHandler():
is_opened(SPIFFS.begin()),
device(NULL)
{}
DeviceHandler::~DeviceHandler() {
delete device;
}
boolean DeviceHandler::setDevice( const String &device_name, const String &device_type ) {
if ( device && (device->getName() == device_name) )
return true;
if (device)
delete device;
if (device = loadDeviceFile( device_name )) {
return DeviceHandler::saveDeviceFile( device );
} else if (device = new Device(device_name, device_type)) {
if (!DeviceHandler::saveDeviceFile( device )) {
delete device;
device = NULL;
} else {
// Invalidate device counter
DeviceHandler::recountDevices(true);
}
}
return device != NULL;
}
boolean DeviceHandler::setDevice( const String &device_name ) {
if ( device && (device->getName() == device_name) )
return true;
if (device)
delete device;
device = loadDeviceFile( device_name );
return device != NULL;
}
boolean DeviceHandler::addDeviceKey( String * attr ) {
if (!device) return false;
return device->addKey( attr )
&& DeviceHandler::saveDeviceFile( device );
}
boolean DeviceHandler::deleteDeviceKey( String &kname ) {
if (!device) return false;
return device->removeKey( kname )
&& DeviceHandler::saveDeviceFile( device );
}
Device & DeviceHandler::getDevice() { return *device; }
boolean DeviceHandler::renameDevice( const String &new_name) {
if (!device) return false;
if (DeviceHandler::existsDeviceFile( new_name ))
return false;
if (DeviceHandler::renameDeviceFile( device->getName(), new_name)) {
device->setName(new_name);
return true;
}
return false;
}
boolean DeviceHandler::renameDeviceFile( const String &old_name, const String &new_name) {
return SPIFFS.rename(DATA_DIR + old_name, DATA_DIR + new_name);
};
boolean DeviceHandler::deleteDevice() {
if (!device) return false;
bool r = true;
if (DeviceHandler::existsDeviceFile( device->getName() ))
r = DeviceHandler::deleteDeviceFile( device->getName() );
// Invalidate device counter
DeviceHandler::recountDevices(true);
if (r) {
delete device;
device = NULL;
}
return r;
}
boolean DeviceHandler::deleteDeviceFile( const String &device_name ) { return SPIFFS.remove(DATA_DIR + device_name); }
boolean DeviceHandler::saveDevice() {
if (!device)
return false;
return DeviceHandler::saveDeviceFile( device );
}
Device * DeviceHandler::loadDeviceFile( const String &device_name ) {
String file_name = DATA_DIR + device_name;
if (!SPIFFS.exists(file_name)) {
Serial.print("Errore: impossibile caricare il file ");
Serial.println(file_name);
return NULL;
}
File f = SPIFFS.open( file_name, "r");
if (!f) {
Serial.println("Errore apertura file dispositivo");
return NULL;
}
// First line is device type
String line = f.readStringUntil(DeviceHandler::EOL);
if (line == "") {
f.close();
Serial.println("Impossibile leggere il tipo di dispositivo");
return NULL;
}
Device * d = new Device( device_name, line );
if (!d) {
f.close();
return NULL;
}
int num = d->getKeysPropertyNum();
String * k_attr = new String[num];
while ( f.available() ) {
line = f.readStringUntil(DeviceHandler::EOL);
if (line.length() == 1)
continue;
int s = -1, e = 0, i = 0;
while ( i < num ) {
e = line.indexOf(DeviceHandler::SEP, ++s);
if ( e == -1 )
break;
k_attr[i++] = line.substring(s, e);
s = e;
}
if (i == num)
d->addKey(k_attr);
}
delete[] k_attr;
f.close();
return d;
}
boolean DeviceHandler::saveDeviceFile( Device * device ) {
String fname = DATA_DIR + device->getName();
File f = SPIFFS.open(fname, "w");
if (!f) {
Serial.print("Errore: impossibile aprire in scrittura il file ");
Serial.println(fname);
return false;
}
// Salvo il tipo di dispositivo
f.print( device->getType() + DeviceHandler::EOL);
int attr_num = device->getKeysPropertyNum();
Key * k = device->getKeys();
while (k) {
String line = "";
for (int i = 0; i < attr_num; i++) {
line += k->getPropertyById(i) + DeviceHandler::SEP;
}
line += DeviceHandler::EOL;
f.print(line);
k = k->getNext();
}
f.close();
return true;
}
boolean DeviceHandler::existsDeviceFile( const String &device_name ) { return SPIFFS.exists(DATA_DIR + device_name); }
String * DeviceHandler::getDevicesName() {
String * names = NULL;
int c = getDevicesNum();
if (c <= 0) return NULL;
names = new String[c];
Dir dir = SPIFFS.openDir(DATA_DIR);
int l = DATA_DIR.length();
while (dir.next())
names[--c] = dir.fileName().substring(l);
return names;
}
int DeviceHandler::getDevicesNum() { return recountDevices(false); }
int DeviceHandler::recountDevices(bool force) {
static int device_num = -1;
if (device_num >= 0 && !force)
return device_num;
Dir dir = SPIFFS.openDir(DATA_DIR);
device_num = 0;
while (dir.next())
device_num++;
return device_num;
}
int DeviceHandler::getDeviceTypesNum() { return Device::TYPE_NUM; }
String * DeviceHandler::getDeviceTypes() { return Device::TYPES; }
String * DeviceHandler::getDeviceTypesDescription() { return Device::TYPES_DESCRIPTION; }