specify the callback function to each topic

protobuf
RangeOfGlitching 3 years ago
parent 83faefde1a
commit fa2824a751

@ -21,7 +21,7 @@ def init_dataFormat(cfg:Read_CMD_Config):
elif cfg.msg_format == "Proto": elif cfg.msg_format == "Proto":
pass pass
else: else:
logging.debug("msg_format not found") logger.debug("msg_format not found")
def on_connect(self, userdata, flags, rc): def on_connect(self, userdata, flags, rc):
logger.info("Connected with result code " + str(rc)) logger.info("Connected with result code " + str(rc))

@ -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.flight_information_msg = flight_information_pb2.flight_information_message()
utils.Proto_msg_to_ros.flyformatioln_msg = flyformatioln_pb2.fly_formation_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.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Proto_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_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_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) 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.Flight_Information_topicToMqtt = cfg.Flight_Information_topicToMqtt
utils.Json_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt utils.Json_msg_to_ros.Fly_Formation_topicToMqtt = cfg.Fly_Formation_topicToMqtt
else: else:
@ -43,7 +46,7 @@ def on_connect(self, userdata, flags, rc):
client.subscribe(cfg.Fly_Formation_topicToMqtt) client.subscribe(cfg.Fly_Formation_topicToMqtt)
def on_disconnect(client, userdata, rc): def on_disconnect(client, userdata, rc):
# logger.info("disconnecting reason " +str(rc)) logger.info("disconnecting reason " +str(rc))
client.connected_flag=False client.connected_flag=False
client.disconnect_flag=True client.disconnect_flag=True

@ -20,27 +20,20 @@ class Proto_msg_to_ros:
Fly_Formation_topicToMqtt = None Fly_Formation_topicToMqtt = None
#Proto #Proto
@classmethod @classmethod
def ros_pub(cls, dataProto): def on_message_Flight_Information(cls, client, userdata, msg):
if dataProto.topic == cls.Flight_Information_topicToMqtt: proto_msg = cls.flight_information_msg.FromString(msg.payload)
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) 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.publisher_Flight_Information.publish(protoTOJson_msg)
cls.rate.sleep() cls.rate.sleep()
elif dataProto.topic == cls.Fly_Formation_topicToMqtt:
proto_msg = cls.fly_formation_msg.FromString(dataProto.payload) @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)
protoTOJson_msg = json_format.MessageToJson(proto_msg, indent=None, preserving_proto_field_name=True, use_integers_for_enums=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.publisher_Fly_Formation.publish(protoTOJson_msg)
cls.rate.sleep() cls.rate.sleep()
else:
print("topic not found")
@staticmethod
def on_message(client, userdata, msg):
Proto_msg_to_ros.ros_pub(msg)
class Json_msg_to_ros: class Json_msg_to_ros:
@ -53,17 +46,11 @@ class Json_msg_to_ros:
Fly_Formation_topicToMqtt = None Fly_Formation_topicToMqtt = None
@classmethod @classmethod
def ros_pub(cls, dataJson): def on_message_Flight_Information(cls, client, userdata, msg):
if dataJson.topic == cls.Flight_Information_topicToMqtt: cls.publisher_Flight_Information.publish(msg.payload.decode("UTF-8"))
cls.publisher_Flight_Information.publish(dataJson.payload.decode("UTF-8"))
cls.rate.sleep() 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 @classmethod
def on_message(client, userdata, msg): def on_message_Fly_Formation(cls, client, userdata, msg):
Json_msg_to_ros.ros_pub(msg) cls.publisher_Fly_Formation.publish(msg.payload.decode("UTF-8"))
cls.rate.sleep()
Loading…
Cancel
Save