From fa2824a75146b6ba9edc505f8adfb8a0dc01482b Mon Sep 17 00:00:00 2001 From: RangeOfGlitching Date: Thu, 2 Mar 2023 21:41:34 +0800 Subject: [PATCH] specify the callback function to each topic --- class_model/src/mqttCmdMain.py | 2 +- class_model/src/mqttSubMain.py | 9 ++- .../utils/protoJson_mqtt_pub_data_to_ros.py | 57 +++++++------------ 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/class_model/src/mqttCmdMain.py b/class_model/src/mqttCmdMain.py index 22d266b..ad378d1 100755 --- a/class_model/src/mqttCmdMain.py +++ b/class_model/src/mqttCmdMain.py @@ -21,7 +21,7 @@ def init_dataFormat(cfg:Read_CMD_Config): elif cfg.msg_format == "Proto": pass else: - logging.debug("msg_format not found") + logger.debug("msg_format not found") def on_connect(self, userdata, flags, rc): logger.info("Connected with result code " + str(rc)) diff --git a/class_model/src/mqttSubMain.py b/class_model/src/mqttSubMain.py index a76f56b..2e78760 100755 --- a/class_model/src/mqttSubMain.py +++ b/class_model/src/mqttSubMain.py @@ -21,7 +21,8 @@ def init_dataFormat(cfg:utils.Read_SUB_Config): utils.Proto_msg_to_ros.flight_information_msg = flight_information_pb2.flight_information_message() utils.Proto_msg_to_ros.flyformatioln_msg = flyformatioln_pb2.fly_formation_message() - client.on_message = utils.Proto_msg_to_ros.on_message + client.message_callback_add(cfg.Flight_Information_topicToMqtt, utils.Proto_msg_to_ros.on_message_Flight_Information) + client.message_callback_add(cfg.Fly_Formation_topicToMqtt, utils.Proto_msg_to_ros.on_message_Fly_Formation) utils.Proto_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt utils.Proto_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt @@ -30,7 +31,9 @@ def init_dataFormat(cfg:utils.Read_SUB_Config): utils.Json_msg_to_ros.publisher_Flight_Information = rospy.Publisher(cfg.ROStopicName_Flight_Information,String,queue_size=10) utils.Json_msg_to_ros.publisher_Fly_Formation = rospy.Publisher(cfg.ROStopicName_Fly_Formation,String,queue_size=10) - client.on_message = utils.Json_msg_to_ros.on_message + client.message_callback_add(cfg.Flight_Information_topicToMqtt, utils.Json_msg_to_ros.on_message_Flight_Information) + client.message_callback_add(cfg.Fly_Formation_topicToMqtt, utils.Json_msg_to_ros.on_message_Fly_Formation) + utils.Json_msg_to_ros.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt utils.Json_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt else: @@ -43,7 +46,7 @@ def on_connect(self, userdata, flags, rc): client.subscribe(cfg.Fly_Formation_topicToMqtt) def on_disconnect(client, userdata, rc): - # logger.info("disconnecting reason " +str(rc)) + logger.info("disconnecting reason " +str(rc)) client.connected_flag=False client.disconnect_flag=True diff --git a/class_model/src/utils/protoJson_mqtt_pub_data_to_ros.py b/class_model/src/utils/protoJson_mqtt_pub_data_to_ros.py index a3c517d..91e116b 100644 --- a/class_model/src/utils/protoJson_mqtt_pub_data_to_ros.py +++ b/class_model/src/utils/protoJson_mqtt_pub_data_to_ros.py @@ -20,28 +20,21 @@ class Proto_msg_to_ros: Fly_Formation_topicToMqtt = None #Proto - @classmethod - def ros_pub(cls, dataProto): - if dataProto.topic == cls.Flight_Information_topicToMqtt: - proto_msg = cls.flight_information_msg.FromString(dataProto.payload) - protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True) - cls.publisher_Flight_Information.publish(protoTOJson_msg) - cls.rate.sleep() - elif dataProto.topic == cls.Fly_Formation_topicToMqtt: - proto_msg = cls.fly_formation_msg.FromString(dataProto.payload) - # protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True) - protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True) - cls.publisher_Fly_Formation.publish(protoTOJson_msg) - cls.rate.sleep() - else: - print("topic not found") - - - @staticmethod - def on_message(client, userdata, msg): - Proto_msg_to_ros.ros_pub(msg) + def on_message_Flight_Information(cls, client, userdata, msg): + proto_msg = cls.flight_information_msg.FromString(msg.payload) + protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True) + cls.publisher_Flight_Information.publish(protoTOJson_msg) + cls.rate.sleep() + @classmethod + def on_message_Fly_Formation(cls, client, userdata, msg): + proto_msg = cls.fly_formation_msg.FromString(msg.payload) + # protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True) + protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=True) + cls.publisher_Fly_Formation.publish(protoTOJson_msg) + cls.rate.sleep() + class Json_msg_to_ros: rate = None @@ -51,19 +44,13 @@ class Json_msg_to_ros: #Mqtt topic: check data from which topic Flight_Information_topicToMqtt = None Fly_Formation_topicToMqtt = None - + @classmethod - def ros_pub(cls, dataJson): - if dataJson.topic == cls.Flight_Information_topicToMqtt: - cls.publisher_Flight_Information.publish(dataJson.payload.decode("UTF-8")) - cls.rate.sleep() - elif dataJson.topic == cls.Fly_Formation_topicToMqtt: - cls.publisher_Fly_Formation.publish(dataJson.payload.decode("UTF-8")) - cls.rate.sleep() - else: - print("topic not found") - - - @staticmethod - def on_message(client, userdata, msg): - Json_msg_to_ros.ros_pub(msg) \ No newline at end of file + def on_message_Flight_Information(cls, client, userdata, msg): + cls.publisher_Flight_Information.publish(msg.payload.decode("UTF-8")) + cls.rate.sleep() + + @classmethod + def on_message_Fly_Formation(cls, client, userdata, msg): + cls.publisher_Fly_Formation.publish(msg.payload.decode("UTF-8")) + cls.rate.sleep() \ No newline at end of file