An AirVantage layer for MQTT
AV_MQTT.cpp@7:da5d90052cec, 2013-08-13 (annotated)
- Committer:
- Nim65s
- Date:
- Tue Aug 13 13:36:45 2013 +0000
- Revision:
- 7:da5d90052cec
- Parent:
- 6:b251f6276497
call_callback is now virtual from niMQTT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nim65s | 0:37b9835622f9 | 1 | #include "AV_MQTT.h" |
Nim65s | 6:b251f6276497 | 2 | #include "picojson.h" |
Nim65s | 0:37b9835622f9 | 3 | |
Nim65s | 6:b251f6276497 | 4 | AV_MQTT::AV_MQTT(char *server, void (*callback)(const char *, const char*), char *username, char *password, char *id, int port, bool debug): |
Nim65s | 6:b251f6276497 | 5 | niMQTT(server, callback, id, port, username, password, debug) { |
Nim65s | 0:37b9835622f9 | 6 | topic = new char[strlen(username) + 15]; |
Nim65s | 0:37b9835622f9 | 7 | strcpy(topic, username); |
Nim65s | 0:37b9835622f9 | 8 | strcat(topic, "/messages/json"); |
Nim65s | 0:37b9835622f9 | 9 | } |
Nim65s | 0:37b9835622f9 | 10 | |
Nim65s | 0:37b9835622f9 | 11 | void AV_MQTT::pub(char *key, char *value) { |
Nim65s | 0:37b9835622f9 | 12 | int key_length = strlen(key); |
Nim65s | 0:37b9835622f9 | 13 | int value_length = strlen(value); |
Nim65s | 0:37b9835622f9 | 14 | char json[key_length + value_length + 36]; |
Nim65s | 0:37b9835622f9 | 15 | strcpy(json, "[{\""); |
Nim65s | 0:37b9835622f9 | 16 | strcat(json, key); |
Nim65s | 0:37b9835622f9 | 17 | strcat(json, "\":[{\"timestamp\":null,\"value\":"); |
Nim65s | 0:37b9835622f9 | 18 | strcat(json, value); |
Nim65s | 0:37b9835622f9 | 19 | strcat(json, "}]}]"); |
Nim65s | 6:b251f6276497 | 20 | niMQTT::pub(topic, json); |
Nim65s | 6:b251f6276497 | 21 | } |
Nim65s | 6:b251f6276497 | 22 | |
Nim65s | 6:b251f6276497 | 23 | void AV_MQTT::call_callback(const char *topic, const char *message) { |
Nim65s | 6:b251f6276497 | 24 | picojson::value v; |
Nim65s | 6:b251f6276497 | 25 | picojson::parse(v, message, message + strlen(message)); |
Nim65s | 6:b251f6276497 | 26 | |
Nim65s | 6:b251f6276497 | 27 | picojson::array a = v.get<picojson::array>()[0].get("write").get<picojson::array>(); //[0].get("mbed_mqtt_example.led_settings").to_str().c_str()); |
Nim65s | 6:b251f6276497 | 28 | |
Nim65s | 6:b251f6276497 | 29 | for (picojson::array::iterator i = a.begin(); i != a.end(); ++i) { |
Nim65s | 6:b251f6276497 | 30 | picojson::object& o = i->get<picojson::object>(); |
Nim65s | 6:b251f6276497 | 31 | for (picojson::object::iterator it = o.begin(); it != o.end(); ++it) { |
Nim65s | 6:b251f6276497 | 32 | callback(it->first.c_str(), it->second.to_str().c_str()); |
Nim65s | 6:b251f6276497 | 33 | } |
Nim65s | 6:b251f6276497 | 34 | } |
Nim65s | 0:37b9835622f9 | 35 | } |