specify the callback function to each topic
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -20,27 +20,20 @@ class Proto_msg_to_ros:
|
||||
Fly_Formation_topicToMqtt = None
|
||||
|
||||
#Proto
|
||||
@classmethod
|
||||
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 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_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:
|
||||
@@ -53,17 +46,11 @@ class Json_msg_to_ros:
|
||||
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")
|
||||
def on_message_Flight_Information(cls, client, userdata, msg):
|
||||
cls.publisher_Flight_Information.publish(msg.payload.decode("UTF-8"))
|
||||
cls.rate.sleep()
|
||||
|
||||
|
||||
@staticmethod
|
||||
def on_message(client, userdata, msg):
|
||||
Json_msg_to_ros.ros_pub(msg)
|
||||
@classmethod
|
||||
def on_message_Fly_Formation(cls, client, userdata, msg):
|
||||
cls.publisher_Fly_Formation.publish(msg.payload.decode("UTF-8"))
|
||||
cls.rate.sleep()
|
||||
Reference in New Issue
Block a user