Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Mon Nov 26 12:45:17 2018 +0000
Revision:
12:237a0f75b4d0
Parent:
11:84121d7b47e9
Added PageAddH1(site, navpage) and PageAddH2(text) plus a small amount of tidying

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:f8998d10763e 1 #include <stdio.h>
andrewboyson 0:f8998d10763e 2 #include "http.h"
andrewboyson 0:f8998d10763e 3 #include "page.h"
andrewboyson 0:f8998d10763e 4 #include "mac.h"
andrewboyson 0:f8998d10763e 5 #include "ip4addr.h"
andrewboyson 0:f8998d10763e 6 #include "ip6addr.h"
andrewboyson 0:f8998d10763e 7
andrewboyson 2:14de8c14afd9 8 void PageAddHeader(const char* site, const char* title, const char* style, const char* script)
andrewboyson 0:f8998d10763e 9 {
andrewboyson 0:f8998d10763e 10 HttpAddText("<!DOCTYPE html>\r\n"
andrewboyson 0:f8998d10763e 11 "<html>\r\n"
andrewboyson 0:f8998d10763e 12 "<head>\r\n");
andrewboyson 2:14de8c14afd9 13 if (site)
andrewboyson 0:f8998d10763e 14 {
andrewboyson 0:f8998d10763e 15 HttpAddText(" <title>");
andrewboyson 2:14de8c14afd9 16 HttpAddText(site);
andrewboyson 2:14de8c14afd9 17 if (title)
andrewboyson 2:14de8c14afd9 18 {
andrewboyson 2:14de8c14afd9 19 HttpAddText(" - ");
andrewboyson 2:14de8c14afd9 20 HttpAddText(title);
andrewboyson 2:14de8c14afd9 21 }
andrewboyson 0:f8998d10763e 22 HttpAddText("</title>\r\n");
andrewboyson 0:f8998d10763e 23 }
andrewboyson 0:f8998d10763e 24 HttpAddText(" <link rel='stylesheet' href='/base.css' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 25 if (style)
andrewboyson 0:f8998d10763e 26 {
andrewboyson 0:f8998d10763e 27 HttpAddText(" <link rel='stylesheet' href='/");
andrewboyson 0:f8998d10763e 28 HttpAddText(style);
andrewboyson 0:f8998d10763e 29 HttpAddText("' type='text/css'/>\r\n");
andrewboyson 0:f8998d10763e 30 }
andrewboyson 0:f8998d10763e 31 if (script)
andrewboyson 0:f8998d10763e 32 {
andrewboyson 0:f8998d10763e 33 HttpAddText(" <script src='/");
andrewboyson 0:f8998d10763e 34 HttpAddText(script);
andrewboyson 0:f8998d10763e 35 HttpAddText("' type='text/javascript'></script>\r\n");
andrewboyson 0:f8998d10763e 36 }
andrewboyson 0:f8998d10763e 37 HttpAddText(" <meta name='viewport' content='width=device-width, initial-scale=1'>\r\n"
andrewboyson 0:f8998d10763e 38 " <link rel='icon' href='/favicon.ico' type='image/x-icon'/>\r\n"
andrewboyson 0:f8998d10763e 39 "</head>\r\n"
andrewboyson 0:f8998d10763e 40 "<body>\r\n");
andrewboyson 0:f8998d10763e 41
andrewboyson 0:f8998d10763e 42 }
andrewboyson 12:237a0f75b4d0 43 void PageAddH1(const char* site, const char* pageName)
andrewboyson 12:237a0f75b4d0 44 {
andrewboyson 12:237a0f75b4d0 45 HttpAddText("<h1>");
andrewboyson 12:237a0f75b4d0 46 HttpAddText(site);
andrewboyson 12:237a0f75b4d0 47 HttpAddText(" - ");
andrewboyson 12:237a0f75b4d0 48 HttpAddText(pageName);
andrewboyson 12:237a0f75b4d0 49 HttpAddText("</h1>\r\n");
andrewboyson 12:237a0f75b4d0 50 }
andrewboyson 12:237a0f75b4d0 51 void PageAddH2(const char* text)
andrewboyson 12:237a0f75b4d0 52 {
andrewboyson 12:237a0f75b4d0 53 HttpAddText("<h2>");
andrewboyson 12:237a0f75b4d0 54 HttpAddText(text);
andrewboyson 12:237a0f75b4d0 55 HttpAddText("</h2>\r\n");
andrewboyson 12:237a0f75b4d0 56 }
andrewboyson 12:237a0f75b4d0 57 void PageAddEnd()
andrewboyson 12:237a0f75b4d0 58 {
andrewboyson 12:237a0f75b4d0 59 HttpAddText("</body>\r\n"
andrewboyson 12:237a0f75b4d0 60 "</html>\r\n");
andrewboyson 12:237a0f75b4d0 61 }
andrewboyson 0:f8998d10763e 62
andrewboyson 0:f8998d10763e 63 void PageAddNavItem(int highlight, char* href, char* title)
andrewboyson 0:f8998d10763e 64 {
andrewboyson 0:f8998d10763e 65 char *p;
andrewboyson 0:f8998d10763e 66 HttpAddText("<li ");
andrewboyson 0:f8998d10763e 67 if (highlight) p = "class='this'";
andrewboyson 0:f8998d10763e 68 else p = " ";
andrewboyson 0:f8998d10763e 69 HttpAddText(p);
andrewboyson 0:f8998d10763e 70 HttpAddText("><a href='");
andrewboyson 0:f8998d10763e 71 HttpAddText(href);
andrewboyson 0:f8998d10763e 72 HttpAddText("'>");
andrewboyson 0:f8998d10763e 73 HttpAddText(title);
andrewboyson 0:f8998d10763e 74 HttpAddText("</a></li>\r\n");
andrewboyson 0:f8998d10763e 75
andrewboyson 0:f8998d10763e 76 }
andrewboyson 0:f8998d10763e 77
andrewboyson 0:f8998d10763e 78 void PageAddLabelledValue(char* label, float labelwidth, char* value)
andrewboyson 0:f8998d10763e 79 {
andrewboyson 0:f8998d10763e 80 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 81 HttpAddText(label);
andrewboyson 0:f8998d10763e 82 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 83 HttpAddText(value);
andrewboyson 0:f8998d10763e 84 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 85 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 86 }
andrewboyson 0:f8998d10763e 87
andrewboyson 0:f8998d10763e 88 void PageAddLabelledMac(char* label, float labelwidth, char* mac)
andrewboyson 0:f8998d10763e 89 {
andrewboyson 0:f8998d10763e 90 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 91 HttpAddText(label);
andrewboyson 0:f8998d10763e 92 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 93 MacHttp (mac);
andrewboyson 0:f8998d10763e 94 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 95 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 96 }
andrewboyson 0:f8998d10763e 97
andrewboyson 0:f8998d10763e 98 void PageAddLabelledIp4(char* label, float labelwidth, uint32_t ip)
andrewboyson 0:f8998d10763e 99 {
andrewboyson 0:f8998d10763e 100 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 101 HttpAddText(label);
andrewboyson 0:f8998d10763e 102 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 103 Ip4AddressHttp (ip);
andrewboyson 0:f8998d10763e 104 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 105 HttpAddText ("</div>\r\n");
andrewboyson 0:f8998d10763e 106 }
andrewboyson 0:f8998d10763e 107
andrewboyson 0:f8998d10763e 108 void PageAddLabelledIp6(char* label, float labelwidth, char* ip)
andrewboyson 0:f8998d10763e 109 {
andrewboyson 0:f8998d10763e 110 HttpAddF ("<div style='width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 111 HttpAddText(label);
andrewboyson 0:f8998d10763e 112 HttpAddText("<span style='float:right;'>");
andrewboyson 0:f8998d10763e 113 Ip6AddressHttp (ip);
andrewboyson 0:f8998d10763e 114 HttpAddText("</span>");
andrewboyson 0:f8998d10763e 115 HttpAddText ("</div>\r\n");
andrewboyson 0:f8998d10763e 116 }
andrewboyson 0:f8998d10763e 117 void PageAddLabelledName(char* label, float labelwidth, char* name, char* suffix)
andrewboyson 0:f8998d10763e 118 {
andrewboyson 0:f8998d10763e 119 char text[100];
andrewboyson 0:f8998d10763e 120 HttpAddText("<div>");
andrewboyson 0:f8998d10763e 121 snprintf(text, sizeof(text), "<div style='width:%.1fem; display:inline-block;'>", labelwidth);
andrewboyson 0:f8998d10763e 122 HttpAddText(text);
andrewboyson 0:f8998d10763e 123 HttpAddText(label);
andrewboyson 0:f8998d10763e 124 HttpAddText("</div><span id='");
andrewboyson 0:f8998d10763e 125 HttpAddText(name);
andrewboyson 0:f8998d10763e 126 HttpAddText("'></span>");
andrewboyson 0:f8998d10763e 127 HttpAddText(suffix);
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 0:f8998d10763e 169 void PageAddCheckInput(char* action, char* label, char* name, char* button)
andrewboyson 0:f8998d10763e 170 {
andrewboyson 0:f8998d10763e 171 HttpAddText("<form action='"); HttpAddText(action); HttpAddText("' method='get'>\r\n");
andrewboyson 0:f8998d10763e 172 HttpAddText("<input type='hidden' name='"); HttpAddText(name); HttpAddText("'>\r\n");
andrewboyson 0:f8998d10763e 173 HttpAddText(label); HttpAddText("\r\n");
andrewboyson 0:f8998d10763e 174 HttpAddText("<input type='submit' value='"); HttpAddText(button); HttpAddText("'>\r\n");
andrewboyson 0:f8998d10763e 175 HttpAddText("</form>\r\n");
andrewboyson 0:f8998d10763e 176 }
andrewboyson 0:f8998d10763e 177 void PageAddTm(struct tm* ptm)
andrewboyson 0:f8998d10763e 178 {
andrewboyson 0:f8998d10763e 179 char text[30];
andrewboyson 0:f8998d10763e 180 snprintf(text, sizeof(text), "%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
andrewboyson 0:f8998d10763e 181 HttpAddText(text);
andrewboyson 0:f8998d10763e 182 switch(ptm->tm_wday)
andrewboyson 0:f8998d10763e 183 {
andrewboyson 0:f8998d10763e 184 case 0: HttpAddText("Sun"); break;
andrewboyson 0:f8998d10763e 185 case 1: HttpAddText("Mon"); break;
andrewboyson 0:f8998d10763e 186 case 2: HttpAddText("Tue"); break;
andrewboyson 0:f8998d10763e 187 case 3: HttpAddText("Wed"); break;
andrewboyson 0:f8998d10763e 188 case 4: HttpAddText("Thu"); break;
andrewboyson 0:f8998d10763e 189 case 5: HttpAddText("Fri"); break;
andrewboyson 0:f8998d10763e 190 case 6: HttpAddText("Sat"); break;
andrewboyson 0:f8998d10763e 191 default: HttpAddText("???"); break;
andrewboyson 0:f8998d10763e 192 }
andrewboyson 0:f8998d10763e 193 snprintf(text, sizeof(text), " %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
andrewboyson 0:f8998d10763e 194 HttpAddText(text);
andrewboyson 0:f8998d10763e 195 if (ptm->tm_isdst > 0) HttpAddText(" BST");
andrewboyson 0:f8998d10763e 196 else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
andrewboyson 0:f8998d10763e 197 else HttpAddText(" UTC");
andrewboyson 0:f8998d10763e 198 }
andrewboyson 0:f8998d10763e 199 void PageAddAjaxToggle(float labelwidth, char* label, char* id, char* request)
andrewboyson 0:f8998d10763e 200 {
andrewboyson 0:f8998d10763e 201 char text[100];
andrewboyson 0:f8998d10763e 202
andrewboyson 0:f8998d10763e 203 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 204 HttpAddText(text);
andrewboyson 0:f8998d10763e 205 HttpAddText(label);
andrewboyson 0:f8998d10763e 206 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 207
andrewboyson 0:f8998d10763e 208 HttpAddText("<div class='toggle' id='");
andrewboyson 0:f8998d10763e 209 HttpAddText(id);
andrewboyson 0:f8998d10763e 210 HttpAddText("' dir='ltr' onclick='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 211 HttpAddText(request);
andrewboyson 0:f8998d10763e 212 HttpAddText("=1\")'>\r\n");
andrewboyson 0:f8998d10763e 213
andrewboyson 0:f8998d10763e 214 HttpAddText("<div class='slot'></div><div class='knob'></div>\r\n");
andrewboyson 0:f8998d10763e 215
andrewboyson 0:f8998d10763e 216 HttpAddText("</div></div>\r\n");
andrewboyson 0:f8998d10763e 217 }
andrewboyson 0:f8998d10763e 218 void PageAddAjaxHex(float labelwidth, char* label, float inputwidth, char* id, char* request)
andrewboyson 0:f8998d10763e 219 {
andrewboyson 0:f8998d10763e 220 char text[100];
andrewboyson 0:f8998d10763e 221
andrewboyson 0:f8998d10763e 222 snprintf(text, sizeof(text), "<div><div style='display: inline-block; width:%.1fem;'>", labelwidth);
andrewboyson 0:f8998d10763e 223 HttpAddText(text);
andrewboyson 0:f8998d10763e 224 HttpAddText(label);
andrewboyson 0:f8998d10763e 225 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 226
andrewboyson 0:f8998d10763e 227 HttpAddText("<input type='text' id='");
andrewboyson 0:f8998d10763e 228 HttpAddText(id);
andrewboyson 0:f8998d10763e 229 HttpAddText("' style='width:");
andrewboyson 0:f8998d10763e 230 sprintf(text, "%.1fem;", inputwidth);
andrewboyson 0:f8998d10763e 231 HttpAddText(text);
andrewboyson 0:f8998d10763e 232 HttpAddText("' onchange='AjaxRequest(\"");
andrewboyson 0:f8998d10763e 233 HttpAddText(request);
andrewboyson 0:f8998d10763e 234 HttpAddText("=\" + this.value)'>\r\n");
andrewboyson 0:f8998d10763e 235
andrewboyson 0:f8998d10763e 236 HttpAddText("</div>\r\n");
andrewboyson 0:f8998d10763e 237 }
andrewboyson 0:f8998d10763e 238
andrewboyson 0:f8998d10763e 239