[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 | #ifndef _MILKCOCOA_H_
|
---|
| 26 | #define _MILKCOCOA_H_
|
---|
| 27 |
|
---|
| 28 | #include "Client.h"
|
---|
| 29 | #include "include/Adafruit/Adafruit_MQTT.h"
|
---|
| 30 | #include "include/Adafruit/Adafruit_MQTT_Client.h"
|
---|
| 31 | #include "include/aJson/aJson.h"
|
---|
| 32 |
|
---|
| 33 | #define MILKCOCOA_SUBSCRIBERS 8
|
---|
[180] | 34 | #define MILKCOCOA_CONNECT_DELAY_MS 5000
|
---|
[144] | 35 |
|
---|
[180] | 36 | #define MILKCOCOA_PUSH_ERRORNO -2
|
---|
| 37 | #define MILKCOCOA_SEND_ERRORNO -2
|
---|
| 38 |
|
---|
| 39 | //#define MILKCOCOA_DEBUG
|
---|
| 40 |
|
---|
| 41 | #define DEBUG_PRINTER Serial
|
---|
| 42 |
|
---|
| 43 | #ifdef MILKCOCOA_DEBUG
|
---|
| 44 | #define MILKCOCOA_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
---|
| 45 | #define MILKCOCOA_DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
---|
| 46 | #define MILKCOCOA_DEBUG_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
|
---|
| 47 | #else
|
---|
| 48 | #define MILKCOCOA_DEBUG_PRINT(...) {}
|
---|
| 49 | #define MILKCOCOA_DEBUG_PRINTLN(...) {}
|
---|
| 50 | #define MILKCOCOA_DEBUG_PRINTBUFFER(buffer, len) {}
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
[144] | 53 | class DataElement {
|
---|
| 54 | public:
|
---|
| 55 | DataElement();
|
---|
| 56 | DataElement(char *json_string);
|
---|
| 57 | ~DataElement();
|
---|
| 58 | void setValue(const char *key, const char *v);
|
---|
| 59 | void setValue(const char *key, int v);
|
---|
| 60 | void setValue(const char *key, double v);
|
---|
| 61 | char *toCharArray();
|
---|
[180] | 62 | bool getString(const char *key, char **pdata);
|
---|
| 63 | bool getInt(const char *key, int *pdata);
|
---|
| 64 | bool getFloat(const char *key, float *pdata);
|
---|
[144] | 65 |
|
---|
| 66 | private:
|
---|
| 67 | aJsonObject *params;
|
---|
| 68 | aJsonObject *paJsonObj;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | typedef void (*GeneralFunction) (DataElement *pelem);
|
---|
| 72 |
|
---|
| 73 | class MilkcocoaSubscriber {
|
---|
| 74 | public:
|
---|
| 75 | Adafruit_MQTT_Subscribe *mqtt_sub;
|
---|
| 76 | GeneralFunction cb;
|
---|
| 77 | char topic[100];
|
---|
| 78 | MilkcocoaSubscriber(GeneralFunction _cb);
|
---|
| 79 | void set_mqtt_sub(Adafruit_MQTT_Subscribe *_mqtt_sub);
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | class Milkcocoa {
|
---|
| 83 | public:
|
---|
| 84 | Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id);
|
---|
| 85 | Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id, const char *_session);
|
---|
| 86 | static Milkcocoa* createWithApiKey(Client *client, const char *host, uint16_t port, const char *app_id, const char *client_id, const char *key, const char *secret);
|
---|
[180] | 87 | int8_t connect(int32_t tmout = 0);
|
---|
| 88 | int8_t loop(uint16_t timeout = 0);
|
---|
| 89 | int8_t push(const char *path, DataElement *pdataelement, uint16_t timeout = 0);
|
---|
| 90 | int8_t send(const char *path, DataElement *pdataelement, uint16_t timeout = 0);
|
---|
[144] | 91 | bool on(const char *path, const char *event, GeneralFunction cb);
|
---|
[180] | 92 | const __FlashStringHelper* connectErrorString(int8_t code) {return mqtt->connectErrorString(code);};
|
---|
| 93 | const __FlashStringHelper* pushErrorString(int8_t code) {if(code == MILKCOCOA_PUSH_ERRORNO) return F("Push error"); return mqtt->connectErrorString(code);};
|
---|
[144] | 94 |
|
---|
| 95 | private:
|
---|
| 96 | const char *app_id;
|
---|
| 97 | char session[128];
|
---|
| 98 | Adafruit_MQTT_Client *mqtt;
|
---|
| 99 | MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | #endif
|
---|