Measure system

Dependencies:   EthernetNetIf mbed RF12B

server/impl/SimpleHandler.cpp

Committer:
benecsj
Date:
2011-03-10
Revision:
1:b26ab2467b1a
Parent:
0:8d62137f7ff4
Child:
2:afe5826411e3

File content as of revision 1:b26ab2467b1a:


/*
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "MeasureSystem.h"
#include "SimpleHandler.h"
#include "DS1820.h"

extern DS1820* probe[2];
extern int devices_found;

//#define __DEBUG
//#include "dbg/dbg.h"

SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) {}

SimpleHandler::~SimpleHandler() {
    HandlerActive = false;
    printf("2-SIMPLE-Handler destroyed\r\n");
}

void SimpleHandler::doGet() {
    printf("2-SIMPLE-In SimpleHandler::doGet()\r\n");

    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));
    } 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");

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));
        }


    printf("2-SIMPLE-Exit SimpleHandler::doGet()\r\n");
}

void SimpleHandler::doPost() {
printf("2-SIMPLE-In SimpleHandler::doPost() STARTED\r\n");

    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");
    }
   
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());

            setContentLen( strlen(temp) );
            respHeaders()["Connection"] = "close";
            writeData(temp, strlen(temp));
printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n");
}

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
}

void SimpleHandler::onClose() { //Connection is closing
    //Nothing to do
}