Skip to content

Commit

Permalink
MessagePack UDP protocol
Browse files Browse the repository at this point in the history
(cherry picked from commit b8f7153916ce634b1182128c3a2e3da3d0c5dae0)
  • Loading branch information
mathieucarbou committed Jun 29, 2024
1 parent 6e292ee commit 3325775
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/YaSolR.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ extern Mycila::Task initMqttSubscribersTask;
extern Mycila::Task initRestApiTask;
extern Mycila::Task initWebTask;

extern Mycila::CircularBuffer<float, 50> jsyRemoteUdpRate;
extern Mycila::CircularBuffer<float, 50> udpMessageRateBuffer;
2 changes: 1 addition & 1 deletion include/YaSolRWebsite.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace YaSolR {
Statistic _relay1SwitchCount = Statistic(&dashboard, YASOLR_LBL_032);
Statistic _relay2SwitchCount = Statistic(&dashboard, YASOLR_LBL_033);

Statistic _jsyRemoteUdpRate = Statistic(&dashboard, YASOLR_LBL_157);
Statistic _udpMessageRateBuffer = Statistic(&dashboard, YASOLR_LBL_157);

Statistic _time = Statistic(&dashboard, YASOLR_LBL_034);
Statistic _uptime = Statistic(&dashboard, YASOLR_LBL_035);
Expand Down
2 changes: 1 addition & 1 deletion src/Website.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void YaSolR::WebsiteClass::updateCards() {
_networkWiFiSSID.set(ESPConnect.getWiFiSSID().c_str());
_relay1SwitchCount.set(String(relay1.getSwitchCount()).c_str());
_relay2SwitchCount.set(String(relay2.getSwitchCount()).c_str());
_jsyRemoteUdpRate.set((String(jsyRemoteUdpRate.rate()) + " msg/s").c_str());
_udpMessageRateBuffer.set((String(udpMessageRateBuffer.rate()) + " msg/s").c_str());
_time.set(Mycila::Time::getLocalStr().c_str());
_uptime.set(Mycila::Time::toDHHMMSS(Mycila::System.getUptime()).c_str());
#ifdef APP_MODEL_TRIAL
Expand Down
48 changes: 33 additions & 15 deletions src/init/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,33 +346,51 @@ Mycila::Task initEventsTask("Init Events", [](void* params) {
});

udp.onPacket([](AsyncUDPPacket packet) {
// [0] uint8_t == message type
// [1] sizeof(Mycila::JSYData)
// [sizeOfBody] uint32_t == CRC32
constexpr size_t sizeOfJSYData = sizeof(Mycila::JSYData);
constexpr size_t sizeOfBody = 1 + sizeOfJSYData;
constexpr size_t sizeOfCRC32 = sizeof(uint32_t);
constexpr size_t sizeOfUDP = sizeOfBody + sizeOfCRC32;
// buffer[0] == MYCILA_UDP_MSG_TYPE_JSY_DATA (1)
// buffer[1] == size_t (4)
// buffer[5] == MsgPack (?)
// buffer[5 + size] == CRC32 (4)

size_t len = packet.length();
if (len != sizeOfUDP)
uint8_t* buffer = packet.data();

if (len < 5 || buffer[0] != YASOLR_UDP_MSG_TYPE_JSY_DATA)
return;

uint8_t* data = packet.data();
const uint8_t type = data[0];
uint32_t size;
memcpy(&size, buffer + 1, 4);

if (type != YASOLR_UDP_MSG_TYPE_JSY_DATA)
if (len != size + 9)
return;

// crc32
FastCRC32 crc32;
crc32.add(data, sizeOfBody);
crc32.add(buffer, size + 5);
uint32_t crc = crc32.calc();
if (memcmp(&crc, data + sizeOfBody, sizeOfCRC32) != 0)

if (memcmp(&crc, buffer + size + 5, 4) != 0)
return;

JsonDocument doc;
deserializeMsgPack(doc, buffer + 5, size);

Mycila::JSYData jsyData;
memcpy(&jsyData, data + 1, sizeOfJSYData);
jsyRemoteUdpRate.add(millis() / 1000.0f);
jsyData.current1 = doc["c1"].as<float>();
jsyData.current2 = doc["c2"].as<float>();
jsyData.energy1 = doc["e1"].as<float>();
jsyData.energy2 = doc["e2"].as<float>();
jsyData.energyReturned1 = doc["er1"].as<float>();
jsyData.energyReturned2 = doc["er2"].as<float>();
jsyData.frequency = doc["f"].as<float>();
jsyData.power1 = doc["p1"].as<float>();
jsyData.power2 = doc["p2"].as<float>();
jsyData.powerFactor1 = doc["pf1"].as<float>();
jsyData.powerFactor2 = doc["pf2"].as<float>();
jsyData.voltage1 = doc["v1"].as<float>();
jsyData.voltage2 = doc["v2"].as<float>();

udpMessageRateBuffer.add(millis() / 1000.0f);

if (grid.updateRemoteJSYData(jsyData)) {
routingTask.resume();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Mycila::RouterOutput output2("output2", dimmerO2, bypassRelayO2, ds18O2, grid, p
Mycila::RouterRelay routerRelay1(relay1);
Mycila::RouterRelay routerRelay2(relay2);

Mycila::CircularBuffer<float, 50> jsyRemoteUdpRate;
Mycila::CircularBuffer<float, 50> udpMessageRateBuffer;

AsyncWebServer webServer(80);
AsyncUDP udp;
Expand Down

0 comments on commit 3325775

Please sign in to comment.