|
|
@@ -6,7 +6,6 @@
|
|
|
#include <GithubClient.h>
|
|
|
#include <IntParsing.h>
|
|
|
#include <Size.h>
|
|
|
-#include <MiLightClient.h>
|
|
|
#include <MiLightRadioConfig.h>
|
|
|
#include <MiLightHttpServer.h>
|
|
|
#include <Settings.h>
|
|
|
@@ -16,6 +15,7 @@
|
|
|
#include <MqttClient.h>
|
|
|
#include <RGBConverter.h>
|
|
|
#include <MiLightDiscoveryServer.h>
|
|
|
+#include <MiLightClient.h>
|
|
|
|
|
|
WiFiManager wifiManager;
|
|
|
|
|
|
@@ -26,9 +26,11 @@ MiLightRadioFactory* radioFactory = NULL;
|
|
|
MiLightHttpServer *httpServer = NULL;
|
|
|
MqttClient* mqttClient = NULL;
|
|
|
MiLightDiscoveryServer* discoveryServer = NULL;
|
|
|
+uint8_t currentRadioType = 0;
|
|
|
|
|
|
int numUdpServers = 0;
|
|
|
MiLightUdpServer** udpServers;
|
|
|
+WiFiUDP udpSeder;
|
|
|
|
|
|
void initMilightUdpServers() {
|
|
|
if (udpServers) {
|
|
|
@@ -63,6 +65,42 @@ void initMilightUdpServers() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void onPacketSentHandler(uint8_t* packet, const MiLightRadioConfig& config) {
|
|
|
+ StaticJsonBuffer<200> buffer;
|
|
|
+ JsonObject& result = buffer.createObject();
|
|
|
+ config.packetFormatter->parsePacket(packet, result);
|
|
|
+
|
|
|
+ uint16_t deviceId = result["device_id"];
|
|
|
+ uint16_t groupId = result["group_id"];
|
|
|
+ MiLightRadioType type = MiLightRadioConfig::fromString(result["device_type"])->type;
|
|
|
+
|
|
|
+ char output[200];
|
|
|
+ result.printTo(output);
|
|
|
+
|
|
|
+ if (mqttClient) {
|
|
|
+ mqttClient->sendUpdate(type, deviceId, groupId, output);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void handleListen() {
|
|
|
+ if (! settings.listenRepeats) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ MiLightRadioConfig* config = MiLightRadioConfig::ALL_CONFIGS[
|
|
|
+ currentRadioType++ % MiLightRadioConfig::NUM_CONFIGS
|
|
|
+ ];
|
|
|
+ milightClient->prepare(*config);
|
|
|
+
|
|
|
+ for (size_t i = 0; i < settings.listenRepeats; i++) {
|
|
|
+ if (milightClient->available()) {
|
|
|
+ uint8_t readPacket[9];
|
|
|
+ milightClient->read(readPacket);
|
|
|
+
|
|
|
+ onPacketSentHandler(readPacket, *config);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
void applySettings() {
|
|
|
if (milightClient) {
|
|
|
@@ -83,6 +121,7 @@ void applySettings() {
|
|
|
|
|
|
milightClient = new MiLightClient(radioFactory);
|
|
|
milightClient->begin();
|
|
|
+ milightClient->onPacketSent(onPacketSentHandler);
|
|
|
|
|
|
if (settings.mqttServer().length() > 0) {
|
|
|
mqttClient = new MqttClient(settings, milightClient);
|
|
|
@@ -134,6 +173,8 @@ void setup() {
|
|
|
httpServer->onSettingsSaved(applySettings);
|
|
|
httpServer->on("/description.xml", HTTP_GET, []() { SSDP.schema(httpServer->client()); });
|
|
|
httpServer->begin();
|
|
|
+
|
|
|
+ Serial.println(F("Setup complete"));
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
@@ -153,6 +194,8 @@ void loop() {
|
|
|
discoveryServer->handleClient();
|
|
|
}
|
|
|
|
|
|
+ handleListen();
|
|
|
+
|
|
|
if (shouldRestart()) {
|
|
|
Serial.println(F("Auto-restart triggered. Restarting..."));
|
|
|
ESP.restart();
|