[144] | 1 | /*
|
---|
| 2 | The MIT License (MIT)
|
---|
| 3 |
|
---|
| 4 | Copyright (c) 2015 Technical Rockstars
|
---|
| 5 | Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
|
---|
| 6 | Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 8 | of this software and associated documentation files (the "Software"), to deal
|
---|
| 9 | in the Software without restriction, including without limitation the rights
|
---|
| 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 11 | copies of the Software, and to permit persons to whom the Software is
|
---|
| 12 | furnished to do so, subject to the following conditions:
|
---|
| 13 |
|
---|
| 14 | The above copyright notice and this permission notice shall be included in all
|
---|
| 15 | copies or substantial portions of the Software.
|
---|
| 16 |
|
---|
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
| 23 | SOFTWARE.
|
---|
| 24 | */
|
---|
| 25 | #include "Milkcocoa.h"
|
---|
| 26 |
|
---|
| 27 | DataElement::DataElement() {
|
---|
| 28 | params = aJson.createObject();
|
---|
| 29 | paJsonObj = aJson.createObject();
|
---|
| 30 | aJson.addItemToObject(paJsonObj, "params", params);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | DataElement::DataElement(char *json_string) {
|
---|
| 34 | paJsonObj = aJson.parse(json_string);
|
---|
[180] | 35 | params = aJson.getObjectItem(paJsonObj, "params");
|
---|
[144] | 36 | }
|
---|
| 37 |
|
---|
| 38 | DataElement::~DataElement() {
|
---|
| 39 | aJson.deleteItem(paJsonObj);
|
---|
| 40 | paJsonObj = NULL;
|
---|
| 41 | params = NULL;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void DataElement::setValue(const char *key, const char *v) {
|
---|
| 45 | aJson.addStringToObject(params, key, v);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void DataElement::setValue(const char *key, int v) {
|
---|
| 49 | aJson.addNumberToObject(params, key, v);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void DataElement::setValue(const char *key, double v) {
|
---|
| 53 | aJson.addNumberToObject(params, key, v);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[180] | 56 | bool DataElement::getString(const char *key, char **pdata) {
|
---|
[144] | 57 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
[180] | 58 | if((obj == NULL) || (obj->type != aJson_String)) {return false;}
|
---|
| 59 | *pdata = obj->valuestring;
|
---|
| 60 | return true;
|
---|
[144] | 61 | }
|
---|
| 62 |
|
---|
[180] | 63 | bool DataElement::getInt(const char *key, int *pdata) {
|
---|
| 64 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
| 65 | if((obj == NULL) || (obj->type != aJson_Int)) {return false;}
|
---|
| 66 | *pdata = obj->valueint;
|
---|
| 67 | return true;
|
---|
[144] | 68 | }
|
---|
| 69 |
|
---|
[180] | 70 | bool DataElement::getFloat(const char *key, float *pdata) {
|
---|
[144] | 71 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
[180] | 72 | if((obj == NULL) || (obj->type != aJson_Float)) {return false;}
|
---|
| 73 | *pdata = obj->valuefloat;
|
---|
| 74 | return true;
|
---|
[144] | 75 | }
|
---|
| 76 |
|
---|
| 77 | char *DataElement::toCharArray() {
|
---|
| 78 | return aJson.print(paJsonObj);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | Milkcocoa::Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id) {
|
---|
| 82 | app_id = _app_id;
|
---|
| 83 | mqtt = new Adafruit_MQTT_Client(client, host, port, client_id, "sdammy", app_id);
|
---|
| 84 |
|
---|
| 85 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 86 | milkcocoaSubscribers[i] = 0;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | Milkcocoa::Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id, const char *_session) {
|
---|
| 92 | sprintf(session, "%s", _session);
|
---|
| 93 | app_id = _app_id;
|
---|
| 94 | mqtt = new Adafruit_MQTT_Client(client, host, port, client_id, session, app_id);
|
---|
| 95 |
|
---|
| 96 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 97 | milkcocoaSubscribers[i] = 0;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | Milkcocoa* Milkcocoa::createWithApiKey(Client *client, const char *host, uint16_t port, const char *app_id, const char *client_id, const char *key, const char *secret)
|
---|
| 103 | {
|
---|
| 104 | char session[60];
|
---|
| 105 | sprintf(session, "k%s:%s", key, secret);
|
---|
| 106 | return new Milkcocoa(client, host, port, app_id, client_id, session);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[180] | 109 | /*
|
---|
| 110 | * tmout
|
---|
| 111 | * 1- : Connect try time.
|
---|
| 112 | * 0 : retry Forever
|
---|
| 113 | * Return code
|
---|
| 114 | * 0 : Success
|
---|
| 115 | * other : Error code (see Adafruit_MQTT::connectErrorString())
|
---|
| 116 | */
|
---|
| 117 | int8_t Milkcocoa::connect(int32_t tmout) {
|
---|
| 118 | int8_t ret;
|
---|
| 119 | int32_t retry = 0;
|
---|
[144] | 120 |
|
---|
| 121 | if (mqtt->connected()) {
|
---|
[180] | 122 | return 0;
|
---|
[144] | 123 | }
|
---|
| 124 |
|
---|
[180] | 125 | MILKCOCOA_DEBUG_PRINTLN("Connecting to MQTT... ");
|
---|
[144] | 126 |
|
---|
| 127 | while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
|
---|
[180] | 128 | mqtt->disconnect();
|
---|
| 129 | // Timeout
|
---|
| 130 | if ((tmout != 0) && (++retry == tmout)) {
|
---|
| 131 | return ret;
|
---|
| 132 | }
|
---|
| 133 | delay(MILKCOCOA_CONNECT_DELAY_MS);
|
---|
[144] | 134 | }
|
---|
[180] | 135 | MILKCOCOA_DEBUG_PRINTLN("MQTT Connected!");
|
---|
| 136 |
|
---|
| 137 | return 0;
|
---|
[144] | 138 | }
|
---|
| 139 |
|
---|
[180] | 140 | int8_t Milkcocoa::push(const char *path, DataElement *pdataelement, uint16_t timeout) {
|
---|
[144] | 141 | char topic[100];
|
---|
[180] | 142 | int8_t ret;
|
---|
[144] | 143 | char *send_array;
|
---|
[180] | 144 |
|
---|
| 145 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 146 |
|
---|
[144] | 147 | sprintf(topic, "%s/%s/push", app_id, path);
|
---|
| 148 | Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
|
---|
| 149 | send_array = pdataelement->toCharArray();
|
---|
[180] | 150 | ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
|
---|
[144] | 151 | free(send_array);
|
---|
| 152 | return ret;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[180] | 155 | int8_t Milkcocoa::send(const char *path, DataElement *pdataelement, uint16_t timeout) {
|
---|
[144] | 156 | char topic[100];
|
---|
[180] | 157 | int8_t ret;
|
---|
| 158 | char *send_array;
|
---|
| 159 |
|
---|
| 160 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 161 |
|
---|
[144] | 162 | sprintf(topic, "%s/%s/send", app_id, path);
|
---|
| 163 | Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
|
---|
| 164 | send_array = pdataelement->toCharArray();
|
---|
[180] | 165 | ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
|
---|
[144] | 166 | free(send_array);
|
---|
| 167 | return ret;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[180] | 170 | /*
|
---|
| 171 | * Return code
|
---|
| 172 | * 0 : Success
|
---|
| 173 | * other : Error code
|
---|
| 174 | * -1,1-6 : see Adafruit_MQTT::connectErrorString()
|
---|
| 175 | */
|
---|
| 176 | int8_t Milkcocoa::loop(uint16_t timeout) {
|
---|
| 177 | int8_t ret;
|
---|
| 178 |
|
---|
| 179 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 180 |
|
---|
[144] | 181 | Adafruit_MQTT_Subscribe *subscription;
|
---|
| 182 | while ((subscription = mqtt->readSubscription(1000))) {
|
---|
| 183 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 184 | if(milkcocoaSubscribers[i] == 0) continue;
|
---|
| 185 | if (subscription == (milkcocoaSubscribers[i]->mqtt_sub) ) {
|
---|
| 186 | DataElement de = DataElement((char *)milkcocoaSubscribers[i]->mqtt_sub->lastread);
|
---|
| 187 | milkcocoaSubscribers[i]->cb( &de );
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[180] | 191 |
|
---|
| 192 | return 0;
|
---|
[144] | 193 | }
|
---|
| 194 |
|
---|
| 195 | bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
|
---|
| 196 | MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb);
|
---|
| 197 | sprintf(sub->topic, "%s/%s/%s", app_id, path, event);
|
---|
| 198 |
|
---|
| 199 | uint8_t i;
|
---|
| 200 | Adafruit_MQTT_Subscribe *mqtt_sub = new Adafruit_MQTT_Subscribe(mqtt, sub->topic);
|
---|
| 201 | sub->set_mqtt_sub(mqtt_sub);
|
---|
| 202 | if(!mqtt->subscribe(mqtt_sub)) {
|
---|
| 203 | return false;
|
---|
| 204 | }
|
---|
| 205 | for (i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 206 | if (milkcocoaSubscribers[i] == sub) {
|
---|
| 207 | return false;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | for (i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 211 | if (milkcocoaSubscribers[i] == 0) {
|
---|
| 212 | milkcocoaSubscribers[i] = sub;
|
---|
| 213 | return true;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | return false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
|
---|
| 220 | cb = _cb;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void MilkcocoaSubscriber::set_mqtt_sub(Adafruit_MQTT_Subscribe *_mqtt_sub) {
|
---|
| 224 | mqtt_sub = _mqtt_sub;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|