mbed OS5に対応したMilkcocoaライブラリのテストバージョンです。
Fork of mbed-os-example-mbed5-blinky by
Milkcocoa.cpp
00001 #include "Milkcocoa.h" 00002 00003 00004 #if 0 00005 #if 0 00006 #include "SoftSerialSendOnry.h" 00007 extern SoftSerialSendOnry pc; 00008 #else 00009 extern Serial pc; 00010 #endif 00011 #define DBG(x) x 00012 #else 00013 #define DBG(x) 00014 #endif 00015 00016 DataElement::DataElement() { 00017 json_msg[0] = '\0'; 00018 strcpy(json_msg,"{\"params\":{"); 00019 } 00020 00021 DataElement::DataElement(char *json_string) { 00022 json_msg[0] = '\0'; 00023 strcpy(json_msg,json_string); 00024 } 00025 00026 void DataElement::setValue(const char *key, const char *v) { 00027 char json_string[64]; 00028 if( json_msg[strlen(json_msg)-1] != '{' ) 00029 { 00030 strcat(json_msg,","); 00031 } 00032 sprintf(json_string,"\"%s\":\"%s\"",key,v); 00033 strcat(json_msg,json_string); 00034 } 00035 00036 void DataElement::setValue(const char *key, int v) { 00037 char json_string[64]; 00038 if( json_msg[strlen(json_msg)-1] != '{' ) 00039 { 00040 strcat(json_msg,","); 00041 } 00042 sprintf(json_string,"\"%s\":\"%d\"",key,v); 00043 strcat(json_msg,json_string); 00044 } 00045 00046 void DataElement::setValue(const char *key, double v) { 00047 char json_string[64]; 00048 if( json_msg[strlen(json_msg)-1] != '{' ) 00049 { 00050 strcat(json_msg,","); 00051 } 00052 sprintf(json_string,"\"%s\":\"%f\"",key,v); 00053 strcat(json_msg,json_string); 00054 } 00055 00056 char *DataElement::getString(const char *key) { 00057 static char _word[64]; 00058 char *p; 00059 int i=0; 00060 00061 strcpy(_word , "\"\0"); 00062 strcat(_word , key ); 00063 strcat(_word , "\"" ); 00064 00065 p = strstr( (char*)json_msg , _word ) + 2 + strlen(_word); 00066 00067 while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '\"') ) 00068 { 00069 _word[i] = p[i]; 00070 i++; 00071 } 00072 _word[i] = '\0'; 00073 00074 return _word; 00075 } 00076 00077 int DataElement::getInt(const char *key) { 00078 return atoi(getString(key)); 00079 } 00080 00081 float DataElement::getFloat(const char *key) { 00082 return atof(getString(key)); 00083 } 00084 00085 char *DataElement::toCharArray() { 00086 if( json_msg[strlen(json_msg)-1] != '{' ) 00087 { 00088 strcat(json_msg,"}"); 00089 } 00090 strcat(json_msg,"}"); 00091 00092 return(json_msg); 00093 } 00094 00095 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id) :cycleThread(Milkcocoa::threadStarter){ 00096 00097 client = _client; 00098 strcpy(servername,host); 00099 portnum = port; 00100 app_id = _app_id; 00101 strcpy(_clientid,client_id); 00102 strcpy(username,"sdammy"); 00103 strcpy(password,app_id); 00104 setLoopCycle(7000); 00105 } 00106 00107 Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) :cycleThread(Milkcocoa::threadStarter){ 00108 00109 client = _client; 00110 strcpy(servername,host); 00111 portnum = port; 00112 app_id = _app_id; 00113 strcpy(_clientid,client_id); 00114 strcpy(username,_session); 00115 strcpy(password,app_id); 00116 setLoopCycle(7000); 00117 } 00118 00119 Milkcocoa* Milkcocoa::createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) { 00120 char session[60]; 00121 sprintf(session, "k%s:%s", key, secret); 00122 return new Milkcocoa(_client, host, port, _app_id, client_id, session); 00123 } 00124 00125 void Milkcocoa::connect() { 00126 00127 if(client->isConnected()) 00128 return; 00129 00130 if(client->connect(servername, portnum)!=0) { 00131 DBG(pc.printf("Network connect err\r\n");) 00132 return; 00133 } 00134 00135 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00136 data.keepAliveInterval = 20; 00137 data.cleansession = 1; 00138 data.MQTTVersion = 4; 00139 data.clientID.cstring = _clientid; 00140 data.username.cstring = username; 00141 data.password.cstring = password; 00142 00143 if (client->connect(data) != 0) { 00144 DBG(pc.printf("Milkcocoa connect err\r\n");) 00145 return; 00146 } 00147 00148 } 00149 00150 bool Milkcocoa::push(const char *path, DataElement dataelement) { 00151 char topic[100]; 00152 char *buf; 00153 MQTT::Message message; 00154 00155 sprintf(topic, "%s/%s/push", app_id, path); 00156 00157 message.qos = MQTT::QOS0; 00158 message.retained = 0; 00159 message.dup = false; 00160 buf = dataelement.toCharArray(); 00161 message.payload = (void*)buf; 00162 message.payloadlen = strlen(buf); 00163 if(client->publish(topic, message)!=0) 00164 return(false); 00165 00166 return true; 00167 } 00168 00169 bool Milkcocoa::send(const char *path, DataElement dataelement) { 00170 char topic[100]; 00171 char *buf; 00172 MQTT::Message message; 00173 00174 sprintf(topic, "%s/%s/send", app_id, path); 00175 message.qos = MQTT::QOS0; 00176 message.retained = 0; 00177 message.dup = false; 00178 buf = dataelement.toCharArray(); 00179 message.payload = (void*)buf; 00180 message.payloadlen = strlen(buf); 00181 if(client->publish(topic, message)!=0) 00182 return false; 00183 00184 return true; 00185 } 00186 00187 void Milkcocoa::loop() { 00188 connect(); 00189 client->yield(RECV_TIMEOUT); 00190 } 00191 00192 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) { 00193 MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb); 00194 sprintf(sub->topic, "%s/%s/%s", app_id, path, event); 00195 00196 if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) { 00197 DBG(pc.printf("Milkcocoa subscribe err\r\n");) 00198 return false; 00199 } 00200 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { 00201 if (milkcocoaSubscribers[i] == sub) { 00202 return false; 00203 } 00204 } 00205 for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { 00206 if (milkcocoaSubscribers[i] == 0) { 00207 milkcocoaSubscribers[i] = sub; 00208 return true; 00209 } 00210 } 00211 return true; 00212 } 00213 00214 void Milkcocoa::setLoopCycle(int cycle) { 00215 loop_cycle = cycle; 00216 } 00217 00218 void Milkcocoa::start() { 00219 cycleThread.signal_set(START_THREAD); 00220 } 00221 00222 void Milkcocoa::cycle_Thread(void) { 00223 cycleThread.signal_wait(START_THREAD); 00224 while(1) { 00225 Timer timer; 00226 timer.start(); 00227 connect(); 00228 client->yield(RECV_TIMEOUT); 00229 timer.stop(); 00230 if( timer.read() < loop_cycle ) Thread::wait(loop_cycle - timer.read()); 00231 } 00232 } 00233 00234 void Milkcocoa::threadStarter(void const *p) { 00235 Milkcocoa *instance = (Milkcocoa*)p; 00236 instance->cycle_Thread(); 00237 } 00238 00239 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) { 00240 cb = _cb; 00241 } 00242
Generated on Wed Jul 13 2022 21:16:17 by
1.7.2
