mqtt/mqtt_client_gen.py
changeset 3994 c399fe412dbd
parent 3993 78f9abfb32a0
child 3995 84a668564748
equal deleted inserted replaced
3993:78f9abfb32a0 3994:c399fe412dbd
   300 
   300 
   301     def GenerateC(self, path, locstr, config):
   301     def GenerateC(self, path, locstr, config):
   302         template = """/* code generated by beremiz MQTT extension */
   302         template = """/* code generated by beremiz MQTT extension */
   303 
   303 
   304 #include <stdint.h>
   304 #include <stdint.h>
       
   305 #include <unistd.h>
   305 #include <pthread.h>
   306 #include <pthread.h>
   306 #include <string.h>
   307 #include <string.h>
       
   308 #include <stdio.h>
   307 
   309 
   308 #include "MQTTClient.h"
   310 #include "MQTTClient.h"
   309 #include "MQTTClientPersistence.h"
   311 #include "MQTTClientPersistence.h"
   310 
   312 
   311 #define _Log(level, ...)                                                                          \\
   313 #define _Log(level, ...)                                                                          \\
   312     {{                                                                                            \\
   314     {{                                                                                            \\
   313         char mstr[256];                                                                           \\
   315         char mstr[256];                                                                           \\
   314         snprintf(mstr, 255, __VA_ARGS__);                                                         \\
   316         snprintf(mstr, 255, __VA_ARGS__);                                                         \\
   315         LogMessage(level, mstr, strlen(mstr));                                                    \\
   317         LogMessage(level, mstr, strlen(mstr));                                                    \\
   316         printf(__VA_ARGS__);                                                                      \\
   318         printf(__VA_ARGS__);                                                                      \\
       
   319         fflush(stdout);                                                                           \\
   317     }}
   320     }}
   318 
   321 
   319 #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
   322 #define LogInfo(...) _Log(LOG_INFO, __VA_ARGS__);
   320 #define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
   323 #define LogError(...) _Log(LOG_CRITICAL, __VA_ARGS__);
   321 #define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
   324 #define LogWarning(...) _Log(LOG_WARNING, __VA_ARGS__);
   413     MQTTClient_destroy(&client);
   416     MQTTClient_destroy(&client);
   414 }}
   417 }}
   415 
   418 
   416 void connectionLost(void* context, char* reason)
   419 void connectionLost(void* context, char* reason)
   417 {{
   420 {{
       
   421     int rc;
   418     LogWarning("ConnectionLost, reconnecting\\n");
   422     LogWarning("ConnectionLost, reconnecting\\n");
   419     _connect_mqtt();
   423     // rc = _connect_mqtt();
   420     /* TODO wait if error */
   424 
       
   425     // if (rc != MQTTCLIENT_SUCCESS) {{
       
   426     //     LogError("MQTT reconnect Failed, waiting 5 seconds, return code %d\\n", rc);
       
   427     //     /* wait if error */
       
   428     //     sleep(5);
       
   429     // }}
   421 }}
   430 }}
   422 
   431 
   423 int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
   432 int messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
   424 {{
   433 {{
   425     int low = 0;
   434     int low = 0;
   536 
   545 
   537 static void *__publish_thread(void *_unused) {{
   546 static void *__publish_thread(void *_unused) {{
   538     int rc = 0;
   547     int rc = 0;
   539     while((rc = pthread_mutex_lock(&MQTT_mutex)) == 0 && !MQTT_stop_thread){{
   548     while((rc = pthread_mutex_lock(&MQTT_mutex)) == 0 && !MQTT_stop_thread){{
   540         pthread_cond_wait(&MQTT_new_data, &MQTT_mutex);
   549         pthread_cond_wait(&MQTT_new_data, &MQTT_mutex);
   541         if(MQTT_any_pub_var_changed){{
   550         if(MQTT_any_pub_var_changed && MQTTClient_isConnected(client)){{
   542 
   551 
   543             /* publish changes, and reset variable's state to UNCHANGED */
   552             /* publish changes, and reset variable's state to UNCHANGED */
   544 {publish_changes}
   553 {publish_changes}
   545             MQTT_any_pub_var_changed = 0;
   554             MQTT_any_pub_var_changed = 0;
   546         }}
   555         }}