[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
|
---|
| 34 |
|
---|
| 35 | class DataElement {
|
---|
| 36 | public:
|
---|
| 37 | DataElement();
|
---|
| 38 | DataElement(char *json_string);
|
---|
| 39 | ~DataElement();
|
---|
| 40 | void setValue(const char *key, const char *v);
|
---|
| 41 | void setValue(const char *key, int v);
|
---|
| 42 | void setValue(const char *key, double v);
|
---|
| 43 | char *toCharArray();
|
---|
| 44 | char *getString(const char *key);
|
---|
| 45 | int getInt(const char *key);
|
---|
| 46 | float getFloat(const char *key);
|
---|
| 47 |
|
---|
| 48 | private:
|
---|
| 49 | aJsonObject *params;
|
---|
| 50 | aJsonObject *paJsonObj;
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | typedef void (*GeneralFunction) (DataElement *pelem);
|
---|
| 54 |
|
---|
| 55 | class MilkcocoaSubscriber {
|
---|
| 56 | public:
|
---|
| 57 | Adafruit_MQTT_Subscribe *mqtt_sub;
|
---|
| 58 | GeneralFunction cb;
|
---|
| 59 | char topic[100];
|
---|
| 60 | MilkcocoaSubscriber(GeneralFunction _cb);
|
---|
| 61 | void set_mqtt_sub(Adafruit_MQTT_Subscribe *_mqtt_sub);
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | class Milkcocoa {
|
---|
| 65 | public:
|
---|
| 66 | Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id);
|
---|
| 67 | Milkcocoa(Client *client, const char *host, uint16_t port, const char *_app_id, const char *client_id, const char *_session);
|
---|
| 68 | 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);
|
---|
| 69 | void connect();
|
---|
| 70 | void loop();
|
---|
| 71 | bool push(const char *path, DataElement *pdataelement);
|
---|
| 72 | bool send(const char *path, DataElement *pdataelement);
|
---|
| 73 | bool on(const char *path, const char *event, GeneralFunction cb);
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | private:
|
---|
| 77 | const char *app_id;
|
---|
| 78 | char session[128];
|
---|
| 79 | Adafruit_MQTT_Client *mqtt;
|
---|
| 80 | MilkcocoaSubscriber *milkcocoaSubscribers[MILKCOCOA_SUBSCRIBERS];
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | #endif
|
---|