function getTypedHistString(tdx) {
var ret="";
var typen = htypes[tdx];
if (tdx<4){
var tstr="[";
for (var i=0;i<48;i++) {
tstr=tstr+HISTORY[typen][i].toFixed(1);
if (i !=47) {
tstr+=",";
}
}
tstr+="]";
return tstr;
} else {
return JSON.stringify(HISTORY[typen]);
}
}
function processrequest(req, res) {
try {
var par=url.parse(req.url,true);
var q=par.query;
var path=par.pathname;
path=path.split('/');
path=path[1];
switch(path) {
case "status.json":
var r='{"lamp":{';
for (var i=0;i<5;i++) {
r+='"'+colors.substr(i*4,4)+'":'+STATUS.LEDs[i].toFixed(2);
if (i<4) {r+=",";}
}
r+='},\n"sensors":{"RH":'+STATUS.RH.toFixed(1)+',"Temp":'+STATUS.Temp.toFixed(1)+',"Pressure":'+STATUS.Pressure.toFixed(2)+',"Air Quality":'+STATUS.AirQual.toFixed(2)+'},\n"Light":'+JSON.stringify(STATUS.Light)+'}';
res.writeHead(200,head);
res.write(r);
res.end();
break;
case "history.json":
res.writeHead(200,head);
var n=0;
res.on('drain',function(){
res.write('"'+htypes[n]+'"');
res.write(":");
res.write(getTypedHistString(n++));
if (n==8) {res.end("}");} else {res.write(',\r\n');}
});
res.write("{");
break;
case "usermsg.cmd":
if (query.msg && query.msg.length > 1) {
usrmsg=query.msg;
MnuS=10;
setTimeout("uplcd();",100);
res.writeHead(200,head);
res.write("{status:\"ok\",message:\""+query.msg+"\"}");
} else {
res.writeHead(400,head);
res.write("{status:\"error\",error:\"No message supplied\"}");
}
res.end();
break;
case "lamp.cmd":
STATUS.LEDs[0]=q.BLUE===undefined ? 0:E.clip(q.BLUE,0,1);
STATUS.LEDs[1]=q.COOL===undefined ? 0:E.clip(q.COOL,0,1);
STATUS.LEDs[2]=q.WARM===undefined ? 0:E.clip(q.WARM,0,1);
STATUS.LEDs[3]=q.YELL===undefined ? 0:E.clip(q.YELL,0,1);
STATUS.LEDs[4]=q.RED===undefined ? 0:E.clip(q.RED,0,1);
setTimeout("upled();",100);
res.writeHead(200,head);
res.write("{status:\"ok\",message:\"lamp state set\"}");
res.end();
break;
case "code.run": //This is why I don't expose this board to requests from outside the LAN ;-)
if (query.code) {
var x="";
try {
x=eval(query.code);
res.writeHead(200,head);
res.write("{status:\"ok\",result:\""+x+"\"}");
}
catch (err) {
res.writeHead(500,head);
res.write("{status:\"error\",error:\""+err.message+"\"}");
}
} else {
res.writeHead(400,head);
res.write("{status:\"error\",error:\"No code supplied.\"}");
}
res.end();
break;
default:
res.writeHead(404,head);
res.write("{status:\"error\",error:\"unknown command\"}");
res.end();
}
}
catch (err){
res.writeHead(500,head);
res.write("{status:\"error\",error:\""+err.message+"\"}");
res.end();
}
}
Works like a charm!
I was thinking about the object.keys() approach - but I don't want to bite myself in the ass if I later add something to HISTORY object, since my code (as you can see) makes assumptions about which index number corresponds to which kind of data, since I have to do float arrays differently from integer arrays.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Works like a charm!
I was thinking about the object.keys() approach - but I don't want to bite myself in the ass if I later add something to HISTORY object, since my code (as you can see) makes assumptions about which index number corresponds to which kind of data, since I have to do float arrays differently from integer arrays.