spejsiot/spejsiot/SpejsNode.h

103 lines
2.0 KiB
C
Raw Normal View History

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();
void initializeMDNS();
2016-06-29 15:00:22 +00:00
void buildMetadata(JsonObjectStream* stream);
2016-09-18 08:30:34 +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
void init(bool debug=false);
2016-06-29 15:00:22 +00:00
bool notify(String key, String value);
bool subscribe(String topic);
2016-09-18 08:30:34 +00:00
void registerEndpoint(String key, Endpoint* cb);
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