mqttc = mqtt_client.Client(config['mqtt']['client_id'], userdata=can_bus) mqttc.on_message = on_mqtt_message mqttc.reconnect_delay_set(1, 10) mqttc.will_set(config['mqtt']['will_topic'], '0xDEAD', 0, retain=True) mqttc.connect(host=config['mqtt']['host'], port=int(config['mqtt']['port']), keepalive=60) mqttc.loop_start() mqttc.subscribe('{}/#'.format(config['mqtt']['prefix']), 0) should_stop = False while not should_stop: try: can_msg = can_bus.recv(5) if can_msg: mqtt_msg = can2mqtt(can_msg) mqttc.publish(topic=mqtt_msg.topic, payload=mqtt_msg.payload, retain=False) mqttc.publish(topic=config['mqtt']['will_topic'], payload='{:d}'.format(int(time())), retain=True) except KeyboardInterrupt as e: print('exiting on keyboard request') should_stop = True except Exception as e: print(str(e)) mqttc.disconnect()
def test_can2mqtt_bad(bad_can_messages): for can_frame in bad_can_messages: with pytest.raises(ValueError): can2mqtt(can_frame)
def test_can2mqtt_not_supported(mqtt_can_messages_not_supported): for mqtt_msg, can_frame in mqtt_can_messages_not_supported: with pytest.raises(HomeCanMessageNotSupported): can2mqtt(can_frame)
def test_can2mqtt_ok(can_mqtt_messages): for mqtt_msg, can_frame in can_mqtt_messages: print(mqtt_msg.payload) print(can2mqtt(can_frame).payload) assert mqtt_msg == can2mqtt(can_frame)
def test_dust_sensor_invalid_frame(can2mqtt_messages_invalid): for mqtt_msg, can_frame in can2mqtt_messages_invalid: with pytest.raises(HomeCanBridgingForbidden): msg = can2mqtt(can_frame)
def test_dust_sensor(can2mqtt_messages_valid): for mqtt_msg, can_frame in can2mqtt_messages_valid: assert mqtt_msg == can2mqtt(can_frame)