summaryrefslogtreecommitdiffstats
path: root/f015_apihandler.ino
blob: eeb974defbb65636c036f5aeb8e4db7454c03500 (plain) (blame)
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
void handleTest() {
  server.send(200, "text/plain", "200: OK");
}

bool preveriprijavo() {
  if(!server.authenticate(program_ime, readfile("/403/webgeslo.txt").c_str())){
    server.requestAuthentication();
    return false;
  } else {
    return true;
  }
}
void handleRoot() {
  if (!preveriprijavo()) return;
  server.sendHeader("Location", "/www/index.html");
  server.send(303);
}
void handleRename() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("i") || server.arg("i") == NULL || !server.hasArg("v") || server.arg("v") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: i(z) in v (pot datoteke, npr. /www/index.html)"));
    return;
  }
  if(SPIFFS.rename(server.arg("i"), server.arg("v"))) {
    server.send(200, "text/html", fihr(200, "uspešno premaknjeno!"));
  } else {
    server.send(500, "text/html", fihr(500, "premikanje ni uspelo!"));
  }
}
void handleTms() { // timeset
  if (!preveriprijavo()) return;
  if (!server.hasArg("s") || server.arg("s") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: s(ec) ter neobvezni: h(r), m(in), s(ec), d(ay), (m)o(nth), y(ear) (ce je prisoten le argument s, nastavimo kot unix timestamp, drugace kot mhsdmy)"));
    return;
  }
  if (!server.hasArg("m") || server.arg("m") == NULL) {
    setTime(String(server.arg("s")).toInt());
    server.send(200, "text/html", fihr(200, "ura nastavljena!"));
  } else {
    if(server.hasArg("h") && server.arg("h") != NULL && server.hasArg("m") && server.arg("m") != NULL && server.hasArg("s") && server.arg("s") != NULL && 
    server.hasArg("d") && server.arg("d") != NULL && server.hasArg("o") && server.arg("o") != NULL && server.hasArg("y") && server.arg("y") != NULL) {
      setTime(String(server.arg("h")).toInt(), String(server.arg("m")).toInt(), String(server.arg("s")).toInt(), String(server.arg("d")).toInt(),
      String(server.arg("o")).toInt(), String(server.arg("y")).toInt());
      server.send(200, "text/html", fihr(200, "ura zamenjana!"));
    } else {
      server.send(400, "text/html", fihr(400, "ce ze delas ura, minuta, sekunda, dan, mesec, leto; morajo biti prisotni vsi argumenti (hmsdoy)"));
    }
  }
}
void handlePin() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("p") || server.arg("p") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: p(in) ter neobvezni: v(rednost) (0-1 za digital, 0-1023 za a) (glede na to ce je prisoten spremeni pinMode)"));
    return;
  }
  if (server.hasArg("v") && server.arg("v") != NULL) {
    pinMode(pin2gpio(server.arg("p")), OUTPUT);
    if(server.arg("p").substring(0, 1) == "a" || server.arg("p").substring(0, 1) == "A" || server.arg("v").toInt() > 1) {
      analogWrite(pin2gpio(server.arg("p")), server.arg("v").toInt());
      server.send(200, "text/html", fihr(200, "OK"));
    } else {
      digitalWrite(pin2gpio(server.arg("p")), server.arg("v").toInt());server.send(200, "text/html", fihr(200, "OK"));
    }
  } else {
    pinMode(pin2gpio(server.arg("p")), INPUT);
    delay(1); // idk
    if(server.arg("p").substring(0, 1) == "a" || server.arg("p").substring(0, 1) == "A") {
      ;
      server.send(200, "text/plain", String(analogRead(pin2gpio(server.arg("p")))));
    } else {
      server.send(200, "text/plain", String(digitalRead(pin2gpio(server.arg("p")))));
      ;
    }
  }
}
void handleRes() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("k") || server.arg("k") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: k(aj), vrednost je lahko: availheap"));
    return;
  }
  server.send(200, "text/plain", String(ESP.getFreeHeap()));
}
void handleSetAP() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("s") || server.arg("s") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: s(sid) ter neobvezni: p(ass), c(hannel), h(idden-bool) in m(AC address npr. EEFABC286787 APja)"));
    return;
  }
  WiFi.softAPdisconnect(true);
  if(server.hasArg("m") && server.arg("m") != NULL) {
    long unsigned int mac; uint8_t macar[6];
    mac = strtoul(server.arg("m").c_str(), NULL, 16);
    memcpy(macar, (char*)&mac, 6);
    wifi_set_macaddr(STATION_IF, macar);
  }
  if (server.hasArg("h") && server.arg("h") != NULL) {
    writefile("/403/hostname.txt", String(server.arg("h")));
  }
  writefile("/403/wifi-ap.txt", String(server.arg("s"))+","+String(server.arg("p"))+","+String(server.arg("c"))+","+String(readfile("/403/hostname.txt")));
  WiFi.softAPConfig(ipas, gatewayas, subnetas);
  WiFi.softAP(server.arg("s").c_str(), server.arg("p").c_str(), server.arg("c").toInt(), readfile("/403/hostname.txt").toInt());
  server.send(200, "text/html", fihr(200, "AP nastavljena"));
}
void handleSetSTA() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("s") || server.arg("s") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: s(sid) ter neobvezni: p(ass), h(ostname) in m(AC address npr. EEFABC286787 STAja)"));
    return;
  }
  WiFi.disconnect(true);
  if(server.hasArg("m") && server.arg("m") != NULL) {
    long unsigned int mac; uint8_t macar[6];
    mac = strtoul(server.arg("m").c_str(), NULL, 16);
    memcpy(macar, (char*)&mac, 6);
    wifi_set_macaddr(SOFTAP_IF, macar);
  }
  if (server.hasArg("h") && server.arg("h") != NULL) writefile("/403/hostname.txt", String(server.arg("h")));
  WiFi.hostname(readfile("/403/hostname.txt").c_str());
  WiFi.begin(server.arg("s").c_str(), server.arg("p").c_str());
  server.send(200, "text/html", fihr(200, "STA nastavljena"));
}
void handleGetID() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("k") || server.arg("k") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: k(aj s(ta MAC)/a(p MAC)/i(p)/c(hipId)/h(ostname)). prebere ID, za pisanje MACa glejte /api/sta in /api/ap"));
    return;
  }
  if(server.arg("k").startsWith("s")) {
    server.send(200, "text/plain", String(WiFi.macAddress()));
  } else if (server.arg("k").startsWith("a")) {
    server.send(200, "text/plain", String(WiFi.softAPmacAddress()));
  } else if (server.arg("k").startsWith("i")) {
    server.send(200, "text/plain", WiFi.localIP().toString());
  } else if (server.arg("k").startsWith("c")) {
    server.send(200, "text/plain", String(ESP.getChipId()));
  } else {
    server.send(200, "text/plain", String(readfile("/403/hostname.txt")));
  }
}
void handlePWM() {
  if (!preveriprijavo()) return;
  if (server.hasArg("v") && server.arg("v") != NULL) {
    writefile("/403/pwmfreq.txt", String(server.arg("v")));
    analogWriteFreq(String(server.arg("v")).toInt());
    server.send(200, "text/html", fihr(200, "PWM nastavljena"));
  } else {
    server.send(400, "text/html", fihr(400, "obvezni argument: v(rednost v Hz)"));
  }
}
void handleRestart() {
  if (!preveriprijavo()) return;
  ESP.restart();
}
void handleDeepSleep() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("m") || server.arg("m") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezni argument: m(ikrosekund)"));
    return;
  }
  ESP.deepSleep(String(server.arg("m")).toInt());
}
void handleUpdate() {
  if (!preveriprijavo()) return;
  int vraat;
  if (!server.hasArg("g") || server.arg("g") == NULL || !server.hasArg("p") || server.arg("p") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezna argumenta: g(ostiteljev IP) in p(ot /do/datoteke.bin), neobvezen: v(rata HTTP (privzeta 80))"));
    return;
  }
  if(String(server.arg("v")).toInt() > 0 && String(server.arg("v")).toInt() < 65536) {
    vraat = String(server.arg("v")).toInt();
  } else {
    vraat = 80;
  }
  t_httpUpdate_return ret = ESPhttpUpdate.update(server.arg("g"), vraat, server.arg("p"), String(verzija[0])+"."+String(verzija[1])+"."+String(verzija[2]));
  switch(ret) {
      case HTTP_UPDATE_FAILED:
          server.send(500, "text/html", fihr(500, "Sistemska napaka: programska posodobitev: ni uspela"));
          break;
      case HTTP_UPDATE_NO_UPDATES:
          server.send(304, "text/html", fihr(304, "Nespremenjeno: programska posodobitev: ni potrebna, verjetno ni nove verzije"));
          break;
      case HTTP_UPDATE_OK:
          server.send(200, "text/html", fihr(200, "OK: programska posodobitev: uspela, po ponovnem zagonu se bo uporabila"));
          break;
  }
}
void handleUnixTimestamp() {
  if (!preveriprijavo()) return;
  server.send(200, "text/plain", String(now()));
}
void handleNewPassword() {
  if (!preveriprijavo()) return;
  if (!server.hasArg("g") || server.arg("g") == NULL) {
    server.send(400, "text/html", fihr(400, "obvezen argument: g(eslo-novo)"));
    return;
  }
  writefile("/403/webgeslo.txt", String(server.arg("g")));
}
void handleFileUpload() {
  if (!preveriprijavo()) return;
  HTTPUpload& upload = server.upload();
  if(upload.status == UPLOAD_FILE_START){
    String filename = upload.filename;
    if(!filename.startsWith("/")) filename = "/"+filename;
    if (razhroscevanje) Serial.print("(razhroscevanje) -> handleFileUpload Name: "); Serial.println(filename);
    fsUploadFile = SPIFFS.open(filename, "w");            // Open the file for writing in SPIFFS (create if it doesn't exist)
    filename = String();
  } else if(upload.status == UPLOAD_FILE_WRITE){
    if(fsUploadFile)
      fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
  } else if(upload.status == UPLOAD_FILE_END){
    if(fsUploadFile) {                                    // If the file was successfully created
      fsUploadFile.close();                               // Close the file again
      Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
      server.send(200, "text/html", fihr(200, "prenos uspel"));
    } else {
      server.send(500, "text/html", fihr(500, "izdelava datoteke ni uspela"));
    }
  }
}
bool handleFileRead(String path) { // send the right file to the client (if it exists)
  if (!preveriprijavo()) return false;
  // Serial.println("handleFileRead: " + path); // 2hc
  if (path.endsWith("/")) path += "index.html";         // If a folder is requested, send the index file
  if(path.indexOf("/403/") >= 0) {
    server.send(403, "text/html", fihr(403, "prepovedano"));
    return true;
  }
  String contentType = getContentType(path);            // Get the MIME type
  if (SPIFFS.exists(path)) {                            // If the file exists
    File file = SPIFFS.open(path, "r");                 // Open it
    size_t sent = server.streamFile(file, contentType); // And send it to the client
    file.close();                                       // Then close the file again
    return true;
  }
  return false;                                         // If the file doesn't exist, return false
}