Junichi Katsu / Milkcocoa-os

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

Committer:
jksoft
Date:
Thu Feb 09 07:26:57 2017 +0000
Revision:
0:0a2f634d3324
Child:
1:8e4149b53a8a
??

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 0:0a2f634d3324 101 cycleThread.start(Milkcocoa::threadStarter,this);
jksoft 0:0a2f634d3324 102 }
jksoft 0:0a2f634d3324 103
jksoft 0:0a2f634d3324 104 Milkcocoa::Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session){
jksoft 0:0a2f634d3324 105 ipstack = new MQTTInterface(nif);
jksoft 0:0a2f634d3324 106 client = new MClient(ipstack);
jksoft 0:0a2f634d3324 107 strcpy(servername,host);
jksoft 0:0a2f634d3324 108 portnum = port;
jksoft 0:0a2f634d3324 109 app_id = _app_id;
jksoft 0:0a2f634d3324 110 strcpy(_clientid,client_id);
jksoft 0:0a2f634d3324 111 strcpy(username,_session);
jksoft 0:0a2f634d3324 112 strcpy(password,app_id);
jksoft 0:0a2f634d3324 113 setLoopCycle(5000);
jksoft 0:0a2f634d3324 114
jksoft 0:0a2f634d3324 115 cycleThread.start(Milkcocoa::threadStarter,this);
jksoft 0:0a2f634d3324 116 }
jksoft 0:0a2f634d3324 117
jksoft 0:0a2f634d3324 118 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 119 char session[60];
jksoft 0:0a2f634d3324 120 sprintf(session, "k%s:%s", key, secret);
jksoft 0:0a2f634d3324 121 return new Milkcocoa(nif, host, port, _app_id, client_id, session);
jksoft 0:0a2f634d3324 122 }
jksoft 0:0a2f634d3324 123
jksoft 0:0a2f634d3324 124 void Milkcocoa::connect() {
jksoft 0:0a2f634d3324 125
jksoft 0:0a2f634d3324 126 if(client->isConnected())
jksoft 0:0a2f634d3324 127 return;
jksoft 0:0a2f634d3324 128
jksoft 0:0a2f634d3324 129 if(client->connect(servername, portnum)!=0) {
jksoft 0:0a2f634d3324 130 DBG(pc.printf("Network connect err\r\n");)
jksoft 0:0a2f634d3324 131 return;
jksoft 0:0a2f634d3324 132 }
jksoft 0:0a2f634d3324 133
jksoft 0:0a2f634d3324 134 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
jksoft 0:0a2f634d3324 135 data.keepAliveInterval = 20;
jksoft 0:0a2f634d3324 136 data.cleansession = 1;
jksoft 0:0a2f634d3324 137 data.MQTTVersion = 4;
jksoft 0:0a2f634d3324 138 data.clientID.cstring = _clientid;
jksoft 0:0a2f634d3324 139 data.username.cstring = username;
jksoft 0:0a2f634d3324 140 data.password.cstring = password;
jksoft 0:0a2f634d3324 141
jksoft 0:0a2f634d3324 142 if (client->connect(data) != 0) {
jksoft 0:0a2f634d3324 143 DBG(pc.printf("Milkcocoa connect err\r\n");)
jksoft 0:0a2f634d3324 144 return;
jksoft 0:0a2f634d3324 145 }
jksoft 0:0a2f634d3324 146
jksoft 0:0a2f634d3324 147 }
jksoft 0:0a2f634d3324 148
jksoft 0:0a2f634d3324 149 bool Milkcocoa::push(const char *path, DataElement dataelement) {
jksoft 0:0a2f634d3324 150 milkcocoa_message_t *message = message_box.alloc();
jksoft 0:0a2f634d3324 151 char *buf;
jksoft 0:0a2f634d3324 152
jksoft 0:0a2f634d3324 153 if(message == NULL) return false;
jksoft 0:0a2f634d3324 154
jksoft 0:0a2f634d3324 155 sprintf(message->topic, "%s/%s/push", app_id, path);
jksoft 0:0a2f634d3324 156 buf = dataelement.toCharArray();
jksoft 0:0a2f634d3324 157 strcpy(message->message , buf);
jksoft 0:0a2f634d3324 158
jksoft 0:0a2f634d3324 159 osStatus stat = message_box.put(message);
jksoft 0:0a2f634d3324 160
jksoft 0:0a2f634d3324 161 if( stat != osOK ) return false;
jksoft 0:0a2f634d3324 162
jksoft 0:0a2f634d3324 163 return true;
jksoft 0:0a2f634d3324 164 }
jksoft 0:0a2f634d3324 165
jksoft 0:0a2f634d3324 166 bool Milkcocoa::send(const char *path, DataElement dataelement) {
jksoft 0:0a2f634d3324 167 milkcocoa_message_t *message = message_box.alloc();
jksoft 0:0a2f634d3324 168 char *buf;
jksoft 0:0a2f634d3324 169
jksoft 0:0a2f634d3324 170 if(message == NULL) return false;
jksoft 0:0a2f634d3324 171
jksoft 0:0a2f634d3324 172 sprintf(message->topic, "%s/%s/send", app_id, path);
jksoft 0:0a2f634d3324 173 buf = dataelement.toCharArray();
jksoft 0:0a2f634d3324 174 strcpy(message->message , buf);
jksoft 0:0a2f634d3324 175
jksoft 0:0a2f634d3324 176 osStatus stat = message_box.put(message);
jksoft 0:0a2f634d3324 177
jksoft 0:0a2f634d3324 178 if( stat != osOK ) return false;
jksoft 0:0a2f634d3324 179
jksoft 0:0a2f634d3324 180 return true;
jksoft 0:0a2f634d3324 181 }
jksoft 0:0a2f634d3324 182
jksoft 0:0a2f634d3324 183 void Milkcocoa::loop() {
jksoft 0:0a2f634d3324 184 connect();
jksoft 0:0a2f634d3324 185 client->yield(RECV_TIMEOUT);
jksoft 0:0a2f634d3324 186 }
jksoft 0:0a2f634d3324 187
jksoft 0:0a2f634d3324 188 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
jksoft 0:0a2f634d3324 189 MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb);
jksoft 0:0a2f634d3324 190 sprintf(sub->topic, "%s/%s/%s", app_id, path, event);
jksoft 0:0a2f634d3324 191
jksoft 0:0a2f634d3324 192 if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) {
jksoft 0:0a2f634d3324 193 DBG(pc.printf("Milkcocoa subscribe err\r\n");)
jksoft 0:0a2f634d3324 194 return false;
jksoft 0:0a2f634d3324 195 }
jksoft 0:0a2f634d3324 196 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 0:0a2f634d3324 197 if (milkcocoaSubscribers[i] == sub) {
jksoft 0:0a2f634d3324 198 return false;
jksoft 0:0a2f634d3324 199 }
jksoft 0:0a2f634d3324 200 }
jksoft 0:0a2f634d3324 201 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 0:0a2f634d3324 202 if (milkcocoaSubscribers[i] == 0) {
jksoft 0:0a2f634d3324 203 milkcocoaSubscribers[i] = sub;
jksoft 0:0a2f634d3324 204 return true;
jksoft 0:0a2f634d3324 205 }
jksoft 0:0a2f634d3324 206 }
jksoft 0:0a2f634d3324 207 return true;
jksoft 0:0a2f634d3324 208 }
jksoft 0:0a2f634d3324 209
jksoft 0:0a2f634d3324 210 void Milkcocoa::setLoopCycle(int cycle) {
jksoft 0:0a2f634d3324 211 loop_cycle = cycle;
jksoft 0:0a2f634d3324 212 }
jksoft 0:0a2f634d3324 213
jksoft 0:0a2f634d3324 214 void Milkcocoa::start() {
jksoft 0:0a2f634d3324 215 cycleThread.signal_set(START_THREAD);
jksoft 0:0a2f634d3324 216 }
jksoft 0:0a2f634d3324 217
jksoft 0:0a2f634d3324 218 void Milkcocoa::cycle_Thread(void) {
jksoft 0:0a2f634d3324 219 cycleThread.signal_wait(START_THREAD);
jksoft 0:0a2f634d3324 220 while(1) {
jksoft 0:0a2f634d3324 221 Timer timer;
jksoft 0:0a2f634d3324 222 timer.start();
jksoft 0:0a2f634d3324 223 connect();
jksoft 0:0a2f634d3324 224
jksoft 0:0a2f634d3324 225 osEvent evt = message_box.get();
jksoft 0:0a2f634d3324 226 if (evt.status == osEventMail) {
jksoft 0:0a2f634d3324 227 milkcocoa_message_t *message = (milkcocoa_message_t*)evt.value.p;
jksoft 0:0a2f634d3324 228 MQTT::Message mq_message;
jksoft 0:0a2f634d3324 229
jksoft 0:0a2f634d3324 230 if(message == NULL) break;
jksoft 0:0a2f634d3324 231
jksoft 0:0a2f634d3324 232 mq_message.qos = MQTT::QOS0;
jksoft 0:0a2f634d3324 233 mq_message.retained = 0;
jksoft 0:0a2f634d3324 234 mq_message.dup = false;
jksoft 0:0a2f634d3324 235 mq_message.payload = (void*)message->message;
jksoft 0:0a2f634d3324 236 mq_message.payloadlen = strlen(message->message);
jksoft 0:0a2f634d3324 237
jksoft 0:0a2f634d3324 238 client->publish(message->topic, mq_message);
jksoft 0:0a2f634d3324 239
jksoft 0:0a2f634d3324 240 message_box.free(message);
jksoft 0:0a2f634d3324 241 }
jksoft 0:0a2f634d3324 242
jksoft 0:0a2f634d3324 243 client->yield(RECV_TIMEOUT);
jksoft 0:0a2f634d3324 244
jksoft 0:0a2f634d3324 245 timer.stop();
jksoft 0:0a2f634d3324 246
jksoft 0:0a2f634d3324 247 if( timer.read() < loop_cycle ) Thread::wait(loop_cycle - timer.read());
jksoft 0:0a2f634d3324 248 }
jksoft 0:0a2f634d3324 249 }
jksoft 0:0a2f634d3324 250
jksoft 0:0a2f634d3324 251 void Milkcocoa::threadStarter(void const *p) {
jksoft 0:0a2f634d3324 252 Milkcocoa *instance = (Milkcocoa*)p;
jksoft 0:0a2f634d3324 253 instance->cycle_Thread();
jksoft 0:0a2f634d3324 254 }
jksoft 0:0a2f634d3324 255
jksoft 0:0a2f634d3324 256 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
jksoft 0:0a2f634d3324 257 cb = _cb;
jksoft 0:0a2f634d3324 258 }
jksoft 0:0a2f634d3324 259