2016-09-18 08:30:34 +00:00
|
|
|
#ifndef SPEJSNODE_H
|
|
|
|
#define SPEJSNODE_H
|
|
|
|
|
2016-06-29 15:00:22 +00:00
|
|
|
#include <user_config.h>
|
|
|
|
#include <common_config.h>
|
|
|
|
#include <SmingCore/SmingCore.h>
|
2016-09-18 08:30:34 +00:00
|
|
|
#include <Endpoint.h>
|
2016-06-29 15:00:22 +00:00
|
|
|
|
2017-10-01 21:24:41 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-12-04 22:45:25 +00:00
|
|
|
#define D0 16
|
|
|
|
#define D1 5
|
|
|
|
#define D2 4
|
|
|
|
#define D3 0
|
|
|
|
#define D4 2
|
|
|
|
#define D5 14
|
|
|
|
#define D6 12
|
|
|
|
#define D7 13
|
|
|
|
#define D8 15
|
|
|
|
|
2017-09-29 17:34:23 +00:00
|
|
|
class LED {
|
|
|
|
int highState = HIGH;
|
|
|
|
int pin = 2;
|
|
|
|
Timer animateTimer;
|
|
|
|
|
|
|
|
void animate() {
|
|
|
|
if (millis() % blinkRate > blinkOn) {
|
|
|
|
digitalWrite(pin, !highState);
|
|
|
|
} else {
|
|
|
|
digitalWrite(pin, highState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
int blinkRate = 4000;
|
|
|
|
int blinkOn = 100;
|
|
|
|
|
|
|
|
LED() { }
|
|
|
|
|
|
|
|
void config(int pin_, bool highState_ = HIGH) {
|
|
|
|
pin = pin_;
|
|
|
|
highState = highState_;
|
|
|
|
|
|
|
|
pinMode(pin, OUTPUT);
|
|
|
|
animateTimer.initializeMs(50, TimerDelegate(&LED::animate, this)).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void idle() {
|
|
|
|
blinkRate = 4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void high() {
|
|
|
|
blinkRate = 300;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-29 15:00:22 +00:00
|
|
|
class SpejsNode {
|
|
|
|
protected:
|
|
|
|
Timer keepaliveTimer;
|
|
|
|
|
2016-09-18 08:30:34 +00:00
|
|
|
HashMap<String, Endpoint*> endpoints;
|
2016-06-29 15:00:22 +00:00
|
|
|
|
|
|
|
void keepAliveHandler();
|
2016-12-04 23:17:51 +00:00
|
|
|
void initializeMDNS();
|
2016-06-29 15:00:22 +00:00
|
|
|
|
2016-10-04 18:33:34 +00:00
|
|
|
void buildMetadata(JsonObjectStream* stream);
|
2016-09-18 08:30:34 +00:00
|
|
|
|
2017-02-05 17:41:39 +00:00
|
|
|
void mqttCallback(String, String);
|
|
|
|
void otaUpdateCallback(bool result);
|
|
|
|
void httpFile(HttpRequest &request, HttpResponse &response);
|
|
|
|
void httpIndex(HttpRequest &request, HttpResponse &response);
|
2016-06-29 15:00:22 +00:00
|
|
|
public:
|
2017-06-12 15:40:23 +00:00
|
|
|
MqttClient mqtt;
|
|
|
|
HttpServer http;
|
|
|
|
|
2016-12-04 22:45:25 +00:00
|
|
|
String deviceID;
|
|
|
|
String deviceType;
|
|
|
|
|
2017-09-29 17:34:23 +00:00
|
|
|
LED statusLED;
|
|
|
|
|
2016-12-04 22:45:25 +00:00
|
|
|
uint8_t currentSlot;
|
|
|
|
|
2016-06-29 15:00:22 +00:00
|
|
|
SpejsNode(String _deviceType) :
|
|
|
|
mqtt(MQTT_BROKER, MQTT_PORT, MqttStringSubscriptionCallback(&SpejsNode::mqttCallback, this)),
|
2016-10-04 19:06:45 +00:00
|
|
|
deviceType(_deviceType) {};
|
2016-06-29 15:00:22 +00:00
|
|
|
|
2017-06-12 15:40:23 +00:00
|
|
|
void onConnected();
|
2017-09-19 23:33:18 +00:00
|
|
|
void gotIP(IPAddress ip, IPAddress netmask, IPAddress gateway);
|
2017-06-12 15:40:23 +00:00
|
|
|
|
2018-06-28 13:35:32 +00:00
|
|
|
void init(bool debug=false);
|
2016-06-29 15:00:22 +00:00
|
|
|
|
|
|
|
bool notify(String key, String value);
|
2017-02-05 17:41:39 +00:00
|
|
|
bool subscribe(String topic);
|
|
|
|
|
2016-09-18 08:30:34 +00:00
|
|
|
void registerEndpoint(String key, Endpoint* cb);
|
2017-02-05 17:41:39 +00:00
|
|
|
|
2017-03-06 16:56:37 +00:00
|
|
|
String DEV_TOPIC(String t);
|
|
|
|
|
2017-10-01 21:24:41 +00:00
|
|
|
void loadJSON(std::vector<EndpointInitializer>);
|
2016-06-29 15:00:22 +00:00
|
|
|
};
|
2016-09-18 08:30:34 +00:00
|
|
|
|
|
|
|
#endif
|