Add helper methods for sending various packets

This commit is contained in:
2024-01-02 21:56:55 +11:00
parent c2c55d6764
commit 4d0af9e98d

View File

@@ -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<Field> 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<Field> 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++;
}
}