mqtt/mqtt_client_gen.py
changeset 3987 cec48fc7ccd0
parent 3986 98bd0bb33ce4
child 3988 150599d9073f
equal deleted inserted replaced
3986:98bd0bb33ce4 3987:cec48fc7ccd0
    38     if v in ["False","0"]:
    38     if v in ["False","0"]:
    39         return False
    39         return False
    40     else:
    40     else:
    41         return bool(v)
    41         return bool(v)
    42 
    42 
    43 lstcolnames  = [ "Topic",  "QoS",  "Retain", "Type", "Location"]
    43 lstcolnames  = [ "Topic",  "QoS",  "Retained", "Type", "Location"]
    44 lstcolwidths = [     100,     50,       100,    100,         50]
    44 lstcolwidths = [     100,     50,         100,    100,         50]
    45 lstcoltypess = [     str,    int,   boolean,    str,        int]
    45 lstcoltypess = [     str,    int,     boolean,    str,        int]
    46 lstcoldeflts = [ "a/b/c",    "1",     False, "DINT",        "0"]
    46 lstcoldeflts = [ "a/b/c",    "1",       False, "DINT",        "0"]
    47 Location_column = lstcolnames.index("Location")
    47 Location_column = lstcolnames.index("Location")
    48 
    48 
    49 directions = ["input", "output"]
    49 directions = ["input", "output"]
    50 
    50 
    51 authParams = {
    51 authParams = {
   438     LogInfo("MQTT Init UserPassword %s,%s\\n", User, Password);                                   \\
   438     LogInfo("MQTT Init UserPassword %s,%s\\n", User, Password);                                   \\
   439 	conn_opts.username = User;                                                                    \\
   439 	conn_opts.username = User;                                                                    \\
   440 	conn_opts.password = Password;
   440 	conn_opts.password = Password;
   441 
   441 
   442 #ifdef USE_MQTT_5
   442 #ifdef USE_MQTT_5
   443 #define MY_SUBSCRIBE(Topic, QoS)                                                                  \\
   443 #define _SUBSCRIBE(Topic, QoS)                                                                  \\
   444         MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL);           \\
   444         MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL);           \\
   445         rc = response.reasonCode;                                                                 \\
   445         rc = response.reasonCode;                                                                 \\
   446         MQTTResponse_free(response);
   446         MQTTResponse_free(response);
   447 #else
   447 #else
   448 #define MY_SUBSCRIBE(Topic, QoS)                                                                  \\
   448 #define _SUBSCRIBE(Topic, QoS)                                                                  \\
   449         rc = MQTTClient_subscribe(client, #Topic, QoS);
   449         rc = MQTTClient_subscribe(client, #Topic, QoS);
   450 #endif
   450 #endif
   451 
   451 
   452 #define INIT_SUBSCRIPTION(Topic, QoS)                                                             \\
   452 #define INIT_SUBSCRIPTION(Topic, QoS)                                                             \\
   453     {{                                                                                            \\
   453     {{                                                                                            \\
   454         MY_SUBSCRIBE(Topic, QoS)                                                                  \\
   454         int rc;                                                                                   \\
       
   455         _SUBSCRIBE(Topic, QoS)                                                                  \\
   455         if (rc != MQTTCLIENT_SUCCESS)                                                             \\
   456         if (rc != MQTTCLIENT_SUCCESS)                                                             \\
   456         {{                                                                                        \\
   457         {{                                                                                        \\
   457             LogError("MQTT client failed to subscribe to '%s', return code %d\\n", #Topic, rc);   \\
   458             LogError("MQTT client failed to subscribe to '%s', return code %d\\n", #Topic, rc);   \\
   458         }}                                                                                        \\
   459         }}                                                                                        \\
   459     }}
   460     }}
       
   461 
       
   462 
       
   463 
       
   464 
       
   465 
       
   466 #ifdef USE_MQTT_5
       
   467 #define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained)                                        \\
       
   468         MQTTResponse response = MQTTClient_publish5(client, #Topic, sizeof(C_type),               \\
       
   469             &PLC_##c_loc_name##_buf, QoS, Retained, NULL, NULL);                                  \\
       
   470         rc = response.reasonCode;                                                                 \\
       
   471         MQTTResponse_free(response);
       
   472 #else
       
   473 #define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained)                                        \\
       
   474         rc = MQTTClient_publish(client, #Topic, sizeof(C_type),                                   \\
       
   475             &PLC_##c_loc_name##_buf, QoS, Retained, NULL);
       
   476 #endif
       
   477 
       
   478 #define INIT_PUBLICATION(Topic, QoS, C_type, c_loc_name, Retained)                                \\
       
   479     {{                                                                                            \\
       
   480         int rc;                                                                                   \\
       
   481         _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained)                                        \\
       
   482         if (rc != MQTTCLIENT_SUCCESS)                                                             \\
       
   483         {{                                                                                        \\
       
   484             LogError("MQTT client failed to subscribe to '%s', return code %d\\n", #Topic, rc);   \\
       
   485         }}                                                                                        \\
       
   486     }}
       
   487 
   460 
   488 
   461 int __init_{locstr}(int argc,char **argv)
   489 int __init_{locstr}(int argc,char **argv)
   462 {{
   490 {{
   463     char *uri = "{uri}";
   491     char *uri = "{uri}";
   464     char *clientID = "{clientID}";
   492     char *clientID = "{clientID}";
   562 
   590 
   563         topics = OrderedDict()
   591         topics = OrderedDict()
   564         for direction, data in self.items():
   592         for direction, data in self.items():
   565             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   593             iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction]
   566             for row in data:
   594             for row in data:
   567                 Topic, QoS, Retain, iec_type, iec_number = row
   595                 Topic, QoS, _Retained, iec_type, iec_number = row
       
   596                 Retained = 1 if _Retained=="True" else 0
   568                 C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
   597                 C_type, iec_size_prefix = MQTT_IEC_types[iec_type]
   569                 c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
   598                 c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number)
   570 
   599 
   571                 formatdict["decl"] += """
   600                 formatdict["decl"] += """
   572 DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
   601 DECL_VAR({iec_type}, {C_type}, {c_loc_name})""".format(**locals())
   579     INIT_SUBSCRIPTION({Topic}, {QoS})""".format(**locals())
   608     INIT_SUBSCRIPTION({Topic}, {QoS})""".format(**locals())
   580                     formatdict["retrieve"] += """
   609                     formatdict["retrieve"] += """
   581     READ_VALUE({c_loc_name}, {C_type})""".format(**locals())
   610     READ_VALUE({c_loc_name}, {C_type})""".format(**locals())
   582 
   611 
   583                 if direction == "output":
   612                 if direction == "output":
   584                     # TODO: publish at init
   613                     formatdict["init"] += """
   585                     # formatdict["init"] += " NOTHING ! publish doesn't need init. "
   614     INIT_PUBLICATION({Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals())
   586                     formatdict["publish"] += """
   615                     formatdict["publish"] += """
   587     WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())
   616     WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())
   588 
   617 
   589         Ccode = template.format(**formatdict)
   618         Ccode = template.format(**formatdict)
   590 
   619