-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpenThingsFramework.h
134 lines (113 loc) · 4.84 KB
/
OpenThingsFramework.h
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
#ifndef OTF_OPENTHINGSFRAMEWORK_H
#define OTF_OPENTHINGSFRAMEWORK_H
#include "Request.h"
#include "Response.h"
#if defined(ARDUINO)
#include <Arduino.h>
#if defined(ESP8266)
#include "Esp8266LocalServer.h"
#define LOCAL_SERVER_CLASS Esp8266LocalServer
#elif defined(ESP32)
#include "Esp32LocalServer.h"
#define LOCAL_SERVER_CLASS Esp32LocalServer
#endif
#else
#include <stdint.h>
#include "LinuxLocalServer.h"
#define LOCAL_SERVER_CLASS LinuxLocalServer
#endif
#ifdef SERIAL_DEBUG
#if defined(ARDUINO)
#define OTF_DEBUG(...) \
Serial.print("OTF: "); \
Serial.printf(__VA_ARGS__)
#else
#define OTF_DEBUG(...) \
fprintf(stdout, "OTF: "); \
fprintf(stdout, __VA_ARGS__)
#endif
#else
#define OTF_DEBUG(...)
#endif
#include "Websocket.h"
// The size of the buffer to store the incoming request line and headers (does not include body). Larger requests will be discarded.
#define HEADERS_BUFFER_SIZE 1536
namespace OTF {
typedef void (*callback_t)(const Request &request, Response &response);
enum CLOUD_STATUS {
/** Indicates that an OTC token was not specified on initialization. */
NOT_ENABLED,
/** Indicates that the device was never able to connect to the server. */
UNABLE_TO_CONNECT,
/** Indicates that the device was previously connected to the server, but got disconnected and has been unable to reconnect. */
DISCONNECTED,
/** Indicates that the device is currently connected to the server. */
CONNECTED
};
class OpenThingsFramework {
private:
LOCAL_SERVER_CLASS localServer = LOCAL_SERVER_CLASS(80);
LocalClient *localClient = nullptr;
WebsocketClient *webSocket = nullptr;
LinkedMap<callback_t> callbacks;
callback_t missingPageCallback;
CLOUD_STATUS cloudStatus = NOT_ENABLED;
unsigned long lastCloudStatusChangeTime = millis();
char *headerBuffer = NULL;
int headerBufferSize = 0;
void webSocketEventCallback(WSEvent_t type, uint8_t *payload, size_t length);
void fillResponse(const Request &req, Response &res);
void localServerLoop();
void setCloudStatus(CLOUD_STATUS status);
static void defaultMissingPageCallback(const Request &req, Response &res);
public:
/**
* Initializes the library to only listen on a local webserver.
* @param webServerPort The local port to bind the webserver to.
* @param hdBuffer externally provided header buffer (optional)
* @param hdBufferSize size of the externally provided header buffer (optional)
*/
OpenThingsFramework(uint16_t webServerPort, char *hdBuffer = NULL, int hdBufferSize = HEADERS_BUFFER_SIZE);
/**
* Initializes the library to listen on a local webserver and connect to a remote websocket.
* @param webServerPort The local port to bind the webserver to.
* @param webSocketHost The host of the remote websocket.
* @param webSocketPort The port of the remote websocket.
* @param deviceKey The unique device key that identifies this device.
* @param useSsl Indicates if SSL should be used when connecting to the websocket.
* @param hdBuffer externally provided header buffer (optional)
* @param hdBufferSize size of the externally provided header buffer (optional)
*/
#if defined(ARDUINO)
OpenThingsFramework(uint16_t webServerPort, const String &webSocketHost, uint16_t webSocketPort,
const String &deviceKey, bool useSsl, char *hdBuffer = NULL, int hdBufferSize = HEADERS_BUFFER_SIZE);
#else
OpenThingsFramework(uint16_t webServerPort, const char *webSocketHost, uint16_t webSocketPort,
const char *deviceKey, bool useSsl, char *hdBuffer = NULL, int hdBufferSize = HEADERS_BUFFER_SIZE);
#endif
/**
* Registers a callback function to run when a request is made to the specified path. The callback function will
* be passed an OpenThingsRequest, and must return an OpenThingsResponse.
* @param path
* @param callback
*/
void on(const char *path, callback_t callback, HTTPMethod method = HTTP_ANY);
#if defined(ARDUINO)
/**
* Registers a callback function to run when a request is made to the specified path. The callback function will
* be passed an OpenThingsRequest, and must return an OpenThingsResponse.
* @param path
* @param callback
*/
void on(const __FlashStringHelper *path, callback_t callback, HTTPMethod method = HTTP_ANY);
#endif
/** Registers a callback function to run when a request is received but its path does not match a registered callback. */
void onMissingPage(callback_t callback);
void loop();
/** Returns the current status of the connection to the OpenThings Cloud server. */
CLOUD_STATUS getCloudStatus();
/** Returns the number of milliseconds since there was last a change in the cloud status. */
unsigned long getTimeSinceLastCloudStatusChange();
};
}// namespace OTF
#endif