diff options
author | Mike Beattie <mike@ethernal.org> | 2012-03-26 22:23:29 +1300 |
---|---|---|
committer | Mike Beattie <mike@ethernal.org> | 2012-03-26 22:23:29 +1300 |
commit | 9b5620412eb08231615f822814e8bf536718bff2 (patch) | |
tree | 584a7c4aa43a0d1563da4ddcb91eeea68ec6e078 /HouseControl.ino | |
parent | 6722d65035dad394c53032fcad39f818d8b80078 (diff) |
Convert config/get to json.
Signed-off-by: Mike Beattie <mike@ethernal.org>
Diffstat (limited to 'HouseControl.ino')
-rw-r--r-- | HouseControl.ino | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/HouseControl.ino b/HouseControl.ino index d735785..13b6a2f 100644 --- a/HouseControl.ino +++ b/HouseControl.ino @@ -125,19 +125,21 @@ WebServer httpServer("", 80); /*********************************************************************************/ /* Utility functions */ -/* -const char* mac_to_str(const uint8_t* macAddr) { - static char buf[32]; - sprintf(buf, "%x:%x:%x:%x:%x:%x\0", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); - return buf; + +char* mac_to_str(void *buf, const uint8_t* macAddr) { + sprintf((char*)buf, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); + return (char*)buf; } -const char* ip_to_str(const uint8_t* ipAddr) { - static char buf[16]; - sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); - return buf; +char* ip_to_str(void *buf, const uint8_t* ipAddr) { + sprintf((char*)buf, "%d.%d.%d.%d", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); + return (char*)buf; +} + +char* time_to_str(void *buf, time_t t) { + sprintf((char*)buf, "%d/%d/%d %02d:%02d:%02d", day(t), month(t), year(t), hour(t), minute(t), second(t)); + return (char*)buf; } -*/ /*********************************************************************************/ /* NTP/Time functions */ @@ -275,50 +277,43 @@ void configHandler(WebServer &server, WebServer::ConnectionType type, char *url_ void configGetHandler(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { /* for a GET or HEAD, send the standard "it's all OK headers" */ - server.httpSuccess("text/plain"); + server.httpSuccess("application/json"); /* we don't output the body for a HEAD request */ if (type == WebServer::GET) { - - sprintf((char*)buf1, "Config Version: %d\n\n\0", config.configVersion); + sprintf((char*)buf1, "{\"info\":{\"version\":%d,\"time\":\"", config.configVersion); server.print((char*)buf1); - sprintf((char*)buf1, "Time Now: %d/%d/%d %02d:%02d:%02d\n\0", day(time), month(time), year(time), hour(time), minute(time), second(time)); - server.print((char*)buf1); - sprintf((char*)buf1, "Last NTP: %d/%d/%d %02d:%02d:%02d\n\n\0", day(lastNTPtime), month(lastNTPtime), year(lastNTPtime), hour(lastNTPtime), minute(lastNTPtime), second(lastNTPtime)); - server.print((char*)buf1); + server.print(time_to_str(buf2, time)); - sprintf((char*)buf1, "MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n\n\0", config.mac[0], config.mac[1], config.mac[2], config.mac[3], config.mac[4], config.mac[5]); - server.print((char*)buf1); - sprintf((char*)buf1, "Default IP Addr: %d.%d.%d.%d\n\0", config.def_ip[0], config.def_ip[1], config.def_ip[2], config.def_ip[3]); - server.print((char*)buf1); - sprintf((char*)buf1, "Default Netmask: %d.%d.%d.%d\n\0", config.def_netmask[0], config.def_netmask[1], config.def_netmask[2], config.def_netmask[3]); - server.print((char*)buf1); - sprintf((char*)buf1, "Default Gateway: %d.%d.%d.%d\n\n\0", config.def_gateway[0], config.def_gateway[1], config.def_gateway[2], config.def_gateway[3]); - server.print((char*)buf1); + server.print("\",\"lastntp\":\""); + server.print(time_to_str(buf2, lastNTPtime)); - sprintf((char*)buf1, "NTP Server: %d.%d.%d.%d\n\0", config.ntpServer[0], config.ntpServer[1], config.ntpServer[2], config.ntpServer[3]); - server.print((char*)buf1); - sprintf((char*)buf1, "UTC Offset: %d\n\n\0", config.UTC_offset); - server.print((char*)buf1); + server.print("\"},\"settings\":{\"mac\":\""); + server.print(mac_to_str(buf2, config.mac)); - sprintf((char*)buf1, "Status Server: %d.%d.%d.%d\n\0", config.notifyHost[0], config.notifyHost[1], config.notifyHost[2], config.notifyHost[3]); - server.print((char*)buf1); - sprintf((char*)buf1, "Status Port: %d\n\n\0", config.notifyPort); - server.print((char*)buf1); + server.print("\",\"def_ip\":\""); + server.print(ip_to_str(buf2, config.def_ip)); - sprintf((char*)buf1, "Front Door unlock time: %d (secs)\n\n\0", config.frontdoor_holdtime); - server.print((char*)buf1); + server.print("\",\"def_nm\":\""); + server.print(ip_to_str(buf2, config.def_netmask)); - sprintf((char*)buf1, "Garage Door grace time: %d (minutes)\n\0", config.garagedoor_nightgracetime); - server.print((char*)buf1); - sprintf((char*)buf1, "Dawn: %02d:00\n\0", config.dawn); + server.print("\",\"def_gw\":\""); + server.print(ip_to_str(buf2, config.def_gateway)); + + server.print("\",\"ntpserver\":\""); + server.print(ip_to_str(buf2, config.ntpServer)); + + sprintf((char*)buf1, "\",\"utcoffset\":%d,\"stathost\":\"", config.UTC_offset); server.print((char*)buf1); - sprintf((char*)buf1, "Dusk: %02d:00\n\n\0", config.dusk); + + server.print(ip_to_str(buf2, config.notifyHost)); + + sprintf((char*)buf1, "\",\"statport\":%u,\"fd_utime\":%u,\"gd_gtime\":%u,", config.notifyPort, config.frontdoor_holdtime, config.garagedoor_nightgracetime); server.print((char*)buf1); - sprintf((char*)buf1, "Bytes Free: %d\n\n\0", freeMemory()); + sprintf((char*)buf1, "\"dawn\":%u,\"dusk\":%u}}", config.dawn, config.dusk); server.print((char*)buf1); } |