From 01547f9a521462b21a4db5998261f190e0c77b49 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 19 Aug 2022 13:26:29 +0200 Subject: [PATCH] Adjustments to make L&G parser work --- src/AmsToMqttBridge.ino | 2 +- src/LNG.cpp | 47 ++++++++++++++++++++++++++--------------- src/LNG.h | 3 ++- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 320a7238..a5726a84 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -859,7 +859,7 @@ bool readHanPort() { // TODO: Split IEC6205675 into DataParserKaifa and DataParserObis. This way we can add other means of parsing, for those other proprietary formats //data = IEC6205675(((char *) (hanBuffer)) + pos, meterState.getMeterType(), &meterConfig, ctx); - data = LNG(((char *) (hanBuffer)) + pos, meterState.getMeterType(), &meterConfig, ctx); + data = LNG(((char *) (hanBuffer)) + pos, meterState.getMeterType(), &meterConfig, ctx, &Debug); } else if(ctx.type == DATA_TAG_DSMR) { data = IEC6205621(((char *) (hanBuffer)) + pos); } diff --git a/src/LNG.cpp b/src/LNG.cpp index d982f874..dea4ecf5 100644 --- a/src/LNG.cpp +++ b/src/LNG.cpp @@ -2,7 +2,7 @@ #include "lwip/def.h" #include "ams/Cosem.h" -LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx) { +LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger) { LngHeader* h = (LngHeader*) payload; if(h->tag == CosemTypeStructure && h->arrayTag == CosemTypeArray) { meterType = AmsTypeLng; @@ -11,55 +11,68 @@ LNG::LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, Da uint8_t* ptr = (uint8_t*) &h[1]; uint8_t* data = ptr + (18*h->arrayLength); // Skip descriptors - for(uint8_t i = 0; i < h->arrayLength; i++) { - LngObisDescriptor* descriptor = (LngObisDescriptor*) ptr; + LngObisDescriptor* descriptor = (LngObisDescriptor*) ptr; + for(uint8_t x = 0; x < h->arrayLength-1; x++) { + ptr = (uint8_t*) &descriptor[1]; + descriptor = (LngObisDescriptor*) ptr; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(L&G) OBIS %d.%d.%d with type 0x%02X", descriptor->obis[2], descriptor->obis[3], descriptor->obis[4], *data); + + CosemData* item = (CosemData*) data; if(descriptor->obis[2] == 1) { if(descriptor->obis[3] == 7) { if(descriptor->obis[4] == 0) { - CosemDLongUnsigned* item = (CosemDLongUnsigned*) data; - activeImportPower = ntohl(item->data); + activeImportPower = ntohl(item->dlu.data); listType = listType >= 1 ? listType : 1; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %d (dlu)", ntohl(item->dlu.data)); } } else if(descriptor->obis[3] == 8) { if(descriptor->obis[4] == 0) { - CosemDLongUnsigned* item = (CosemDLongUnsigned*) data; - activeImportCounter = ntohl(item->data); + activeImportCounter = ntohl(item->dlu.data); listType = listType >= 3 ? listType : 3; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %d (dlu)", ntohl(item->dlu.data)); } } } else if(descriptor->obis[2] == 2) { if(descriptor->obis[3] == 7) { if(descriptor->obis[4] == 0) { - CosemDLongUnsigned* item = (CosemDLongUnsigned*) data; - activeExportPower = ntohl(item->data); + activeExportPower = ntohl(item->dlu.data); listType = listType >= 2 ? listType : 2; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %d (dlu)", ntohl(item->dlu.data)); } } else if(descriptor->obis[3] == 8) { if(descriptor->obis[4] == 0) { - CosemDLongUnsigned* item = (CosemDLongUnsigned*) data; - activeExportCounter = ntohl(item->data); + activeExportCounter = ntohl(item->dlu.data); listType = listType >= 3 ? listType : 3; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %d (dlu)", ntohl(item->dlu.data)); } } } else if(descriptor->obis[2] == 96) { if(descriptor->obis[3] == 1) { if(descriptor->obis[4] == 0) { - CosemString* item = (CosemString*) data; - char str[item->length+1]; - memcpy(str, item->data, item->length); - str[item->length] = '\0'; + char str[item->oct.length+1]; + memcpy(str, item->oct.data, item->oct.length); + str[item->oct.length] = '\0'; meterId = String(str); + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %s (oct)", str); + } else if(descriptor->obis[4] == 1) { + char str[item->oct.length+1]; + memcpy(str, item->oct.data, item->oct.length); + str[item->oct.length] = '\0'; + meterModel = String(str); + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf(" and value %s (oct)", str); } } } - ptr = (uint8_t*) &descriptor[1]; + if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("\n"); if((*data) == 0x09) { - data += (*data+1)+2; + data += (*(data+1))+2; } else { data += 5; } + + lastUpdateMillis = millis(); } } } \ No newline at end of file diff --git a/src/LNG.h b/src/LNG.h index 19b14061..f448b105 100644 --- a/src/LNG.h +++ b/src/LNG.h @@ -4,6 +4,7 @@ #include "AmsData.h" #include "AmsConfiguration.h" #include "ams/DataParser.h" +#include "RemoteDebug.h" struct LngHeader { uint8_t tag; @@ -23,7 +24,7 @@ struct LngObisDescriptor { class LNG : public AmsData { public: - LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx); + LNG(const char* payload, uint8_t useMeterType, MeterConfig* meterConfig, DataParserContext &ctx, RemoteDebug* debugger); }; #endif