First

Dependents:   mbed-os-rest-api-V1-1 mbed-os-rest-api-V1-2 mbed-os-rest-api-V1-2-2

RestAPI_Manager.cpp

Committer:
asaewing
Date:
2018-10-08
Revision:
0:b3b078af7b16
Child:
1:4db49ba6e885

File content as of revision 0:b3b078af7b16:

#include "module_for_all.h"

RestAPI_Manager::RestAPI_Manager() {
    _ip = "192.168.20.101";
    _ip_s = std::string(_ip);
    _main = 1;
}

RestAPI_Manager::RestAPI_Manager(char* IP, std::string Main) {
    _ip = IP;
    _ip_s = std::string(_ip);
    _main = Main;
}

void RestAPI_Manager::SetConfig(char* IP, std::string Main) {
    _ip = IP;
    _ip_s = std::string(_ip);
    _main = Main;
    printf("RestAPI_Manager::SetConfig OK\r\n");
}

std::string RestAPI_Manager::runCommand(std::string HTTP_METHOD, std::string API_COMMAND) {
    std::string _response = "";
    
    if (HTTP_METHOD.compare("GET") == 0) {
        _response.append(switchCommand_GET(API_COMMAND));
        //_response.append("001 GET");
        
    } else if (HTTP_METHOD.compare("POST") == 0) {
        _response.append(switchCommand_POST(API_COMMAND));
        //_response.append("002 POST");
        
    } else {
        /*response.append("\r\n");
        response.append(HTTP_HEADER_FIELDS);
        response.append("\r\n\r\n");
        response.append("000 No Method\r\n");*/
        //makeResponse("000 No Method");
        _response.append("000 No Method");
    }
    
    return _response;
}

std::string RestAPI_Manager::switchCommand_GET(std::string API_COMMAND) {
    int count = countRoot(API_COMMAND);
    
    std::vector<std::string> _commandList;
    _commandList.reserve(10);
    split(API_COMMAND, _commandList, "/");
    
    std::string returnString = "";
    
    //returnString.append(_commandList[0]);
    //returnString.append("\r\n");
    
    if (_commandList[0].compare("who") == 0) {
        /*printf("mbed #");
        printf(_main);
        printf("mbed #\r\n");*/
        returnString.append("mbed #");
        //char* tmpS;
        //sprintf(tmpS,"%d",_main);
        //std::string tmpS2(tmpS);
        returnString.append(_main);
        returnString.append("\r\n");
        returnString.append(_ip_s);
    } else if (_commandList[0].compare("ir_test") == 0) {

        AnalogIn  IR_Test(PA_3);
        float tmp_v = IR_Test.read()*1000.0f;
        printf("Voltage: %f\n", tmp_v);
        
        /*std::ostringstream ss;
        ss << tmp_v;
        std::string ss2(ss.str());*/
        
        /*printf("mbed #");
        printf(_main);
        printf("mbed #\r\n");*/
        
        
        char* tmpS;
        sprintf(tmpS,"%5.5f%",tmp_v);
        std::string tmpS2(tmpS);
        returnString.append(tmpS);
    } else {
        returnString.append("No Command");
    }
    /*std::string tmp = "";
    
    //char buf[20];
    char* buf;
    sprintf(buf,"%03d", count);
    string tmpCount(buf);
    tmp.append("count:");
    tmp.append(tmpCount);
    tmp.append("\r\n");
    
    for(int ii=0;ii<count-1;ii++) {
        sprintf(buf,"%03d", ii);
        string tmpII(buf);

        tmp.append(tmpII);
        tmp.append(":");
        tmp.append(_commandList[ii]);
        tmp.append(",");
    }
    tmp.append("\r\n");
    tmp.append(API_COMMAND);
    
    printf("### %%% \r\n");
    printf(stringToChar(API_COMMAND));
    printf("\r\n");
    printf(stringToChar(tmp));
    printf("### %%% \r\n");
    return tmp;*/
    
    return returnString;
}

std::string RestAPI_Manager::switchCommand_POST(std::string API_COMMAND) {
    /*int count = countRoot(API_COMMAND);
    
    std::vector<std::string> _commandList;
    _commandList.reserve(10);
    split(API_COMMAND, _commandList, "/");
    std::string tmp = "";
    
    char buf[20];
    sprintf(buf,"%03d", count);
    string tmpCount(buf);
    tmp.append("count:");
    tmp.append(tmpCount);
    tmp.append("\r\n");
    
    for(int ii=0;ii<count-1;ii++) {
        sprintf(buf,"%03d", ii);
        string tmpII(buf);

        tmp.append(tmpII);
        tmp.append(":");
        tmp.append(_commandList[ii]);
        tmp.append(",");
    }
    tmp.append("\r\n");
    tmp.append(API_COMMAND);
    
    printf("### %%% \r\n");
    printf(stringToChar(API_COMMAND));
    printf("\r\n");
    printf(stringToChar(tmp));
    printf("### %%% \r\n");
    return tmp;*/
    int count = countRoot(API_COMMAND);
    
    std::vector<std::string> _commandList;
    _commandList.reserve(10);
    split(API_COMMAND, _commandList, "/");
    
    std::string returnString = "";
    
    //returnString.append(_commandList[0]);
    //returnString.append("\r\n");
    
    if (_commandList[0].compare("who2") == 0) {
        /*printf("mbed #");
        printf(_main);
        printf("mbed #\r\n");*/
        returnString.append("mbed #");
        //char* tmpS;
        //sprintf(tmpS,"%d",_main);
        //std::string tmpS2(tmpS);
        returnString.append(_main);
        returnString.append("\r\n");
        returnString.append(_ip_s);
    } else {
        returnString.append("No Command");
    }
    
    return returnString;
}