Junichi Katsu / Milkcocoa-os

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

Committer:
jksoft
Date:
Thu May 18 14:47:30 2017 +0000
Revision:
3:cddf81a87de3
Parent:
1:8e4149b53a8a
Child:
4:9cfd43d8de16
???????????push?send???????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:0a2f634d3324 1 #include "Milkcocoa.h"
jksoft 0:0a2f634d3324 2
jksoft 0:0a2f634d3324 3 #if 1
jksoft 0:0a2f634d3324 4 extern RawSerial pc;
jksoft 0:0a2f634d3324 5
jksoft 0:0a2f634d3324 6 #define DBG(x) x
jksoft 0:0a2f634d3324 7 #else
jksoft 0:0a2f634d3324 8 #define DBG(x)
jksoft 0:0a2f634d3324 9 #endif
jksoft 0:0a2f634d3324 10
jksoft 0:0a2f634d3324 11 DataElement::DataElement() {
jksoft 0:0a2f634d3324 12 json_msg[0] = '\0';
jksoft 0:0a2f634d3324 13 strcpy(json_msg,"{\"params\":{");
jksoft 0:0a2f634d3324 14 }
jksoft 0:0a2f634d3324 15
jksoft 0:0a2f634d3324 16 DataElement::DataElement(char *json_string) {
jksoft 0:0a2f634d3324 17 json_msg[0] = '\0';
jksoft 0:0a2f634d3324 18 strcpy(json_msg,json_string);
jksoft 0:0a2f634d3324 19 }
jksoft 0:0a2f634d3324 20
jksoft 0:0a2f634d3324 21 void DataElement::setValue(const char *key, const char *v) {
jksoft 0:0a2f634d3324 22 char json_string[64];
jksoft 0:0a2f634d3324 23 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 0:0a2f634d3324 24 {
jksoft 0:0a2f634d3324 25 strcat(json_msg,",");
jksoft 0:0a2f634d3324 26 }
jksoft 0:0a2f634d3324 27 sprintf(json_string,"\"%s\":\"%s\"",key,v);
jksoft 0:0a2f634d3324 28 strcat(json_msg,json_string);
jksoft 0:0a2f634d3324 29 }
jksoft 0:0a2f634d3324 30
jksoft 0:0a2f634d3324 31 void DataElement::setValue(const char *key, int v) {
jksoft 0:0a2f634d3324 32 char json_string[64];
jksoft 0:0a2f634d3324 33 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 0:0a2f634d3324 34 {
jksoft 0:0a2f634d3324 35 strcat(json_msg,",");
jksoft 0:0a2f634d3324 36 }
jksoft 0:0a2f634d3324 37 sprintf(json_string,"\"%s\":\"%d\"",key,v);
jksoft 0:0a2f634d3324 38 strcat(json_msg,json_string);
jksoft 0:0a2f634d3324 39 }
jksoft 0:0a2f634d3324 40
jksoft 0:0a2f634d3324 41 void DataElement::setValue(const char *key, double v) {
jksoft 0:0a2f634d3324 42 char json_string[64];
jksoft 0:0a2f634d3324 43 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 0:0a2f634d3324 44 {
jksoft 0:0a2f634d3324 45 strcat(json_msg,",");
jksoft 0:0a2f634d3324 46 }
jksoft 0:0a2f634d3324 47 sprintf(json_string,"\"%s\":\"%f\"",key,v);
jksoft 0:0a2f634d3324 48 strcat(json_msg,json_string);
jksoft 0:0a2f634d3324 49 }
jksoft 0:0a2f634d3324 50
jksoft 0:0a2f634d3324 51 char *DataElement::getString(const char *key) {
jksoft 0:0a2f634d3324 52 static char _word[64];
jksoft 0:0a2f634d3324 53 char *p;
jksoft 0:0a2f634d3324 54 int i=0;
jksoft 0:0a2f634d3324 55
jksoft 0:0a2f634d3324 56 strcpy(_word , "\"\0");
jksoft 0:0a2f634d3324 57 strcat(_word , key );
jksoft 0:0a2f634d3324 58 strcat(_word , "\"" );
jksoft 0:0a2f634d3324 59
jksoft 0:0a2f634d3324 60 p = strstr( (char*)json_msg , _word ) + 2 + strlen(_word);
jksoft 0:0a2f634d3324 61
jksoft 0:0a2f634d3324 62 while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '\"') )
jksoft 0:0a2f634d3324 63 {
jksoft 0:0a2f634d3324 64 _word[i] = p[i];
jksoft 0:0a2f634d3324 65 i++;
jksoft 0:0a2f634d3324 66 }
jksoft 0:0a2f634d3324 67 _word[i] = '\0';
jksoft 0:0a2f634d3324 68
jksoft 0:0a2f634d3324 69 return _word;
jksoft 0:0a2f634d3324 70 }
jksoft 0:0a2f634d3324 71
jksoft 0:0a2f634d3324 72 int DataElement::getInt(const char *key) {
jksoft 0:0a2f634d3324 73 return atoi(getString(key));
jksoft 0:0a2f634d3324 74 }
jksoft 0:0a2f634d3324 75
jksoft 0:0a2f634d3324 76 float DataElement::getFloat(const char *key) {
jksoft 0:0a2f634d3324 77 return atof(getString(key));
jksoft 0:0a2f634d3324 78 }
jksoft 0:0a2f634d3324 79
jksoft 0:0a2f634d3324 80 char *DataElement::toCharArray() {
jksoft 0:0a2f634d3324 81 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 0:0a2f634d3324 82 {
jksoft 0:0a2f634d3324 83 strcat(json_msg,"}");
jksoft 0:0a2f634d3324 84 }
jksoft 0:0a2f634d3324 85 strcat(json_msg,"}");
jksoft 0:0a2f634d3324 86
jksoft 0:0a2f634d3324 87 return(json_msg);
jksoft 0:0a2f634d3324 88 }
jksoft 0:0a2f634d3324 89
jksoft 0:0a2f634d3324 90 Milkcocoa::Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id){
jksoft 0:0a2f634d3324 91 ipstack = new MQTTInterface(nif);
jksoft 0:0a2f634d3324 92 client = new MClient(ipstack);
jksoft 0:0a2f634d3324 93 strcpy(servername,host);
jksoft 0:0a2f634d3324 94 portnum = port;
jksoft 0:0a2f634d3324 95 app_id = _app_id;
jksoft 0:0a2f634d3324 96 strcpy(_clientid,client_id);
jksoft 0:0a2f634d3324 97 strcpy(username,"sdammy");
jksoft 0:0a2f634d3324 98 strcpy(password,app_id);
jksoft 0:0a2f634d3324 99 setLoopCycle(5000);
jksoft 0:0a2f634d3324 100
jksoft 1:8e4149b53a8a 101 cycleThread1.start(Milkcocoa::threadStarter1,this);
jksoft 1:8e4149b53a8a 102 cycleThread2.start(Milkcocoa::threadStarter2,this);
jksoft 0:0a2f634d3324 103 }
jksoft 0:0a2f634d3324 104
jksoft 0:0a2f634d3324 105 Milkcocoa::Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session){
jksoft 0:0a2f634d3324 106 ipstack = new MQTTInterface(nif);
jksoft 0:0a2f634d3324 107 client = new MClient(ipstack);
jksoft 0:0a2f634d3324 108 strcpy(servername,host);
jksoft 0:0a2f634d3324 109 portnum = port;
jksoft 0:0a2f634d3324 110 app_id = _app_id;
jksoft 0:0a2f634d3324 111 strcpy(_clientid,client_id);
jksoft 0:0a2f634d3324 112 strcpy(username,_session);
jksoft 0:0a2f634d3324 113 strcpy(password,app_id);
jksoft 0:0a2f634d3324 114 setLoopCycle(5000);
jksoft 0:0a2f634d3324 115
jksoft 1:8e4149b53a8a 116 cycleThread1.start(Milkcocoa::threadStarter1,this);
jksoft 1:8e4149b53a8a 117 cycleThread2.start(Milkcocoa::threadStarter2,this);
jksoft 0:0a2f634d3324 118 }
jksoft 0:0a2f634d3324 119
jksoft 0:0a2f634d3324 120 Milkcocoa* Milkcocoa::createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) {
jksoft 0:0a2f634d3324 121 char session[60];
jksoft 0:0a2f634d3324 122 sprintf(session, "k%s:%s", key, secret);
jksoft 0:0a2f634d3324 123 return new Milkcocoa(nif, host, port, _app_id, client_id, session);
jksoft 0:0a2f634d3324 124 }
jksoft 0:0a2f634d3324 125
jksoft 0:0a2f634d3324 126 void Milkcocoa::connect() {
jksoft 0:0a2f634d3324 127
jksoft 0:0a2f634d3324 128 if(client->isConnected())
jksoft 0:0a2f634d3324 129 return;
jksoft 0:0a2f634d3324 130
jksoft 0:0a2f634d3324 131 if(client->connect(servername, portnum)!=0) {
jksoft 0:0a2f634d3324 132 DBG(pc.printf("Network connect err\r\n");)
jksoft 0:0a2f634d3324 133 return;
jksoft 0:0a2f634d3324 134 }
jksoft 0:0a2f634d3324 135
jksoft 0:0a2f634d3324 136 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
jksoft 0:0a2f634d3324 137 data.keepAliveInterval = 20;
jksoft 0:0a2f634d3324 138 data.cleansession = 1;
jksoft 0:0a2f634d3324 139 data.MQTTVersion = 4;
jksoft 0:0a2f634d3324 140 data.clientID.cstring = _clientid;
jksoft 0:0a2f634d3324 141 data.username.cstring = username;
jksoft 0:0a2f634d3324 142 data.password.cstring = password;
jksoft 0:0a2f634d3324 143
jksoft 0:0a2f634d3324 144 if (client->connect(data) != 0) {
jksoft 0:0a2f634d3324 145 DBG(pc.printf("Milkcocoa connect err\r\n");)
jksoft 0:0a2f634d3324 146 return;
jksoft 0:0a2f634d3324 147 }
jksoft 0:0a2f634d3324 148
jksoft 0:0a2f634d3324 149 }
jksoft 0:0a2f634d3324 150
jksoft 0:0a2f634d3324 151 bool Milkcocoa::push(const char *path, DataElement dataelement) {
jksoft 0:0a2f634d3324 152 milkcocoa_message_t *message = message_box.alloc();
jksoft 0:0a2f634d3324 153 char *buf;
jksoft 0:0a2f634d3324 154
jksoft 0:0a2f634d3324 155 if(message == NULL) return false;
jksoft 0:0a2f634d3324 156
jksoft 0:0a2f634d3324 157 sprintf(message->topic, "%s/%s/push", app_id, path);
jksoft 0:0a2f634d3324 158 buf = dataelement.toCharArray();
jksoft 0:0a2f634d3324 159 strcpy(message->message , buf);
jksoft 0:0a2f634d3324 160
jksoft 0:0a2f634d3324 161 osStatus stat = message_box.put(message);
jksoft 0:0a2f634d3324 162
jksoft 0:0a2f634d3324 163 if( stat != osOK ) return false;
jksoft 0:0a2f634d3324 164
jksoft 0:0a2f634d3324 165 return true;
jksoft 0:0a2f634d3324 166 }
jksoft 0:0a2f634d3324 167
jksoft 3:cddf81a87de3 168 bool Milkcocoa::push(const char *path, char *data) {
jksoft 3:cddf81a87de3 169 milkcocoa_message_t *message = message_box.alloc();
jksoft 3:cddf81a87de3 170 char *buf;
jksoft 3:cddf81a87de3 171
jksoft 3:cddf81a87de3 172 if(message == NULL) return false;
jksoft 3:cddf81a87de3 173
jksoft 3:cddf81a87de3 174 sprintf(message->topic, "%s/%s/push", app_id, path);
jksoft 3:cddf81a87de3 175
jksoft 3:cddf81a87de3 176 strcpy(message->message , "{\"params\":{");
jksoft 3:cddf81a87de3 177 strcat(message->message , data);
jksoft 3:cddf81a87de3 178 strcat(message->message , "}");
jksoft 3:cddf81a87de3 179
jksoft 3:cddf81a87de3 180 osStatus stat = message_box.put(message);
jksoft 3:cddf81a87de3 181
jksoft 3:cddf81a87de3 182 if( stat != osOK ) return false;
jksoft 3:cddf81a87de3 183
jksoft 3:cddf81a87de3 184 return true;
jksoft 3:cddf81a87de3 185 }
jksoft 3:cddf81a87de3 186
jksoft 0:0a2f634d3324 187 bool Milkcocoa::send(const char *path, DataElement dataelement) {
jksoft 0:0a2f634d3324 188 milkcocoa_message_t *message = message_box.alloc();
jksoft 0:0a2f634d3324 189 char *buf;
jksoft 0:0a2f634d3324 190
jksoft 0:0a2f634d3324 191 if(message == NULL) return false;
jksoft 0:0a2f634d3324 192
jksoft 0:0a2f634d3324 193 sprintf(message->topic, "%s/%s/send", app_id, path);
jksoft 0:0a2f634d3324 194 buf = dataelement.toCharArray();
jksoft 0:0a2f634d3324 195 strcpy(message->message , buf);
jksoft 0:0a2f634d3324 196
jksoft 0:0a2f634d3324 197 osStatus stat = message_box.put(message);
jksoft 0:0a2f634d3324 198
jksoft 0:0a2f634d3324 199 if( stat != osOK ) return false;
jksoft 0:0a2f634d3324 200
jksoft 0:0a2f634d3324 201 return true;
jksoft 0:0a2f634d3324 202 }
jksoft 0:0a2f634d3324 203
jksoft 3:cddf81a87de3 204 bool Milkcocoa::send(const char *path, char *data) {
jksoft 3:cddf81a87de3 205 milkcocoa_message_t *message = message_box.alloc();
jksoft 3:cddf81a87de3 206 char *buf;
jksoft 3:cddf81a87de3 207
jksoft 3:cddf81a87de3 208 if(message == NULL) return false;
jksoft 3:cddf81a87de3 209
jksoft 3:cddf81a87de3 210 sprintf(message->topic, "%s/%s/send", app_id, path);
jksoft 3:cddf81a87de3 211
jksoft 3:cddf81a87de3 212 strcpy(message->message , "{\"params\":{");
jksoft 3:cddf81a87de3 213 strcat(message->message , data);
jksoft 3:cddf81a87de3 214 strcat(message->message , "}");
jksoft 3:cddf81a87de3 215
jksoft 3:cddf81a87de3 216 osStatus stat = message_box.put(message);
jksoft 3:cddf81a87de3 217
jksoft 3:cddf81a87de3 218 if( stat != osOK ) return false;
jksoft 3:cddf81a87de3 219
jksoft 3:cddf81a87de3 220 return true;
jksoft 3:cddf81a87de3 221 }
jksoft 3:cddf81a87de3 222
jksoft 0:0a2f634d3324 223 void Milkcocoa::loop() {
jksoft 0:0a2f634d3324 224 connect();
jksoft 0:0a2f634d3324 225 client->yield(RECV_TIMEOUT);
jksoft 0:0a2f634d3324 226 }
jksoft 0:0a2f634d3324 227
jksoft 0:0a2f634d3324 228 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
jksoft 0:0a2f634d3324 229 MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb);
jksoft 0:0a2f634d3324 230 sprintf(sub->topic, "%s/%s/%s", app_id, path, event);
jksoft 0:0a2f634d3324 231
jksoft 0:0a2f634d3324 232 if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) {
jksoft 0:0a2f634d3324 233 DBG(pc.printf("Milkcocoa subscribe err\r\n");)
jksoft 0:0a2f634d3324 234 return false;
jksoft 0:0a2f634d3324 235 }
jksoft 0:0a2f634d3324 236 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 0:0a2f634d3324 237 if (milkcocoaSubscribers[i] == sub) {
jksoft 0:0a2f634d3324 238 return false;
jksoft 0:0a2f634d3324 239 }
jksoft 0:0a2f634d3324 240 }
jksoft 0:0a2f634d3324 241 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 0:0a2f634d3324 242 if (milkcocoaSubscribers[i] == 0) {
jksoft 0:0a2f634d3324 243 milkcocoaSubscribers[i] = sub;
jksoft 0:0a2f634d3324 244 return true;
jksoft 0:0a2f634d3324 245 }
jksoft 0:0a2f634d3324 246 }
jksoft 0:0a2f634d3324 247 return true;
jksoft 0:0a2f634d3324 248 }
jksoft 0:0a2f634d3324 249
jksoft 0:0a2f634d3324 250 void Milkcocoa::setLoopCycle(int cycle) {
jksoft 0:0a2f634d3324 251 loop_cycle = cycle;
jksoft 0:0a2f634d3324 252 }
jksoft 0:0a2f634d3324 253
jksoft 0:0a2f634d3324 254 void Milkcocoa::start() {
jksoft 1:8e4149b53a8a 255 cycleThread1.signal_set(START_THREAD);
jksoft 1:8e4149b53a8a 256 cycleThread2.signal_set(START_THREAD);
jksoft 0:0a2f634d3324 257 }
jksoft 0:0a2f634d3324 258
jksoft 1:8e4149b53a8a 259 void Milkcocoa::cycle_Thread1(void) {
jksoft 1:8e4149b53a8a 260 cycleThread1.signal_wait(START_THREAD);
jksoft 0:0a2f634d3324 261 while(1) {
jksoft 0:0a2f634d3324 262 Timer timer;
jksoft 0:0a2f634d3324 263 timer.start();
jksoft 0:0a2f634d3324 264 connect();
jksoft 1:8e4149b53a8a 265
jksoft 1:8e4149b53a8a 266 client->yield(RECV_TIMEOUT);
jksoft 0:0a2f634d3324 267
jksoft 1:8e4149b53a8a 268 int sub_time = loop_cycle - timer.read();
jksoft 1:8e4149b53a8a 269 if( sub_time > 0 ){
jksoft 1:8e4149b53a8a 270 Thread::wait(sub_time);
jksoft 1:8e4149b53a8a 271 }
jksoft 1:8e4149b53a8a 272 timer.stop();
jksoft 1:8e4149b53a8a 273 }
jksoft 1:8e4149b53a8a 274 }
jksoft 1:8e4149b53a8a 275 void Milkcocoa::cycle_Thread2(void) {
jksoft 1:8e4149b53a8a 276 cycleThread2.signal_wait(START_THREAD);
jksoft 1:8e4149b53a8a 277 while(1) {
jksoft 1:8e4149b53a8a 278
jksoft 0:0a2f634d3324 279 osEvent evt = message_box.get();
jksoft 0:0a2f634d3324 280 if (evt.status == osEventMail) {
jksoft 0:0a2f634d3324 281 milkcocoa_message_t *message = (milkcocoa_message_t*)evt.value.p;
jksoft 0:0a2f634d3324 282 MQTT::Message mq_message;
jksoft 0:0a2f634d3324 283
jksoft 0:0a2f634d3324 284 if(message == NULL) break;
jksoft 0:0a2f634d3324 285
jksoft 0:0a2f634d3324 286 mq_message.qos = MQTT::QOS0;
jksoft 0:0a2f634d3324 287 mq_message.retained = 0;
jksoft 0:0a2f634d3324 288 mq_message.dup = false;
jksoft 0:0a2f634d3324 289 mq_message.payload = (void*)message->message;
jksoft 0:0a2f634d3324 290 mq_message.payloadlen = strlen(message->message);
jksoft 0:0a2f634d3324 291
jksoft 0:0a2f634d3324 292 client->publish(message->topic, mq_message);
jksoft 0:0a2f634d3324 293
jksoft 0:0a2f634d3324 294 message_box.free(message);
jksoft 1:8e4149b53a8a 295 }
jksoft 1:8e4149b53a8a 296 Thread::wait(1000);
jksoft 0:0a2f634d3324 297 }
jksoft 0:0a2f634d3324 298 }
jksoft 0:0a2f634d3324 299
jksoft 1:8e4149b53a8a 300 void Milkcocoa::threadStarter1(void const *p) {
jksoft 0:0a2f634d3324 301 Milkcocoa *instance = (Milkcocoa*)p;
jksoft 1:8e4149b53a8a 302 instance->cycle_Thread1();
jksoft 1:8e4149b53a8a 303 }
jksoft 1:8e4149b53a8a 304 void Milkcocoa::threadStarter2(void const *p) {
jksoft 1:8e4149b53a8a 305 Milkcocoa *instance = (Milkcocoa*)p;
jksoft 1:8e4149b53a8a 306 instance->cycle_Thread2();
jksoft 0:0a2f634d3324 307 }
jksoft 0:0a2f634d3324 308
jksoft 0:0a2f634d3324 309 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
jksoft 0:0a2f634d3324 310 cb = _cb;
jksoft 0:0a2f634d3324 311 }
jksoft 0:0a2f634d3324 312