1#pragma once
2
3#include <ArduinoJson.h>
4#include <LittleFS.h>
5#include <ESP8266WiFi.h>
6#include <ESP8266WebServer.h>
7
8extern DynamicJsonDocument readJsonFile(const char* fileName);
9extern void writeJsonFile(const char* file_path, DynamicJsonDocument& doc);
10extern void listDir(const char* dirname, uint8_t levels);
11extern void storageInfo();
12extern void BLINK(int times);
13extern String encryption_str(int encryption);
14
15void listDir(const char* dirname, uint8_t levels) {
16 File root = LittleFS.open(dirname,"r");
17 if (!root) {
18 Serial.println("Failed to open directory");
19 return;
20 }
21
22 if (!root.isDirectory()) {
23 Serial.println("Not a directory");
24 return;
25 }
26
27 File file = root.openNextFile();
28 while (file) {
29 if (file.isDirectory()) {
30 Serial.println("-"+String(file.name()));
31 if (levels) {
32 listDir(file.name(), levels - 1);
33 }
34 } else {
35 Serial.print(" |- ");
36 Serial.print(file.name());
37 Serial.print(" SIZE: ");
38 Serial.print(file.size());
39 Serial.println(" bytes");
40 }
41
42 file = root.openNextFile();
43 }
44}
45
46DynamicJsonDocument readJsonFile(const char* file_path) {
47 DynamicJsonDocument doc(4000);
48 File file = LittleFS.open(file_path, "r");
49 deserializeJson(doc, file);
50 file.close();
51 return doc;
52}
53
54void writeJsonFile(const char* file_path, DynamicJsonDocument& doc) {
55 File file = LittleFS.open(file_path, "w");
56 serializeJson(doc, file);
57 file.close();
58}
59
60void storageInfo(){
61 FSInfo fs_info;
62 LittleFS.info(fs_info);
63 Serial.printf("LittleFS Total space: %u KB\n", fs_info.totalBytes/1000);
64 Serial.printf("LittleFS Used space: %u KB\n", fs_info.usedBytes/1000);
65 Serial.printf("LittleFS Free space: %u KB\n", (fs_info.totalBytes - fs_info.usedBytes)/1000);
66}
67
68void BLINK(int times) {
69 for (int counter = 0; counter < times * 2 ; counter++)
70 {
71 digitalWrite(BUILTIN_LED, counter % 2);
72 delay(500);
73 }
74}
75
76String encryption_str(int encryption)
77{
78 switch (encryption)
79 {
80 case ENC_TYPE_WEP:
81 return "WEP";
82 case ENC_TYPE_TKIP:
83 return "WPA";
84 case ENC_TYPE_CCMP:
85 return "WPA2";
86 case ENC_TYPE_NONE:
87 return "OPEN";
88 case ENC_TYPE_AUTO:
89 return "AUTO";
90 default:
91 return "UNKNOWN";
92 }
93}