Measure system

Dependencies:   EthernetNetIf mbed RF12B

Revision:
2:afe5826411e3
Parent:
1:b26ab2467b1a
Child:
3:799d8c61fb03
--- a/server/impl/SimpleHandler.cpp	Thu Mar 10 19:56:45 2011 +0000
+++ b/server/impl/SimpleHandler.cpp	Sun Mar 27 07:56:59 2011 +0000
@@ -28,6 +28,8 @@
 extern DS1820* probe[2];
 extern int devices_found;
 
+#define CHUNK_SIZE 128
+
 //#define __DEBUG
 //#include "dbg/dbg.h"
 
@@ -40,83 +42,27 @@
 
 void SimpleHandler::doGet() {
     printf("2-SIMPLE-In SimpleHandler::doGet()\r\n");
-
+    printf("rootpath: %s  path: %s\r\n",rootPath().c_str(),path().c_str());
     if (path().compare("/currentdata") == 0) {
-        char resp[32] = "Temps";
-        char temp[32] ;
-        probe[0]->convert_temperature(DS1820::all_devices);
-        for (int i=0; i<devices_found; i++) {
-            sprintf(temp," - %3.1f",(probe[i]->temperature('c')));
-            strcat(resp,temp);
-        }
-        sprintf( temp , "\r\n");
-        strcat(resp,temp);
-
-        setContentLen( strlen(resp) );
-        respHeaders()["Connection"] = "close";
-        writeData(resp, strlen(resp));
+        CurrentData();
     } else
         if (path().compare("/currenthtml") == 0) {
-        char temp[1000];
-
- printf("2-SIMPLE- Generating html code\r\n");
-        
-string htmltext ("");
-htmltext +="<html>";
-htmltext +="<head>";
-htmltext += "<title>mbed Measure System</title>";
-htmltext +="</head>";
-htmltext +="<body bgcolor=""#0000FF"" text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">";
-htmltext +="<div align=""center"">";
-htmltext +="<h1>Current values</h1>";
-
-printf("2-SIMPLE- Reading temps\r\n");
-
-        probe[0]->convert_temperature(DS1820::all_devices);
-        for (int i=0; i<devices_found; i++) {
-            sprintf(temp,"%d.)  %3.1f C",(i+1),(probe[i]->temperature('c')));
-            htmltext +="<h1>";
-            htmltext += temp;
-            htmltext +="</h1>";
-        }
-
-printf("2-SIMPLE- Generating html code\r\n");
+            CurrentHtml();
+        } else if (path().compare("/reset") == 0) {
+        ResetMe();
+        } else if (path().compare("/deletelogs") == 0) {
+            DeleteLogs();
+        } else if (path().compare("/deletesyslog") == 0) {
+            DeleteSysLog();
+        }   else {
+            if (0 ==path().find("/interval/")) {
+                SetInterval();
+            } else if (0 ==path().find("/deletelog/")) {
+                DeleteLog();
+            } else {
+                InvalidCommand();
+            }
 
-htmltext +="<h1><a href=""javascript:location.reload(true)"">Refresh</a></h1>";
-htmltext +="<h1><a href=""javascript:history.back()"">Return</a></h1>";
-htmltext +="</div>";
-htmltext +="</body>";
-htmltext +="</html>";
-
-strcpy(temp, htmltext.c_str());
-            setContentLen( strlen(temp) );
-            respHeaders()["Connection"] = "close";
-            writeData(temp, strlen(temp));
-            
-        }
-        else if (path().compare("/logs") == 0) {
-        char temp[1000];
-        string htmltext ("");
-htmltext +="<html>\r\n";
-htmltext +="<head>\r\n";
-htmltext +="</head>\r\n";
-htmltext +="<body bgcolor=""#0000FF"" ";
-htmltext += "text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">\r\n";
-htmltext +="<h2>Logged measures</h2>\r\n";
-htmltext +="</body>\r\n";
-htmltext +="</html>\r\n";
-        
-        strcpy(temp, htmltext.c_str());
-            setContentLen( strlen(temp) );
-            respHeaders()["Connection"] = "close";
-            writeData(temp, strlen(temp));
-            
-        }
-        else {
-            char temp[] = "Invalid com command.";
-            setContentLen( strlen(temp) );
-            respHeaders()["Connection"] = "close";
-            writeData(temp, strlen(temp));
         }
 
 
@@ -124,54 +70,72 @@
 }
 
 void SimpleHandler::doPost() {
-printf("2-SIMPLE-In SimpleHandler::doPost() STARTED\r\n");
+    printf("2-SIMPLE-In SimpleHandler::doPost() STARTED\r\n");
+
+    int res=-1;
+    char data[dataLen()+1];
+    res = readData(data,dataLen());
+    data[dataLen()] = 0;
+    printf("2-SIMPLE-<Data received: %d  |  %d>\r\n%s\r\n2-SIMPLE-<END>\r\n",dataLen(),res,data);
+    if (res == dataLen()) {
+        postOK = 1;
+    } else {
+        postOK = 0;
+    }
 
     if (path().compare("/start") == 0) {
-    MeasureStart();
-    }
-    else     if (path().compare("/stop") == 0) {
-    printf("CALLING MEASURE STOP\r\n");
-    MeasureStop();
-    }
-    else
-    {
-    printf ("PATH = %s\r\n ",path().c_str());
-    printf ("INVALID POST COMMAND\r\n");
+        MeasureStart();
+    } else     if (path().compare("/stop") == 0) {
+        printf("CALLING MEASURE STOP\r\n");
+        MeasureStop();
+    } else     if (path().compare("/saveconfig") == 0 & postOK==1) {
+        printf("CALLING CONFIG SAVER\r\n");
+        ReceiveConfig(data);
+    } else     if (path().compare("/login") == 0 & postOK==1) {
+        printf("CALLING USER CHECK\r\n");
+        Login(data);
+        return;
+    } else {
+        printf ("PATH = %s\r\n ",path().c_str());
+        printf ("INVALID POST COMMAND\r\n");
     }
-   
-string htmltext ("");
-htmltext +="<html>\r\n";
-htmltext +="<head>\r\n";
-htmltext +="</head>\r\n";
-htmltext +="<body bgcolor=""#0000FF"" ";
-htmltext += "text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">\r\n";
-htmltext +="<h2>Command received.</h2>\r\n";
-htmltext +="<script type=""text/javascript"">\r\n";
-htmltext += "setTimeout(""history.back()"",1500);";
-htmltext +="</script>\r\n";
-htmltext +="</body>\r\n";
-htmltext +="</html>\r\n";
 
-printf("2-SIMPLE-In SimpleHandler::doPost() htmltext done\r\n");
-
-char temp[300];
-strcpy(temp, htmltext.c_str());
+    string htmltext ("");
+    htmltext +="<html>\r\n";
+    htmltext +="<head>\r\n";
+    htmltext +="</head>\r\n";
+    htmltext +="<body bgcolor=""#0000FF"" ";
+    htmltext += "text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">\r\n";
+    if (postOK ==1) {
+        htmltext +="<h2>Post received.</h2>\r\n";
+    } else {
+        htmltext +="<h2>Post not received.</h2>\r\n";
+    }
+    htmltext +="<script type=""text/javascript"">\r\n";
+    htmltext += "setTimeout(""history.back()"",1500);";
+    htmltext +="</script>\r\n";
+    htmltext +="</body>\r\n";
+    htmltext +="</html>\r\n";
 
-            setContentLen( strlen(temp) );
-            respHeaders()["Connection"] = "close";
-            writeData(temp, strlen(temp));
-printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n");
+    printf("2-SIMPLE-In SimpleHandler::doPost() htmltext done\r\n");
+
+    char temp[350];
+    strcpy(temp, htmltext.c_str());
+
+    setContentLen( strlen(temp) );
+    respHeaders()["Connection"] = "close";
+    writeData(temp, strlen(temp));
+    printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n");
+}
+
+void SimpleHandler::onReadable() {
+
 }
 
 void SimpleHandler::doHead() {
 
 }
 
-
-void SimpleHandler::onReadable() { //Data has been read
-
-}
-
 void SimpleHandler::onWriteable() { //Data has been written & buf is free
     printf("2-SIMPLE-SimpleHandler::onWriteable() event\r\n");
     close(); //Data written, we can close the connection
@@ -180,3 +144,192 @@
 void SimpleHandler::onClose() { //Connection is closing
     //Nothing to do
 }
+
+
+//----------------------COM HANDLERS--------------------------------
+
+//-----------------------RESET ME-----------------------------------
+void SimpleHandler::ResetMe() {
+reset = true;
+    char temp[] = "Reset command accepted.";
+    setContentLen( strlen(temp) );
+    respHeaders()["Connection"] = "close";
+    writeData(temp, strlen(temp));
+}
+
+//--------------------------CURRENT DATA----------------------------
+void SimpleHandler::CurrentData() {
+    char resp[100] = "";
+    char temp[100] ;
+
+    char buf[50];
+    ctTime = time(NULL);
+    ctTime += (clockoffset*3600);
+    strftime(buf,sizeof(buf), "%H:%M:%S\r\n", localtime(&ctTime));
+    strcat(resp,buf);
+
+    probe[0]->convert_temperature(DS1820::all_devices);
+    for (int i=0; i<devices_found; i++) {
+        sprintf(temp,"%d.)%3.1fC  ",(i+1),(probe[i]->temperature('c')));
+        strcat(resp,temp);
+    }
+
+    setContentLen( strlen(resp) );
+    respHeaders()["Connection"] = "close";
+    writeData(resp, strlen(resp));
+}
+//--------------------------CURRENT HTML----------------------------
+void SimpleHandler::CurrentHtml() {
+    char temp[1000];
+    printf("2-SIMPLE- Generating html code\r\n");
+
+    string htmltext ("");
+    htmltext +="<html>";
+    htmltext +="<head>";
+    htmltext += "<title>mbed Measure System</title>";
+    htmltext +="</head>";
+    htmltext +="<body bgcolor=""#0000FF"" text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">";
+    htmltext +="<div align=""center"">";
+    htmltext +="<h1>Current values</h1>";
+
+    printf("2-SIMPLE- Reading temps\r\n");
+
+    probe[0]->convert_temperature(DS1820::all_devices);
+    for (int i=0; i<devices_found; i++) {
+        sprintf(temp,"%d.)  %3.1f C",(i+1),(probe[i]->temperature('c')));
+        htmltext +="<h1>";
+        htmltext += temp;
+        htmltext +="</h1>";
+    }
+
+    printf("2-SIMPLE- Generating html code\r\n");
+
+    htmltext +="<h1><a href=""javascript:location.reload(true)"">Refresh</a></h1>";
+    htmltext +="<h1><a href=""javascript:history.back()"">Return</a></h1>";
+    htmltext +="</div>";
+    htmltext +="</body>";
+    htmltext +="</html>";
+
+    strcpy(temp, htmltext.c_str());
+    setContentLen( strlen(temp) );
+    respHeaders()["Connection"] = "close";
+    writeData(temp, strlen(temp));
+}
+//--------------------INVALID COMMAND-----------------------
+void SimpleHandler::InvalidCommand() {
+    char temp[] = "Invalid com command.";
+    setContentLen( strlen(temp) );
+    respHeaders()["Connection"] = "close";
+    writeData(temp, strlen(temp));
+}
+//-----------------------READY------------------------------------
+void SimpleHandler::Response(char *input) {
+    setContentLen( strlen(input) );
+    respHeaders()["Connection"] = "close";
+    writeData(input, strlen(input));
+}
+//-------------------DELETE LOGS-------------------------------------
+void SimpleHandler::DeleteLogs() {
+    DIR *d = opendir(DATA_FOLDER);
+    string file;
+    struct dirent *p;
+    printf("\nList of files in the directory %s:\r\n", DATA_FOLDER);
+    while ( (p = readdir(d)) != NULL ) {
+
+        file = "";
+        file +=DATA_FOLDER;
+        file += "/";
+        file +=  p->d_name;
+        printf("Deleting - %s\r\n", file.c_str());
+        if (logging==1) {
+            if (file.find(logfile)==0) {
+                continue;
+            }
+        }
+        remove( file.c_str() );
+    }
+    closedir(d);
+    Response("Done");
+}
+//-------------------------Delete single log file----------------------
+void SimpleHandler::DeleteLog() {
+    string text = path();
+    string text2 = DATA_FOLDER;
+    text2 += text.substr (10);
+
+    if (logging==1) {
+        if (text2.compare(logfile)==0) {
+            Response("CantDelete logging file.");
+            return;
+        }
+    }
+
+    printf("Deleting log: %s\r\n",text2.c_str());
+    remove (text2.c_str());
+    Response("Done");
+}
+//-------------------------Delete system log file----------------------
+void SimpleHandler::DeleteSysLog() {
+    remove (LOGGER_FILE);
+    LogWrite("");
+    Response("Done");
+}
+
+
+//----------------------SET INTERVAL------------------------------
+void SimpleHandler::SetInterval() {
+    string text = path();
+    string text2 = text.substr (10);
+    printf("Interval value: %s\r\n",text2.c_str());
+
+    if (0!= strlen(text2.c_str())) {
+        int i =atoi(text2.c_str());
+        if (i!=0) {
+            interval = i;
+            SaveConfig();
+        }
+    }
+    Response("Done");
+}
+//-----------------------RECEIVE CONFIG-----------------------------
+void SimpleHandler::ReceiveConfig(char* data) {
+//---Save config--------
+    FILE *fs_src;
+    fs_src    = fopen( CONFIG_FILE, "w" );
+    fprintf(fs_src,"%s",data);
+    fclose( fs_src );
+//---Load new config----
+    LoadConfig();
+}
+//----------------------LOGIN CHECK---------------------------------
+void  SimpleHandler::Login(char* data) {
+    bool ok = false;
+    char temp[5];
+
+    if (strlen(data)>7) {
+        char    buffer[50 ];
+        string str ="";
+        FILE *fs_src;
+        fs_src    = fopen( LOGIN_FILE, "r" );
+        while (fgets(buffer, 100, fs_src)) {
+            str = buffer;
+            printf("Storedlogin:(%s)\r\n",buffer);
+            if (str.find(data)==0) {
+                ok = true;
+            }
+        }
+
+        fclose( fs_src );
+    }
+
+    if (ok) {
+        strcpy(temp, "OK");
+    } else {
+        strcpy(temp, "NO");
+    }
+
+    setContentLen( strlen(temp) );
+    respHeaders()["Connection"] = "close";
+    writeData(temp, strlen(temp));
+    printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n");
+}
\ No newline at end of file