A wrapper class for talking to Axeda from MBED devices. Uses HTTPClient and MbedJSONValue classes.

Dependents:   axeda_wrapper_dev MTS_Axeda_Example

AxedaWrapper simplifies pushing data to Axeda's cloud.

Uses HTTPClient and MbedJSONValue libs:

http://mbed.org/users/donatien/code/HTTPClient/

http://mbed.org/users/samux/code/MbedJSONValue/

AxedaWrapper.cpp

Committer:
mfiore
Date:
2013-12-20
Revision:
1:d42aaf6f2e19
Parent:
0:d472df0659a9
Child:
2:99baa98f84a3

File content as of revision 1:d42aaf6f2e19:

#include "AxedaWrapper.h"
#include "HTTPJsonText.h"
#include <string>
#include <vector>
#include <stdlib.h>

using namespace mts;

static const char* PATH_BASE = "/ammp/data/1/";

AxedaWrapper::AxedaWrapper(const std::string& serial, const std::string& model, const std::string& host, int port) : _timeout(30000) {
    _http_client = new HTTPClient();
    _data = new MbedJSONValue();
    
    char buf[256];
    std::string path = PATH_BASE + model + "!" + serial;
    sprintf(buf, "http://%s:%d%s", host.c_str(), port, path.c_str());
    _url = std::string(buf);
}

AxedaWrapper::~AxedaWrapper() {
    delete _http_client;
    delete _data;
}

void AxedaWrapper::addKvp(const std::string& key, const std::string& value) {
    (*_data)["data"][0]["di"][key] = value;
}

void AxedaWrapper::addKvp(const char* key, const char* value) {
    (*_data)["data"][0]["di"][std::string(key)] = value;
}

void AxedaWrapper::addKvp(const std::string& key, int value) {
    (*_data)["data"][0]["di"][key] = value;
}

void AxedaWrapper::addKvp(const char* key, int value) {
    (*_data)["data"][0]["di"][std::string(key)] = value;
}

void AxedaWrapper::addKvp(const std::string& key, double value) {
    (*_data)["data"][0]["di"][key] = value;
}

void AxedaWrapper::addKvp(const char* key, double value) {
    (*_data)["data"][0]["di"][std::string(key)] = value;
}

void AxedaWrapper::addKvp(const std::string& key, bool value) {
    (*_data)["data"][0]["di"][key] = value;
}

void AxedaWrapper::addKvp(const char* key, bool value) {
    (*_data)["data"][0]["di"][std::string(key)] = value;
}

void AxedaWrapper::deleteAllKvp() {
    delete _data;
    _data = new MbedJSONValue();
}

int AxedaWrapper::kvpMapSize() {
    return _data->size();
}

bool AxedaWrapper::sendKvp(const std::string& key, const std::string& value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendKvp(const char* key, const char* value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendKvp(const std::string& key, int value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendKvp(const char* key, int value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendKvp(const std::string& key, double value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendKvp(const char* key, double value) {
    MbedJSONValue data;
    data["data"][0]["di"][key] = value;
    return sendBase(data.serialize());
}

bool AxedaWrapper::sendAllKvp(bool clear) {
    std::string data = _data->serialize();
    bool ret = sendBase(data);
    if (ret && clear) {
        deleteAllKvp();
    }
    return ret;
}

bool AxedaWrapper::sendBase(const std::string& data) {
    int ret;
    char buf[512] = {0};
    HTTPJsonText to_send((char*) data.c_str());
    HTTPText to_recv(buf, sizeof(buf));
    
    ret = _http_client->post(_url.c_str(), to_send, &to_recv, _timeout);
    if (ret != HTTP_OK) {
        return false;
    }
    return true;
}