diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 40f453f9a..05fb79050 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -162,6 +162,7 @@ void Script_ticker4_end(void) { #endif #endif +extern uint8_t sml_json_enable; #if defined(EEP_SCRIPT_SIZE) && !defined(ESP32) @@ -208,7 +209,7 @@ void alt_eeprom_readBytes(uint32_t adr, uint32_t len, uint8_t *buf) { #define EPOCH_OFFSET 1546300800 enum {OPER_EQU=1,OPER_PLS,OPER_MIN,OPER_MUL,OPER_DIV,OPER_PLSEQU,OPER_MINEQU,OPER_MULEQU,OPER_DIVEQU,OPER_EQUEQU,OPER_NOTEQU,OPER_GRTEQU,OPER_LOWEQU,OPER_GRT,OPER_LOW,OPER_PERC,OPER_XOR,OPER_AND,OPER_OR,OPER_ANDEQU,OPER_OREQU,OPER_XOREQU,OPER_PERCEQU}; -enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD,SCRIPT_EVENT_HANDLED}; +enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD,SCRIPT_EVENT_HANDLED,SML_JSON_ENABLE}; #ifdef USE_UFILESYS @@ -2985,6 +2986,11 @@ chknext: len = 0; goto exit; } + if (!strncmp(vname, "smlj", 4)) { + fvar = sml_json_enable; + tind->index = SML_JSON_ENABLE; + goto exit_settable; + } #endif //USE_SML_M break; case 't': @@ -4527,6 +4533,11 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) { case SCRIPT_EVENT_HANDLED: glob_script_mem.event_handeled = *dfvar; break; +#if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) + case SML_JSON_ENABLE: + sml_json_enable = *dfvar; + break; +#endif } sysv_type = 0; } diff --git a/tasmota/xsns_53_sml.ino b/tasmota/xsns_53_sml.ino index e7d291a98..8816e0fac 100755 --- a/tasmota/xsns_53_sml.ino +++ b/tasmota/xsns_53_sml.ino @@ -49,8 +49,9 @@ #define SPECIAL_SS #endif -#undef TMSBSIZ +#ifndef TMSBSIZ #define TMSBSIZ 256 +#endif // addresses a bug in meter DWS74 //#define DWS74_BUG @@ -469,7 +470,7 @@ uint8_t dvalid[SML_MAX_VARS]; struct METER_DESC const *meter_desc_p; const uint8_t *meter_p; -uint8_t meter_spos[MAX_METERS]; +uint16_t meter_spos[MAX_METERS]; // software serial pointers #ifdef ESP8266 @@ -820,13 +821,12 @@ uint8_t Serial_peek() { } uint8_t sml_logindex; +char log_data[128]; void Dump2log(void) { - int16_t index=0,hcnt=0; uint32_t d_lastms; uint8_t dchars[16]; - char log_data[128]; //if (!SML_SAVAILABLE) return; @@ -1258,7 +1258,7 @@ void sml_shift_in(uint32_t meters,uint32_t shard) { // QQ,ZZ,PB,SB,NN ..... CRC, ACK SYNC if (meter_spos[meters]>4+5) { // get telegramm lenght - uint8_t tlen=smltbuf[meters][4]+5; + uint16_t tlen=smltbuf[meters][4]+5; // test crc if (smltbuf[meters][tlen]=ebus_CalculateCRC(smltbuf[meters],tlen)) { ebus_esc(smltbuf[meters],tlen); @@ -1548,15 +1548,17 @@ void SML_Decode(uint8_t index) { #endif if (*mp == '#') { // get string value + getstr: mp++; if (meter_desc_p[mindex].type == 'o') { - for (uint8_t p=0; p> %s"),mp); // get scaling factor double fac=CharToDouble((char*)mp); meter_vars[vindex]/=fac; SML_Immediate_MQTT((const char*)mp,vindex,mindex); } + dvalid[vindex] = 1; } } nextsect: @@ -1737,10 +1754,20 @@ void SML_Show(boolean json) { cp=strchr(mp,'@'); if (cp) { cp++; + tststr: if (*cp=='#') { // meter id sprintf(tpowstr,"\"%s\"",&meter_id[mindex][0]); mid=1; + } else if (*cp=='(') { + if (meter_desc_p[mindex].type=='o') { + cp++; + strtol((char*)cp,(char**)&cp, 10); + cp++; + goto tststr; + } else { + mid=0; + } } else { mid=0; } @@ -1987,154 +2014,181 @@ void SML_Init(void) { if (meter_script==99) { // use script definition if (script_meter) free(script_meter); - script_meter=0; - uint8_t *tp=0; - uint16_t index=0; - uint8_t section=0; - uint8_t srcpin=0; - char *lp=glob_script_mem.scriptptr; - sml_send_blocks=0; + script_meter = 0; + uint8_t *tp = 0; + uint16_t index = 0; + uint8_t section = 0; + uint8_t srcpin = 0; + uint8_t dec_line = 0; + char *lp = glob_script_mem.scriptptr; + sml_send_blocks = 0; while (lp) { if (!section) { - if (*lp=='>' && *(lp+1)=='M') { - lp+=2; - meters_used=strtol(lp,0,10); - section=1; - uint32_t mlen=SML_getscriptsize(lp); - if (mlen==0) return; // missing end # - script_meter=(uint8_t*)calloc(mlen,1); + if (*lp == '>' && *(lp+1) == 'M') { + lp += 2; + meters_used = strtol(lp, 0, 10); + section = 1; + uint32_t mlen = SML_getscriptsize(lp); + if (mlen == 0) return; // missing end # + script_meter = (uint8_t*)calloc(mlen, 1); if (!script_meter) { goto dddef_exit; } - tp=script_meter; + tp = script_meter; goto next_line; } } else { - if (!*lp || *lp=='#' || *lp=='>') { - if (*(tp-1)=='|') *(tp-1)=0; + if (!*lp || *lp == '#' || *lp == '>') { + if (*(tp-1) == '|') *(tp-1) = 0; break; } - if (*lp=='+') { + if (*lp == '+') { // add descriptor +1,1,c,0,10,H20 //toLogEOL(">>",lp); lp++; - index=*lp&7; - lp+=2; - if (index<1 || index>meters_used) goto next_line; + index = *lp&7; + lp += 2; + if (index < 1 || index > meters_used) { + AddLog(LOG_LEVEL_INFO, PSTR("illegal meter number!")); + goto next_line; + } index--; - srcpin=strtol(lp,&lp,10); + srcpin = strtol(lp,&lp,10); if (Gpio_used(srcpin)) { AddLog(LOG_LEVEL_INFO, PSTR("gpio rx double define!")); dddef_exit: if (script_meter) free(script_meter); - script_meter=0; - meters_used=METERS_USED; + script_meter = 0; + meters_used = METERS_USED; goto init10; } - script_meter_desc[index].srcpin=srcpin; - if (*lp!=',') goto next_line; + script_meter_desc[index].srcpin = srcpin; + if (*lp != ',') goto next_line; lp++; - script_meter_desc[index].type=*lp; + script_meter_desc[index].type = *lp; lp++; - if (*lp!=',') { - script_meter_desc[index].sopt=*lp&7; + if (*lp != ',') { + script_meter_desc[index].sopt = *lp&7; lp++; } else { - script_meter_desc[index].sopt=0; + script_meter_desc[index].sopt = 0; } lp++; - script_meter_desc[index].flag=strtol(lp,&lp,10); - if (*lp!=',') goto next_line; + script_meter_desc[index].flag = strtol(lp, &lp, 10); + if (*lp != ',') goto next_line; lp++; - script_meter_desc[index].params=strtol(lp,&lp,10); - if (*lp!=',') goto next_line; + script_meter_desc[index].params = strtol(lp, &lp, 10); + if (*lp != ',') goto next_line; lp++; - script_meter_desc[index].prefix[7]=0; - for (uint32_t cnt=0; cnt<8; cnt++) { - if (*lp==SCRIPT_EOL || *lp==',') { - script_meter_desc[index].prefix[cnt]=0; + script_meter_desc[index].prefix[7] = 0; + for (uint32_t cnt = 0; cnt < 8; cnt++) { + if (*lp == SCRIPT_EOL || *lp == ',') { + script_meter_desc[index].prefix[cnt] = 0; break; } - script_meter_desc[index].prefix[cnt]=*lp++; + script_meter_desc[index].prefix[cnt] = *lp++; } - if (*lp==',') { + if (*lp == ',') { lp++; - script_meter_desc[index].trxpin=strtol(lp,&lp,10); + script_meter_desc[index].trxpin = strtol(lp, &lp, 10); if (Gpio_used(script_meter_desc[index].trxpin)) { AddLog(LOG_LEVEL_INFO, PSTR("gpio tx double define!")); goto dddef_exit; } - if (*lp!=',') goto next_line; + if (*lp != ',') goto next_line; lp++; - script_meter_desc[index].tsecs=strtol(lp,&lp,10); - if (*lp==',') { + script_meter_desc[index].tsecs = strtol(lp, &lp, 10); + if (*lp == ',') { lp++; char txbuff[256]; - uint32_t txlen=0,tx_entries=1; - for (uint32_t cnt=0; cnt>",lp); // add meters line -1,1-0:1.8.0*255(@10000,H2OIN,cbm,COUNTER,4| - if (*lp1=='-') lp1++; - uint8_t mnum=strtol(lp1,0,10); - if (mnum<1 || mnum>meters_used) goto next_line; - while (1) { - if (*lp1==0) { - *tp++='|'; + if (*lp1 == '-') lp1++; + uint8_t mnum = strtol(lp1, 0, 10); + if (mnum < 1 || mnum > meters_used) { + AddLog(LOG_LEVEL_INFO, PSTR("illegal meter number!")); + goto next_line; + } + // 1,=h————————————— + if (strncmp(lp1 + 1, ",=h", 3)) { + dec_line++; + if (dec_line >= SML_MAX_VARS) { + AddLog(LOG_LEVEL_INFO, PSTR("too many decode lines: %d !"), dec_line); goto next_line; } - *tp++=*lp1++; + } + while (1) { + if (*lp1 == 0) { + *tp++ = '|'; + goto next_line; + } + *tp++ = *lp1++; index++; - if (index>=METER_DEF_SIZE) break; + if (index >= METER_DEF_SIZE) break; } } #else - if (*lp=='-' || isdigit(*lp)) { + if (*lp == '-' || isdigit(*lp)) { //toLogEOL(">>",lp); // add meters line -1,1-0:1.8.0*255(@10000,H2OIN,cbm,COUNTER,4| - if (*lp=='-') lp++; - uint8_t mnum=strtol(lp,0,10); - if (mnum<1 || mnum>meters_used) goto next_line; - while (1) { - if (*lp==SCRIPT_EOL) { - if (*(tp-1)!='|') *tp++='|'; + if (*lp == '-') lp++; + uint8_t mnum = strtol(lp,0,10); + if (mnum < 1 || mnum > meters_used) { + AddLog(LOG_LEVEL_INFO, PSTR("illegal meter number!")); + goto next_line; + } + if (strncmp(lp + 1, ",=h", 3)) { + dec_line++; + if (dec_line >= SML_MAX_VARS) { + AddLog(LOG_LEVEL_INFO, PSTR("too many decode lines: %d !"), dec_line); goto next_line; } - *tp++=*lp++; - index++; - if (index>=METER_DEF_SIZE) break; } + + while (1) { + if (*lp == SCRIPT_EOL) { + if (*(tp-1) != '|') *tp++ = '|'; + goto next_line; + } + *tp++ = *lp++; + index++; + if (index >= METER_DEF_SIZE) break; + } + } #endif