Code written by Paul Rabbat, Angel Daruna, Jarel Hawkins, and Cordel Williams
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient NTPClient PinDetect SDFileSystem mbed-rpc mbed-rtos mbed wave_player
Fork of ECE4180_FinalProject by
Formatter.cpp
00001 #include "Formatter.h" 00002 #include "mbed.h" 00003 #include "RPCObjectManager.h" 00004 #include "EthernetInterface.h" 00005 #include "alarmContainer.h" 00006 #include <string> 00007 00008 const char *SIMPLE_HTML_CODE = "\ 00009 <!DOCTYPE html>\ 00010 <html>\ 00011 <head>\ 00012 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ 00013 <title>Your AC</title>\ 00014 </head>\ 00015 <body>"; 00016 00017 00018 const char* INTERACTIVE_HTML_CODE_1 = "\ 00019 <!DOCTYPE html> \ 00020 <html>\ 00021 <head>\ 00022 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\ 00023 <title>Your Alarm Clock</title>\ 00024 <script type=\"text/javascript\">\ 00025 var ip = \"%s\";\ 00026 function submitCreateForm()\ 00027 {\ 00028 var url = \"http://\" + ip + \"/Alarm/\" + \"new?name=Alarm\";\ 00029 location.href= url;\ 00030 }\ 00031 function submitCallFuncForm()\ 00032 {\ 00033 var command = document.getElementById(\"command\").value;\ 00034 if(command === \"\") \ 00035 return; \ 00036 var tmp = command.split(\' \');\ 00037 var url = tmp[0];\ 00038 if(tmp.length > 1)\ 00039 url += \"?\";\ 00040 for(var i = 1; i < tmp.length; ++i)\ 00041 {\ 00042 url += \"arg\" + i + \"=\" + tmp[i];\ 00043 if(i+1 < tmp.length)\ 00044 url += \"&\";\ 00045 }\ 00046 location.href = url;\ 00047 }\ 00048 function submitAlarmPreferences()\ 00049 {\ 00050 var h = document.getElementById(\"hour\").value;\ 00051 var m = document.getElementById(\"minute\").value;\ 00052 var a = document.getElementById(\"ampm\").value;\ 00053 var url = \"/Alarm/updateAlarmPreference?arg1=\" + h + \"&arg2=\" + m + \"&arg2=\" + a;\ 00054 location.href = url;\ 00055 }\ 00056 function setAlarm()\ 00057 {\ 00058 var url = \"/Alarm/setAlarm\";\ 00059 location.href = url;\ 00060 }\ 00061 </script>\ 00062 </head> \ 00063 <body>"; 00064 00065 const char* INTERACTIVE_HTML_CODE_2 = "</body> \ 00066 </html>"; 00067 00068 static char chunk[4096]; 00069 00070 Formatter::Formatter(int nb): 00071 currentChunk(0), 00072 nbChunk(nb) 00073 { 00074 } 00075 00076 char* Formatter::get_page(char *reply) 00077 { 00078 chunk[0] = '\0'; 00079 00080 if(currentChunk < nbChunk) 00081 { 00082 get_chunk(currentChunk, reply); 00083 currentChunk++; 00084 } 00085 else 00086 currentChunk = 0; 00087 00088 return chunk; 00089 } 00090 00091 void Formatter::get_chunk(const int c, char *reply) 00092 { 00093 strcat(chunk, reply); 00094 } 00095 00096 SimpleHTMLFormatter::SimpleHTMLFormatter(): 00097 Formatter() 00098 { 00099 } 00100 00101 void SimpleHTMLFormatter::get_chunk(const int c, char* reply) 00102 { 00103 strcat(chunk, SIMPLE_HTML_CODE); 00104 00105 if(!RPCObjectManager::instance().is_empty()) 00106 { 00107 strcat(chunk, "<ul>"); 00108 for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin(); 00109 itor != RPCObjectManager::instance().end(); 00110 ++itor) 00111 { 00112 strcat(chunk, "<li>"); 00113 strcat(chunk, *itor); 00114 strcat(chunk, "</li>"); 00115 } 00116 strcat(chunk, "</ul>"); 00117 } 00118 00119 strcat(chunk, "</body></html>"); 00120 } 00121 00122 InteractiveHTMLFormatter::InteractiveHTMLFormatter(): 00123 Formatter(3) 00124 { 00125 } 00126 00127 void InteractiveHTMLFormatter::get_chunk(const int c, char *reply) 00128 { 00129 int alarmCreated = 0; 00130 00131 if(c == 0) 00132 sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress()); 00133 00134 else if(c == 1){ 00135 if(!RPCObjectManager::instance().is_empty()){ 00136 alarmCreated = 1; 00137 } 00138 strcat(chunk, " "); 00139 00140 if(!alarmCreated){ 00141 strcat(chunk, "<p><input type=\"button\" value=\"Create Alarm\" onclick=\"javascript:submitCreateForm();\"></p>"); 00142 }else{ 00143 strcat(chunk, "<h3>Set Your Alarm:</h3>"); 00144 strcat(chunk, "<form>"); 00145 00146 /******* HOUR *******/ 00147 strcat(chunk, "<select id=\"hour\">"); 00148 strcat(chunk, "<option "); 00149 if(_alarm.hours == 1){ strcat(chunk, "selected=\"selected\""); } 00150 strcat(chunk, "value=\"1\">1</option>"); 00151 strcat(chunk, "<option "); 00152 if(_alarm.hours == 2){ strcat(chunk, "selected=\"selected\""); } 00153 strcat(chunk, "value=\"2\">2</option>"); 00154 strcat(chunk, "<option "); 00155 if(_alarm.hours == 3){ strcat(chunk, "selected=\"selected\""); } 00156 strcat(chunk, "value=\"3\">3</option>"); 00157 strcat(chunk, "<option "); 00158 if(_alarm.hours == 4){ strcat(chunk, "selected=\"selected\""); } 00159 strcat(chunk, "value=\"4\">4</option>"); 00160 strcat(chunk, "<option "); 00161 if(_alarm.hours == 5){ strcat(chunk, "selected=\"selected\""); } 00162 strcat(chunk, "value=\"5\">5</option>"); 00163 strcat(chunk, "<option "); 00164 if(_alarm.hours == 6){ strcat(chunk, "selected=\"selected\""); } 00165 strcat(chunk, "value=\"6\">6</option>"); 00166 strcat(chunk, "<option "); 00167 if(_alarm.hours == 7){ strcat(chunk, "selected=\"selected\""); } 00168 strcat(chunk, "value=\"7\">7</option>"); 00169 strcat(chunk, "<option "); 00170 if(_alarm.hours == 8){ strcat(chunk, "selected=\"selected\""); } 00171 strcat(chunk, "value=\"8\">8</option>"); 00172 strcat(chunk, "<option "); 00173 if(_alarm.hours == 9){ strcat(chunk, "selected=\"selected\""); } 00174 strcat(chunk, "value=\"9\">9</option>"); 00175 strcat(chunk, "<option "); 00176 if(_alarm.hours == 10){ strcat(chunk, "selected=\"selected\""); } 00177 strcat(chunk, "value=\"10\">10</option>"); 00178 strcat(chunk, "<option "); 00179 if(_alarm.hours == 11){ strcat(chunk, "selected=\"selected\""); } 00180 strcat(chunk, "value=\"11\">11</option>"); 00181 strcat(chunk, "<option "); 00182 if(_alarm.hours == 12 || _alarm.hours == 0){ strcat(chunk, "selected=\"selected\""); } 00183 strcat(chunk, "value=\"12\">12</option>"); 00184 strcat(chunk, "</select>"); 00185 00186 strcat(chunk, ":"); 00187 00188 /******* MINUTE *******/ 00189 strcat(chunk, "<select id=\"minute\">"); 00190 strcat(chunk, "<option "); 00191 if(_alarm.minutes == 0){ strcat(chunk, "selected=\"selected\""); } 00192 strcat(chunk, "value=\"0\">00</option>"); 00193 strcat(chunk, "<option "); 00194 if(_alarm.minutes == 1){ strcat(chunk, "selected=\"selected\""); } 00195 strcat(chunk, "value=\"1\">01</option>"); 00196 strcat(chunk, "<option "); 00197 if(_alarm.minutes == 2){ strcat(chunk, "selected=\"selected\""); } 00198 strcat(chunk, "value=\"2\">02</option>"); 00199 strcat(chunk, "<option "); 00200 if(_alarm.minutes == 3){ strcat(chunk, "selected=\"selected\""); } 00201 strcat(chunk, "value=\"3\">03</option>"); 00202 strcat(chunk, "<option "); 00203 if(_alarm.minutes == 4){ strcat(chunk, "selected=\"selected\""); } 00204 strcat(chunk, "value=\"4\">04</option>"); 00205 strcat(chunk, "<option "); 00206 if(_alarm.minutes == 5){ strcat(chunk, "selected=\"selected\""); } 00207 strcat(chunk, "value=\"5\">05</option>"); 00208 strcat(chunk, "<option "); 00209 if(_alarm.minutes == 6){ strcat(chunk, "selected=\"selected\""); } 00210 strcat(chunk, "value=\"6\">06</option>"); 00211 strcat(chunk, "<option "); 00212 if(_alarm.minutes == 7){ strcat(chunk, "selected=\"selected\""); } 00213 strcat(chunk, "value=\"7\">07</option>"); 00214 strcat(chunk, "<option "); 00215 if(_alarm.minutes == 8){ strcat(chunk, "selected=\"selected\""); } 00216 strcat(chunk, "value=\"8\">08</option>"); 00217 strcat(chunk, "<option "); 00218 if(_alarm.minutes == 9){ strcat(chunk, "selected=\"selected\""); } 00219 strcat(chunk, "value=\"9\">09</option>"); 00220 00221 strcat(chunk, "<option "); 00222 if(_alarm.minutes == 10){ strcat(chunk, "selected=\"selected\""); } 00223 strcat(chunk, "value=\"10\">10</option>"); 00224 strcat(chunk, "<option "); 00225 if(_alarm.minutes == 11){ strcat(chunk, "selected=\"selected\""); } 00226 strcat(chunk, "value=\"11\">11</option>"); 00227 strcat(chunk, "<option "); 00228 if(_alarm.minutes == 12){ strcat(chunk, "selected=\"selected\""); } 00229 strcat(chunk, "value=\"12\">12</option>"); 00230 strcat(chunk, "<option "); 00231 if(_alarm.minutes == 13){ strcat(chunk, "selected=\"selected\""); } 00232 strcat(chunk, "value=\"13\">13</option>"); 00233 strcat(chunk, "<option "); 00234 if(_alarm.minutes == 14){ strcat(chunk, "selected=\"selected\""); } 00235 strcat(chunk, "value=\"14\">14</option>"); 00236 strcat(chunk, "<option "); 00237 if(_alarm.minutes == 15){ strcat(chunk, "selected=\"selected\""); } 00238 strcat(chunk, "value=\"15\">15</option>"); 00239 strcat(chunk, "<option "); 00240 if(_alarm.minutes == 16){ strcat(chunk, "selected=\"selected\""); } 00241 strcat(chunk, "value=\"16\">16</option>"); 00242 strcat(chunk, "<option "); 00243 if(_alarm.minutes == 17){ strcat(chunk, "selected=\"selected\""); } 00244 strcat(chunk, "value=\"17\">17</option>"); 00245 strcat(chunk, "<option "); 00246 if(_alarm.minutes == 18){ strcat(chunk, "selected=\"selected\""); } 00247 strcat(chunk, "value=\"18\">18</option>"); 00248 strcat(chunk, "<option "); 00249 if(_alarm.minutes == 19){ strcat(chunk, "selected=\"selected\""); } 00250 strcat(chunk, "value=\"19\">19</option>"); 00251 strcat(chunk, "<option "); 00252 if(_alarm.minutes == 20){ strcat(chunk, "selected=\"selected\""); } 00253 strcat(chunk, "value=\"20\">20</option>"); 00254 strcat(chunk, "<option "); 00255 if(_alarm.minutes == 21){ strcat(chunk, "selected=\"selected\""); } 00256 strcat(chunk, "value=\"21\">21</option>"); 00257 strcat(chunk, "<option "); 00258 if(_alarm.minutes == 22){ strcat(chunk, "selected=\"selected\""); } 00259 strcat(chunk, "value=\"22\">22</option>"); 00260 strcat(chunk, "<option "); 00261 if(_alarm.minutes == 23){ strcat(chunk, "selected=\"selected\""); } 00262 strcat(chunk, "value=\"23\">23</option>"); 00263 strcat(chunk, "<option "); 00264 if(_alarm.minutes == 24){ strcat(chunk, "selected=\"selected\""); } 00265 strcat(chunk, "value=\"24\">24</option>"); 00266 strcat(chunk, "<option "); 00267 if(_alarm.minutes == 25){ strcat(chunk, "selected=\"selected\""); } 00268 strcat(chunk, "value=\"25\">25</option>"); 00269 strcat(chunk, "<option "); 00270 if(_alarm.minutes == 26){ strcat(chunk, "selected=\"selected\""); } 00271 strcat(chunk, "value=\"26\">26</option>"); 00272 strcat(chunk, "<option "); 00273 if(_alarm.minutes == 27){ strcat(chunk, "selected=\"selected\""); } 00274 strcat(chunk, "value=\"27\">27</option>"); 00275 strcat(chunk, "<option "); 00276 if(_alarm.minutes == 28){ strcat(chunk, "selected=\"selected\""); } 00277 strcat(chunk, "value=\"28\">28</option>"); 00278 strcat(chunk, "<option "); 00279 if(_alarm.minutes == 29){ strcat(chunk, "selected=\"selected\""); } 00280 strcat(chunk, "value=\"29\">29</option>"); 00281 00282 strcat(chunk, "<option "); 00283 if(_alarm.minutes == 30){ strcat(chunk, "selected=\"selected\""); } 00284 strcat(chunk, "value=\"30\">30</option>"); 00285 strcat(chunk, "<option "); 00286 if(_alarm.minutes == 31){ strcat(chunk, "selected=\"selected\""); } 00287 strcat(chunk, "value=\"31\">31</option>"); 00288 strcat(chunk, "<option "); 00289 if(_alarm.minutes == 32){ strcat(chunk, "selected=\"selected\""); } 00290 strcat(chunk, "value=\"32\">32</option>"); 00291 strcat(chunk, "<option "); 00292 if(_alarm.minutes == 33){ strcat(chunk, "selected=\"selected\""); } 00293 strcat(chunk, "value=\"33\">33</option>"); 00294 strcat(chunk, "<option "); 00295 if(_alarm.minutes == 34){ strcat(chunk, "selected=\"selected\""); } 00296 strcat(chunk, "value=\"34\">34</option>"); 00297 strcat(chunk, "<option "); 00298 if(_alarm.minutes == 35){ strcat(chunk, "selected=\"selected\""); } 00299 strcat(chunk, "value=\"35\">35</option>"); 00300 strcat(chunk, "<option "); 00301 if(_alarm.minutes == 36){ strcat(chunk, "selected=\"selected\""); } 00302 strcat(chunk, "value=\"36\">36</option>"); 00303 strcat(chunk, "<option "); 00304 if(_alarm.minutes == 37){ strcat(chunk, "selected=\"selected\""); } 00305 strcat(chunk, "value=\"37\">37</option>"); 00306 strcat(chunk, "<option "); 00307 if(_alarm.minutes == 38){ strcat(chunk, "selected=\"selected\""); } 00308 strcat(chunk, "value=\"38\">38</option>"); 00309 strcat(chunk, "<option "); 00310 if(_alarm.minutes == 39){ strcat(chunk, "selected=\"selected\""); } 00311 strcat(chunk, "value=\"39\">39</option>"); 00312 strcat(chunk, "<option "); 00313 if(_alarm.minutes == 40){ strcat(chunk, "selected=\"selected\""); } 00314 strcat(chunk, "value=\"40\">40</option>"); 00315 strcat(chunk, "<option "); 00316 if(_alarm.minutes == 41){ strcat(chunk, "selected=\"selected\""); } 00317 strcat(chunk, "value=\"41\">41</option>"); 00318 strcat(chunk, "<option "); 00319 if(_alarm.minutes == 42){ strcat(chunk, "selected=\"selected\""); } 00320 strcat(chunk, "value=\"42\">42</option>"); 00321 strcat(chunk, "<option "); 00322 if(_alarm.minutes == 43){ strcat(chunk, "selected=\"selected\""); } 00323 strcat(chunk, "value=\"43\">43</option>"); 00324 strcat(chunk, "<option "); 00325 if(_alarm.minutes == 44){ strcat(chunk, "selected=\"selected\""); } 00326 strcat(chunk, "value=\"44\">44</option>"); 00327 strcat(chunk, "<option "); 00328 if(_alarm.minutes == 45){ strcat(chunk, "selected=\"selected\""); } 00329 strcat(chunk, "value=\"45\">45</option>"); 00330 strcat(chunk, "<option "); 00331 if(_alarm.minutes == 46){ strcat(chunk, "selected=\"selected\""); } 00332 strcat(chunk, "value=\"46\">46</option>"); 00333 strcat(chunk, "<option "); 00334 if(_alarm.minutes == 47){ strcat(chunk, "selected=\"selected\""); } 00335 strcat(chunk, "value=\"47\">47</option>"); 00336 strcat(chunk, "<option "); 00337 if(_alarm.minutes == 48){ strcat(chunk, "selected=\"selected\""); } 00338 strcat(chunk, "value=\"48\">48</option>"); 00339 strcat(chunk, "<option "); 00340 if(_alarm.minutes == 49){ strcat(chunk, "selected=\"selected\""); } 00341 strcat(chunk, "value=\"49\">49</option>"); 00342 strcat(chunk, "<option "); 00343 if(_alarm.minutes == 50){ strcat(chunk, "selected=\"selected\""); } 00344 strcat(chunk, "value=\"50\">50</option>"); 00345 strcat(chunk, "<option "); 00346 if(_alarm.minutes == 51){ strcat(chunk, "selected=\"selected\""); } 00347 strcat(chunk, "value=\"51\">51</option>"); 00348 strcat(chunk, "<option "); 00349 if(_alarm.minutes == 52){ strcat(chunk, "selected=\"selected\""); } 00350 strcat(chunk, "value=\"52\">52</option>"); 00351 strcat(chunk, "<option "); 00352 if(_alarm.minutes == 53){ strcat(chunk, "selected=\"selected\""); } 00353 strcat(chunk, "value=\"53\">53</option>"); 00354 strcat(chunk, "<option "); 00355 if(_alarm.minutes == 54){ strcat(chunk, "selected=\"selected\""); } 00356 strcat(chunk, "value=\"54\">54</option>"); 00357 strcat(chunk, "<option "); 00358 if(_alarm.minutes == 55){ strcat(chunk, "selected=\"selected\""); } 00359 strcat(chunk, "value=\"55\">55</option>"); 00360 strcat(chunk, "<option "); 00361 if(_alarm.minutes == 56){ strcat(chunk, "selected=\"selected\""); } 00362 strcat(chunk, "value=\"56\">56</option>"); 00363 strcat(chunk, "<option "); 00364 if(_alarm.minutes == 57){ strcat(chunk, "selected=\"selected\""); } 00365 strcat(chunk, "value=\"57\">57</option>"); 00366 strcat(chunk, "<option "); 00367 if(_alarm.minutes == 58){ strcat(chunk, "selected=\"selected\""); } 00368 strcat(chunk, "value=\"58\">58</option>"); 00369 strcat(chunk, "<option "); 00370 if(_alarm.minutes == 59){ strcat(chunk, "selected=\"selected\""); } 00371 strcat(chunk, "value=\"59\">59</option>"); 00372 00373 strcat(chunk, "</select>"); 00374 00375 /******* AM PM *******/ 00376 strcat(chunk, "<select id=\"ampm\">"); 00377 strcat(chunk, "<option "); 00378 if(strcmp(_alarm.amPm,"AM") == 0){ strcat(chunk, "selected=\"selected\""); } 00379 strcat(chunk, "value=\"0\">AM</option>"); 00380 strcat(chunk, "<option "); 00381 if(strcmp(_alarm.amPm,"PM") == 0){ strcat(chunk, "selected=\"selected\""); } 00382 strcat(chunk, "value=\"1\">PM</option>"); 00383 strcat(chunk, "</select>"); 00384 strcat(chunk, "<br><p>"); 00385 strcat(chunk, "<input type=\"button\" value=\"Save\" onclick=\"javascript:submitAlarmPreferences();\">"); 00386 strcat(chunk, "<input type=\"button\" value=\"Set\" onclick=\"javascript:setAlarm();\">"); 00387 strcat(chunk, "</p><br></form>"); 00388 } 00389 } 00390 else if(c == 2) 00391 strcat(chunk, INTERACTIVE_HTML_CODE_2); 00392 }
Generated on Fri Jul 15 2022 01:49:53 by 1.7.2