Sensos Module Updated

Dependents:   SmartLabProyect

Fork of Middleware by Roberto Herrera

StringManager.cpp

Committer:
RoHe
Date:
2015-12-09
Revision:
2:a56763667f20
Parent:
1:fd355dc296b1

File content as of revision 2:a56763667f20:

/*

StringManager.cpp

Only to improve the clarity of main .cpp
This don't have any new classes

This import is use to manage the strings

Last update by RoHe 16/11/2015
*/

#include <string.h>
#include "StringManager.h"

StringManager::StringManager()
{
    varInt=5;
}

bool StringManager::is_good(void)
{
    return true;
}

int StringManager::getVar(void)
{
    return varInt;
}

char* StringManager::echo(void)
{
    return "Hello from Library";
}

char* StringManager::processString(char* cadena, char* ipServer)
{
    /*
    largo = strlen(cadena) // Para obtener el largo de una cadena
    strcpy(destino, origen) // Copia el contenido de origen en destino
                            // destino debe ser lo suficientemente grande
    strcat(destino, origen) // Agrega el contenido de origen al final de destino
                            // destino debe ser lo suficientemente grande
    resultado = strcmp(cadena1, cadena2) // Compara dos cadenas
                // devuelve un valor menor, igual o mayor que 0 según si cadena1 es menor,
                // igual o mayor que cadena2, respectivamente.
    posicion = strchr(cadena, caracter) // Devuelve la posición en memoria de la primer
                // aparición de caracter dentro de cadena
    posicion = strstr(cadena,subcadena) // Devuelve la posición en memoria de la primer
                // aparición de subcadena dentro de cadena
    */

    //Case: "set:light:1" -> light ON
    //if(strncmp(cadena,"set:light:1",strlen(cadena)-2)) {
    if(strcmp(cadena,"set:light:1\r\n")==0) {
        int set_light = SetLight(true);
        //bool isOKLight = getLight();
        return "reply:set:light:1:192.168.1.82:success";
        //Case: "set:light:1" -> light ON
    } else if(strcmp(cadena,"set:light:0\r\n")==0) {
        int set_light = SetLight(false);
        //bool isOKLight = readLight();
        return "reply:set:light:0:192.168.1.82:succes";
    }
    //Case: "get:people"
    else if(strcmp(cadena,"get:people\r\n")==0) {
        return stringMakerGetPeople(ipServer);
    }
    //Case: "get:presence"
    else if(strcmp(cadena,"get:presence\r\n")==0) {
        return stringMakerGetPresence(ipServer);
    }
    //Case: "get:door" -> 1 = open , 0 = close
    else if(strcmp(cadena,"get:door\r\n")==0) {
        return stringMakerGetDoor(ipServer);
    }
    //Case: "get:light" -> 1 = ON , 0 = OFF
    else if(strcmp(cadena,"get:light\r\n")==0) {
        return stringMakerGetLight(ipServer);
    }
    //Case: "get:window:1"
    else if(strcmp(cadena,"get:window:1\r\n")==0) {
        return stringMakerGetWindow1(ipServer);
    }
    //Case: "get:window:2"
    else if(strcmp(cadena,"get:window:2\r\n")==0) {
        return stringMakerGetWindow2(ipServer);
    }
    //Case: "get:window:3"
    else if(strcmp(cadena,"get:window:3\r\n")==0) {
        return stringMakerGetWindow3(ipServer);
    }
    //Case: "get:room:<IPbroker>"
    else if(strcmp(cadena,"get:room\r\n")==0) {
        return stringMakerGetRoom(ipServer);
    }
    return "Error,it does NOT in the correct format...";
}

char* StringManager::stringMakerGetLight(char* ipServer)
{
    bool get_light /*= getLight()*/;
    get_light=true; //HARDCODED please remove
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_light) {
        strcat(initialString,":light:1");
    } else {
        strcat(initialString,":light:0");
    }
    return initialString;
}

char* StringManager::stringMakerGetDoor(char* ipServer)
{
    bool get_door = GetDoorStatus();
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_door) {
        strcat(initialString,":door:1");
    } else {
        strcat(initialString,":door:0");
    }
    return initialString;
}

char* StringManager::stringMakerGetPresence(char* ipServer)
{
    bool get_presence = GetPrescenceStatus();
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_presence) {
        strcat(initialString,":presence:1");
    } else {
        strcat(initialString,":presence:0");
    }
    return initialString;
}
char* StringManager::stringMakerGetWindow1(char* ipServer)
{
    int get_window1 = GetWindowStatus(1);
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_window1==1) {
        strcat(initialString,":window:1:1");
    } else {
        strcat(initialString,":window:1:0");
    }
    return initialString;
}
char* StringManager::stringMakerGetWindow2(char* ipServer)
{
    int get_window2 = GetWindowStatus(2);
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_window2==1) {
        strcat(initialString,":window:2:1");
    } else {
        strcat(initialString,":window:2:0");
    }
    return initialString;
}
char* StringManager::stringMakerGetWindow3(char* ipServer)
{
    int get_window3 = GetWindowStatus(3);
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(get_window3==1) {
        strcat(initialString,":window:3:1");
    } else {
        strcat(initialString,":window:3:0");
    }
    return initialString;
}

char* StringManager::stringMakerGetPeople(char* ipServer)
{
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    if(true) {
        strcat(initialString,":people:12");
    } else {
        strcat(initialString,":people:0");
    }
    return initialString;
}

char* StringManager::stringMakerGetRoom(char* ipServer)
{
    bool get_light /*= getLight()*/;
    bool get_door = GetDoorStatus();
    bool get_presence = GetPrescenceStatus();
    int get_persons = GetPersonStatus();
    int get_window1 = GetWindowStatus(1);
    int get_window2 = GetWindowStatus(2);
    int get_window3 = GetWindowStatus(3);
    strcpy(initialString,"reply:get:status:");
    strcat(initialString,ipServer);
    //GET DOOR
    if(get_door) {
        strcat(initialString,":door:1");
    } else {
        strcat(initialString,":door:0");
    }
    //GET LIGHT
    get_light=true; //HARDCODED please remove
    if(get_light) {
        strcat(initialString,":light:1");
    } else {
        strcat(initialString,":light:0");
    }
    //GET WINDOW 1
    if(get_window1==1) {
        strcat(initialString,":window:1:1");
    } else {
        strcat(initialString,":window:1:0");
    }
    //GET WINDOW 2
    if(get_window2==1) {
        strcat(initialString,":2:1");
    } else {
        strcat(initialString,":2:0");
    }
    //GET WINDOW 3
    if(get_window3==1) {
        strcat(initialString,":3:1");
    } else {
        strcat(initialString,":3:0");
    }
    //GET PEOPLE
    if(true) {
        strcat(initialString,":people:12");
    } else {
        strcat(initialString,":people:0");
    }
    //GET PRESENCE
    if(get_presence) {
        strcat(initialString,":presence:1");
    } else {
        strcat(initialString,":presence:0");
    }

    return initialString;

}