|
| 1 | +/** |
| 2 | + * |
| 3 | + * EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device). |
| 4 | + * |
| 5 | + * Copyright (C) 2018-2019 Thomas Friedrichsmeier |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or (at |
| 10 | + * your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | +**/ |
| 21 | + |
| 22 | +#ifndef EMBAJAXOUTPUTDRIVERGENERIC_H |
| 23 | +#define EMBAJAXOUTPUTDRIVERGENERIC_H |
| 24 | + |
| 25 | +#if defined (EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION) |
| 26 | +#error Duplicate definition of output driver. Fix your include-directives. |
| 27 | +#endif |
| 28 | +#define EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION |
| 29 | + |
| 30 | +#if not defined EmbAJAXOutputDriverWebServerClass |
| 31 | +#error Please define EmbAJAXOutputDriverWebServerClass or #include appropriate hardware specific driver |
| 32 | +#endif |
| 33 | + |
| 34 | +/** @brief Output driver implementation. This implementation should work for most arduino web servers with minimal adjustmnets. */ |
| 35 | +class EmbAJAXOutputDriver : public EmbAJAXOutputDriverBase { |
| 36 | +public: |
| 37 | + /** To register an WebServer with EmbAJAX, simply create a (globaL) instance of this class. |
| 38 | + @param server pointer to the server. The class of this is usually an auto-detected sensible default for the platform, e.g. ESP8266WebServer on ESP8266. */ |
| 39 | + EmbAJAXOutputDriver(EmbAJAXOutputDriverWebServerClass *server) { |
| 40 | + EmbAJAXBase::setDriver(this); |
| 41 | + _server = server; |
| 42 | + } |
| 43 | + void printHeader(bool html) override { |
| 44 | + _server->setContentLength(CONTENT_LENGTH_UNKNOWN); |
| 45 | + if (html) { |
| 46 | + _server->send(200, "text/html", ""); |
| 47 | + } else { |
| 48 | + _server->send(200, "text/json", ""); |
| 49 | + } |
| 50 | + } |
| 51 | + void printContent(const char *content) override { |
| 52 | + if (content[0] != '\0') _server->sendContent(content); // NOTE: There seems to be a bug in the ESP8266 server when sending empty string. |
| 53 | + } |
| 54 | + const char* getArg(const char* name, char* buf, int buflen) override { |
| 55 | + _server->arg(name).toCharArray (buf, buflen); |
| 56 | + return buf; |
| 57 | + }; |
| 58 | +private: |
| 59 | + EmbAJAXOutputDriverWebServerClass *_server; |
| 60 | +}; |
| 61 | + |
| 62 | +#endif |
0 commit comments