An AirVantage layer for MQTT
AV_MQTT.cpp@6:b251f6276497, 2013-08-13 (annotated)
- Committer:
- Nim65s
- Date:
- Tue Aug 13 12:31:37 2013 +0000
- Revision:
- 6:b251f6276497
- Parent:
- 1:7f1a11a70a2b
- Child:
- 7:da5d90052cec
is now a daugther of niMQTT, and able to parse the JSON from AirVantage
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::publish_received() { |
Nim65s | 6:b251f6276497 | 24 | //remaining length |
Nim65s | 6:b251f6276497 | 25 | int remaining_length = decode_remaining_length(); |
Nim65s | 6:b251f6276497 | 26 | |
Nim65s | 6:b251f6276497 | 27 | // topic |
Nim65s | 6:b251f6276497 | 28 | char mqtt_utf8_length[2]; |
Nim65s | 6:b251f6276497 | 29 | socket->receive(mqtt_utf8_length, 2); |
Nim65s | 6:b251f6276497 | 30 | int utf8_length = mqtt_utf8_length[0] * 256 + mqtt_utf8_length[1]; |
Nim65s | 6:b251f6276497 | 31 | |
Nim65s | 6:b251f6276497 | 32 | if (debug) printf("PUBLISH Received: %i, %i\r\n", remaining_length, utf8_length); |
Nim65s | 6:b251f6276497 | 33 | |
Nim65s | 6:b251f6276497 | 34 | char topic[utf8_length + 1]; |
Nim65s | 6:b251f6276497 | 35 | socket->receive(topic, utf8_length); |
Nim65s | 6:b251f6276497 | 36 | topic[utf8_length] = 0; |
Nim65s | 6:b251f6276497 | 37 | |
Nim65s | 6:b251f6276497 | 38 | // payload |
Nim65s | 6:b251f6276497 | 39 | int message_length = remaining_length - utf8_length - 2; |
Nim65s | 6:b251f6276497 | 40 | char message[message_length + 1]; |
Nim65s | 6:b251f6276497 | 41 | socket->receive(message, message_length); |
Nim65s | 6:b251f6276497 | 42 | message[message_length] = 0; |
Nim65s | 6:b251f6276497 | 43 | |
Nim65s | 6:b251f6276497 | 44 | waiting_new_packet = true; |
Nim65s | 6:b251f6276497 | 45 | |
Nim65s | 6:b251f6276497 | 46 | call_callback(topic, message); |
Nim65s | 6:b251f6276497 | 47 | // ^^^^^ only 5 characters that differs from niMQTT::publish_received... TODO |
Nim65s | 6:b251f6276497 | 48 | } |
Nim65s | 6:b251f6276497 | 49 | |
Nim65s | 6:b251f6276497 | 50 | void AV_MQTT::call_callback(const char *topic, const char *message) { |
Nim65s | 6:b251f6276497 | 51 | picojson::value v; |
Nim65s | 6:b251f6276497 | 52 | picojson::parse(v, message, message + strlen(message)); |
Nim65s | 6:b251f6276497 | 53 | |
Nim65s | 6:b251f6276497 | 54 | 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 | 55 | |
Nim65s | 6:b251f6276497 | 56 | for (picojson::array::iterator i = a.begin(); i != a.end(); ++i) { |
Nim65s | 6:b251f6276497 | 57 | picojson::object& o = i->get<picojson::object>(); |
Nim65s | 6:b251f6276497 | 58 | for (picojson::object::iterator it = o.begin(); it != o.end(); ++it) { |
Nim65s | 6:b251f6276497 | 59 | callback(it->first.c_str(), it->second.to_str().c_str()); |
Nim65s | 6:b251f6276497 | 60 | } |
Nim65s | 6:b251f6276497 | 61 | } |
Nim65s | 0:37b9835622f9 | 62 | } |