diff --git a/src/main.cpp b/src/main.cpp index 3c59035..2ccd1dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,11 +25,13 @@ 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); + +bool sendOpenPacket(uint16_t destination); +bool sendClosePacket(uint16_t destination); +bool sendStopPacket(uint16_t destination); +bool sendSetPosition(uint16_t destination, float percentage); +bool sendFetchPosition(uint16_t destination); +bool sendPacket(Packet *packet); unsigned long lastCommandMillis = 0; @@ -111,64 +113,38 @@ void onConnectionEstablished() { client.publish("hotdog/availability", "online", true); } -void sendOpenPacket(uint16_t destination) { +bool 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++; - } + return sendPacket(&packet); } -void sendClosePacket(uint16_t destination) { +bool 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++; - } + return sendPacket(&packet); } -void sendStopPacket(uint16_t destination) { +bool 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++; - } + return sendPacket(&packet); } -void sendSetPosition(uint16_t destination, float percentage) { +bool 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; @@ -179,22 +155,14 @@ void sendSetPosition(uint16_t destination, float percentage) { packet.parameters = FieldsParameters {fields}; - bool didSend = powerView.sendPacket(&packet); - if (!didSend) { - Serial.println("Failed to send"); - } else { - lastRollingCode1++; - lastRollingCode2++; - } + return sendPacket(&packet); } -void sendFetchPosition(uint16_t destination) { +bool 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; @@ -204,11 +172,20 @@ void sendFetchPosition(uint16_t destination) { packet.parameters = FieldsParameters {fields}; - bool didSend = powerView.sendPacket(&packet); + return sendPacket(&packet); +} + +bool sendPacket(Packet *packet) { + packet->rollingCode1 = lastRollingCode1 + 1; + packet->rollingCode2 = lastRollingCode2 + 1; + + bool didSend = powerView.sendPacket(packet); if (!didSend) { Serial.println("Failed to send"); + return false; } else { lastRollingCode1++; lastRollingCode2++; + return true; } } \ No newline at end of file