Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Mar 06 11:43:29 2019 +0000
Revision:
56:f6e814fe0159
Parent:
46:1822fdbe6c0c
Child:
57:8fa31ff4e773
Updated button input to align the button

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:f8998d10763e 1 #include <stdio.h>
andrewboyson 30:6a08abbe6301 2 #include "http.h"
andrewboyson 30:6a08abbe6301 3 #include "page.h"
andrewboyson 30:6a08abbe6301 4 #include "page-derived.h"
andrewboyson 30:6a08abbe6301 5 #include "mac.h"
andrewboyson 0:f8998d10763e 6 #include "ip4addr.h"
andrewboyson 0:f8998d10763e 7 #include "ip6addr.h"
andrewboyson 0:f8998d10763e 8
andrewboyson 2:14de8c14afd9 9 void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
andrewboyson 0:f8998d10763e 10 {
andrewboyson 0:f8998d10763e 11 HttpAddText("<!DOCTYPE html>\r\n"
andrewboyson 0:f8998d10763e 12 "<html>\r\n"
andrewboyson 0:f8998d10763e 13 "<head>\r\n");
andrewboyson 2:14de8c14afd9 14 if (site)
andrewboyson 0:f8998d10763e 15 {
andrewboyson 0:f8998d10763e 16 HttpAddText(" <title>");
andrewboyson 2:14de8c14afd9 17 HttpAddText(site);
andrewboyson 2:14de8c14afd9 18 if (title)
andrewboyson 2:14de8c14afd9 19 {
andrewboyson 2:14de8c14afd9 20 HttpAddText(" - ");
andrewboyson 2:14de8c14afd9 21 HttpAddText(title);
andrewboyson 2:14de8c14afd9 22 }
andrewboyson 0:f8998d10763e 23 HttpAddText("</title>\r\n");
andrewboyson 0:f8998d10763e 24 }
andrewboyson 0:f8998d10763e 25 HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 26 if (style)
andrewboyson 0:f8998d10763e 27 {
andrewboyson 0:f8998d10763e 28 HttpAddText(" <link rel='stylesheet' href='/");
andrewboyson 0:f8998d10763e 29 HttpAddText(style);
andrewboyson 0:f8998d10763e 30 HttpAddText("' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 31 }
andrewboyson 0:f8998d10763e 32 if (script)
andrewboyson 0:f8998d10763e 33 {
andrewboyson 0:f8998d10763e 34 HttpAddText(" <script src='/");
andrewboyson 0:f8998d10763e 35 HttpAddText(script);
andrewboyson 0:f8998d10763e 36 HttpAddText("' type='text/javascript'></script>\r\n");
andrewboyson 0:f8998d10763e 37 }
andrewboyson 0:f8998d10763e 38 HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
andrewboyson 0:f8998d10763e 39 " <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n"
andrewboyson 0:f8998d10763e 40 "</head>\r\n"
andrewboyson 0:f8998d10763e 41 "<body>\r\n");
andrewboyson 0:f8998d10763e 42
andrewboyson 0:f8998d10763e 43 }
andrewboyson 12:237a0f75b4d0 44 void PageAddH1(const char* site, const char* pageName)
andrewboyson 12:237a0f75b4d0 45 {
andrewboyson 12:237a0f75b4d0 46 HttpAddText("<h1>");
andrewboyson 12:237a0f75b4d0 47 HttpAddText(site);
andrewboyson 12:237a0f75b4d0 48 HttpAddText(" - ");
andrewboyson 12:237a0f75b4d0 49 HttpAddText(pageName);
andrewboyson 12:237a0f75b4d0 50 HttpAddText("</h1>\r\n");
andrewboyson 12:237a0f75b4d0 51 }
andrewboyson 12:237a0f75b4d0 52 void PageAddH2(const char* text)
andrewboyson 12:237a0f75b4d0 53 {
andrewboyson 12:237a0f75b4d0 54 HttpAddText("<h2>");
andrewboyson 12:237a0f75b4d0 55 HttpAddText(text);
andrewboyson 12:237a0f75b4d0 56 HttpAddText("</h2>\r\n");
andrewboyson 12:237a0f75b4d0 57 }
andrewboyson 12:237a0f75b4d0 58 void PageAddEnd()
andrewboyson 12:237a0f75b4d0 59 {
andrewboyson 12:237a0f75b4d0 60 HttpAddText("</body>\r\n"
andrewboyson 12:237a0f75b4d0 61 "</html>\r\n");
andrewboyson 12:237a0f75b4d0 62 }
andrewboyson 0:f8998d10763e 63
andrewboyson 0:f8998d10763e 64 void PageAddNavItem(int highlight, char* href, char* title)
andrewboyson 0:f8998d10763e 65 {
andrewboyson 0:f8998d10763e 66 char *p;
andrewboyson 0:f8998d10763e 67 HttpAddText("<li ");
andrewboyson 0:f8998d10763e 68 if (highlight) p = "class='this'";
andrewboyson 0:f8998d10763e 69 else p = " ";
andrewboyson 0:f8998d10763e 70 HttpAddText(p);
andrewboyson 0:f8998d10763e 71 HttpAddText("><a href='");
andrewboyson 0:f8998d10763e 72 HttpAddText(href);
andrewboyson 0:f8998d10763e 73 HttpAddText("'>");
andrewboyson 0:f8998d10763e 74 HttpAddText(title);
andrewboyson 0:f8998d10763e 75 HttpAddText("</a></li>\r\n");
andrewboyson 0:f8998d10763e 76
andrewboyson 0:f8998d10763e 77 }
andrewboyson 30:6a08abbe6301 78 void PageAddNav(int page)
andrewboyson 30:6a08abbe6301 79 {
andrewboyson 30:6a08abbe6301 80 HttpAddText("<nav><ul>\r\n");
andrewboyson 30:6a08abbe6301 81 PageAddNavDerived(page);
andrewboyson 46:1822fdbe6c0c 82 PageAddNavItem(page == CLOCK_PAGE, "/clock", "Clock");
andrewboyson 46:1822fdbe6c0c 83 PageAddNavItem(page == FAULT_PAGE, "/fault", "Fault");
andrewboyson 46:1822fdbe6c0c 84 PageAddNavItem(page == NET_PAGE, "/net", "Net");
andrewboyson 46:1822fdbe6c0c 85 PageAddNavItem(page == LOG_PAGE, "/log", "Log");
andrewboyson 46:1822fdbe6c0c 86 PageAddNavItem(page == TRACE_PAGE, "/trace", "Trace");
andrewboyson 46:1822fdbe6c0c 87 PageAddNavItem(page == FIRMWARE_PAGE, "/firmware", "Firmware");
andrewboyson 30:6a08abbe6301 88 HttpAddText("</ul></nav>\r\n");
andrewboyson 30:6a08abbe6301 89 }
andrewboyson 0:f8998d10763e 90
andrewboyson 0:f8998d10763e 91 void PageAddLabelledValue(char* label, float labelwidth, char* value)
andrewboyson 0:f8998d10763e 92 {
andrewboyson 0:f8998d10763e 93 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 94 HttpAddText(label);
andrewboyson 0:f8998d10763e 95 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 96 HttpAddText(value);
andrewboyson 0:f8998d10763e 97 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 98 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 99 }
andrewboyson 0:f8998d10763e 100
andrewboyson 0:f8998d10763e 101 void PageAddLabelledMac(char* label, float labelwidth, char* mac)
andrewboyson 0:f8998d10763e 102 {
andrewboyson 0:f8998d10763e 103 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 104 HttpAddText(label);
andrewboyson 0:f8998d10763e 105 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 106 MacHttp (mac);
andrewboyson 0:f8998d10763e 107 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 108 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 109 }
andrewboyson 0:f8998d10763e 110
andrewboyson 0:f8998d10763e 111 void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip)
andrewboyson 0:f8998d10763e 112 {
andrewboyson 0:f8998d10763e 113 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 114 HttpAddText(label);
andrewboyson 0:f8998d10763e 115 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 116 Ip4AddressHttp (ip);
andrewboyson 0:f8998d10763e 117 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 118 HttpAddText ("</div>\r\n");
andrewboyson 0:f8998d10763e 119 }
andrewboyson 0:f8998d10763e 120
andrewboyson 0:f8998d10763e 121 void PageAddLabelledIp6(char* label, float labelwidth, char* ip)
andrewboyson 0:f8998d10763e 122 {
andrewboyson 0:f8998d10763e 123 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 124 HttpAddText(label);
andrewboyson 0:f8998d10763e 125 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 126 Ip6AddressHttp (ip);
andrewboyson 0:f8998d10763e 127 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 128 HttpAddText ("</div>\r\n");
andrewboyson 0:f8998d10763e 129 }
andrewboyson 0:f8998d10763e 130 void PageAddLabelledName(char* label, float labelwidth, char* name, char* suffix)
andrewboyson 0:f8998d10763e 131 {
andrewboyson 0:f8998d10763e 132 char text[100];
andrewboyson 0:f8998d10763e 133 HttpAddText("<div>");
andrewboyson 0:f8998d10763e 134 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", labelwidth);
andrewboyson 0:f8998d10763e 135 HttpAddText(text);
andrewboyson 0:f8998d10763e 136 HttpAddText(label);
andrewboyson 0:f8998d10763e 137 HttpAddText("</div><span id='");
andrewboyson 0:f8998d10763e 138 HttpAddText(name);
andrewboyson 0:f8998d10763e 139 HttpAddText("'></span>");
andrewboyson 0:f8998d10763e 140 HttpAddText(suffix);
andrewboyson 0:f8998d10763e 141 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 142 }
andrewboyson 0:f8998d10763e 143 void PageAddLabelledOnOff(char* label, float labelwidth, int value)
andrewboyson 0:f8998d10763e 144 {
andrewboyson 0:f8998d10763e 145 if (value) PageAddLabelledValue(label, labelwidth, "On");
andrewboyson 0:f8998d10763e 146 else PageAddLabelledValue(label, labelwidth, "Off");
andrewboyson 0:f8998d10763e 147 }
andrewboyson 0:f8998d10763e 148 void PageAddLabelledInt(char* label, float labelwidth, int value)
andrewboyson 0:f8998d10763e 149 {
andrewboyson 0:f8998d10763e 150 char text[30];
andrewboyson 11:84121d7b47e9 151 snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
andrewboyson 0:f8998d10763e 152 PageAddLabelledValue(label, labelwidth, text);
andrewboyson 0:f8998d10763e 153 }
andrewboyson 0:f8998d10763e 154 void PageAddTextInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, char* value)
andrewboyson 0:f8998d10763e 155 {
andrewboyson 0:f8998d10763e 156 HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n");
andrewboyson 0:f8998d10763e 157
andrewboyson 0:f8998d10763e 158 char text[100];
andrewboyson 0:f8998d10763e 159 if (width < 0.01)
andrewboyson 0:f8998d10763e 160 {
andrewboyson 0:f8998d10763e 161 HttpAddText("<div>");
andrewboyson 0:f8998d10763e 162 }
andrewboyson 0:f8998d10763e 163 else
andrewboyson 0:f8998d10763e 164 {
andrewboyson 0:f8998d10763e 165 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", width);
andrewboyson 0:f8998d10763e 166 HttpAddText(text);
andrewboyson 0:f8998d10763e 167 }
andrewboyson 0:f8998d10763e 168 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label);
andrewboyson 0:f8998d10763e 169 HttpAddText(text);
andrewboyson 0:f8998d10763e 170 snprintf(text, sizeof(text), "<input type='text' name='%s' style='width:%.1fem;' value='%s'>", name, inputwidth, value);
andrewboyson 0:f8998d10763e 171 HttpAddText(text);
andrewboyson 0:f8998d10763e 172 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 173 HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n</form>\r\n");
andrewboyson 0:f8998d10763e 174
andrewboyson 0:f8998d10763e 175 }
andrewboyson 0:f8998d10763e 176 void PageAddIntInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, int value)
andrewboyson 0:f8998d10763e 177 {
andrewboyson 0:f8998d10763e 178 char text[30];
andrewboyson 0:f8998d10763e 179 snprintf(text, sizeof(text), "%d", value);
andrewboyson 0:f8998d10763e 180 PageAddTextInput(action, width, label, labelwidth, name, inputwidth, text);
andrewboyson 0:f8998d10763e 181 }
andrewboyson 56:f6e814fe0159 182 void PageAddCheckInput(char* action, char* label, float labelwidth, char* name, char* button)
andrewboyson 0:f8998d10763e 183 {
andrewboyson 56:f6e814fe0159 184 char text[100];
andrewboyson 0:f8998d10763e 185 HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n");
andrewboyson 0:f8998d10763e 186 HttpAddText("<input type='hidden' name='"); HttpAddText(name); HttpAddText("'>\r\n");
andrewboyson 56:f6e814fe0159 187 if (labelwidth < 0.1)
andrewboyson 56:f6e814fe0159 188 {
andrewboyson 56:f6e814fe0159 189 HttpAddText(label); HttpAddText("\r\n");
andrewboyson 56:f6e814fe0159 190 }
andrewboyson 56:f6e814fe0159 191 else
andrewboyson 56:f6e814fe0159 192 {
andrewboyson 56:f6e814fe0159 193 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label);
andrewboyson 56:f6e814fe0159 194 HttpAddText(text);
andrewboyson 56:f6e814fe0159 195 }
andrewboyson 0:f8998d10763e 196 HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n");
andrewboyson 0:f8998d10763e 197 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 198 }
andrewboyson 0:f8998d10763e 199 void PageAddTm(struct tm* ptm)
andrewboyson 0:f8998d10763e 200 {
andrewboyson 0:f8998d10763e 201 char text[30];
andrewboyson 0:f8998d10763e 202 snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
andrewboyson 0:f8998d10763e 203 HttpAddText(text);
andrewboyson 0:f8998d10763e 204 switch(ptm->tm_wday)
andrewboyson 0:f8998d10763e 205 {
andrewboyson 0:f8998d10763e 206 case 0: HttpAddText("Sun"); break;
andrewboyson 0:f8998d10763e 207 case 1: HttpAddText("Mon"); break;
andrewboyson 0:f8998d10763e 208 case 2: HttpAddText("Tue"); break;
andrewboyson 0:f8998d10763e 209 case 3: HttpAddText("Wed"); break;
andrewboyson 0:f8998d10763e 210 case 4: HttpAddText("Thu"); break;
andrewboyson 0:f8998d10763e 211 case 5: HttpAddText("Fri"); break;
andrewboyson 0:f8998d10763e 212 case 6: HttpAddText("Sat"); break;
andrewboyson 0:f8998d10763e 213 default: HttpAddText("???"); break;
andrewboyson 0:f8998d10763e 214 }
andrewboyson 0:f8998d10763e 215 snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
andrewboyson 0:f8998d10763e 216 HttpAddText(text);
andrewboyson 0:f8998d10763e 217 if (ptm->tm_isdst > 0) HttpAddText(" BST");
andrewboyson 0:f8998d10763e 218 else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
andrewboyson 0:f8998d10763e 219 else HttpAddText(" UTC");
andrewboyson 0:f8998d10763e 220 }
andrewboyson 0:f8998d10763e 221 void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request)
andrewboyson 0:f8998d10763e 222 {
andrewboyson 0:f8998d10763e 223 char text[100];
andrewboyson 0:f8998d10763e 224
andrewboyson 0:f8998d10763e 225 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 226 HttpAddText(text);
andrewboyson 0:f8998d10763e 227 HttpAddText(label);
andrewboyson 0:f8998d10763e 228 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 229
andrewboyson 0:f8998d10763e 230 HttpAddText("<div class='toggle' id='");
andrewboyson 0:f8998d10763e 231 HttpAddText(id);
andrewboyson 0:f8998d10763e 232 HttpAddText("' dir='ltr' onclick='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 233 HttpAddText(request);
andrewboyson 0:f8998d10763e 234 HttpAddText("=1\")'>\r\n");
andrewboyson 0:f8998d10763e 235
andrewboyson 0:f8998d10763e 236 HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 0:f8998d10763e 237
andrewboyson 0:f8998d10763e 238 HttpAddText("</div></div>\r\n");
andrewboyson 0:f8998d10763e 239 }
andrewboyson 31:b5ca802195a7 240 void PageAddAjaxInput(float labelwidth, char* label, float inputwidth, char* id, char* request)
andrewboyson 0:f8998d10763e 241 {
andrewboyson 0:f8998d10763e 242 char text[100];
andrewboyson 0:f8998d10763e 243
andrewboyson 0:f8998d10763e 244 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 245 HttpAddText(text);
andrewboyson 0:f8998d10763e 246 HttpAddText(label);
andrewboyson 0:f8998d10763e 247 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 248
andrewboyson 0:f8998d10763e 249 HttpAddText("<input type='text' id='");
andrewboyson 0:f8998d10763e 250 HttpAddText(id);
andrewboyson 0:f8998d10763e 251 HttpAddText("' style='width:");
andrewboyson 0:f8998d10763e 252 sprintf(text, "%.1fem;", inputwidth);
andrewboyson 0:f8998d10763e 253 HttpAddText(text);
andrewboyson 0:f8998d10763e 254 HttpAddText("' onchange='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 255 HttpAddText(request);
andrewboyson 0:f8998d10763e 256 HttpAddText("=\" + this.value)'>\r\n");
andrewboyson 0:f8998d10763e 257
andrewboyson 0:f8998d10763e 258 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 259 }
andrewboyson 0:f8998d10763e 260
andrewboyson 0:f8998d10763e 261