Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Revision:
30:6a08abbe6301
Parent:
12:237a0f75b4d0
Child:
31:b5ca802195a7
diff -r 4d649264d6b6 -r 6a08abbe6301 page/page.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/page/page.c	Thu Jan 17 13:07:53 2019 +0000
@@ -0,0 +1,251 @@
+#include <stdio.h>
+#include "http.h"
+#include "page.h"
+#include "page-derived.h"
+#include "mac.h"
+#include "ip4addr.h"
+#include "ip6addr.h"
+
+void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
+{
+    HttpAddText("<!DOCTYPE html>\r\n"
+                     "<html>\r\n"
+                     "<head>\r\n");
+    if (site)
+    {
+        HttpAddText("   <title>");
+        HttpAddText(site);
+        if (title)
+        {
+            HttpAddText(" - ");
+            HttpAddText(title);
+        }
+        HttpAddText("</title>\r\n");
+    }
+    HttpAddText("   <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
+    if (style)
+    {
+        HttpAddText("   <link rel='stylesheet' href='/");
+        HttpAddText(style);
+        HttpAddText("' type='text/css'/>\r\n");
+    }
+    if (script)
+    {
+        HttpAddText("   <script src='/");
+        HttpAddText(script);
+        HttpAddText("' type='text/javascript'></script>\r\n");
+    }
+    HttpAddText("   <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
+                     "   <link rel='icon'       href='/favicon.ico' type='image/x-icon'/>\r\n"
+                     "</head>\r\n"
+                     "<body>\r\n");
+
+}
+void PageAddH1(const char* site, const char* pageName)
+{
+    HttpAddText("<h1>");
+    HttpAddText(site);
+    HttpAddText(" - ");
+    HttpAddText(pageName);
+    HttpAddText("</h1>\r\n");
+}
+void PageAddH2(const char* text)
+{
+    HttpAddText("<h2>");
+    HttpAddText(text);
+    HttpAddText("</h2>\r\n");
+}
+void PageAddEnd()
+{
+    HttpAddText("</body>\r\n"
+                "</html>\r\n");
+}
+
+void PageAddNavItem(int highlight, char* href, char* title)
+{
+    char *p;
+    HttpAddText("<li ");
+    if (highlight) p = "class='this'";
+    else           p = "            ";
+    HttpAddText(p);
+    HttpAddText("><a href='");
+    HttpAddText(href);
+    HttpAddText("'>");
+    HttpAddText(title);
+    HttpAddText("</a></li>\r\n");
+
+}
+void PageAddNav(int page)
+{
+    HttpAddText("<nav><ul>\r\n");
+    PageAddNavDerived(page);
+    PageAddNavItem(page ==   CLOCK_PAGE, "/clock",   "Clock");
+    PageAddNavItem(page ==   FAULT_PAGE, "/fault",   "Fault");
+    PageAddNavItem(page ==     NET_PAGE, "/net",     "Net");
+    PageAddNavItem(page ==     LOG_PAGE, "/log",     "Log");
+    PageAddNavItem(page ==   TRACE_PAGE, "/trace",   "Trace");
+    HttpAddText("</ul></nav>\r\n");
+}
+
+void PageAddLabelledValue(char* label, float labelwidth, char* value)
+{
+    HttpAddF   ("<div style='width:%.1fem;'>", labelwidth);
+    HttpAddText(label);
+    HttpAddText("<span style='float:right;'>");
+    HttpAddText(value);
+    HttpAddText("</span>");
+    HttpAddText("</div>\r\n");
+}
+
+void PageAddLabelledMac(char* label, float labelwidth, char* mac)
+{
+    HttpAddF   ("<div style='width:%.1fem;'>", labelwidth);
+    HttpAddText(label);
+    HttpAddText("<span style='float:right;'>");
+    MacHttp         (mac);
+    HttpAddText("</span>");
+    HttpAddText("</div>\r\n");
+}
+
+void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip)
+{
+    HttpAddF   ("<div style='width:%.1fem;'>", labelwidth);
+    HttpAddText(label);
+    HttpAddText("<span style='float:right;'>");
+    Ip4AddressHttp  (ip);
+    HttpAddText("</span>");
+    HttpAddText ("</div>\r\n");
+}
+
+void PageAddLabelledIp6(char* label, float labelwidth, char* ip)
+{
+    HttpAddF   ("<div style='width:%.1fem;'>", labelwidth);
+    HttpAddText(label);
+    HttpAddText("<span style='float:right;'>");
+    Ip6AddressHttp  (ip);
+    HttpAddText("</span>");
+    HttpAddText ("</div>\r\n");
+}
+void PageAddLabelledName(char* label, float labelwidth, char* name, char* suffix)
+{
+    char text[100];
+    HttpAddText("<div>");
+    snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", labelwidth);
+    HttpAddText(text);
+    HttpAddText(label);
+    HttpAddText("</div><span id='");
+    HttpAddText(name);
+    HttpAddText("'></span>");
+    HttpAddText(suffix);
+    HttpAddText("</div>\r\n");
+}
+void PageAddLabelledOnOff(char* label, float labelwidth, int value)
+{
+    if (value) PageAddLabelledValue(label, labelwidth, "On");
+    else       PageAddLabelledValue(label, labelwidth, "Off");
+}
+void PageAddLabelledInt(char* label, float labelwidth, int value)
+{
+    char text[30];
+    snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant. 
+    PageAddLabelledValue(label, labelwidth, text);
+}
+void PageAddTextInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, char* value)
+{
+    HttpAddText("<form action='");    HttpAddText(action);    HttpAddText("' method='get'>\r\n");
+    
+    char text[100];
+    if (width < 0.01)
+    {
+        HttpAddText("<div>");
+    }
+    else
+    {
+        snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", width);
+        HttpAddText(text);
+    }
+    snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label);
+    HttpAddText(text);
+    snprintf(text, sizeof(text), "<input type='text' name='%s' style='width:%.1fem;' value='%s'>", name, inputwidth, value);
+    HttpAddText(text);
+    HttpAddText("</div>\r\n");
+    HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n</form>\r\n");
+
+}
+void PageAddIntInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, int value)
+{    
+    char text[30];
+    snprintf(text, sizeof(text), "%d", value);
+    PageAddTextInput(action, width, label, labelwidth, name, inputwidth, text);
+}
+void PageAddCheckInput(char* action, char* label, char* name, char* button)
+{
+    HttpAddText("<form action='");              HttpAddText(action); HttpAddText("' method='get'>\r\n");
+    HttpAddText("<input type='hidden' name='"); HttpAddText(name);   HttpAddText("'>\r\n");
+    HttpAddText(label); HttpAddText("\r\n");
+    HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n");
+    HttpAddText("</form>\r\n");
+}
+void PageAddTm(struct tm* ptm)
+{
+    char text[30];
+    snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
+    HttpAddText(text);
+    switch(ptm->tm_wday)
+    {
+        case  0: HttpAddText("Sun"); break;
+        case  1: HttpAddText("Mon"); break;
+        case  2: HttpAddText("Tue"); break;
+        case  3: HttpAddText("Wed"); break;
+        case  4: HttpAddText("Thu"); break;
+        case  5: HttpAddText("Fri"); break;
+        case  6: HttpAddText("Sat"); break;
+        default: HttpAddText("???"); break;
+    }
+    snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
+    HttpAddText(text);
+    if      (ptm->tm_isdst  > 0) HttpAddText(" BST");
+    else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
+    else                         HttpAddText(" UTC");
+}
+void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request)
+{
+    char text[100];
+    
+    snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
+    HttpAddText(text);
+    HttpAddText(label);
+    HttpAddText("</div>\r\n");
+    
+    HttpAddText("<div class='toggle' id='");
+    HttpAddText(id);
+    HttpAddText("' dir='ltr' onclick='AjaxRequest(\"");
+    HttpAddText(request);
+    HttpAddText("=1\")'>\r\n");
+    
+    HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n");
+    
+    HttpAddText("</div></div>\r\n");
+}
+void PageAddAjaxHex(float labelwidth, char* label, float inputwidth, char* id, char* request)
+{    
+    char text[100];
+    
+    snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
+    HttpAddText(text);
+    HttpAddText(label);
+    HttpAddText("</div>\r\n");
+    
+    HttpAddText("<input type='text' id='");
+    HttpAddText(id);
+    HttpAddText("' style='width:");
+    sprintf(text, "%.1fem;", inputwidth);
+    HttpAddText(text);
+    HttpAddText("' onchange='AjaxRequest(\"");
+    HttpAddText(request);
+    HttpAddText("=\" + this.value)'>\r\n");
+    
+    HttpAddText("</div>\r\n");
+}
+
+