Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Mar 07 13:40:12 2019 +0000
Revision:
57:8fa31ff4e773
Parent:
56:f6e814fe0159
Child:
58:e5ab14ef6ea6
Tidied page module ajax routine

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 PageAddLabelledOnOff(char* label, float labelwidth, int value)
andrewboyson 0:f8998d10763e 131 {
andrewboyson 0:f8998d10763e 132 if (value) PageAddLabelledValue(label, labelwidth, "On");
andrewboyson 0:f8998d10763e 133 else PageAddLabelledValue(label, labelwidth, "Off");
andrewboyson 0:f8998d10763e 134 }
andrewboyson 0:f8998d10763e 135 void PageAddLabelledInt(char* label, float labelwidth, int value)
andrewboyson 0:f8998d10763e 136 {
andrewboyson 0:f8998d10763e 137 char text[30];
andrewboyson 11:84121d7b47e9 138 snprintf(text, sizeof(text), "%8d", value); //Right align with enough spaces so that the length is always constant.
andrewboyson 0:f8998d10763e 139 PageAddLabelledValue(label, labelwidth, text);
andrewboyson 0:f8998d10763e 140 }
andrewboyson 0:f8998d10763e 141 void PageAddTextInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, char* value)
andrewboyson 0:f8998d10763e 142 {
andrewboyson 0:f8998d10763e 143 HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n");
andrewboyson 0:f8998d10763e 144
andrewboyson 0:f8998d10763e 145 char text[100];
andrewboyson 0:f8998d10763e 146 if (width < 0.01)
andrewboyson 0:f8998d10763e 147 {
andrewboyson 0:f8998d10763e 148 HttpAddText("<div>");
andrewboyson 0:f8998d10763e 149 }
andrewboyson 0:f8998d10763e 150 else
andrewboyson 0:f8998d10763e 151 {
andrewboyson 0:f8998d10763e 152 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", width);
andrewboyson 0:f8998d10763e 153 HttpAddText(text);
andrewboyson 0:f8998d10763e 154 }
andrewboyson 0:f8998d10763e 155 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label);
andrewboyson 0:f8998d10763e 156 HttpAddText(text);
andrewboyson 0:f8998d10763e 157 snprintf(text, sizeof(text), "<input type='text' name='%s' style='width:%.1fem;' value='%s'>", name, inputwidth, value);
andrewboyson 0:f8998d10763e 158 HttpAddText(text);
andrewboyson 0:f8998d10763e 159 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 160 HttpAddText("<input type='submit' value='Set' style='display:none;'>\r\n</form>\r\n");
andrewboyson 0:f8998d10763e 161
andrewboyson 0:f8998d10763e 162 }
andrewboyson 0:f8998d10763e 163 void PageAddIntInput(char* action, float width, char* label, float labelwidth, char* name, float inputwidth, int value)
andrewboyson 0:f8998d10763e 164 {
andrewboyson 0:f8998d10763e 165 char text[30];
andrewboyson 0:f8998d10763e 166 snprintf(text, sizeof(text), "%d", value);
andrewboyson 0:f8998d10763e 167 PageAddTextInput(action, width, label, labelwidth, name, inputwidth, text);
andrewboyson 0:f8998d10763e 168 }
andrewboyson 56:f6e814fe0159 169 void PageAddCheckInput(char* action, char* label, float labelwidth, char* name, char* button)
andrewboyson 0:f8998d10763e 170 {
andrewboyson 56:f6e814fe0159 171 char text[100];
andrewboyson 0:f8998d10763e 172 HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n");
andrewboyson 0:f8998d10763e 173 HttpAddText("<input type='hidden' name='"); HttpAddText(name); HttpAddText("'>\r\n");
andrewboyson 56:f6e814fe0159 174 if (labelwidth < 0.1)
andrewboyson 56:f6e814fe0159 175 {
andrewboyson 56:f6e814fe0159 176 HttpAddText(label); HttpAddText("\r\n");
andrewboyson 56:f6e814fe0159 177 }
andrewboyson 56:f6e814fe0159 178 else
andrewboyson 56:f6e814fe0159 179 {
andrewboyson 56:f6e814fe0159 180 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>%s</div>\r\n", labelwidth, label);
andrewboyson 56:f6e814fe0159 181 HttpAddText(text);
andrewboyson 56:f6e814fe0159 182 }
andrewboyson 0:f8998d10763e 183 HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n");
andrewboyson 0:f8998d10763e 184 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 185 }
andrewboyson 0:f8998d10763e 186 void PageAddTm(struct tm* ptm)
andrewboyson 0:f8998d10763e 187 {
andrewboyson 0:f8998d10763e 188 char text[30];
andrewboyson 0:f8998d10763e 189 snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
andrewboyson 0:f8998d10763e 190 HttpAddText(text);
andrewboyson 0:f8998d10763e 191 switch(ptm->tm_wday)
andrewboyson 0:f8998d10763e 192 {
andrewboyson 0:f8998d10763e 193 case 0: HttpAddText("Sun"); break;
andrewboyson 0:f8998d10763e 194 case 1: HttpAddText("Mon"); break;
andrewboyson 0:f8998d10763e 195 case 2: HttpAddText("Tue"); break;
andrewboyson 0:f8998d10763e 196 case 3: HttpAddText("Wed"); break;
andrewboyson 0:f8998d10763e 197 case 4: HttpAddText("Thu"); break;
andrewboyson 0:f8998d10763e 198 case 5: HttpAddText("Fri"); break;
andrewboyson 0:f8998d10763e 199 case 6: HttpAddText("Sat"); break;
andrewboyson 0:f8998d10763e 200 default: HttpAddText("???"); break;
andrewboyson 0:f8998d10763e 201 }
andrewboyson 0:f8998d10763e 202 snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
andrewboyson 0:f8998d10763e 203 HttpAddText(text);
andrewboyson 0:f8998d10763e 204 if (ptm->tm_isdst > 0) HttpAddText(" BST");
andrewboyson 0:f8998d10763e 205 else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
andrewboyson 0:f8998d10763e 206 else HttpAddText(" UTC");
andrewboyson 0:f8998d10763e 207 }
andrewboyson 0:f8998d10763e 208 void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request)
andrewboyson 0:f8998d10763e 209 {
andrewboyson 0:f8998d10763e 210 char text[100];
andrewboyson 0:f8998d10763e 211
andrewboyson 0:f8998d10763e 212 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 213 HttpAddText(text);
andrewboyson 0:f8998d10763e 214 HttpAddText(label);
andrewboyson 0:f8998d10763e 215 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 216
andrewboyson 0:f8998d10763e 217 HttpAddText("<div class='toggle' id='");
andrewboyson 0:f8998d10763e 218 HttpAddText(id);
andrewboyson 0:f8998d10763e 219 HttpAddText("' dir='ltr' onclick='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 220 HttpAddText(request);
andrewboyson 0:f8998d10763e 221 HttpAddText("=1\")'>\r\n");
andrewboyson 0:f8998d10763e 222
andrewboyson 0:f8998d10763e 223 HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 0:f8998d10763e 224
andrewboyson 0:f8998d10763e 225 HttpAddText("</div></div>\r\n");
andrewboyson 0:f8998d10763e 226 }
andrewboyson 31:b5ca802195a7 227 void PageAddAjaxInput(float labelwidth, char* label, float inputwidth, char* id, char* request)
andrewboyson 0:f8998d10763e 228 {
andrewboyson 0:f8998d10763e 229 char text[100];
andrewboyson 0:f8998d10763e 230
andrewboyson 0:f8998d10763e 231 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 232 HttpAddText(text);
andrewboyson 0:f8998d10763e 233 HttpAddText(label);
andrewboyson 0:f8998d10763e 234 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 235
andrewboyson 0:f8998d10763e 236 HttpAddText("<input type='text' id='");
andrewboyson 0:f8998d10763e 237 HttpAddText(id);
andrewboyson 0:f8998d10763e 238 HttpAddText("' style='width:");
andrewboyson 0:f8998d10763e 239 sprintf(text, "%.1fem;", inputwidth);
andrewboyson 0:f8998d10763e 240 HttpAddText(text);
andrewboyson 0:f8998d10763e 241 HttpAddText("' onchange='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 242 HttpAddText(request);
andrewboyson 0:f8998d10763e 243 HttpAddText("=\" + this.value)'>\r\n");
andrewboyson 0:f8998d10763e 244
andrewboyson 0:f8998d10763e 245 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 246 }
andrewboyson 57:8fa31ff4e773 247 void PageAddAjaxLabelled(float labelwidth, char* label, char* id, char* suffix)
andrewboyson 57:8fa31ff4e773 248 {
andrewboyson 57:8fa31ff4e773 249 HttpAddText("<div>");
andrewboyson 57:8fa31ff4e773 250 HttpAddF ("<div style='width:%.1fem; display:inline-block;'>%s</div>", labelwidth, label);
andrewboyson 57:8fa31ff4e773 251 HttpAddText("<span id='"); HttpAddText(id); HttpAddText("'></span>");
andrewboyson 57:8fa31ff4e773 252 HttpAddText(suffix);
andrewboyson 57:8fa31ff4e773 253 HttpAddText("</div>\r\n");
andrewboyson 57:8fa31ff4e773 254 }
andrewboyson 0:f8998d10763e 255
andrewboyson 0:f8998d10763e 256