Added helper for sending a Packet
This commit is contained in:
75
src/main.cpp
75
src/main.cpp
@@ -25,11 +25,13 @@ EspMQTTClient client(
|
|||||||
);
|
);
|
||||||
|
|
||||||
void processPacket(const Packet*);
|
void processPacket(const Packet*);
|
||||||
void sendOpenPacket(uint16_t destination);
|
|
||||||
void sendClosePacket(uint16_t destination);
|
bool sendOpenPacket(uint16_t destination);
|
||||||
void sendStopPacket(uint16_t destination);
|
bool sendClosePacket(uint16_t destination);
|
||||||
void sendSetPosition(uint16_t destination, float percentage);
|
bool sendStopPacket(uint16_t destination);
|
||||||
void sendFetchPosition(uint16_t destination);
|
bool sendSetPosition(uint16_t destination, float percentage);
|
||||||
|
bool sendFetchPosition(uint16_t destination);
|
||||||
|
bool sendPacket(Packet *packet);
|
||||||
|
|
||||||
unsigned long lastCommandMillis = 0;
|
unsigned long lastCommandMillis = 0;
|
||||||
|
|
||||||
@@ -111,64 +113,38 @@ void onConnectionEstablished() {
|
|||||||
client.publish("hotdog/availability", "online", true);
|
client.publish("hotdog/availability", "online", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendOpenPacket(uint16_t destination) {
|
bool sendOpenPacket(uint16_t destination) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
packet.destination = destination;
|
packet.destination = destination;
|
||||||
packet.source = 0x0000;
|
packet.source = 0x0000;
|
||||||
packet.type = PacketType::OPEN;
|
packet.type = PacketType::OPEN;
|
||||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
|
||||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
|
||||||
|
|
||||||
bool didSend = powerView.sendPacket(&packet);
|
return sendPacket(&packet);
|
||||||
if (!didSend) {
|
|
||||||
Serial.println("Failed to send");
|
|
||||||
} else {
|
|
||||||
lastRollingCode1++;
|
|
||||||
lastRollingCode2++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendClosePacket(uint16_t destination) {
|
bool sendClosePacket(uint16_t destination) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
packet.destination = destination;
|
packet.destination = destination;
|
||||||
packet.source = 0x0000;
|
packet.source = 0x0000;
|
||||||
packet.type = PacketType::CLOSE;
|
packet.type = PacketType::CLOSE;
|
||||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
|
||||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
|
||||||
|
|
||||||
bool didSend = powerView.sendPacket(&packet);
|
return sendPacket(&packet);
|
||||||
if (!didSend) {
|
|
||||||
Serial.println("Failed to send");
|
|
||||||
} else {
|
|
||||||
lastRollingCode1++;
|
|
||||||
lastRollingCode2++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendStopPacket(uint16_t destination) {
|
bool sendStopPacket(uint16_t destination) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
packet.destination = destination;
|
packet.destination = destination;
|
||||||
packet.source = 0x0000;
|
packet.source = 0x0000;
|
||||||
packet.type = PacketType::STOP;
|
packet.type = PacketType::STOP;
|
||||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
|
||||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
|
||||||
|
|
||||||
bool didSend = powerView.sendPacket(&packet);
|
return sendPacket(&packet);
|
||||||
if (!didSend) {
|
|
||||||
Serial.println("Failed to send");
|
|
||||||
} else {
|
|
||||||
lastRollingCode1++;
|
|
||||||
lastRollingCode2++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendSetPosition(uint16_t destination, float percentage) {
|
bool sendSetPosition(uint16_t destination, float percentage) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
packet.destination = destination;
|
packet.destination = destination;
|
||||||
packet.source = 0x0000;
|
packet.source = 0x0000;
|
||||||
packet.type = PacketType::FIELD_COMMAND;
|
packet.type = PacketType::FIELD_COMMAND;
|
||||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
|
||||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
|
||||||
|
|
||||||
std::vector<Field> fields;
|
std::vector<Field> fields;
|
||||||
|
|
||||||
@@ -179,22 +155,14 @@ void sendSetPosition(uint16_t destination, float percentage) {
|
|||||||
|
|
||||||
packet.parameters = FieldsParameters {fields};
|
packet.parameters = FieldsParameters {fields};
|
||||||
|
|
||||||
bool didSend = powerView.sendPacket(&packet);
|
return sendPacket(&packet);
|
||||||
if (!didSend) {
|
|
||||||
Serial.println("Failed to send");
|
|
||||||
} else {
|
|
||||||
lastRollingCode1++;
|
|
||||||
lastRollingCode2++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendFetchPosition(uint16_t destination) {
|
bool sendFetchPosition(uint16_t destination) {
|
||||||
Packet packet;
|
Packet packet;
|
||||||
packet.destination = destination;
|
packet.destination = destination;
|
||||||
packet.source = 0x0000;
|
packet.source = 0x0000;
|
||||||
packet.type = PacketType::FIELD_COMMAND;
|
packet.type = PacketType::FIELD_COMMAND;
|
||||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
|
||||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
|
||||||
|
|
||||||
std::vector<Field> fields;
|
std::vector<Field> fields;
|
||||||
|
|
||||||
@@ -204,11 +172,20 @@ void sendFetchPosition(uint16_t destination) {
|
|||||||
|
|
||||||
packet.parameters = FieldsParameters {fields};
|
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) {
|
if (!didSend) {
|
||||||
Serial.println("Failed to send");
|
Serial.println("Failed to send");
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
lastRollingCode1++;
|
lastRollingCode1++;
|
||||||
lastRollingCode2++;
|
lastRollingCode2++;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user