forked from rvdbreemen/DSMRlogger-Next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIFFSstuff.ino
782 lines (681 loc) · 22.4 KB
/
SPIFFSstuff.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
***************************************************************************
** Program : SPIFFSstuff, part of DSMRlogger-Next
** Version : v2.3.0-rc5
**
** Copyright (c) 2020 Willem Aandewiel
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
int16_t bytesWritten;
//static FSInfo SPIFFSinfo;
//====================================================================
void readLastStatus()
{
char buffer[50] = "";
char dummy[50] = "";
char spiffsTimestamp[20] = "";
File _file = SPIFFS.open("/DSMRstatus.csv", "r");
if (!_file)
{
DebugTln("read(): No /DSMRstatus.csv found ..");
}
if (_file.available())
{
int l = _file.readBytesUntil('\n', buffer, sizeof(buffer));
buffer[l] = 0;
DebugTf("read lastUpdate[%s]\r\n", buffer);
sscanf(buffer, "%[^;]; %u; %u; %[^;]", spiffsTimestamp, &nrReboots, &slotErrors, dummy);
DebugTf("values timestamp[%s], nrReboots[%u], slotErrors[%u], dummy[%s]\r\n", spiffsTimestamp, nrReboots, slotErrors, dummy);
yield();
}
_file.close();
if (strlen(spiffsTimestamp) != 13)
{
strncpy(spiffsTimestamp, "010101010101X", sizeof(spiffsTimestamp));
}
snprintf(actTimestamp, sizeof(actTimestamp), "%s", spiffsTimestamp);
} // readLastStatus()
//====================================================================
void writeLastStatus()
{
if (ESP.getFreeHeap() < 7000) // to prevent firmware from crashing!
{
DebugTf("Bailout due to low heap (%d bytes)\r\n", ESP.getFreeHeap());
writeToSysLog("Bailout low heap (%d bytes)", ESP.getFreeHeap());
esp_reboot(); //due to low memory, let's restart.
return;
}
char buffer[50] = "";
DebugTf("writeLastStatus() => %s; %u; %u;\r\n", actTimestamp, nrReboots, slotErrors);
writeToSysLog("writeLastStatus() => %s; %u; %u;", actTimestamp, nrReboots, slotErrors);
File _file = SPIFFS.open("/DSMRstatus.csv", "w");
if (!_file)
{
DebugTln("write(): No /DSMRstatus.csv found ..");
}
snprintf(buffer, sizeof(buffer), "%-13.13s; %010u; %010u; %s;\n", actTimestamp, nrReboots, slotErrors, "meta data");
_file.print(buffer);
_file.flush();
_file.close();
} // writeLastStatus()
//===========================================================================================
bool buildDataRecordFromSM(char *recIn)
{
static float GG = 1;
char record[DATA_RECLEN + 1] = "";
char key[10] = "";
uint16_t recSlot = timestampToHourSlot(actTimestamp, strlen(actTimestamp));
strlcpy(key, actTimestamp + 0, 9);
snprintf(record, sizeof(record), (char *)DATA_FORMAT, key, (float)DSMRdata.energy_delivered_tariff1, (float)DSMRdata.energy_delivered_tariff2, (float)DSMRdata.energy_returned_tariff1, (float)DSMRdata.energy_returned_tariff2
#ifdef USE_PRE40_PROTOCOL
,
(float)DSMRdata.gas_delivered2);
#else
,
(float)DSMRdata.gas_delivered);
#endif
// DATA + \n + \0
fillRecord(record, DATA_RECLEN);
strlcpy(recIn, record, DATA_RECLEN);
} // buildDataRecordFromSM()
//===========================================================================================
uint16_t buildDataRecordFromJson(char *recIn, String jsonIn)
{
//static float GG = 1;
char record[DATA_RECLEN + 1] = "";
String wOut[10];
String wPair[5];
char uKey[15] = "";
float uEDT1 = 0.0;
float uEDT2 = 0.0;
float uERT1 = 0.0;
float uERT2 = 0.0;
float uGDT = 0.0;
uint16_t recSlot;
DebugTln(jsonIn);
jsonIn.replace("{", "");
jsonIn.replace("}", "");
jsonIn.replace("\"", "");
int8_t wp = splitString(jsonIn.c_str(), ',', wOut, 9);
for (int f = 0; f < wp; f++)
{
splitString(wOut[f].c_str(), ':', wPair, 4);
if (Verbose2)
DebugTf("[%d] -> [%s]\r\n", f, wOut[f].c_str());
if (wPair[0].indexOf("recid") == 0)
strlcpy(uKey, wPair[1].c_str(), 10);
if (wPair[0].indexOf("edt1") == 0)
uEDT1 = wPair[1].toFloat();
if (wPair[0].indexOf("edt2") == 0)
uEDT2 = wPair[1].toFloat();
if (wPair[0].indexOf("ert1") == 0)
uERT1 = wPair[1].toFloat();
if (wPair[0].indexOf("ert2") == 0)
uERT2 = wPair[1].toFloat();
if (wPair[0].indexOf("gdt") == 0)
uGDT = wPair[1].toFloat();
}
strlcat(uKey, "0101X", 15);
recSlot = timestampToMonthSlot(uKey, strlen(uKey));
DebugTf("MONTHS: Write [%s] to slot[%02d] in %s\r\n", uKey, recSlot, MONTHS_FILE);
snprintf(record, sizeof(record), (char *)DATA_FORMAT, uKey, (float)uEDT1, (float)uEDT2, (float)uERT1, (float)uERT2, (float)uGDT);
// DATA + \n + \0
fillRecord(record, DATA_RECLEN);
strlcpy(recIn, record, DATA_RECLEN);
return recSlot;
} // buildDataRecordFromJson()
//===========================================================================================
void writeDataToFile(const char *fileName, const char *record, uint16_t slot, int8_t fileType)
{
uint16_t offset = 0;
if (!isNumericp(record, 8))
{
DebugTf("timeStamp [%-13.13s] not valid\r\n", record);
slotErrors++;
return;
}
if (!SPIFFS.exists(fileName))
{
switch (fileType)
{
case HOURS:
createFile(fileName, _NO_HOUR_SLOTS_);
break;
case DAYS:
createFile(fileName, _NO_DAY_SLOTS_);
break;
case MONTHS:
createFile(fileName, _NO_MONTH_SLOTS_);
break;
}
}
File dataFile = SPIFFS.open(fileName, "r+"); // read and write ..
if (!dataFile)
{
DebugTf("Error opening [%s]\r\n", fileName);
return;
}
// slot goes from 0 to _NO_OF_SLOTS_
// we need to add 1 to slot to skip header record!
offset = ((slot + 1) * DATA_RECLEN);
dataFile.seek(offset, SeekSet);
int32_t bytesWritten = dataFile.print(record);
if (bytesWritten != DATA_RECLEN)
{
DebugTf("ERROR! slot[%02d]: written [%d] bytes but should have been [%d]\r\n", slot, bytesWritten, DATA_RECLEN);
writeToSysLog("ERROR! slot[%02d]: written [%d] bytes but should have been [%d]", slot, bytesWritten, DATA_RECLEN);
}
dataFile.close();
} // writeDataToFile()
//===========================================================================================
void writeDataToFiles()
{
char record[DATA_RECLEN + 1] = "";
uint16_t recSlot;
buildDataRecordFromSM(record);
DebugTf(">%s\r\n", record); // record ends in a \n
// update HOURS
recSlot = timestampToHourSlot(actTimestamp, strlen(actTimestamp));
if (Verbose1)
DebugTf("HOURS: Write to slot[%02d] in %s\r\n", recSlot, HOURS_FILE);
writeDataToFile(HOURS_FILE, record, recSlot, HOURS);
writeToSysLog("HOURS: actTimestamp[%s], recSlot[%d]", actTimestamp, recSlot);
// update DAYS
recSlot = timestampToDaySlot(actTimestamp, strlen(actTimestamp));
if (Verbose1)
DebugTf("DAYS: Write to slot[%02d] in %s\r\n", recSlot, DAYS_FILE);
writeDataToFile(DAYS_FILE, record, recSlot, DAYS);
// update MONTHS
recSlot = timestampToMonthSlot(actTimestamp, strlen(actTimestamp));
if (Verbose1)
DebugTf("MONTHS: Write to slot[%02d] in %s\r\n", recSlot, MONTHS_FILE);
writeDataToFile(MONTHS_FILE, record, recSlot, MONTHS);
} // writeDataToFiles(fileType, dataStruct newDat, int8_t slotNr)
//===========================================================================================
void readOneSlot(int8_t fileType, const char *fileName, uint8_t recNr, uint8_t readSlot, bool doJson, const char *rName)
{
uint16_t slot, maxSlots = 0, offset;
char buffer[DATA_RECLEN + 2] = "";
char recID[10] = "";
float EDT1, EDT2, ERT1, ERT2, GDT;
switch (fileType)
{
case HOURS:
maxSlots = _NO_HOUR_SLOTS_;
break;
case DAYS:
maxSlots = _NO_DAY_SLOTS_;
break;
case MONTHS:
maxSlots = _NO_MONTH_SLOTS_;
break;
}
if (!SPIFFS.exists(fileName))
{
DebugTf("File [%s] does not excist!\r\n", fileName);
return;
}
File dataFile = SPIFFS.open(fileName, "r+"); // read and write ..
if (!dataFile)
{
DebugTf("Error opening [%s]\r\n", fileName);
return;
}
slot = (readSlot % maxSlots);
// slot goes from 0 to _NO_OF_SLOTS_
// we need to add 1 to slot to skip header record!
offset = ((slot + 1) * DATA_RECLEN);
dataFile.seek(offset, SeekSet);
int l = dataFile.readBytesUntil('\n', buffer, sizeof(buffer));
buffer[l] = 0;
if (l >= (DATA_RECLEN - 1)) // '\n' is skipped by readBytesUntil()
{
if (!isNumericp(buffer, 8)) // first 8 bytes is YYMMDDHH
{
{
Debugf("slot[%02d]==>timeStamp [%-13.13s] not valid!!\r\n", slot, buffer);
writeToSysLog("slot[%02d]==>timeStamp [%-13.13s] not valid!!", slot, buffer);
}
}
else
{
if (doJson)
{
sscanf(buffer, "%[^;];%f;%f;%f;%f;%f", recID, &EDT1, &EDT2, &ERT1, &ERT2, &GDT);
sendNestedJsonObj(recNr++, recID, slot, EDT1, EDT2, ERT1, ERT2, GDT);
}
else
{
Debugf("slot[%02d]->[%s]\r\n", slot, buffer);
}
}
}
dataFile.close();
} // readOneSlot()
//===========================================================================================
void readSlotFromTimestamp(int8_t fileType, const char *fileName, const char *timeStamp, bool doJson, const char *rName)
{
uint16_t firstSlot = 0, maxSlots = 0;
DebugTf("timeStamp[%s]\r\n", timeStamp);
switch (fileType)
{
case HOURS:
firstSlot = timestampToHourSlot(timeStamp, strlen(timeStamp));
maxSlots = _NO_HOUR_SLOTS_;
break;
case DAYS:
firstSlot = timestampToDaySlot(timeStamp, strlen(timeStamp));
maxSlots = _NO_DAY_SLOTS_;
break;
case MONTHS:
firstSlot = timestampToMonthSlot(timeStamp, strlen(timeStamp));
maxSlots = _NO_MONTH_SLOTS_;
break;
}
firstSlot += maxSlots;
DebugTf("firstSlot[%d] -> slot[%d]\r\n", firstSlot, (firstSlot % maxSlots));
readOneSlot(fileType, fileName, firstSlot, 0, doJson, rName);
} // readSlotFromTimestamp()
//===========================================================================================
void readAllSlots(int8_t fileType, const char *fileName, const char *timeStamp, bool doJson, const char *rName)
{
int16_t startSlot, endSlot, nrSlots, recNr = 0;
switch (fileType)
{
case HOURS:
startSlot = timestampToHourSlot(timeStamp, strlen(timeStamp));
nrSlots = _NO_HOUR_SLOTS_;
break;
case DAYS:
startSlot = timestampToDaySlot(timeStamp, strlen(timeStamp));
nrSlots = _NO_DAY_SLOTS_;
break;
case MONTHS:
startSlot = timestampToMonthSlot(timeStamp, strlen(timeStamp));
nrSlots = _NO_MONTH_SLOTS_;
break;
}
endSlot = nrSlots + startSlot;
//startSlot += nrSlots;
DebugTf("start[%02d], endSlot[%02d]\r\n", (startSlot % nrSlots), endSlot);
for (uint16_t s = startSlot; s < endSlot; s++)
{
readOneSlot(fileType, fileName, s, recNr++, false, "");
}
} // readAllSlots()
//===========================================================================================
bool createFile(const char *fileName, uint16_t noSlots)
{
DebugTf("fileName[%s], fileRecLen[%d]\r\n", fileName, DATA_RECLEN);
File dataFile = SPIFFS.open(fileName, "a"); // create File
// -- first write fileHeader ----------------------------------------
snprintf(cMsg, sizeof(cMsg), "%s", DATA_CSV_HEADER); // you cannot modify *fileHeader!!!
fillRecord(cMsg, DATA_RECLEN);
DebugT(cMsg);
Debugln(F("\r"));
bytesWritten = dataFile.print(cMsg);
if (bytesWritten != DATA_RECLEN)
{
DebugTf("ERROR!! slotNr[%d]: written [%d] bytes but should have been [%d] for Header\r\n", 0, bytesWritten, DATA_RECLEN);
}
DebugTln(F(".. that went well! Now add next record ..\r"));
// -- as this file is empty, write one data record ------------
snprintf(cMsg, sizeof(cMsg), "%02d%02d%02d%02d", 0, 0, 0, 0);
snprintf(cMsg, sizeof(cMsg), DATA_FORMAT, cMsg, 0.000, 0.000, 0.000, 0.000, 0.000);
fillRecord(cMsg, DATA_RECLEN);
for (int r = 1; r <= noSlots; r++)
{
DebugTf("Write [%s] Data[%-9.9s]\r\n", fileName, cMsg);
dataFile.seek((r * DATA_RECLEN), SeekSet);
bytesWritten = dataFile.print(cMsg);
if (bytesWritten != DATA_RECLEN)
{
DebugTf("ERROR!! recNo[%d]: written [%d] bytes but should have been [%d] \r\n", r, bytesWritten, DATA_RECLEN);
}
} // for ..
dataFile.close();
dataFile = SPIFFS.open(fileName, "r+"); // open for Read & writing
if (!dataFile)
{
DebugTf("Something is very wrong writing to [%s]\r\n", fileName);
return false;
}
dataFile.close();
return true;
} // createFile()
//===========================================================================================
void fillRecord(char *record, int8_t len)
{
int8_t s = 0, l = 0;
while (record[s] != '\0' && record[s] != '\n')
{
s++;
}
if (Verbose1)
DebugTf("Length of record is [%d] bytes\r\n", s);
for (l = s; l < (len - 2); l++)
{
record[l] = ' ';
}
record[l] = ';';
record[l + 1] = '\n';
record[len] = '\0';
while (record[l] != '\0')
{
l++;
}
if (Verbose1)
DebugTf("Length of record is now [%d] bytes\r\n", l);
} // fillRecord()
//====================================================================
uint16_t timestampToHourSlot(const char *TS, int8_t len)
{
//char aSlot[5];
time_t t1 = epoch((char *)TS, strlen(TS), false);
uint32_t nrHours = t1 / SECS_PER_HOUR;
//sprintf(aSlot, "%d", ((nrDays % KEEP_DAYS_HOURS) *24) + hour(t1));
//uint8_t uSlot = String(aSlot).toInt();
uint8_t recSlot = (nrHours % _NO_HOUR_SLOTS_);
if (Verbose1)
DebugTf("===>>>>> HOUR[%02d] => recSlot[%02d]\r\n", hour(t1), recSlot);
if (recSlot < 0 || recSlot >= _NO_HOUR_SLOTS_)
{
DebugTf("HOUR: Some serious error! Slot is [%d]\r\n", recSlot);
recSlot = _NO_HOUR_SLOTS_;
slotErrors++;
}
return recSlot;
} // timestampToHourSlot()
//====================================================================
uint16_t timestampToDaySlot(const char *TS, int8_t len)
{
//char aSlot[5];
time_t t1 = epoch((char *)TS, strlen(TS), false);
uint32_t nrDays = t1 / SECS_PER_DAY;
uint16_t recSlot = (nrDays % _NO_DAY_SLOTS_);
if (Verbose1)
DebugTf("===>>>>> DAY[%02d] => recSlot[%02d]\r\n", day(t1), recSlot);
if (recSlot < 0 || recSlot >= _NO_DAY_SLOTS_)
{
DebugTf("DAY: Some serious error! Slot is [%d]\r\n", recSlot);
recSlot = _NO_DAY_SLOTS_;
slotErrors++;
}
return recSlot;
} // timestampToDaySlot()
//====================================================================
uint16_t timestampToMonthSlot(const char *TS, int8_t len)
{
//char aSlot[5];
time_t t1 = epoch((char *)TS, strlen(TS), false);
uint32_t nrMonths = ((year(t1) - 1) * 12) + month(t1); // eg: year(2023) * 12 = 24276 + month(9) = 202309
uint16_t recSlot = (nrMonths % _NO_MONTH_SLOTS_); // eg: 24285 % _NO_MONTH_SLOT_
if (Verbose1)
DebugTf("===>>>>> MONTH[%02d] => recSlot[%02d]\r\n", month(t1), recSlot);
if (recSlot < 0 || recSlot >= _NO_MONTH_SLOTS_)
{
DebugTf("MONTH: Some serious error! Slot is [%d]\r\n", recSlot);
recSlot = _NO_MONTH_SLOTS_;
slotErrors++;
}
return recSlot;
} // timestampToMonthSlot()
//===========================================================================================
int32_t freeSpace()
{
#if defined(ESP8266)
FSInfo SPIFFSinfo;
SPIFFS.info(SPIFFSinfo);
Debugln((int32_t)(SPIFFSinfo.totalBytes - SPIFFSinfo.usedBytes) + " bytes ruimte in SPIFFS");
return (int32_t)(SPIFFSinfo.totalBytes - SPIFFSinfo.usedBytes);
#elif defined(ESP32)
return (SPIFFS.totalBytes() - SPIFFS.usedBytes());
#endif
} // freeSpace()
//===========================================================================================
#if defined(ESP8266)
void ESP8266_listFiles()
{
typedef struct _fileMeta
{
char Name[20];
int32_t Size;
} fileMeta;
_fileMeta dirMap[30];
int fileNr = 0;
Dir dir = SPIFFS.openDir("/"); // List files on SPIFFS
while (dir.next())
{
dirMap[fileNr].Name[0] = '\0';
strncat(dirMap[fileNr].Name, dir.fileName().substring(1).c_str(), 19); // remove leading '/'
dirMap[fileNr].Size = dir.fileSize();
fileNr++;
}
// -- bubble sort dirMap op .Name--
for (int8_t y = 0; y < fileNr; y++)
{
yield();
for (int8_t x = y + 1; x < fileNr; x++)
{
//DebugTf("y[%d], x[%d] => seq[x][%s] ", y, x, dirMap[x].Name);
if (strcasecmp(dirMap[x].Name, dirMap[y].Name) <= 0)
{
fileMeta temp = dirMap[y];
dirMap[y] = dirMap[x];
dirMap[x] = temp;
} /* end if */
//Debugln();
} /* end for */
} /* end for */
DebugTln(F("\r\n"));
for (int f = 0; f < fileNr; f++)
{
Debugf("%-25s %6d bytes \r\n", dirMap[f].Name, dirMap[f].Size);
yield();
}
SPIFFS.info(SPIFFSinfo);
Debugln(F("\r"));
if (freeSpace() < (10 * SPIFFSinfo.blockSize))
Debugf("Available SPIFFS space [%6d]kB (LOW ON SPACE!!!)\r\n", (freeSpace() / 1024));
else
Debugf("Available SPIFFS space [%6d]kB\r\n", (freeSpace() / 1024));
Debugf(" SPIFFS Size [%6d]kB\r\n", (SPIFFSinfo.totalBytes / 1024));
Debugf(" SPIFFS block Size [%6d]bytes\r\n", SPIFFSinfo.blockSize);
Debugf(" SPIFFS page Size [%6d]bytes\r\n", SPIFFSinfo.pageSize);
Debugf(" SPIFFS max.Open Files [%6d]\r\n\r\n", SPIFFSinfo.maxOpenFiles);
} // ESP8266_listFiles()
#elif defined(ESP32)
void ESP32_listFiles()
{
typedef struct _fileMeta
{
char Name[20];
int32_t Size;
} fileMeta;
_fileMeta dirMap[30];
int fileNr = 0;
File root = SPIFFS.open("/"); // List files on SPIFFS
if (!root)
{
DebugTln("- failed to open directory");
return;
}
if (!root.isDirectory())
{
DebugTln(" - not a directory");
return;
}
File file = root.openNextFile();
while (file)
{
if (file.isDirectory())
{
DebugT(" DIR : ");
Debugln(file.name());
// directory is skipped
}
else
{
DebugT(" FILE: ");
Debug(file.name());
Debug("\tSIZE: ");
Debugln(file.size());
dirMap[fileNr].Name[0] = '\0';
strncat(dirMap[fileNr].Name, file.name(), 29); // first copy file.name() to dirMap
memmove(dirMap[fileNr].Name, dirMap[fileNr].Name + 1, strlen(dirMap[fileNr].Name)); // remove leading '/'
dirMap[fileNr].Size = file.size();
}
file = root.openNextFile();
fileNr++;
}
// -- bubble sort dirMap op .Name--
for (int8_t y = 0; y < fileNr; y++)
{
yield();
for (int8_t x = y + 1; x < fileNr; x++)
{
//DebugTf("y[%d], x[%d] => seq[x][%s] ", y, x, dirMap[x].Name);
if (strcasecmp(dirMap[x].Name, dirMap[y].Name) <= 0)
{
fileMeta temp = dirMap[y];
dirMap[y] = dirMap[x];
dirMap[x] = temp;
} /* end if */
//Debugln();
} /* end for */
} /* end for */
DebugTln(F("\r\n"));
for (int f = 0; f < fileNr; f++)
{
Debugf("%-25s %6d bytes \r\n", dirMap[f].Name, dirMap[f].Size);
yield();
}
Debugln(F("\r"));
Debugf("Available SPIFFS space [%6d]kB\r\n", (freeSpace() / 1024));
Debugf(" SPIFFS Size [%6d]kB\r\n", (SPIFFS.totalBytes() / 1024));
// Debugf(" SPIFFS block Size [%6d]bytes\r\n", SPIFFS.blockSize());
// Debugf(" SPIFFS page Size [%6d]bytes\r\n", SPIFFS.pageSize());
// Debugf(" SPIFFS max.Open Files [%6d]\r\n\r\n", SPIFFS.maxOpenFiles());
} // ESP32_listFiles()
#endif
void listFiles() // Senden aller Daten an den Client
{
#if defined(ESP8266)
ESP8266_listFiles();
#elif defined(ESP32)
ESP32_listFiles();
#endif
} // listFiles()
//===========================================================================================
bool eraseFile()
{
char eName[30] = "";
//--- erase buffer
while (TelnetStream.available() > 0)
{
yield();
(char)TelnetStream.read();
}
Debug("Enter filename to erase: ");
TelnetStream.setTimeout(10000);
TelnetStream.readBytesUntil('\n', eName, sizeof(eName));
TelnetStream.setTimeout(1000);
//--- remove control chars like \r and \n ----
//--- and shift all char's one to the right --
for (int i = strlen(eName); i > 0; i--)
{
eName[i] = eName[i - 1];
if (eName[i] < ' ')
eName[i] = '\0';
}
//--- add leading slash on position 0
eName[0] = '/';
if (SPIFFS.exists(eName))
{
Debugf("\r\nErasing [%s] from SPIFFS\r\n\n", eName);
SPIFFS.remove(eName);
}
else
{
Debugf("\r\nfile [%s] not found..\r\n\n", eName);
}
//--- empty buffer ---
while (TelnetStream.available() > 0)
{
yield();
(char)TelnetStream.read();
}
} // eraseFile()
//===========================================================================================
bool DSMRfileExist(const char *fileName, bool doDisplay)
{
char fName[30] = "";
if (fileName[0] != '/')
{
strlcat(fName, "/", 5);
}
strlcat(fName, fileName, 29);
DebugTf("check if [%s] exists .. ", fName);
if (settingOledType > 0)
{
oled_Print_Msg(1, "Bestaat:", 10);
oled_Print_Msg(2, fName, 10);
oled_Print_Msg(3, "op SPIFFS?", 250);
}
if (!SPIFFS.exists(fName))
{
if (doDisplay)
{
Debugln(F("NO! Error!!"));
if (settingOledType > 0)
{
oled_Print_Msg(3, "Nee! FOUT!", 6000);
}
writeToSysLog("Error! File [%s] not found!", fName);
return false;
}
else
{
Debugln(F("NO! "));
if (settingOledType > 0)
{
oled_Print_Msg(3, "Nee! ", 6000);
}
writeToSysLog("File [%s] not found!", fName);
return false;
}
}
else
{
Debugln(F("Yes! OK!"));
if (settingOledType > 0)
{
oled_Print_Msg(3, "JA! (OK!)", 250);
}
}
return true;
} // DSMRfileExist()
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/