Everything works except AMPM and snooze

Dependencies:   4DGL-uLCD-SE EthernetInterface HTTPClient NTPClient PinDetect SDFileSystem mbed-rpc mbed-rtos mbed wave_player

Fork of ECE4180_FinalProject by Paul Rabbat

Revision:
0:0a99e3fc2a46
Child:
1:d7f65be2640d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Formatter.cpp	Sat Nov 15 19:36:44 2014 +0000
@@ -0,0 +1,345 @@
+#include "Formatter.h"
+#include "mbed.h"
+#include "RPCObjectManager.h"
+#include "EthernetInterface.h"
+#include "alarmContainer.h"
+#include <string>
+
+/* itoa:  convert n to characters in s */
+void itoa( unsigned long long int value, char *str)
+{   
+   int i,j;
+   char temp[30];
+   for(i=0; value > 0; i++){    
+       str[i] = value%10+'0';
+       value=value/10;
+    }
+    for(j=0;i>=0;j++,i--){
+        temp[j]=str[i-1];
+    }
+    for(i=0;i<j;i++){
+        str[i]=temp[i];
+    }   
+}
+
+const char *SIMPLE_HTML_CODE = "\
+<!DOCTYPE html>\
+<html>\
+<head>\
+<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
+<title>Your AC</title>\
+</head>\
+ <body>";
+
+
+const char* INTERACTIVE_HTML_CODE_1 = "\
+<!DOCTYPE html> \
+<html>\
+<head>\
+<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
+<title>Your Alarm Clock</title>\
+<script type=\"text/javascript\">\
+var ip = \"%s\";\
+function submitCreateForm()\
+{\
+var url = \"http://\" + ip + \"/Alarm/\" + \"new?name=Alarm\";\
+location.href= url;\
+}\
+function submitCallFuncForm()\
+{\
+var command = document.getElementById(\"command\").value;\
+if(command === \"\") \
+return; \
+var tmp = command.split(\' \');\
+var url = tmp[0];\
+if(tmp.length > 1)\
+url += \"?\";\
+for(var i = 1; i < tmp.length; ++i)\
+{\
+url += \"arg\" + i + \"=\" + tmp[i];\
+if(i+1 < tmp.length)\
+url += \"&\";\
+}\
+location.href = url;\
+}\
+function submitAlarmPreferences()\
+{\
+var h = document.getElementById(\"hour\").value;\
+var m = document.getElementById(\"minute\").value;\
+var a = document.getElementById(\"ampm\").value;\
+var url = \"/Alarm/updateAlarmPreference?arg1=\" + h + \"&arg2=\" + m + \"&arg2=\" + a;\
+location.href = url;\
+}\
+function setAlarm()\
+{\
+var url = \"/Alarm/setAlarm\";\
+location.href = url;\
+}\
+</script>\
+</head> \
+<body>";
+    
+const char* INTERACTIVE_HTML_CODE_2 = "</body> \
+</html>";
+
+static char chunk[1024];
+        
+Formatter::Formatter(int nb):
+currentChunk(0),
+nbChunk(nb)
+{
+}    
+
+char* Formatter::get_page(char *reply)
+{
+    chunk[0] = '\0';
+
+    if(currentChunk < nbChunk)
+    {
+        get_chunk(currentChunk, reply);
+        currentChunk++;
+    }
+    else
+        currentChunk = 0;
+    
+    return chunk;
+}    
+
+void Formatter::get_chunk(const int c, char *reply)
+{
+    strcat(chunk, reply);
+}
+
+SimpleHTMLFormatter::SimpleHTMLFormatter():
+Formatter()
+{
+}
+
+void SimpleHTMLFormatter::get_chunk(const int c, char* reply)
+{
+    strcat(chunk, SIMPLE_HTML_CODE);
+    
+    if(reply != NULL && strlen(reply) != 0)
+    {
+        strcat(chunk, "RPC reply : ");
+        strcat(chunk, reply);
+    }
+        
+    if(!RPCObjectManager::instance().is_empty())
+    {
+        strcat(chunk, "<ul>");
+        for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
+            itor != RPCObjectManager::instance().end();
+            ++itor)
+        {
+            strcat(chunk, "<li>");
+            strcat(chunk, *itor);
+            strcat(chunk, "</li>");
+        }
+        strcat(chunk, "</ul>");
+    }
+    
+    strcat(chunk, "</body></html>");
+}
+
+InteractiveHTMLFormatter::InteractiveHTMLFormatter():
+Formatter(3)
+{
+}
+
+void InteractiveHTMLFormatter::get_chunk(const int c, char *reply)
+{
+    int alarmCreated = 0;
+    
+    if(c == 0)
+        sprintf(chunk, INTERACTIVE_HTML_CODE_1, EthernetInterface::getIPAddress());
+
+    else if(c == 1)
+    {
+        if(reply != NULL && strlen(reply) != 0)
+        {
+            strcat(chunk, "RPC reply : ");
+            strcat(chunk, reply);
+        }
+        if(!RPCObjectManager::instance().is_empty())
+        {
+            alarmCreated = 1;
+            
+            strcat(chunk, "<p>Objects created :</p>");
+
+            strcat(chunk, "<ul>");
+            for(std::list<char*>::iterator itor = RPCObjectManager::instance().begin();
+                itor != RPCObjectManager::instance().end();
+                ++itor)
+            {
+                strcat(chunk, "<li>");
+                strcat(chunk, *itor);
+                strcat(chunk, " (<a href=\"http://");
+                strcat(chunk, EthernetInterface::getIPAddress());
+                strcat(chunk, "/");
+                strcat(chunk, *itor);
+                strcat(chunk, "/delete\">delete</a>)");
+                strcat(chunk, "</li>");
+            }
+            strcat(chunk, "</ul>");
+        }
+        strcat(chunk, " ");
+        
+        if(!alarmCreated)
+        {
+            strcat(chunk, "<p><input type=\"button\" value=\"Create Alarm\" onclick=\"javascript:submitCreateForm();\"></p>");
+        }
+        else
+        {
+            strcat(chunk, "<h3>Set Your Alarm:</h3>");
+            strcat(chunk, "<form>");
+            
+            /******* HOUR *******/
+            strcat(chunk, "<select id=\"hour\">");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 1){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"1\">1</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 2){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"2\">2</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 3){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"3\">3</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 4){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"4\">4</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 5){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"5\">5</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 6){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"6\">6</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 7){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"7\">7</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 8){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"8\">8</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 9){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"9\">9</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 10){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"10\">10</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 11){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"11\">11</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.hours == 12 || _alarm.hours == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"12\">12</option>");
+            strcat(chunk, "</select>");
+            
+            strcat(chunk, ":");
+            
+          
+            /******* MINUTE *******/  
+            strcat(chunk, "<select id=\"minute\">");
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"0\">00</option>");
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 1){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"1\">01</option>");
+            strcat(chunk, "</select>");
+            
+            /******* AM PM *******/
+            strcat(chunk, "<select id=\"ampm\">");
+            strcat(chunk, "<option ");
+            if(strcmp(_alarm.amPm,"AM") == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"0\">AM</option>");
+            
+            strcat(chunk, "<option ");
+            if(strcmp(_alarm.amPm,"PM") == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"1\">PM</option>");
+        
+            strcat(chunk, "</select>");
+            
+            
+            
+
+           /* strcat(chunk, "<select id=\"minute\">");
+                        
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 1){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"1\">1</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 2){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"2\">2</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 3){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"3\">3</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 4){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"4\">4</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 5){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"5\">5</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 6){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"6\">6</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 7){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"7\">7</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 8){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"8\">8</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 9){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"9\">9</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 10){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"10\">10</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 11){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"11\">11</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.minutes == 12 || _alarm.minutes == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"12\">12</option>");
+
+
+            strcat(chunk, "</select>");
+            */
+            
+           
+            
+            
+          /*  strcat(chunk, "<select id=\"ampm\">");
+            strcat(chunk, "<option ");
+            if(_alarm.amPm == 0){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"0\">AM</option>");
+            
+            strcat(chunk, "<option ");
+            if(_alarm.amPm == 1){ strcat(chunk, "selected=\"selected\""); }
+            strcat(chunk, "value=\"1\">PM</option>");
+            
+            strcat(chunk, "</select>");*/
+            
+            
+            //strcat(chunk, "AMPM: <input type= \"text\" id=\"ampm\" maxlength=127>");  
+            strcat(chunk, "<br><p>");
+            strcat(chunk, "<input type=\"button\" value=\"Save\" onclick=\"javascript:submitAlarmPreferences();\">");
+            //strcat(chunk, "</p><br><p>");
+            strcat(chunk, "<input type=\"button\" value=\"Set\" onclick=\"javascript:setAlarm();\">");
+            strcat(chunk, "</p><br></form>");
+            //strcat(chunk, "</form>");   
+        }
+    }
+    else if(c == 2)
+        strcat(chunk, INTERACTIVE_HTML_CODE_2);
+}