Junichi Katsu / Milkcocoa-os

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

Revision:
6:1b5ec3a15d67
Parent:
5:dd68fa651637
Child:
7:f1e123331cad
--- a/Milkcocoa.cpp	Fri May 19 01:33:48 2017 +0000
+++ b/Milkcocoa.cpp	Thu Jun 01 00:23:41 2017 +0000
@@ -1,6 +1,6 @@
 #include "Milkcocoa.h"
 
-#if 1
+#if 0
 extern RawSerial pc;
 
 #define DBG(x)  x
@@ -96,10 +96,13 @@
 	strcpy(_clientid,client_id);
 	strcpy(username,"sdammy");
 	strcpy(password,app_id);
+	
+#ifdef __MILKCOCOA_THREAD
 	setLoopCycle(5000);
 	
 	cycleThread1.start(Milkcocoa::threadStarter1,this);
 	cycleThread2.start(Milkcocoa::threadStarter2,this);
+#endif
 }
 
 Milkcocoa::Milkcocoa(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session){
@@ -111,10 +114,13 @@
 	strcpy(_clientid,client_id);
 	strcpy(username,_session);
 	strcpy(password,app_id);
+	
+#ifdef __MILKCOCOA_THREAD
 	setLoopCycle(5000);
 	
 	cycleThread1.start(Milkcocoa::threadStarter1,this);
 	cycleThread2.start(Milkcocoa::threadStarter2,this);
+#endif
 }
 
 Milkcocoa* Milkcocoa::createWithApiKey(NetworkInterface* nif, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) {
@@ -166,6 +172,7 @@
 }
 
 bool Milkcocoa::push(const char *path, char *data) {
+
 	milkcocoa_message_t *message = message_box.alloc();
 	char *buf;
 	
@@ -197,7 +204,7 @@
 	osStatus stat = message_box.put(message);
 	
 	if( stat != osOK ) return false;
-	
+
 	return true;
 }
 
@@ -216,13 +223,32 @@
 	osStatus stat = message_box.put(message);
 	
 	if( stat != osOK ) return false;
-	
+
 	return true;
 }
 
 void Milkcocoa::loop() {
 	connect();
-	client->yield(RECV_TIMEOUT);
+	client->yield(1);
+
+#ifndef __MILKCOCOA_THREAD
+	osEvent evt = message_box.get(0);
+	if (evt.status == osEventMail) {
+		milkcocoa_message_t *message = (milkcocoa_message_t*)evt.value.p;
+		MQTT::Message mq_message;
+		if(message != NULL) {
+			mq_message.qos = MQTT::QOS0;
+			mq_message.retained = 0;
+			mq_message.dup = false;
+			mq_message.payload = (void*)message->message;
+			mq_message.payloadlen = strlen(message->message);
+		
+			client->publish(message->topic, mq_message);
+			
+			message_box.free(message);
+		}
+	}
+#endif
 }
 
 bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) {
@@ -247,10 +273,10 @@
 	return true;
 }
 
+#ifdef __MILKCOCOA_THREAD
 void Milkcocoa::setLoopCycle(int cycle) {
 	loop_cycle = cycle;
 }
-
 void Milkcocoa::start() {
 	cycleThread1.signal_set(START_THREAD);
 	cycleThread2.signal_set(START_THREAD);
@@ -305,6 +331,7 @@
   Milkcocoa *instance = (Milkcocoa*)p;
   instance->cycle_Thread2();
 }
+#endif
 
 MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) {
   cb = _cb;