104 lines
2.1 KiB
C++
104 lines
2.1 KiB
C++
#ifndef SPEJSNODE_H
|
|
#define SPEJSNODE_H
|
|
|
|
#include <user_config.h>
|
|
#include <common_config.h>
|
|
#include <SmingCore/SmingCore.h>
|
|
#include <Endpoint.h>
|
|
|
|
#include <vector>
|
|
|
|
#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
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
class SpejsNode {
|
|
protected:
|
|
Timer keepaliveTimer;
|
|
|
|
HashMap<String, Endpoint*> endpoints;
|
|
|
|
void keepAliveHandler();
|
|
void initializeMDNS();
|
|
|
|
void buildMetadata(JsonObjectStream* stream);
|
|
|
|
void mqttCallback(String, String);
|
|
void otaUpdateCallback(bool result);
|
|
void httpFile(HttpRequest &request, HttpResponse &response);
|
|
void httpIndex(HttpRequest &request, HttpResponse &response);
|
|
void httpMetadata(HttpRequest &request, HttpResponse &response);
|
|
public:
|
|
MqttClient mqtt;
|
|
HttpServer http;
|
|
|
|
String deviceID;
|
|
String deviceType;
|
|
|
|
LED statusLED;
|
|
|
|
uint8_t currentSlot;
|
|
|
|
SpejsNode(String _deviceType) :
|
|
mqtt(MQTT_BROKER, MQTT_PORT, MqttStringSubscriptionCallback(&SpejsNode::mqttCallback, this)),
|
|
deviceType(_deviceType) {};
|
|
|
|
void onConnected();
|
|
void gotIP(IPAddress ip, IPAddress netmask, IPAddress gateway);
|
|
|
|
void init();
|
|
|
|
bool notify(String key, String value);
|
|
bool subscribe(String topic);
|
|
|
|
void registerEndpoint(String key, Endpoint* cb);
|
|
|
|
String DEV_TOPIC(String t);
|
|
|
|
void loadJSON(std::vector<EndpointInitializer>);
|
|
};
|
|
|
|
#endif
|