# HG changeset patch # User Edouard Tisserant # Date 1720791377 -7200 # Node ID cec48fc7ccd07b6cd93e0f223900176bf2fb647c # Parent 98bd0bb33ce4ec96464e1b70674d1cb3049334a0 MQTT: WIP, now publishes initial values at init. diff -r 98bd0bb33ce4 -r cec48fc7ccd0 mqtt/mqtt_client_gen.py --- a/mqtt/mqtt_client_gen.py Fri Jul 12 11:24:06 2024 +0200 +++ b/mqtt/mqtt_client_gen.py Fri Jul 12 15:36:17 2024 +0200 @@ -40,10 +40,10 @@ else: return bool(v) -lstcolnames = [ "Topic", "QoS", "Retain", "Type", "Location"] -lstcolwidths = [ 100, 50, 100, 100, 50] -lstcoltypess = [ str, int, boolean, str, int] -lstcoldeflts = [ "a/b/c", "1", False, "DINT", "0"] +lstcolnames = [ "Topic", "QoS", "Retained", "Type", "Location"] +lstcolwidths = [ 100, 50, 100, 100, 50] +lstcoltypess = [ str, int, boolean, str, int] +lstcoldeflts = [ "a/b/c", "1", False, "DINT", "0"] Location_column = lstcolnames.index("Location") directions = ["input", "output"] @@ -440,24 +440,52 @@ conn_opts.password = Password; #ifdef USE_MQTT_5 -#define MY_SUBSCRIBE(Topic, QoS) \\ +#define _SUBSCRIBE(Topic, QoS) \\ MQTTResponse response = MQTTClient_subscribe5(client, #Topic, QoS, NULL, NULL); \\ rc = response.reasonCode; \\ MQTTResponse_free(response); #else -#define MY_SUBSCRIBE(Topic, QoS) \\ +#define _SUBSCRIBE(Topic, QoS) \\ rc = MQTTClient_subscribe(client, #Topic, QoS); #endif #define INIT_SUBSCRIPTION(Topic, QoS) \\ {{ \\ - MY_SUBSCRIBE(Topic, QoS) \\ + int rc; \\ + _SUBSCRIBE(Topic, QoS) \\ if (rc != MQTTCLIENT_SUCCESS) \\ {{ \\ LogError("MQTT client failed to subscribe to '%s', return code %d\\n", #Topic, rc); \\ }} \\ }} + + + + +#ifdef USE_MQTT_5 +#define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \\ + MQTTResponse response = MQTTClient_publish5(client, #Topic, sizeof(C_type), \\ + &PLC_##c_loc_name##_buf, QoS, Retained, NULL, NULL); \\ + rc = response.reasonCode; \\ + MQTTResponse_free(response); +#else +#define _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \\ + rc = MQTTClient_publish(client, #Topic, sizeof(C_type), \\ + &PLC_##c_loc_name##_buf, QoS, Retained, NULL); +#endif + +#define INIT_PUBLICATION(Topic, QoS, C_type, c_loc_name, Retained) \\ + {{ \\ + int rc; \\ + _PUBLISH(Topic, QoS, C_type, c_loc_name, Retained) \\ + if (rc != MQTTCLIENT_SUCCESS) \\ + {{ \\ + LogError("MQTT client failed to subscribe to '%s', return code %d\\n", #Topic, rc); \\ + }} \\ + }} + + int __init_{locstr}(int argc,char **argv) {{ char *uri = "{uri}"; @@ -564,7 +592,8 @@ for direction, data in self.items(): iec_direction_prefix = {"input": "__I", "output": "__Q"}[direction] for row in data: - Topic, QoS, Retain, iec_type, iec_number = row + Topic, QoS, _Retained, iec_type, iec_number = row + Retained = 1 if _Retained=="True" else 0 C_type, iec_size_prefix = MQTT_IEC_types[iec_type] c_loc_name = iec_direction_prefix + iec_size_prefix + locstr + "_" + str(iec_number) @@ -581,8 +610,8 @@ READ_VALUE({c_loc_name}, {C_type})""".format(**locals()) if direction == "output": - # TODO: publish at init - # formatdict["init"] += " NOTHING ! publish doesn't need init. " + formatdict["init"] += """ + INIT_PUBLICATION({Topic}, {QoS}, {C_type}, {c_loc_name}, {Retained})""".format(**locals()) formatdict["publish"] += """ WRITE_VALUE({c_loc_name}, {C_type})""".format(**locals())