From 4d0af9e98d1c0a21f87ae2042a55baeccb611a30 Mon Sep 17 00:00:00 2001 From: Matt Way Date: Tue, 2 Jan 2024 21:56:55 +1100 Subject: [PATCH] Add helper methods for sending various packets --- src/main.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0778834..eddd988 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,11 @@ EspMQTTClient client( ); void processPacket(const Packet*); +void sendOpenPacket(uint16_t destination); +void sendClosePacket(uint16_t destination); +void sendStopPacket(uint16_t destination); +void sendSetPosition(uint16_t destination, float percentage); +void sendFetchPosition(uint16_t destination); void setup() { Serial.begin(SER_BAUDRATE); @@ -81,11 +86,11 @@ void onConnectionEstablished() { client.subscribe("hotdog/test_mqtt_blind/set", [] (const String &payload) { uint16_t shadeID = 0x4EF1; if (payload == "OPEN") { - // TODO: Build open Packet and send + sendOpenPacket(shadeID); } else if (payload == "CLOSE") { - // TODO: Build close Packet and send + sendClosePacket(shadeID); } else if (payload == "STOP") { - // TODO: Build stop Packet and send + sendStopPacket(shadeID); // TODO: Schedule fetching position of blind } }); @@ -93,9 +98,111 @@ void onConnectionEstablished() { client.subscribe("hotdog/test_mqtt_blind/set_position", [] (const String &payload) { uint16_t shadeID = 0x4EF1; float percentage = payload.toInt() / 100.0f; - // TODO: Build set position Packet and send + sendSetPosition(shadeID, percentage); // TODO: Schedule fetching position of blind }); client.publish("hotdog/availability", "online", true); } + +void sendOpenPacket(uint16_t destination) { + Packet packet; + packet.destination = destination; + packet.source = 0x0000; + packet.type = PacketType::OPEN; + packet.rollingCode1 = lastRollingCode1 + 1; + packet.rollingCode2 = lastRollingCode2 + 1; + + bool didSend = powerView.sendPacket(&packet); + if (!didSend) { + Serial.println("Failed to send"); + } else { + lastRollingCode1++; + lastRollingCode2++; + } +} + +void sendClosePacket(uint16_t destination) { + Packet packet; + packet.destination = destination; + packet.source = 0x0000; + packet.type = PacketType::CLOSE; + packet.rollingCode1 = lastRollingCode1 + 1; + packet.rollingCode2 = lastRollingCode2 + 1; + + bool didSend = powerView.sendPacket(&packet); + if (!didSend) { + Serial.println("Failed to send"); + } else { + lastRollingCode1++; + lastRollingCode2++; + } +} + +void sendStopPacket(uint16_t destination) { + Packet packet; + packet.destination = destination; + packet.source = 0x0000; + packet.type = PacketType::STOP; + packet.rollingCode1 = lastRollingCode1 + 1; + packet.rollingCode2 = lastRollingCode2 + 1; + + bool didSend = powerView.sendPacket(&packet); + if (!didSend) { + Serial.println("Failed to send"); + } else { + lastRollingCode1++; + lastRollingCode2++; + } +} + +void sendSetPosition(uint16_t destination, float percentage) { + Packet packet; + packet.destination = destination; + packet.source = 0x0000; + packet.type = PacketType::FIELD_COMMAND; + packet.rollingCode1 = lastRollingCode1 + 1; + packet.rollingCode2 = lastRollingCode2 + 1; + + std::vector fields; + + uint8_t identifier = 0x50; // position + FieldType type = FieldType::SET; + uint16_t position = (uint16_t)(0xFFFF * percentage); + fields.push_back(Field{identifier, type, true, position}); + + packet.parameters = FieldsParameters {fields}; + + bool didSend = powerView.sendPacket(&packet); + if (!didSend) { + Serial.println("Failed to send"); + } else { + lastRollingCode1++; + lastRollingCode2++; + } +} + +void sendFetchPosition(uint16_t destination) { + Packet packet; + packet.destination = destination; + packet.source = 0x0000; + packet.type = PacketType::FIELD_COMMAND; + packet.rollingCode1 = lastRollingCode1 + 1; + packet.rollingCode2 = lastRollingCode2 + 1; + + std::vector fields; + + uint8_t identifier = 0x50; // position + FieldType type = FieldType::FETCH; + fields.push_back(Field{identifier, type, false, std::monostate{}}); + + packet.parameters = FieldsParameters {fields}; + + bool didSend = powerView.sendPacket(&packet); + if (!didSend) { + Serial.println("Failed to send"); + } else { + lastRollingCode1++; + lastRollingCode2++; + } +} \ No newline at end of file