mbed OS5に対応したMilkcocoaライブラリのテストバージョンです。

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
jksoft
Date:
Tue Jan 24 13:41:36 2017 +0000
Revision:
24:6ba1245bf049
??????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 24:6ba1245bf049 1 #include "Milkcocoa.h"
jksoft 24:6ba1245bf049 2
jksoft 24:6ba1245bf049 3
jksoft 24:6ba1245bf049 4 #if 0
jksoft 24:6ba1245bf049 5 #if 0
jksoft 24:6ba1245bf049 6 #include "SoftSerialSendOnry.h"
jksoft 24:6ba1245bf049 7 extern SoftSerialSendOnry pc;
jksoft 24:6ba1245bf049 8 #else
jksoft 24:6ba1245bf049 9 extern Serial pc;
jksoft 24:6ba1245bf049 10 #endif
jksoft 24:6ba1245bf049 11 #define DBG(x) x
jksoft 24:6ba1245bf049 12 #else
jksoft 24:6ba1245bf049 13 #define DBG(x)
jksoft 24:6ba1245bf049 14 #endif
jksoft 24:6ba1245bf049 15
jksoft 24:6ba1245bf049 16 DataElement::DataElement() {
jksoft 24:6ba1245bf049 17 json_msg[0] = '\0';
jksoft 24:6ba1245bf049 18 strcpy(json_msg,"{\"params\":{");
jksoft 24:6ba1245bf049 19 }
jksoft 24:6ba1245bf049 20
jksoft 24:6ba1245bf049 21 DataElement::DataElement(char *json_string) {
jksoft 24:6ba1245bf049 22 json_msg[0] = '\0';
jksoft 24:6ba1245bf049 23 strcpy(json_msg,json_string);
jksoft 24:6ba1245bf049 24 }
jksoft 24:6ba1245bf049 25
jksoft 24:6ba1245bf049 26 void DataElement::setValue(const char *key, const char *v) {
jksoft 24:6ba1245bf049 27 char json_string[64];
jksoft 24:6ba1245bf049 28 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 24:6ba1245bf049 29 {
jksoft 24:6ba1245bf049 30 strcat(json_msg,",");
jksoft 24:6ba1245bf049 31 }
jksoft 24:6ba1245bf049 32 sprintf(json_string,"\"%s\":\"%s\"",key,v);
jksoft 24:6ba1245bf049 33 strcat(json_msg,json_string);
jksoft 24:6ba1245bf049 34 }
jksoft 24:6ba1245bf049 35
jksoft 24:6ba1245bf049 36 void DataElement::setValue(const char *key, int v) {
jksoft 24:6ba1245bf049 37 char json_string[64];
jksoft 24:6ba1245bf049 38 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 24:6ba1245bf049 39 {
jksoft 24:6ba1245bf049 40 strcat(json_msg,",");
jksoft 24:6ba1245bf049 41 }
jksoft 24:6ba1245bf049 42 sprintf(json_string,"\"%s\":\"%d\"",key,v);
jksoft 24:6ba1245bf049 43 strcat(json_msg,json_string);
jksoft 24:6ba1245bf049 44 }
jksoft 24:6ba1245bf049 45
jksoft 24:6ba1245bf049 46 void DataElement::setValue(const char *key, double v) {
jksoft 24:6ba1245bf049 47 char json_string[64];
jksoft 24:6ba1245bf049 48 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 24:6ba1245bf049 49 {
jksoft 24:6ba1245bf049 50 strcat(json_msg,",");
jksoft 24:6ba1245bf049 51 }
jksoft 24:6ba1245bf049 52 sprintf(json_string,"\"%s\":\"%f\"",key,v);
jksoft 24:6ba1245bf049 53 strcat(json_msg,json_string);
jksoft 24:6ba1245bf049 54 }
jksoft 24:6ba1245bf049 55
jksoft 24:6ba1245bf049 56 char *DataElement::getString(const char *key) {
jksoft 24:6ba1245bf049 57 static char _word[64];
jksoft 24:6ba1245bf049 58 char *p;
jksoft 24:6ba1245bf049 59 int i=0;
jksoft 24:6ba1245bf049 60
jksoft 24:6ba1245bf049 61 strcpy(_word , "\"\0");
jksoft 24:6ba1245bf049 62 strcat(_word , key );
jksoft 24:6ba1245bf049 63 strcat(_word , "\"" );
jksoft 24:6ba1245bf049 64
jksoft 24:6ba1245bf049 65 p = strstr( (char*)json_msg , _word ) + 2 + strlen(_word);
jksoft 24:6ba1245bf049 66
jksoft 24:6ba1245bf049 67 while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '\"') )
jksoft 24:6ba1245bf049 68 {
jksoft 24:6ba1245bf049 69 _word[i] = p[i];
jksoft 24:6ba1245bf049 70 i++;
jksoft 24:6ba1245bf049 71 }
jksoft 24:6ba1245bf049 72 _word[i] = '\0';
jksoft 24:6ba1245bf049 73
jksoft 24:6ba1245bf049 74 return _word;
jksoft 24:6ba1245bf049 75 }
jksoft 24:6ba1245bf049 76
jksoft 24:6ba1245bf049 77 int DataElement::getInt(const char *key) {
jksoft 24:6ba1245bf049 78 return atoi(getString(key));
jksoft 24:6ba1245bf049 79 }
jksoft 24:6ba1245bf049 80
jksoft 24:6ba1245bf049 81 float DataElement::getFloat(const char *key) {
jksoft 24:6ba1245bf049 82 return atof(getString(key));
jksoft 24:6ba1245bf049 83 }
jksoft 24:6ba1245bf049 84
jksoft 24:6ba1245bf049 85 char *DataElement::toCharArray() {
jksoft 24:6ba1245bf049 86 if( json_msg[strlen(json_msg)-1] != '{' )
jksoft 24:6ba1245bf049 87 {
jksoft 24:6ba1245bf049 88 strcat(json_msg,"}");
jksoft 24:6ba1245bf049 89 }
jksoft 24:6ba1245bf049 90 strcat(json_msg,"}");
jksoft 24:6ba1245bf049 91
jksoft 24:6ba1245bf049 92 return(json_msg);
jksoft 24:6ba1245bf049 93 }
jksoft 24:6ba1245bf049 94
jksoft 24:6ba1245bf049 95 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id) :cycleThread(Milkcocoa::threadStarter){
jksoft 24:6ba1245bf049 96
jksoft 24:6ba1245bf049 97 client = _client;
jksoft 24:6ba1245bf049 98 strcpy(servername,host);
jksoft 24:6ba1245bf049 99 portnum = port;
jksoft 24:6ba1245bf049 100 app_id = _app_id;
jksoft 24:6ba1245bf049 101 strcpy(_clientid,client_id);
jksoft 24:6ba1245bf049 102 strcpy(username,"sdammy");
jksoft 24:6ba1245bf049 103 strcpy(password,app_id);
jksoft 24:6ba1245bf049 104 setLoopCycle(7000);
jksoft 24:6ba1245bf049 105 }
jksoft 24:6ba1245bf049 106
jksoft 24:6ba1245bf049 107 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) :cycleThread(Milkcocoa::threadStarter){
jksoft 24:6ba1245bf049 108
jksoft 24:6ba1245bf049 109 client = _client;
jksoft 24:6ba1245bf049 110 strcpy(servername,host);
jksoft 24:6ba1245bf049 111 portnum = port;
jksoft 24:6ba1245bf049 112 app_id = _app_id;
jksoft 24:6ba1245bf049 113 strcpy(_clientid,client_id);
jksoft 24:6ba1245bf049 114 strcpy(username,_session);
jksoft 24:6ba1245bf049 115 strcpy(password,app_id);
jksoft 24:6ba1245bf049 116 setLoopCycle(7000);
jksoft 24:6ba1245bf049 117 }
jksoft 24:6ba1245bf049 118
jksoft 24:6ba1245bf049 119 Milkcocoa* Milkcocoa::createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) {
jksoft 24:6ba1245bf049 120 char session[60];
jksoft 24:6ba1245bf049 121 sprintf(session, "k%s:%s", key, secret);
jksoft 24:6ba1245bf049 122 return new Milkcocoa(_client, host, port, _app_id, client_id, session);
jksoft 24:6ba1245bf049 123 }
jksoft 24:6ba1245bf049 124
jksoft 24:6ba1245bf049 125 void Milkcocoa::connect() {
jksoft 24:6ba1245bf049 126
jksoft 24:6ba1245bf049 127 if(client->isConnected())
jksoft 24:6ba1245bf049 128 return;
jksoft 24:6ba1245bf049 129
jksoft 24:6ba1245bf049 130 if(client->connect(servername, portnum)!=0) {
jksoft 24:6ba1245bf049 131 DBG(pc.printf("Network connect err\r\n");)
jksoft 24:6ba1245bf049 132 return;
jksoft 24:6ba1245bf049 133 }
jksoft 24:6ba1245bf049 134
jksoft 24:6ba1245bf049 135 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
jksoft 24:6ba1245bf049 136 data.keepAliveInterval = 20;
jksoft 24:6ba1245bf049 137 data.cleansession = 1;
jksoft 24:6ba1245bf049 138 data.MQTTVersion = 4;
jksoft 24:6ba1245bf049 139 data.clientID.cstring = _clientid;
jksoft 24:6ba1245bf049 140 data.username.cstring = username;
jksoft 24:6ba1245bf049 141 data.password.cstring = password;
jksoft 24:6ba1245bf049 142
jksoft 24:6ba1245bf049 143 if (client->connect(data) != 0) {
jksoft 24:6ba1245bf049 144 DBG(pc.printf("Milkcocoa connect err\r\n");)
jksoft 24:6ba1245bf049 145 return;
jksoft 24:6ba1245bf049 146 }
jksoft 24:6ba1245bf049 147
jksoft 24:6ba1245bf049 148 }
jksoft 24:6ba1245bf049 149
jksoft 24:6ba1245bf049 150 bool Milkcocoa::push(const char *path, DataElement dataelement) {
jksoft 24:6ba1245bf049 151 char topic[100];
jksoft 24:6ba1245bf049 152 char *buf;
jksoft 24:6ba1245bf049 153 MQTT::Message message;
jksoft 24:6ba1245bf049 154
jksoft 24:6ba1245bf049 155 sprintf(topic, "%s/%s/push", app_id, path);
jksoft 24:6ba1245bf049 156
jksoft 24:6ba1245bf049 157 message.qos = MQTT::QOS0;
jksoft 24:6ba1245bf049 158 message.retained = 0;
jksoft 24:6ba1245bf049 159 message.dup = false;
jksoft 24:6ba1245bf049 160 buf = dataelement.toCharArray();
jksoft 24:6ba1245bf049 161 message.payload = (void*)buf;
jksoft 24:6ba1245bf049 162 message.payloadlen = strlen(buf);
jksoft 24:6ba1245bf049 163 if(client->publish(topic, message)!=0)
jksoft 24:6ba1245bf049 164 return(false);
jksoft 24:6ba1245bf049 165
jksoft 24:6ba1245bf049 166 return true;
jksoft 24:6ba1245bf049 167 }
jksoft 24:6ba1245bf049 168
jksoft 24:6ba1245bf049 169 bool Milkcocoa::send(const char *path, DataElement dataelement) {
jksoft 24:6ba1245bf049 170 char topic[100];
jksoft 24:6ba1245bf049 171 char *buf;
jksoft 24:6ba1245bf049 172 MQTT::Message message;
jksoft 24:6ba1245bf049 173
jksoft 24:6ba1245bf049 174 sprintf(topic, "%s/%s/send", app_id, path);
jksoft 24:6ba1245bf049 175 message.qos = MQTT::QOS0;
jksoft 24:6ba1245bf049 176 message.retained = 0;
jksoft 24:6ba1245bf049 177 message.dup = false;
jksoft 24:6ba1245bf049 178 buf = dataelement.toCharArray();
jksoft 24:6ba1245bf049 179 message.payload = (void*)buf;
jksoft 24:6ba1245bf049 180 message.payloadlen = strlen(buf);
jksoft 24:6ba1245bf049 181 if(client->publish(topic, message)!=0)
jksoft 24:6ba1245bf049 182 return false;
jksoft 24:6ba1245bf049 183
jksoft 24:6ba1245bf049 184 return true;
jksoft 24:6ba1245bf049 185 }
jksoft 24:6ba1245bf049 186
jksoft 24:6ba1245bf049 187 void Milkcocoa::loop() {
jksoft 24:6ba1245bf049 188 connect();
jksoft 24:6ba1245bf049 189 client->yield(RECV_TIMEOUT);
jksoft 24:6ba1245bf049 190 }
jksoft 24:6ba1245bf049 191
jksoft 24:6ba1245bf049 192 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
jksoft 24:6ba1245bf049 193 MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb);
jksoft 24:6ba1245bf049 194 sprintf(sub->topic, "%s/%s/%s", app_id, path, event);
jksoft 24:6ba1245bf049 195
jksoft 24:6ba1245bf049 196 if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) {
jksoft 24:6ba1245bf049 197 DBG(pc.printf("Milkcocoa subscribe err\r\n");)
jksoft 24:6ba1245bf049 198 return false;
jksoft 24:6ba1245bf049 199 }
jksoft 24:6ba1245bf049 200 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 24:6ba1245bf049 201 if (milkcocoaSubscribers[i] == sub) {
jksoft 24:6ba1245bf049 202 return false;
jksoft 24:6ba1245bf049 203 }
jksoft 24:6ba1245bf049 204 }
jksoft 24:6ba1245bf049 205 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) {
jksoft 24:6ba1245bf049 206 if (milkcocoaSubscribers[i] == 0) {
jksoft 24:6ba1245bf049 207 milkcocoaSubscribers[i] = sub;
jksoft 24:6ba1245bf049 208 return true;
jksoft 24:6ba1245bf049 209 }
jksoft 24:6ba1245bf049 210 }
jksoft 24:6ba1245bf049 211 return true;
jksoft 24:6ba1245bf049 212 }
jksoft 24:6ba1245bf049 213
jksoft 24:6ba1245bf049 214 void Milkcocoa::setLoopCycle(int cycle) {
jksoft 24:6ba1245bf049 215 loop_cycle = cycle;
jksoft 24:6ba1245bf049 216 }
jksoft 24:6ba1245bf049 217
jksoft 24:6ba1245bf049 218 void Milkcocoa::start() {
jksoft 24:6ba1245bf049 219 cycleThread.signal_set(START_THREAD);
jksoft 24:6ba1245bf049 220 }
jksoft 24:6ba1245bf049 221
jksoft 24:6ba1245bf049 222 void Milkcocoa::cycle_Thread(void) {
jksoft 24:6ba1245bf049 223 cycleThread.signal_wait(START_THREAD);
jksoft 24:6ba1245bf049 224 while(1) {
jksoft 24:6ba1245bf049 225 Timer timer;
jksoft 24:6ba1245bf049 226 timer.start();
jksoft 24:6ba1245bf049 227 connect();
jksoft 24:6ba1245bf049 228 client->yield(RECV_TIMEOUT);
jksoft 24:6ba1245bf049 229 timer.stop();
jksoft 24:6ba1245bf049 230 if( timer.read() < loop_cycle ) Thread::wait(loop_cycle - timer.read());
jksoft 24:6ba1245bf049 231 }
jksoft 24:6ba1245bf049 232 }
jksoft 24:6ba1245bf049 233
jksoft 24:6ba1245bf049 234 void Milkcocoa::threadStarter(void const *p) {
jksoft 24:6ba1245bf049 235 Milkcocoa *instance = (Milkcocoa*)p;
jksoft 24:6ba1245bf049 236 instance->cycle_Thread();
jksoft 24:6ba1245bf049 237 }
jksoft 24:6ba1245bf049 238
jksoft 24:6ba1245bf049 239 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
jksoft 24:6ba1245bf049 240 cb = _cb;
jksoft 24:6ba1245bf049 241 }
jksoft 24:6ba1245bf049 242