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