scripter support for google charts in WEBUI

This commit is contained in:
gemu2015 2020-05-18 10:29:32 +02:00
parent dcde10eac4
commit e043788de8
1 changed files with 134 additions and 4 deletions

View File

@ -3592,6 +3592,11 @@ const char HTTP_FORM_SDC_HREF[] PROGMEM =
uint8_t reject(char *name) { uint8_t reject(char *name) {
char *lcp = strrchr(name,'/');
if (lcp) {
name=lcp+1;
}
while (*name=='/') name++; while (*name=='/') name++;
if (*name=='_') return 1; if (*name=='_') return 1;
if (*name=='.') return 1; if (*name=='.') return 1;
@ -4863,6 +4868,17 @@ const char SCRIPT_MSG_TEXTINP[] PROGMEM =
const char SCRIPT_MSG_NUMINP[] PROGMEM = const char SCRIPT_MSG_NUMINP[] PROGMEM =
"<div><center><label><b>%s</b><input min='%s' max='%s' step='%s' value='%s' type='number' style='width:200px' onfocusin='pr(0)' onfocusout='pr(1)' onchange='siva(value,\"%s\")'></label></div>"; "<div><center><label><b>%s</b><input min='%s' max='%s' step='%s' value='%s' type='number' style='width:200px' onfocusin='pr(0)' onfocusout='pr(1)' onchange='siva(value,\"%s\")'></label></div>";
const char SCRIPT_MSG_GTABLE[] PROGMEM =
"<script type='text/javascript' src='https://www.gstatic.com/charts/loader.js'></script>"
"<script type='text/javascript'>google.charts.load('current',{packages:['corechart']});</script>"
"<script language='JavaScript'>function drawChart(){var data=google.visualization.arrayToDataTable([";
const char SCRIPT_MSG_GTABLEb[] PROGMEM =
"]);var options={title:'%s',isStacked:false};var chart=new google.visualization.%s(document.getElementById('chart%1d'));chart.draw(data,options);}"
"google.charts.setOnLoadCallback(drawChart);</script>";
const char SCRIPT_MSG_GTE1[] PROGMEM = "'%s'";
void ScriptGetVarname(char *nbuf,char *sp, uint32_t blen) { void ScriptGetVarname(char *nbuf,char *sp, uint32_t blen) {
uint32_t cnt; uint32_t cnt;
@ -4882,6 +4898,7 @@ void ScriptWebShow(char mc) {
char tmp[128]; char tmp[128];
uint8_t optflg=0; uint8_t optflg=0;
char *lp=glob_script_mem.section_ptr+2; char *lp=glob_script_mem.section_ptr+2;
uint8_t chartindex=1;
while (lp) { while (lp) {
while (*lp==SCRIPT_EOL) { while (*lp==SCRIPT_EOL) {
lp++; lp++;
@ -5062,7 +5079,6 @@ void ScriptWebShow(char mc) {
dtostrfd(max,4,maxstr); dtostrfd(max,4,maxstr);
dtostrfd(step,4,stepstr); dtostrfd(step,4,stepstr);
WSContentSend_PD(SCRIPT_MSG_NUMINP,label,minstr,maxstr,stepstr,vstr,vname); WSContentSend_PD(SCRIPT_MSG_NUMINP,label,minstr,maxstr,stepstr,vstr,vname);
} else { } else {
Replace_Cmd_Vars(lin,0,tmp,sizeof(tmp)); Replace_Cmd_Vars(lin,0,tmp,sizeof(tmp));
if (optflg) { if (optflg) {
@ -5073,8 +5089,123 @@ void ScriptWebShow(char mc) {
} }
} else { } else {
if (*lin==mc) { if (*lin==mc) {
Replace_Cmd_Vars(lin+1,0,tmp,sizeof(tmp)); lin++;
WSContentSend_PD(PSTR("%s"),tmp); if (!strncmp(lin,"tb(",3)) {
// get google table
struct T_INDEX ind;
uint8_t vtype;
char *lp=lin+3;
uint8 entries=0;
#define MAX_GARRAY 4
float *arrays[MAX_GARRAY];
uint8_t anum=0;
while (anum<MAX_GARRAY) {
if (*lp==')' || *lp==0) break;
char *lp1=lp;
lp=isvar(lp,&vtype,&ind,0,0,0);
if (vtype!=VAR_NV) {
SCRIPT_SKIP_SPACES
uint8_t index=glob_script_mem.type[ind.index].index;
if ((vtype&STYPE)==0) {
// numeric result
//Serial.printf("numeric\n");
if (glob_script_mem.type[index].bits.is_filter) {
//Serial.printf("numeric array\n");
uint8_t len=0;
float *fa=Get_MFAddr(index,&len);
//Serial.printf(">> 2 %d\n",(uint32_t)*fa);
if (fa && len>=entries) {
if (!entries) {entries = len;}
// add array to list
arrays[anum]=fa;
anum++;
}
}
} else {
lp=lp1;
break;
}
}
}
//Serial.printf("arrays %d\n",anum);
//Serial.printf("entries %d\n",entries);
WSContentSend_PD(SCRIPT_MSG_GTABLE);
// we know how many arrays and the number of entries
// we need to fetch the labels now
WSContentSend_PD("[");
for (uint32_t cnt=0; cnt<anum+1; cnt++) {
char label[SCRIPT_MAXSSIZE];
lp=GetStringResult(lp,OPER_EQU,label,0);
SCRIPT_SKIP_SPACES
WSContentSend_PD(SCRIPT_MSG_GTE1,label);
//Serial.printf("labels %s\n",label);
if (cnt<anum) { WSContentSend_PD(","); }
}
WSContentSend_PD("],");
// now we have to export the values
// fetch label part only once in combo string
char label[SCRIPT_MAXSSIZE];
lp=GetStringResult(lp,OPER_EQU,label,0);
SCRIPT_SKIP_SPACES
char *lblp=label;
for (uint32_t cnt=0; cnt<entries; cnt++) {
WSContentSend_PD("['");
char lbl[16];
strncpy(lbl,lblp,sizeof(lbl));
for (uint32_t i=0; i<strlen(lblp); i++) {
if (lblp[i]=='|') {
lbl[i]=0;
lblp+=i+1;
break;
}
lbl[i]=lblp[i];
}
WSContentSend_PD(lbl);
WSContentSend_PD("',");
for (uint32_t ind=0; ind<anum; ind++) {
char acbuff[32];
float *fp=arrays[ind];
dtostrfd(fp[cnt],glob_script_mem.script_dprec,acbuff);
WSContentSend_PD("%s",acbuff);
if (ind<anum-1) { WSContentSend_PD(","); }
}
WSContentSend_PD("]");
if (cnt<entries-1) { WSContentSend_PD(","); }
}
char header[SCRIPT_MAXSSIZE];
lp=GetStringResult(lp,OPER_EQU,header,0);
SCRIPT_SKIP_SPACES
const char *type;
if (*lp!=')') {
switch (*lp) {
case 'l':
type="LineChart";
break;
case 'b':
type="BarChart";
break;
case 'p':
type="PieChart";
break;
default:
type="ColumnChart";
break;
}
} else {
type="ColumnChart";
}
WSContentSend_PD(SCRIPT_MSG_GTABLEb,header,type,chartindex);
chartindex++;
} else {
Replace_Cmd_Vars(lin,0,tmp,sizeof(tmp));
WSContentSend_PD(PSTR("%s"),tmp);
}
} }
} }
} }
@ -5437,6 +5568,5 @@ bool Xdrv10(uint8_t function)
} }
#endif // Do not USE_RULES #endif // Do not USE_RULES
#endif // USE_SCRIPT #endif // USE_SCRIPT