#ifndef WEBSERVERJSON_H_
#define WEBSERVERJSON_H_
#include <WebServer.h>
#include "pinheader2.h"
#include "index.html.h"
WebServer server(80);
char cJson[2048];
bool bHandleJsonSend=false;
void JsonBegin(int iFrameNr=0){
cJson[0]=0;
strcat(cJson,"{");
};
void JsonEnd(){
strcat(cJson,"\"end\":{}}");
};
// iInPin = 1 to 8
void JsonAppendPin(int iInPin){
if (iInPin > 8) return;
if (iInPin < 1) return;
char cTmpBuf[128];
char used[8];
if (pinUsage[iInPin-1].bInUse) strcpy (used,"USED");
else strcpy (used,"----");
sprintf (cTmpBuf,"\"l%i\":{\"use\":\"%s\",\"txt\":\"%s\"},", iInPin, used, pinUsage[iInPin-1].cUsedBy);
strcat(cJson,cTmpBuf);
};
void handleDataPreview(){
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200,"text/html", "Stub");
};
void handleJson(){
runTest();
// Output:
JsonBegin();
for (int n=1; n<=8; n++){
JsonAppendPin(n);
Serial.printf ("%2i :",n);
if (pinUsage[n-1].bInUse == true) Serial.print("USED ");
else Serial.print("---- ");
Serial.printf("%s\r\n",pinUsage[n-1].cUsedBy);
}; // for
JsonEnd();
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200,"text/html", cJson);
//bHandleJsonSend=true;
};
void _handleJsonSend(){
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200,"text/html", cJson);
Serial.println(cJson);delay(30); // DEBUG
bHandleJsonSend=false;
};
void handleRoot() {
char cTmpBuf[512];
strcat(cTmpBuf,"<a href=\"/dataPreview\">Data Preview</a><br>\r\n");
strcat(cTmpBuf,"<a href=\"/calData.js\">calData.js</a><br>\r\n");
strcat(cTmpBuf,"<a href=\"/data.js\">data.js</a><br>\r\n");
server.sendHeader("Access-Control-Allow-Origin","*");
server.send(200, "text/html", index_html);
};
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
};
server.send(404, "text/plain", message);
};
void startHttpServer(){
server.on("/", handleRoot);
server.on("/data.js", handleJson);
server.on("/dataPreview", handleDataPreview);
server.onNotFound(handleNotFound);
server.begin();
}; // StartHttpServer
#endif //WEBSERVERJSON_H_