summaryrefslogtreecommitdiff
path: root/platformio/relayctl/src/http_server.h
blob: 35f7c081ca6f53b7377ae2db0a162c74a95e3aa2 (plain)
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
#pragma once
#include <ESP8266WebServer.h>
#include <Ticker.h>
#include <memory>
#include <list>
#include <utility>
#include "config.h"
#include "wifi.h"
#include "static.h"

namespace homekit {

struct OTAStatus {
    bool invalidMd5;

    OTAStatus() : invalidMd5(false) {}

    inline void clean() {
        invalidMd5 = false;
    }
};

using files::StaticFile;

class HttpServer {
private:
    ESP8266WebServer server;
    Ticker restartTimer;
    std::shared_ptr<std::list<wifi::ScanResult>> scanResults;
    OTAStatus ota;

    char* scanBuf;
    size_t scanBufSize;

    void sendGzip(const StaticFile& file, PGM_P content_type);
    void sendError(const String& message);

    bool getInputParam(const char* field_name, size_t max_len, String& dst);

public:
    explicit HttpServer(std::shared_ptr<std::list<wifi::ScanResult>> scanResults)
        : server(80)
        , scanResults(std::move(scanResults))
        , scanBufSize(512) {
        scanBuf = new char[scanBufSize];
    };

    ~HttpServer() {
        delete[] scanBuf;
    }

    void start();
    void loop();
};

}