Merge pull request #10341 from gemu2015/fixes

Fixes
This commit is contained in:
Theo Arends 2021-01-01 16:11:15 +01:00 committed by GitHub
commit 7e62aa548d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 236 additions and 141 deletions

View File

@ -499,6 +499,17 @@ uint8_t fast_script=0;
uint8_t glob_script=0;
uint32_t script_lastmillis;
void Script_AddLog_P(uint32_t loglevel, PGM_P formatP, ...) {
char log_data[128];
va_list arg;
va_start(arg, formatP);
vsnprintf_P(log_data, sizeof(log_data), formatP, arg);
va_end(arg);
AddLogData(loglevel, log_data);
}
void flt2char(float num, char *nbuff) {
dtostrfd(num, glob_script_mem.script_dprec, nbuff);
}
@ -578,7 +589,9 @@ float *Get_MFAddr(uint8_t index, uint16_t *len, uint16_t *ipos);
int16_t Init_Scripter(void) {
char *script;
int16_t err = 0;
script = glob_script_mem.script_ram;
if (!*script) return -999;
// scan lines for >DEF
uint16_t lines = 0;
@ -586,15 +599,56 @@ char *script;
uint16_t svars = 0;
uint16_t vars = 0;
char *lp = script;
char vnames[MAXVARS*10];
char *vnames_p = vnames;
uint16_t imemsize = (MAXVARS*10) + 4;
uint8_t *imemptr = (uint8_t*)calloc(imemsize, 1);
if (!imemptr) {
return -7;
}
//ClaimSerial();
//SetSerialBaudrate(115200);
//Serial.printf("size %d\n",imemsize);
//Serial.printf("stack %d\n",GetStack()); // 2848
// 2896
//char vnames[MAXVARS*10];
char *vnames = (char*)imemptr;
char *vnp[MAXVARS];
float fvalues[MAXVARS];
struct T_INDEX vtypes[MAXVARS];
/*
uint32_t imemp = (uint32_t)imemptr;
imemp += (MAXVARS*10);
imemp = (imemp & 0xfffc) + 4;
Serial.printf(">1 %x\n",imemp);
char *vnp[MAXVARS];
//char **vnp = (char**)imemp;
imemp += (sizeof(char*)*MAXVARS);
imemp = (imemp & 0xfffc) + 4;
Serial.printf(">2 %x\n",imemp);
float fvalues[MAXVARS];
//float *fvalues = (float*)imemp;
imemp += (sizeof(float*)*MAXVARS);
imemp = (imemp & 0xfffc) + 4;
Serial.printf(">3 %x\n",imemp);
struct T_INDEX vtypes[MAXVARS];
//struct T_INDEX *vtypes = (struct T_INDEX*)imemp;
*/
char *vnames_p = vnames;
char **vnp_p = vnp;
char strings[MAXSVARS*SCRIPT_MAXSSIZE];
char *snp[MAXSVARS];
struct M_FILT mfilt[MAXFILT];
char *strings_p = strings;
char *snp[MAXSVARS];
char **snp_p = snp;
uint8_t numperm = 0;
uint8_t numflt = 0;
@ -603,10 +657,6 @@ char *script;
glob_script_mem.max_ssize = SCRIPT_SVARSIZE;
glob_script_mem.scriptptr = 0;
if (!*script) return -999;
float fvalues[MAXVARS];
struct T_INDEX vtypes[MAXVARS];
char init = 0;
while (1) {
// check line
@ -674,6 +724,7 @@ char *script;
vtypes[vars].index = numflt;
numflt++;
if (numflt>MAXFILT) {
if (imemptr) free(imemptr);
return -6;
}
} else {
@ -699,6 +750,7 @@ char *script;
if (!vtypes[vars].bits.is_filter) vtypes[vars].index = nvars;
nvars++;
if (nvars>MAXNVARS) {
if (imemptr) free(imemptr);
return -1;
}
if (vtypes[vars].bits.is_filter) {
@ -731,11 +783,13 @@ char *script;
vtypes[vars].index = svars;
svars++;
if (svars>MAXSVARS) {
if (imemptr) free(imemptr);
return -2;
}
}
vars++;
if (vars>MAXVARS) {
if (imemptr) free(imemptr);
return -3;
}
}
@ -786,6 +840,7 @@ char *script;
uint8_t *script_mem;
script_mem = (uint8_t*)calloc(script_mem_size, 1);
if (!script_mem) {
if (imemptr) free(imemptr);
return -4;
}
glob_script_mem.script_mem = script_mem;
@ -841,7 +896,6 @@ char *script;
//memcpy(script_mem,strings,size);
script_mem += size;
// now must recalc memory offsets
uint16_t index = 0;
#ifdef SCRIPT_LARGE_VNBUFF
@ -864,11 +918,13 @@ char *script;
index++;
if (index > MAXVNSIZ) {
free(glob_script_mem.script_mem);
if (imemptr) free(imemptr);
return -5;
}
}
// variables usage info
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: nv=%d, tv=%d, vns=%d, ram=%d"), nvars, svars, index, glob_script_mem.script_mem_size);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: nv=%d, tv=%d, vns=%d, ram=%d"), nvars, svars, index, glob_script_mem.script_mem_size);
// copy string variables
char *cp1 = glob_script_mem.glob_snp;
@ -940,7 +996,6 @@ char *script;
}
}
#ifdef USE_SCRIPT_FATFS
if (!glob_script_mem.script_sd_found) {
@ -985,8 +1040,10 @@ char *script;
}
#endif //USE_SCRIPT_GLOBVARS
return 0;
if (imemptr) {
free(imemptr);
}
return err;
}
#ifdef USE_SCRIPT_FATFS
@ -1072,10 +1129,10 @@ void Script_Init_UDP() {
if (glob_script_mem.udp_flags.udp_connected) return;
if (Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_UPNP "SCRIPT UDP started"));
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_UPNP "SCRIPT UDP started"));
glob_script_mem.udp_flags.udp_connected = 1;
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_UPNP "SCRIPT UDP failed"));
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_UPNP "SCRIPT UDP failed"));
glob_script_mem.udp_flags.udp_connected = 0;
}
}
@ -1089,7 +1146,7 @@ void Script_PollUdp(void) {
int32_t len = Script_PortUdp.read(packet_buffer, SCRIPT_UDP_BUFFER_SIZE - 1);
packet_buffer[len] = 0;
script_udp_remote_ip = Script_PortUdp.remoteIP();
AddLog_P(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %s"), packet_buffer, len, script_udp_remote_ip.toString().c_str());
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %s"), packet_buffer, len, script_udp_remote_ip.toString().c_str());
char *lp=packet_buffer;
if (!strncmp(lp,"=>", 2)) {
lp += 2;
@ -1108,10 +1165,10 @@ void Script_PollUdp(void) {
uint32_t index;
uint32_t res = match_vars(vnam, &fp, &sp, &index);
if (res == NUM_RES) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("num var found - %s - %d - %d"), vnam, res, index);
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("num var found - %s - %d - %d"), vnam, res, index);
*fp=CharToFloat(cp + 1);
} else if (res == STR_RES) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("string var found - %s - %d - %d"), vnam, res, index);
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("string var found - %s - %d - %d"), vnam, res, index);
strlcpy(sp, cp + 1, SCRIPT_MAXSSIZE);
} else {
// error var not found
@ -1145,10 +1202,10 @@ void script_udp_sendvar(char *vname,float *fp,char *sp) {
char flstr[16];
dtostrfd(*fp, 8, flstr);
strcat(sbuf, flstr);
AddLog_P(LOG_LEVEL_DEBUG, PSTR("num var updated - %s"), sbuf);
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("num var updated - %s"), sbuf);
} else {
strcat(sbuf, sp);
AddLog_P(LOG_LEVEL_DEBUG, PSTR("string var updated - %s"), sbuf);
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("string var updated - %s"), sbuf);
}
Script_PortUdp.beginPacket(IPAddress(239, 255, 255, 250), SCRIPT_UDP_PORT);
// Udp.print(String("RET UC: ") + String(recv_Packet));
@ -1269,7 +1326,7 @@ void Set_MFVal(uint8_t index, uint16_t bind, float val) {
if (val < 0 || val >= maxind) val = 0;
mflp->index = val;
} else {
if (bind > 1 && bind <= maxind) {
if (bind >= 1 && bind <= maxind) {
mflp->rbuff[bind-1] = val;
}
}
@ -2053,7 +2110,7 @@ chknext:
if (!glob_script_mem.file_flags[cnt].is_open) {
if (mode==0) {
#ifdef DEBUG_FS
AddLog_P(LOG_LEVEL_INFO, PSTR("open file for read %d"), cnt);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("open file for read %d"), cnt);
#endif
glob_script_mem.files[cnt] = fsp->open(str, FILE_READ);
if (glob_script_mem.files[cnt].isDirectory()) {
@ -2067,12 +2124,12 @@ chknext:
if (mode==1) {
glob_script_mem.files[cnt] = fsp->open(str,FILE_WRITE);
#ifdef DEBUG_FS
AddLog_P(LOG_LEVEL_INFO, PSTR("open file for write %d"), cnt);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("open file for write %d"), cnt);
#endif
} else {
glob_script_mem.files[cnt] = fsp->open(str,FILE_APPEND);
#ifdef DEBUG_FS
AddLog_P(LOG_LEVEL_INFO, PSTR("open file for append %d"), cnt);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("open file for append %d"), cnt);
#endif
}
}
@ -2080,7 +2137,7 @@ chknext:
fvar = cnt;
glob_script_mem.file_flags[cnt].is_open = 1;
} else {
AddLog_P(LOG_LEVEL_INFO, PSTR("file open failed"));
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("file open failed"));
}
break;
}
@ -2095,7 +2152,7 @@ chknext:
uint8_t ind = fvar;
if (ind>=SFS_MAX) ind = SFS_MAX - 1;
#ifdef DEBUG_FS
AddLog_P(LOG_LEVEL_INFO, PSTR("closing file %d"), ind);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("closing file %d"), ind);
#endif
glob_script_mem.files[ind].close();
glob_script_mem.file_flags[ind].is_open = 0;
@ -2231,7 +2288,7 @@ chknext:
} else {
fvar = 0;
}
//AddLog_P(LOG_LEVEL_INFO, PSTR("picture save: %d"), len);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("picture save: %d"), len);
} else {
fvar = 0;
}
@ -3624,7 +3681,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
void toLog(const char *str) {
if (!str) return;
AddLog_P(LOG_LEVEL_INFO, str);
Script_AddLog_P(LOG_LEVEL_INFO, str);
}
@ -4332,7 +4389,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonParserObject *jo) {
}
cmd[count] = *lp++;
}
//AddLog_P(LOG_LEVEL_INFO, tmp);
//Script_AddLog_P(LOG_LEVEL_INFO, tmp);
// replace vars in cmd
char *tmp = cmdmem + SCRIPT_CMDMEM / 2;
Replace_Cmd_Vars(cmd, 0, tmp, SCRIPT_CMDMEM / 2);
@ -4344,7 +4401,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonParserObject *jo) {
} else {
if (!sflag) {
tasm_cmd_activ = 1;
AddLog_P(glob_script_mem.script_loglevel&0x7f, PSTR("Script: performs \"%s\""), tmp);
Script_AddLog_P(glob_script_mem.script_loglevel&0x7f, PSTR("Script: performs \"%s\""), tmp);
} else if (sflag==2) {
// allow recursive call
} else {
@ -4867,13 +4924,13 @@ uint8_t sc_state;
// upload script and start immediately
void script_upload_start(void) {
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: file upload execute"));
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: file upload execute"));
HTTPUpload& upload = Webserver->upload();
if (upload.status == UPLOAD_FILE_START) {
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload start"));
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload start"));
script_ex_ptr = (uint8_t*)glob_script_mem.script_ram;
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload file %s, %d"),upload.filename.c_str(),upload.totalSize);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload file %s, %d"),upload.filename.c_str(),upload.totalSize);
if (strcmp(upload.filename.c_str(), "execute_script")) {
Web.upload_error = 1;
@ -4891,7 +4948,7 @@ void script_upload_start(void) {
bitWrite(Settings.rule_enabled, 0, 0);
} else if(upload.status == UPLOAD_FILE_WRITE) {
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload write"));
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload write"));
uint32_t csiz = upload.currentSize;
uint32_t tsiz = glob_script_mem.script_size - 1;
if (uplsize<tsiz) {
@ -4905,20 +4962,20 @@ void script_upload_start(void) {
script_ex_ptr += csiz;
uplsize += csiz;
}
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: write %d - %d"),csiz,uplsize);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: write %d - %d"),csiz,uplsize);
}
//if (upload_file) upload_file.write(upload.buf,upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END) {
//if (upload_file) upload_file.close();
if (Web.upload_error) {
AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload error"));
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload error"));
} else {
*script_ex_ptr = 0;
bitWrite(Settings.rule_enabled, 0, sc_state);
SaveScript();
SaveScriptEnd();
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload success"));
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload success"));
}
} else {
Web.upload_error = 1;
@ -4993,7 +5050,7 @@ void ListDir(char *path, uint8_t depth) {
if (lcp) {
ep = lcp + 1;
}
//AddLog_P(LOG_LEVEL_INFO, PSTR("entry: %s"),ep);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("entry: %s"),ep);
time_t tm = entry.getLastWrite();
char tstr[24];
strftime(tstr, 22, "%d-%m-%Y - %H:%M:%S ", localtime(&tm));
@ -5085,7 +5142,7 @@ void ScriptFileUploadSuccess(void) {
File upload_file;
void script_upload(void) {
//AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: file upload"));
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: file upload"));
HTTPUpload& upload = Webserver->upload();
if (upload.status == UPLOAD_FILE_START) {
char npath[48];
@ -5103,7 +5160,7 @@ void script_upload(void) {
} else if(upload.status == UPLOAD_FILE_END) {
if (upload_file) upload_file.close();
if (Web.upload_error) {
AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload error"));
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload error"));
}
} else {
Web.upload_error=1;
@ -5116,13 +5173,13 @@ uint8_t DownloadFile(char *file) {
WiFiClient download_Client;
if (!fsp->exists(file)) {
AddLog_P(LOG_LEVEL_INFO,PSTR("file not found"));
Script_AddLog_P(LOG_LEVEL_INFO,PSTR("file not found"));
return 0;
}
download_file = fsp->open(file, FILE_READ);
if (!download_file) {
AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file"));
Script_AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file"));
return 0;
}
@ -5189,7 +5246,7 @@ void HandleScriptConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_SCRIPT));
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_SCRIPT));
#ifdef USE_SCRIPT_FATFS
if (Webserver->hasArg("d1")) {
@ -5320,7 +5377,7 @@ void ScriptSaveSettings(void) {
strlcpy(glob_script_mem.script_ram, str.c_str(), glob_script_mem.script_size);
if (glob_script_mem.script_ram[0]!='>' && glob_script_mem.script_ram[1]!='D') {
AddLog_P(LOG_LEVEL_INFO, PSTR("script error: must start with >D"));
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("script error: must start with >D"));
bitWrite(Settings.rule_enabled, 0, 0);
}
@ -5333,14 +5390,14 @@ void ScriptSaveSettings(void) {
//
uint32_t script_compress(char *dest, uint32_t size) {
//AddLog_P(LOG_LEVEL_INFO,PSTR("in string: %s len = %d"),glob_script_mem.script_ram,strlen(glob_script_mem.script_ram));
//Script_AddLog_P(LOG_LEVEL_INFO,PSTR("in string: %s len = %d"),glob_script_mem.script_ram,strlen(glob_script_mem.script_ram));
uint32_t len_compressed = SCRIPT_COMPRESS(glob_script_mem.script_ram, strlen(glob_script_mem.script_ram), dest, size);
if (len_compressed > 0) {
dest[len_compressed] = 0;
AddLog_P(LOG_LEVEL_INFO,PSTR("script compressed to %d bytes = %d %%"),len_compressed,len_compressed * 100 / strlen(glob_script_mem.script_ram));
Script_AddLog_P(LOG_LEVEL_INFO,PSTR("script compressed to %d bytes = %d %%"),len_compressed,len_compressed * 100 / strlen(glob_script_mem.script_ram));
return 0;
} else {
AddLog_P(LOG_LEVEL_INFO, PSTR("script compress error: %d"), len_compressed);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("script compress error: %d"), len_compressed);
return 1;
}
}
@ -5367,7 +5424,7 @@ void SaveScriptEnd(void) {
int16_t res = Init_Scripter();
if (res) {
AddLog_P(LOG_LEVEL_INFO, PSTR("script init error: %d"), res);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("script init error: %d"), res);
return;
}
@ -5693,7 +5750,7 @@ void Script_Check_Hue(String *response) {
}
*response += String(EncodeLightId(hue_devs + TasmotaGlobal.devices_present + 1))+"\":";
Script_HueStatus(response, hue_devs);
//AddLog_P(LOG_LEVEL_INFO, PSTR("Hue: %s - %d "),response->c_str(), hue_devs);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Hue: %s - %d "),response->c_str(), hue_devs);
}
hue_devs++;
@ -5708,7 +5765,7 @@ void Script_Check_Hue(String *response) {
}
#if 0
if (response) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("Hue: %d"), hue_devs);
Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("Hue: %d"), hue_devs);
toLog(">>>>");
toLog(response->c_str());
toLog(response->c_str()+LOGSZ);
@ -5853,7 +5910,7 @@ void Script_Handle_Hue(String *path) {
} else {
response = FPSTR(sHUE_ERROR_JSON);
}
AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " Result (%s)"), response.c_str());
Script_AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " Result (%s)"), response.c_str());
WSSend(code, CT_JSON, response);
if (resp) {
Run_Scripter(">E", 2, 0);
@ -5867,7 +5924,7 @@ bool Script_SubCmd(void) {
if (!bitRead(Settings.rule_enabled, 0)) return false;
if (tasm_cmd_activ) return false;
//AddLog_P(LOG_LEVEL_INFO,PSTR(">> %s, %s, %d, %d "),XdrvMailbox.topic, XdrvMailbox.data, XdrvMailbox.payload, XdrvMailbox.index);
//Script_AddLog_P(LOG_LEVEL_INFO,PSTR(">> %s, %s, %d, %d "),XdrvMailbox.topic, XdrvMailbox.data, XdrvMailbox.payload, XdrvMailbox.index);
char command[CMDSZ];
strlcpy(command, XdrvMailbox.topic, CMDSZ);
@ -5895,7 +5952,7 @@ bool Script_SubCmd(void) {
}
//toLog(cmdbuff);
uint32_t res = Run_Scripter(cmdbuff, tlen + 1, 0);
//AddLog_P(LOG_LEVEL_INFO,">>%d",res);
//Script_AddLog_P(LOG_LEVEL_INFO,">>%d",res);
if (res) {
return false;
}
@ -6026,10 +6083,87 @@ void dateTime(uint16_t* date, uint16_t* time) {
#endif //USE_SCRIPT_FATFS
//#define DEBUG_MQTT_EVENT
#ifdef SUPPORT_MQTT_EVENT
//#define DEBUG_MQTT_EVENT
uint32_t JsonParsePath(JsonParserObject *jobj, const char *spath, char delim, float *nres, char *sres, uint32_t slen) {
uint32_t res = 0;
const char *cp = spath;
#ifdef DEBUG_MQTT_EVENT
// Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: parsing json key: %s from json: %s"), cp, jpath);
#endif
JsonParserObject obj = *jobj;
JsonParserObject lastobj = obj;
char selem[32];
uint8_t aindex = 0;
String value = "";
while (1) {
// read next element
for (uint32_t sp=0; sp<sizeof(selem)-1; sp++) {
if (!*cp || *cp==delim) {
selem[sp] = 0;
cp++;
break;
}
selem[sp] = *cp++;
}
#ifdef DEBUG_MQTT_EVENT
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: cmp current key: %s"), selem);
#endif
// check for array
char *sp = strchr(selem,'[');
if (sp) {
*sp = 0;
aindex = atoi(sp+1);
}
// now check element
obj = obj[selem];
if (!obj.isValid()) {
#ifdef DEBUG_MQTT_EVENT
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: obj invalid: %s"), selem);
#endif
JsonParserToken tok = lastobj[selem];
if (tok.isValid()) {
if (tok.isArray()) {
JsonParserArray array = JsonParserArray(tok);
value = array[aindex].getStr();
if (array.isNum()) {
if (nres) *nres=tok.getFloat();
res = 1;
} else {
res = 2;
}
} else {
value = tok.getStr();
if (tok.isNum()) {
if (nres) *nres=tok.getFloat();
res = 1;
} else {
res = 2;
}
}
}
#ifdef DEBUG_MQTT_EVENT
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: token invalid: %s"), selem);
#endif
break;
}
if (obj.isObject()) {
lastobj = obj;
continue;
}
if (!*cp) break;
}
strlcpy(sres,value.c_str(),slen);
return res;
}
#ifndef MQTT_EVENT_MSIZE
#define MQTT_EVENT_MSIZE 256
#endif
@ -6058,7 +6192,7 @@ bool ScriptMqttData(void)
String sData = XdrvMailbox.data;
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
#endif
MQTT_Subscription event_item;
@ -6068,7 +6202,7 @@ bool ScriptMqttData(void)
uint8_t json_valid = 0;
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: Match MQTT message Topic %s with subscription topic %s and key %s"), sTopic.c_str(), event_item.Topic.c_str(),event_item.Key.c_str());
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: Match MQTT message Topic %s with subscription topic %s and key %s"), sTopic.c_str(), event_item.Topic.c_str(),event_item.Key.c_str());
#endif
if (sTopic.startsWith(event_item.Topic)) {
//This topic is subscribed by us, so serve it
@ -6077,6 +6211,7 @@ bool ScriptMqttData(void)
String lkey;
if (event_item.Key.length() == 0) { //If did not specify Key
value = sData;
json_valid = 1;
} else { //If specified Key, need to parse Key/Value from JSON data
@ -6103,80 +6238,32 @@ bool ScriptMqttData(void)
json_valid = 1;
}
#else
JsonParser parser((char*)sData.c_str());
const char *cp = event_item.Key.c_str();
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: parsing json key: %s from json: %s"), cp, sData.c_str());
#endif
JsonParserObject obj = parser.getRootObject();
JsonParserObject lastobj = obj;
char selem[32];
uint8_t aindex = 0;
while (1) {
// read next element
for (uint32_t sp=0; sp<sizeof(selem)-1; sp++) {
if (!*cp || *cp=='.') {
selem[sp] = 0;
cp++;
index++;
break;
}
selem[sp] = *cp++;
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: cmp current key: %s"), selem);
#endif
// check for array
char *sp = strchr(selem,'[');
if (sp) {
*sp = 0;
aindex = atoi(sp+1);
}
// now check element
obj = obj[selem];
if (!obj.isValid()) {
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: obj invalid: %s"), selem);
#endif
JsonParserToken tok = lastobj[selem];
if (tok.isValid()) {
if (tok.isArray()) {
JsonParserArray array = JsonParserArray(tok);
value = array[aindex].getStr();
} else {
value = tok.getStr();
}
json_valid = 1;
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: token invalid: %s"), selem);
#endif
break;
}
if (obj.isObject()) {
lastobj = obj;
continue;
}
if (!*cp) break;
char sres[64];
uint32_t res = JsonParsePath(&obj, event_item.Key.c_str(), '.', NULL, sres, sizeof(sres));
if (res) {
json_valid = 1;
value = sres;
}
#endif
}
if (json_valid) {
value.trim();
char sbuffer[128];
if (json_valid) {
value.trim();
char sbuffer[128];
if (!strncmp(lkey.c_str(), "Epoch", 5)) {
uint32_t ep = atoi(value.c_str()) - (uint32_t)EPOCH_OFFSET;
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(), ep);
} else {
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=\"%s\"\n"), event_item.Event.c_str(), value.c_str());
}
if (!strncmp(lkey.c_str(), "Epoch", 5)) {
uint32_t ep = atoi(value.c_str()) - (uint32_t)EPOCH_OFFSET;
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(), ep);
} else {
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=\"%s\"\n"), event_item.Event.c_str(), value.c_str());
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: setting script var %s"), sbuffer);
Script_AddLog_P(LOG_LEVEL_INFO, PSTR("Script: setting script var %s"), sbuffer);
#endif
//toLog(sbuffer);
execute_script(sbuffer);
//toLog(sbuffer);
execute_script(sbuffer);
}
}
}
}
@ -6221,7 +6308,7 @@ String ScriptSubscribe(const char *data, int data_len)
}
}
}
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: Subscribe command with parameters: %s, %s, %s."), event_name.c_str(), topic.c_str(), key.c_str());
//Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: Subscribe command with parameters: %s, %s, %s."), event_name.c_str(), topic.c_str(), key.c_str());
//event_name.toUpperCase();
if (event_name.length() > 0 && topic.length() > 0) {
//Search all subscriptions
@ -6242,7 +6329,7 @@ String ScriptSubscribe(const char *data, int data_len)
topic.concat("/#");
}
}
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: New topic: %s."), topic.c_str());
// Script_AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: New topic: %s."), topic.c_str());
//MQTT Subscribe
subscription_item.Event = event_name;
subscription_item.Topic = topic.substring(0, topic.length() - 2); //Remove "/#" so easy to match
@ -6511,7 +6598,7 @@ void ScriptFullWebpage(void) {
WSContentBegin(200, CT_HTML);
ScriptWebShow('w');
WSContentEnd();
Serial.printf("fwp update sv %s\n",stmp.c_str() );
//Serial.printf("fwp update sv %s\n",stmp.c_str() );
return; //goto redraw;
// } else {
// Serial.printf("fwp update %s\n",stmp.c_str() );
@ -6519,7 +6606,7 @@ void ScriptFullWebpage(void) {
return;
} else {
Serial.printf("fwp other %s\n",stmp.c_str() );
//Serial.printf("fwp other %s\n",stmp.c_str() );
}
WSContentBegin(200, CT_HTML);
@ -6547,7 +6634,7 @@ void Script_Check_HTML_Setvars(void) {
if (Webserver->hasArg("sv")) {
String stmp = Webserver->arg("sv");
Serial.printf("fwp has arg dv %s\n", stmp.c_str());
//Serial.printf("fwp has arg dv %s\n", stmp.c_str());
char cmdbuf[64];
memset(cmdbuf, 0, sizeof(cmdbuf));
char *cp = cmdbuf;
@ -7613,7 +7700,7 @@ bool Xdrv10(uint8_t function)
if (len_decompressed>0) glob_script_mem.script_ram[len_decompressed] = 0;
// indicates scripter use compression
bitWrite(Settings.rule_once, 6, 1);
//AddLog_P(LOG_LEVEL_INFO, PSTR("decompressed script len %d"),len_decompressed);
//Script_AddLog_P(LOG_LEVEL_INFO, PSTR("decompressed script len %d"),len_decompressed);
#else // USE_SCRIPT_COMPRESSION
// indicates scripter does not use compression
bitWrite(Settings.rule_once, 6, 0);
@ -7695,7 +7782,7 @@ bool Xdrv10(uint8_t function)
#endif // ESP
#endif // USE_SCRIPT_FATFS>=0
AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount OK!"));
Script_AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount OK!"));
//fsp->dateTimeCallback(dateTime);
glob_script_mem.script_sd_found = 1;
@ -7717,7 +7804,7 @@ bool Xdrv10(uint8_t function)
glob_script_mem.flags = 1;
} else {
AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount failed!"));
Script_AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount failed!"));
glob_script_mem.script_sd_found = 0;
}
#endif // USE_SCRIPT_FATFS

View File

@ -55,6 +55,8 @@ struct CORE2_globs {
struct CORE2_ADC {
float vbus_v;
float batt_v;
float vbus_c;
float batt_c;
float temp;
int16_t x;
int16_t y;
@ -72,6 +74,8 @@ void CORE2_Module_Init(void) {
I2cSetActiveFound(AXP_ADDR, "AXP192");
core2_globs.Axp.SetAdcState(true);
// motor voltage
core2_globs.Axp.SetLDOVoltage(3,2000);
core2_globs.Mpu.Init();
I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886");
@ -91,13 +95,7 @@ void CORE2_Init(void) {
TIME_T tmpTime;
TasmotaGlobal.ntp_force_sync = true; //force to sync with ntp
// Rtc.utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231
// from this line, we just copy the function from "void RtcSecond()" at the support.ino ,line 2143 and above
// We need it to set rules etc.
BreakTime(Rtc.utc_time, tmpTime);
if (Rtc.utc_time < START_VALID_TIME ) {
//ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again
}
Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year);
Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year);
AddLog_P(LOG_LEVEL_INFO, PSTR("Set time from BM8563 to RTC (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
@ -119,7 +117,9 @@ void CORE2_audio_power(bool power) {
#ifdef USE_WEBSERVER
const char HTTP_CORE2[] PROGMEM =
"{s}VBUS Voltage" "{m}%s V" "{e}"
"{s}VBUS Current" "{m}%s mA" "{e}"
"{s}BATT Voltage" "{m}%s V" "{e}"
"{s}BATT Current" "{m}%s mA" "{e}"
"{s}Chip Temperature" "{m}%s C" "{e}";
#ifdef USE_MPU6886
const char HTTP_CORE2_MPU[] PROGMEM =
@ -131,27 +131,31 @@ const char HTTP_CORE2_MPU[] PROGMEM =
void CORE2_loop(uint32_t flg) {
Sync_RTOS_TIME();
}
void CORE2_WebShow(uint32_t json) {
char vstring[32];
char bvstring[32];
char cstring[32];
char bcstring[32];
char tstring[32];
dtostrfd(core2_adc.vbus_v, 3, vstring);
dtostrfd(core2_adc.batt_v, 3, bvstring);
dtostrfd(core2_adc.vbus_c, 1, cstring);
dtostrfd(core2_adc.batt_c, 1, bcstring);
dtostrfd(core2_adc.temp, 2, tstring);
if (json) {
ResponseAppend_P(PSTR(",\"CORE2\":{\"VBV\":%s,\"BV\":%s,\"CT\":%s"), vstring, bvstring, tstring);
ResponseAppend_P(PSTR(",\"CORE2\":{\"VBV\":%s,\"BV\":%s,\"VBC\":%s,\"BC\":%s,\"CT\":%s"), vstring, cstring, bvstring, bcstring, tstring);
#ifdef USE_MPU6886
ResponseAppend_P(PSTR(",\"MPUX\":%d,\"MPUY\":%d,\"MPUZ\":%d"), core2_adc.x, core2_adc.y, core2_adc.z);
#endif
ResponseJsonEnd();
} else {
WSContentSend_PD(HTTP_CORE2, vstring, bvstring, tstring);
WSContentSend_PD(HTTP_CORE2, vstring, cstring, bvstring, bcstring, tstring);
#ifdef USE_MPU6886
WSContentSend_PD(HTTP_CORE2_MPU, core2_adc.x, core2_adc.y, core2_adc.z);
@ -368,13 +372,17 @@ void CORE2_EverySecond(void) {
CORE2_DoShutdown();
}
}
Sync_RTOS_TIME();
}
}
// currents are not supported by hardware implementation
void CORE2_GetADC(void) {
core2_adc.vbus_v = core2_globs.Axp.GetVBusVoltage();
core2_adc.batt_v = core2_globs.Axp.GetBatVoltage();
core2_adc.vbus_c = core2_globs.Axp.GetVinCurrent();
core2_adc.batt_c = core2_globs.Axp.GetBatCurrent();
core2_adc.temp = core2_globs.Axp.GetTempInAXP192();
#ifdef USE_MPU6886
float x;

View File

@ -824,7 +824,7 @@ void Dump2log(void) {
int16_t index=0,hcnt=0;
uint32_t d_lastms;
uint8_t dchars[16];
char log_data[LOGSZ]; // May be a lot smaller...
char log_data[128];
//if (!SML_SAVAILABLE) return;