def _on_message(self, client, userdata, msg): payload = str(msg.payload) logger.infof('received message from mqtt: {}', payload) matcher = self._cmd_payload_re.match(payload) if matcher: cmd = matcher.group('cmd') device_id = matcher.group('device_id') if cmd == 'circle': self._do_circle() elif cmd == 'square': self._do_square() elif cmd == 'triangle': self._do_triangle() elif cmd == 'cross': self._do_stop() elif cmd == 'up': self._do_forward() elif cmd == 'down': self._do_backward() elif cmd == 'left': self._do_turnleft() elif cmd == 'right': self._do_turnright() else: logger.warnf('unknown cmd: {}', payload) cmd = 'UNKNOWN CMD: {}'.format(cmd) topic = findItem(self._params.mqtt.topics, 'key', 'command_sender_exec').name fmt = findItem(self._params.mqtt.topics, 'key', 'command_sender_exec').format self.__client.publish(topic, fmt.format(device_id=device_id, cmd=cmd)) else: logger.warnf('unkown payload: {}', payload) logger.debugf('active threds = {}', threading.active_count())
def _on_receive(self, data): logger.infof('received message from ros : {}', data.data) now = datetime.now(pytz.utc).strftime('%Y-%m-%dT%H:%M:%S.%f%z') topic = findItem(self._params.mqtt.topics, 'key', 'attribute_receiver').name fmt = findItem(self._params.mqtt.topics, 'key', 'attribute_receiver').format msg = fmt.format(timestamp=now, temperature=data.data) self.__client.publish(topic, msg)
def __init__(self, node_name): self.node_name = node_name self.__client = mqtt.Client(protocol=mqtt.MQTTv311) self.__client.on_connect = self._on_connect self.__client.on_message = self._on_message rospy.on_shutdown(self._do_stop) rospy.on_shutdown(self.__client.disconnect) rospy.on_shutdown(self.__client.loop_stop) self._params = getParams(rospy.get_param("~")) topic = findItem(self._params.ros.topics, 'key', 'turtlesim') self.__ros_pub = rospy.Publisher(topic.name, Twist, queue_size=10) self.__do_move = False self.__lock = threading.Lock() self._cmd_payload_re = re.compile( findItem(self._params.mqtt.topics, 'key', 'command_sender').re)
def main(): try: rospy.init_node(NODE_NAME) logger.warnf('Start test node : {}', NODE_NAME) topic = findItem(getParams(rospy.get_param("~")).ros.topics, 'key', 'temperature') pub = rospy.Publisher(topic.name, Float32, queue_size=10) r = rospy.Rate(2) while not rospy.is_shutdown(): pub.publish(Float32(1.0)) r.sleep() except rospy.ROSInterruptException: pass
def test_find_none1(self, obj, key_name, key_value): if obj == 'a': a = self.d.a else: a = obj result = findItem(a, key_name, key_value) if obj == 'a' and key_name == 'key' and key_value == 'KEY1': self.assertEqual(result, self.a_type(key='KEY1', value='VALUE1')) self.assertEqual(result.key, 'KEY1') self.assertEqual(result.value, 'VALUE1') else: self.assertIsNone(result)
def __init__(self, node_name): self.node_name = node_name self.__client = mqtt.Client(protocol=mqtt.MQTTv311) self.__client.on_connect = self._on_connect rospy.on_shutdown(self.__client.disconnect) rospy.on_shutdown(self.__client.loop_stop) self._params = getParams(rospy.get_param("~")) topic = findItem(self._params.ros.topics, 'key', 'temperature') self.__ros_sub = rospy.Subscriber(topic.name, Float32, self._on_receive, queue_size=10)
def _on_connect(self, client, userdata, flags, response_code): logger.infof('mqtt connect status={}', response_code) client.subscribe( findItem(self._params.mqtt.topics, 'key', 'command_sender').name)