|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <WiFi.h> |
| 4 | +#include <CWPreferences.h> |
| 5 | +#include "StatusController.h" |
| 6 | +#include "SettingsWebPage.h" |
| 7 | + |
| 8 | +WiFiServer server(80); |
| 9 | + |
| 10 | +struct ClockwiseWebServer |
| 11 | +{ |
| 12 | + String httpBuffer; |
| 13 | + |
| 14 | + static ClockwiseWebServer *getInstance() |
| 15 | + { |
| 16 | + static ClockwiseWebServer base; |
| 17 | + return &base; |
| 18 | + } |
| 19 | + |
| 20 | + void startWebServer() |
| 21 | + { |
| 22 | + server.begin(); |
| 23 | + StatusController::getInstance()->blink_led(100, 3); |
| 24 | + } |
| 25 | + |
| 26 | + void stopWebServer() |
| 27 | + { |
| 28 | + server.stop(); |
| 29 | + } |
| 30 | + |
| 31 | + void handleHttpRequest() |
| 32 | + { |
| 33 | + WiFiClient client = server.available(); |
| 34 | + if (client) |
| 35 | + { |
| 36 | + StatusController::getInstance()->blink_led(100, 1); |
| 37 | + |
| 38 | + while (client.connected()) |
| 39 | + { |
| 40 | + if (client.available()) |
| 41 | + { |
| 42 | + char c = client.read(); |
| 43 | + httpBuffer.concat(c); |
| 44 | + |
| 45 | + if (c == '\n') |
| 46 | + { |
| 47 | + uint8_t method_pos = httpBuffer.indexOf(' '); |
| 48 | + uint8_t path_pos = httpBuffer.indexOf(' ', method_pos + 1); |
| 49 | + |
| 50 | + String method = httpBuffer.substring(0, method_pos); |
| 51 | + String path = httpBuffer.substring(method_pos + 1, path_pos); |
| 52 | + String key = ""; |
| 53 | + String value = ""; |
| 54 | + |
| 55 | + if (method == "POST" && path.indexOf('?') > 0) |
| 56 | + { |
| 57 | + key = path.substring(path.indexOf('?') + 1, path.indexOf('=')); |
| 58 | + value = path.substring(path.indexOf('=') + 1, ' '); |
| 59 | + path = path.substring(0, path.indexOf('?')); |
| 60 | + } |
| 61 | + |
| 62 | + processRequest(client, method, path, key, value); |
| 63 | + httpBuffer = ""; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + delay(1); |
| 69 | + client.stop(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + void processRequest(WiFiClient client, String method, String path, String key, String value) |
| 74 | + { |
| 75 | + if (method == "GET" && path == "/") { |
| 76 | + client.println("HTTP/1.0 200 OK"); |
| 77 | + client.println("Content-Type: text/html"); |
| 78 | + client.println(); |
| 79 | + client.println(SETTINGS_PAGE); |
| 80 | + } else if (method == "GET" && path == "/get") { |
| 81 | + getCurrentSettings(client); |
| 82 | + } else if (method == "POST" && path == "/restart") { |
| 83 | + client.println("HTTP/1.0 204 No Content"); |
| 84 | + delay(1000); |
| 85 | + ESP.restart(); |
| 86 | + } else if (method == "POST" && path == "/set") { |
| 87 | + ClockwiseParams::getInstance()->load(); |
| 88 | + //a baby seal has died due this ifs |
| 89 | + if (key == ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT) { |
| 90 | + ClockwiseParams::getInstance()->displayBright = value.toInt(); |
| 91 | + } else if (key == ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN) { |
| 92 | + ClockwiseParams::getInstance()->swapBlueGreen = (value == "1"); |
| 93 | + } else if (key == ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT) { |
| 94 | + ClockwiseParams::getInstance()->use24hFormat = (value == "1"); |
| 95 | + } else if (key == ClockwiseParams::getInstance()->PREF_TIME_ZONE) { |
| 96 | + ClockwiseParams::getInstance()->timeZone = value; |
| 97 | + } |
| 98 | + ClockwiseParams::getInstance()->save(); |
| 99 | + client.println("HTTP/1.0 204 No Content"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + void getCurrentSettings(WiFiClient client) { |
| 105 | + ClockwiseParams::getInstance()->load(); |
| 106 | + |
| 107 | + char response[256]; |
| 108 | + snprintf(response, sizeof(response), "{\"%s\":%d,\"%s\":%d,\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\"}", \ |
| 109 | + ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT, |
| 110 | + ClockwiseParams::getInstance()->displayBright, |
| 111 | + ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN, |
| 112 | + ClockwiseParams::getInstance()->swapBlueGreen, |
| 113 | + ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT, |
| 114 | + ClockwiseParams::getInstance()->use24hFormat, |
| 115 | + ClockwiseParams::getInstance()->PREF_TIME_ZONE, |
| 116 | + ClockwiseParams::getInstance()->timeZone.c_str(), |
| 117 | + ClockwiseParams::getInstance()->PREF_WIFI_SSID, |
| 118 | + ClockwiseParams::getInstance()->wifiSsid.c_str()); |
| 119 | + |
| 120 | + client.println("HTTP/1.0 200 OK"); |
| 121 | + client.println("Content-Type: application/json"); |
| 122 | + client.println(); |
| 123 | + client.println(response); |
| 124 | + } |
| 125 | + |
| 126 | +}; |
0 commit comments