source: rtos_arduino/trunk/arduino_lib/libraries/Milkcocoa_Arduino_SDK/Milkcocoa.cpp@ 144

Last change on this file since 144 was 144, checked in by ertl-honda, 8 years ago

Milkcocoa用のMQTTライブラリの追加と,ESP8266用ライブラリの名称変更.

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