Class: TEF::FurComs::SerialToMQTT

Inherits:
Serial show all
Defined in:
lib/tef/furcoms/serial_to_mqtt.rb

Overview

Note:

This class suffers from the same restrictions as the Serial class. Use a different PC to FurComs connection if possible!

FurComs SerialToMQTT Bridge.

This class will extend Serial with a few internal functions to bridge the FurComs bus connection to MQTT.

See Also:

Instance Method Summary collapse

Methods inherited from Serial

#send_message

Methods inherited from Base

#on_message, #send_message

Constructor Details

#initialize(port, mqtt, topic = nil) ⇒ SerialToMQTT

Note:

This class can not provide full arbitration handling. This may cause issues in busy bus conditions!

Initialize a Serial To MQTT bridge instance.

This will open the given Serial port and begin reading/writing onto the FurComs bus. It will additionally subscribe to the MQTT ‘topic’, or ‘FurComs/#topic/Send/#’ using the /dev/DEVICE part of the port argument if topic is left nil. Any message received on this topic will be sent as FurComs messages.

Received messages are first handed out to internal callbacks, and will then be sent off to ‘FurComs/#topic/Received/#msg_topic’

Parameters:

  • port (String)

    Device port to use for the FurComs connection

  • mqtt

    MQTT Handler class. Must support subscribe_to and publish_to

  • topic (nil, String) (defaults to: nil)

    MQTT topic to use for receiving and sending. must end with a ‘/’



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tef/furcoms/serial_to_mqtt.rb', line 35

def initialize(port, mqtt, topic = nil)
	super(port);

	@mqtt = mqtt;
	@mqtt_topic = topic || "FurComs/#{port.sub('/dev/', '')}/";

	@mqtt.subscribe_to @mqtt_topic + 'Send/#' do |data, msg_topic|
		msg_topic = msg_topic.join('/')

		next if data.length + msg_topic.length > 250
		next unless msg_topic =~ /^[\w\s\/]*$/

		send_message msg_topic, data
	end
end