Setoption108 add SerialNumber to topix prefix

optimize code to sed json with response
This commit is contained in:
Charles 2020-08-16 20:53:28 +02:00
parent 30e786c0b8
commit c9f8421187
1 changed files with 190 additions and 185 deletions

View File

@ -108,6 +108,7 @@ const char kLabel[] PROGMEM =
TInfo tinfo; // Teleinfo object
TasmotaSerial *TInfoSerial = nullptr;
_Mode_e tinfo_mode = TINFO_MODE_HISTORIQUE;
char serialNumber[13] = ""; // Serial number is 12 char long
bool tinfo_found = false;
int contrat;
int tarif;
@ -145,19 +146,17 @@ Comments: should have been initialised with a
====================================================================== */
void ADPSCallback(uint8_t phase)
{
char adco[13];
// n = phase number 1 to 3
if (phase == 0){
phase = 1;
}
if (tinfo_mode == TINFO_MODE_HISTORIQUE) {
if (getValueFromLabelIndex(LABEL_ADCO, adco) ) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":{\"ADPS\":%i}}"), adco, phase );
MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data);
}
}
Response_P(PSTR("{"));
ResponseAppend_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":{\"ADPS\":%i}}"), serialNumber, phase );
ResponseJsonEnd();
// Publish adding ADCO serial number into the topic
MqttPublishPrefixTopic_P(RESULT_OR_TELE, serialNumber, false);
AddLog_P2(LOG_LEVEL_INFO, PSTR("ADPS on phase %d"), phase);
}
@ -186,7 +185,12 @@ void DataCallback(struct _ValueList * me, uint8_t flags)
}
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TIC: Label[%02d] %s=%s"), ilabel, labelName, me->value);
if (flags & TINFO_FLAGS_ADDED) { c = '#'; }
if (flags & TINFO_FLAGS_UPDATED) { c = '*'; }
if (flags & TINFO_FLAGS_STRING) { c = '$'; }
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TIC: [%d]%c %s=%s"), ilabel, c , me->name, me->value);
if (ilabel<LABEL_END) {
// Current tariff (legacy)
if (ilabel == LABEL_PTEC)
@ -301,11 +305,71 @@ void DataCallback(struct _ValueList * me, uint8_t flags)
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TIC: ISousc set to %d"), isousc);
}
// Serial Number of device
else if (ilabel == LABEL_ADCO || ilabel == LABEL_ADSC)
{
strcpy(serialNumber, me->value);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TIC: %s set to %s"), me->name, serialNumber);
}
if (flags & TINFO_FLAGS_ADDED) { c = '#'; }
if (flags & TINFO_FLAGS_UPDATED) { c = '*'; }
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TIC: %c %s=%s"),c , me->name, me->value);
}
}
}
/* ======================================================================
Function: responseDumpTInfo
Purpose : add teleinfo values into JSON response
Input : -
Output : -
Comments: -
====================================================================== */
void ResponseAppendTInfo()
{
struct _ValueList * me = tinfo.getList();
char sep = ' '; // First JSON value separator
char * p ;
boolean isNumber ;
// Loop thru all the teleinfo frame but
// always check we don't buffer overflow of MQTT data
while (me->next) {
// go to next node
me = me->next;
if (me->name && me->value && *me->name && *me->value) {
isNumber = true;
p = me->value;
// Specific treatment serial number don't convert to number later
if (strcmp(me->name, "ADCO")==0 || strcmp(me->name, "ADSC")==0) {
isNumber = false;
} else {
// check if value is number
while (*p && isNumber) {
if ( *p < '0' || *p > '9' ) {
isNumber = false;
}
p++;
}
}
ResponseAppend_P( PSTR("%c\"%s\":"), sep, me->name );
if (!isNumber || (me->flags & TINFO_FLAGS_STRING) ) {
ResponseAppend_P( PSTR("\"%s\""), me->value );
} else {
ResponseAppend_P( PSTR("%d"), atoi(me->value));
}
// Now JSON separator is needed
sep =',';
}
}
}
/* ======================================================================
@ -323,50 +387,13 @@ void NewFrameCallback(struct _ValueList * me)
// send teleinfo full frame only if setup like that
// see setOption108
if (Settings.flag4.teleinfo_rawdata) {
struct _ValueList * me = tinfo.getList();
char sep = ' '; // First JSON value separator
char * p ;
boolean isNumber ;
Response_P(PSTR("{"));
// Loop thru all the teleinfo frame but
// always check we don't buffer overflow of MQTT data
while (me->next) {
// go to next node
me = me->next;
if (me->name && me->value && *me->name && *me->value) {
isNumber = true;
p = me->value;
// check if value is number
while (*p && isNumber) {
if ( *p < '0' || *p > '9' ) {
isNumber = false;
}
p++;
}
ResponseAppend_P( PSTR("%c\"%s\":"), sep, me->name );
if (!isNumber) {
ResponseAppend_P( PSTR("\"%s\""), me->value );
} else {
ResponseAppend_P( PSTR("%d"), atoi(me->value));
}
// Now separator is comma
sep =',';
}
}
ResponseAppendTInfo();
ResponseJsonEnd();
MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data);
// Publish adding ADCO serial number into the topic
// Need setOption4 to be enabled
MqttPublishPrefixTopic_P(RESULT_OR_TELE, serialNumber, false);
}
}
/* ======================================================================
@ -500,6 +527,7 @@ Output : -
Comments: -
====================================================================== */
#ifdef USE_WEBSERVER
const char HTTP_ENERGY_ID_TELEINFO[] PROGMEM = "{s}ID{m}%s{e}" ;
const char HTTP_ENERGY_INDEX_TELEINFO[] PROGMEM = "{s}%s{m}%s " D_UNIT_WATTHOUR "{e}" ;
const char HTTP_ENERGY_PAPP_TELEINFO[] PROGMEM = "{s}" D_POWERUSAGE "{m}%d " D_UNIT_WATT "{e}" ;
const char HTTP_ENERGY_IINST_TELEINFO[] PROGMEM = "{s}" D_CURRENT "{m}%d " D_UNIT_AMPERE "{e}" ;
@ -512,9 +540,6 @@ const char HTTP_ENERGY_PMAX_TELEINFO[] PROGMEM = "{s}" D_MAX_POWER "{m}%d" D_UN
void TInfoShow(bool json)
{
char name[32];
char value[32];
// Since it's an Energy device , current, voltage and power are
// already present on the telemetry frame. No need to add here
// Just add the raw label/values of the teleinfo frame
@ -527,34 +552,7 @@ void TInfoShow(bool json)
// add teleinfo full frame only if no teleinfo raw data setup
if (!Settings.flag4.teleinfo_rawdata) {
struct _ValueList * me = tinfo.getList();
// Loop thru all the teleinfo frame
while (me->next) {
// go to next node
me = me->next;
if (me->name && me->value && *me->name && *me->value) {
boolean isNumber = true;
char * p = me->value;
// check if value is number
while (*p && isNumber) {
if ( *p < '0' || *p > '9' ) {
isNumber = false;
}
p++;
}
// this will add "" on not number values
ResponseAppend_P(PSTR(",\"%s\":"), me->name);
if (!isNumber) {
ResponseAppend_P(PSTR("\"%s\""), me->value);
} else {
ResponseAppend_P(PSTR("%u"), atol(me->value));
}
}
}
ResponseAppendTInfo();
}
@ -562,6 +560,9 @@ void TInfoShow(bool json)
}
else
{
char name[32];
char value[32];
if (getValueFromLabelIndex(LABEL_HCHC, value) ) {
GetTextIndexed(name, sizeof(name), LABEL_HCHC, kLabel);
WSContentSend_PD(HTTP_ENERGY_INDEX_TELEINFO, name, value);
@ -601,6 +602,10 @@ void TInfoShow(bool json)
}
}
}
// Serial number ADCO or ADSC
WSContentSend_PD(HTTP_ENERGY_ID_TELEINFO, serialNumber);
#endif // USE_WEBSERVER
}
}