[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 | #include "stdio.h"
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | DataElement::DataElement() {
|
---|
| 30 | params = aJson.createObject();
|
---|
| 31 | paJsonObj = aJson.createObject();
|
---|
| 32 | aJson.addItemToObject(paJsonObj, "params", params);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | DataElement::DataElement(char *json_string) {
|
---|
| 36 | paJsonObj = aJson.parse(json_string);
|
---|
[180] | 37 | params = aJson.getObjectItem(paJsonObj, "params");
|
---|
[144] | 38 | }
|
---|
| 39 |
|
---|
| 40 | DataElement::~DataElement() {
|
---|
| 41 | aJson.deleteItem(paJsonObj);
|
---|
| 42 | paJsonObj = NULL;
|
---|
| 43 | params = NULL;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | void DataElement::setValue(const char *key, const char *v) {
|
---|
| 47 | aJson.addStringToObject(params, key, v);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | void DataElement::setValue(const char *key, int v) {
|
---|
| 51 | aJson.addNumberToObject(params, key, v);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void DataElement::setValue(const char *key, double v) {
|
---|
| 55 | aJson.addNumberToObject(params, key, v);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[180] | 58 | bool DataElement::getString(const char *key, char **pdata) {
|
---|
[144] | 59 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
[180] | 60 | if((obj == NULL) || (obj->type != aJson_String)) {return false;}
|
---|
| 61 | *pdata = obj->valuestring;
|
---|
| 62 | return true;
|
---|
[144] | 63 | }
|
---|
| 64 |
|
---|
[180] | 65 | bool DataElement::getInt(const char *key, int *pdata) {
|
---|
| 66 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
| 67 | if((obj == NULL) || (obj->type != aJson_Int)) {return false;}
|
---|
| 68 | *pdata = obj->valueint;
|
---|
| 69 | return true;
|
---|
[144] | 70 | }
|
---|
| 71 |
|
---|
[180] | 72 | bool DataElement::getFloat(const char *key, float *pdata) {
|
---|
[144] | 73 | aJsonObject* obj = aJson.getObjectItem(params, key);
|
---|
[180] | 74 | if((obj == NULL) || (obj->type != aJson_Float)) {return false;}
|
---|
| 75 | *pdata = obj->valuefloat;
|
---|
| 76 | return true;
|
---|
[144] | 77 | }
|
---|
| 78 |
|
---|
| 79 | char *DataElement::toCharArray() {
|
---|
| 80 | return aJson.print(paJsonObj);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | Milkcocoa::Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id) {
|
---|
| 84 | app_id = _app_id;
|
---|
| 85 | mqtt = new Adafruit_MQTT_Client(client, host, port, client_id, "sdammy", app_id);
|
---|
| 86 |
|
---|
| 87 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 88 | milkcocoaSubscribers[i] = 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | Milkcocoa::Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id, const char *_session) {
|
---|
| 94 | sprintf(session, "%s", _session);
|
---|
| 95 | app_id = _app_id;
|
---|
| 96 | mqtt = new Adafruit_MQTT_Client(client, host, port, client_id, session, app_id);
|
---|
| 97 |
|
---|
| 98 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 99 | milkcocoaSubscribers[i] = 0;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | 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)
|
---|
| 105 | {
|
---|
| 106 | char session[60];
|
---|
| 107 | sprintf(session, "k%s:%s", key, secret);
|
---|
| 108 | return new Milkcocoa(client, host, port, app_id, client_id, session);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[180] | 111 | /*
|
---|
| 112 | * tmout
|
---|
| 113 | * 1- : Connect try time.
|
---|
| 114 | * 0 : retry Forever
|
---|
| 115 | * Return code
|
---|
| 116 | * 0 : Success
|
---|
| 117 | * other : Error code (see Adafruit_MQTT::connectErrorString())
|
---|
| 118 | */
|
---|
| 119 | int8_t Milkcocoa::connect(int32_t tmout) {
|
---|
| 120 | int8_t ret;
|
---|
| 121 | int32_t retry = 0;
|
---|
[144] | 122 |
|
---|
| 123 | if (mqtt->connected()) {
|
---|
[180] | 124 | return 0;
|
---|
[144] | 125 | }
|
---|
| 126 |
|
---|
[180] | 127 | MILKCOCOA_DEBUG_PRINTLN("Connecting to MQTT... ");
|
---|
[144] | 128 |
|
---|
| 129 | while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
|
---|
[180] | 130 | mqtt->disconnect();
|
---|
| 131 | // Timeout
|
---|
| 132 | if ((tmout != 0) && (++retry == tmout)) {
|
---|
| 133 | return ret;
|
---|
| 134 | }
|
---|
| 135 | delay(MILKCOCOA_CONNECT_DELAY_MS);
|
---|
[144] | 136 | }
|
---|
[180] | 137 | MILKCOCOA_DEBUG_PRINTLN("MQTT Connected!");
|
---|
| 138 |
|
---|
| 139 | return 0;
|
---|
[144] | 140 | }
|
---|
| 141 |
|
---|
[180] | 142 | int8_t Milkcocoa::push(const char *path, DataElement *pdataelement, uint16_t timeout) {
|
---|
[144] | 143 | char topic[100];
|
---|
[180] | 144 | int8_t ret;
|
---|
[144] | 145 | char *send_array;
|
---|
[180] | 146 |
|
---|
| 147 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 148 |
|
---|
[144] | 149 | sprintf(topic, "%s/%s/push", app_id, path);
|
---|
| 150 | Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
|
---|
| 151 | send_array = pdataelement->toCharArray();
|
---|
[180] | 152 | ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
|
---|
[144] | 153 | free(send_array);
|
---|
| 154 | return ret;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[180] | 157 | int8_t Milkcocoa::send(const char *path, DataElement *pdataelement, uint16_t timeout) {
|
---|
[144] | 158 | char topic[100];
|
---|
[180] | 159 | int8_t ret;
|
---|
| 160 | char *send_array;
|
---|
| 161 |
|
---|
| 162 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 163 |
|
---|
[144] | 164 | sprintf(topic, "%s/%s/send", app_id, path);
|
---|
| 165 | Adafruit_MQTT_Publish pushPublisher = Adafruit_MQTT_Publish(mqtt, topic);
|
---|
| 166 | send_array = pdataelement->toCharArray();
|
---|
[180] | 167 | ret = (pushPublisher.publish(send_array))? 0 : MILKCOCOA_PUSH_ERRORNO;
|
---|
[144] | 168 | free(send_array);
|
---|
| 169 | return ret;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[180] | 172 | /*
|
---|
| 173 | * Return code
|
---|
| 174 | * 0 : Success
|
---|
| 175 | * other : Error code
|
---|
| 176 | * -1,1-6 : see Adafruit_MQTT::connectErrorString()
|
---|
| 177 | */
|
---|
| 178 | int8_t Milkcocoa::loop(uint16_t timeout) {
|
---|
| 179 | int8_t ret;
|
---|
| 180 |
|
---|
| 181 | if((ret = connect(timeout)) != 0){return ret;}
|
---|
| 182 |
|
---|
[144] | 183 | Adafruit_MQTT_Subscribe *subscription;
|
---|
| 184 | while ((subscription = mqtt->readSubscription(1000))) {
|
---|
| 185 | for (uint8_t i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 186 | if(milkcocoaSubscribers[i] == 0) continue;
|
---|
| 187 | if (subscription == (milkcocoaSubscribers[i]->mqtt_sub) ) {
|
---|
| 188 | DataElement de = DataElement((char *)milkcocoaSubscribers[i]->mqtt_sub->lastread);
|
---|
| 189 | milkcocoaSubscribers[i]->cb( &de );
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
[180] | 193 |
|
---|
| 194 | return 0;
|
---|
[144] | 195 | }
|
---|
| 196 |
|
---|
| 197 | bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
|
---|
| 198 | MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb);
|
---|
| 199 | sprintf(sub->topic, "%s/%s/%s", app_id, path, event);
|
---|
| 200 |
|
---|
| 201 | uint8_t i;
|
---|
| 202 | Adafruit_MQTT_Subscribe *mqtt_sub = new Adafruit_MQTT_Subscribe(mqtt, sub->topic);
|
---|
| 203 | sub->set_mqtt_sub(mqtt_sub);
|
---|
| 204 | if(!mqtt->subscribe(mqtt_sub)) {
|
---|
| 205 | return false;
|
---|
| 206 | }
|
---|
| 207 | for (i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 208 | if (milkcocoaSubscribers[i] == sub) {
|
---|
| 209 | return false;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | for (i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
|
---|
| 213 | if (milkcocoaSubscribers[i] == 0) {
|
---|
| 214 | milkcocoaSubscribers[i] = sub;
|
---|
| 215 | return true;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | return false;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
|
---|
| 222 | cb = _cb;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void MilkcocoaSubscriber::set_mqtt_sub(Adafruit_MQTT_Subscribe *_mqtt_sub) {
|
---|
| 226 | mqtt_sub = _mqtt_sub;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|