Ethernet
Dependents: MilkcocoaSample_Eth_K64F
Fork of Milkcocoa_EthernetIF by
Diff: Milkcocoa.cpp
- Revision:
- 1:4a634c06c5dc
- Parent:
- 0:23e533c4b1ec
- Child:
- 6:f1309bb5b44d
diff -r 23e533c4b1ec -r 4a634c06c5dc Milkcocoa.cpp --- a/Milkcocoa.cpp Tue Dec 15 09:56:32 2015 +0000 +++ b/Milkcocoa.cpp Fri Dec 18 04:30:59 2015 +0000 @@ -1,8 +1,12 @@ #include "Milkcocoa.h" -#if 1 +#if 0 +#if 0 #include "SoftSerialSendOnry.h" extern SoftSerialSendOnry pc; +#else +extern Serial pc; +#endif #define DBG(x) x #else #define DBG(x) @@ -87,9 +91,9 @@ return(json_msg); } -Milkcocoa::Milkcocoa(MQTTESP8266 *ipstack, const char *host, uint16_t port, const char *_app_id, const char *client_id) : client(MQTT::Client<MQTTESP8266, Countdown>(*ipstack)) { +Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id) { - _ipstack = ipstack; + client = _client; strcpy(servername,host); portnum = port; app_id = _app_id; @@ -99,9 +103,9 @@ } -Milkcocoa::Milkcocoa(MQTTESP8266 *ipstack, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) : client(MQTT::Client<MQTTESP8266, Countdown>(*ipstack)) { +Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) { - _ipstack = ipstack; + client = _client; strcpy(servername,host); portnum = port; app_id = _app_id; @@ -111,18 +115,18 @@ } -Milkcocoa* Milkcocoa::createWithApiKey(MQTTESP8266 *ipstack, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) { +Milkcocoa* Milkcocoa::createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) { char session[60]; sprintf(session, "k%s:%s", key, secret); - return new Milkcocoa(ipstack, host, port, _app_id, client_id, session); + return new Milkcocoa(_client, host, port, _app_id, client_id, session); } void Milkcocoa::connect() { - if(client.isConnected()) + if(client->isConnected()) return; - - if(_ipstack->connect(servername, portnum)!=0) { + + if(client->connect(servername, portnum)!=0) { DBG(pc.printf("Network connect err\r\n");) return; } @@ -135,7 +139,7 @@ data.username.cstring = username; data.password.cstring = password; - if (client.connect(data) != 0) { + if (client->connect(data) != 0) { DBG(pc.printf("Milkcocoa connect err\r\n");) return; } @@ -154,7 +158,7 @@ buf = dataelement.toCharArray(); message.payload = (void*)buf; message.payloadlen = strlen(buf); - if(client.publish(topic, message)!=0) + if(client->publish(topic, message)!=0) return(false); return true; @@ -172,7 +176,7 @@ buf = dataelement.toCharArray(); message.payload = (void*)buf; message.payloadlen = strlen(buf); - if(client.publish(topic, message)!=0) + if(client->publish(topic, message)!=0) return false; return true; @@ -180,18 +184,31 @@ void Milkcocoa::loop() { connect(); - client.yield(RECV_TIMEOUT); + client->yield(RECV_TIMEOUT); } bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) { - static char topic[100]; + MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb); + sprintf(sub->topic, "%s/%s/%s", app_id, path, event); - sprintf(topic, "%s/%s/%s", app_id, path, event); - - if (client.subscribe(topic, MQTT::QOS0, cb) != 0) { + if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) { DBG(pc.printf("Milkcocoa subscribe err\r\n");) return false; } + for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { + if (milkcocoaSubscribers[i] == sub) { + return false; + } + } + for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { + if (milkcocoaSubscribers[i] == 0) { + milkcocoaSubscribers[i] = sub; + return true; + } + } return true; } +MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) { + cb = _cb; +} \ No newline at end of file