Class: TEF::FurComs::MQTT

Inherits:
Base
  • Object
show all
Includes:
XasLogger::Mix
Defined in:
lib/tef/furcoms/mqtt.rb

Overview

FurComs MQTT wrapper

This class will connect to a MQTT Broker, and use it as a bridge to a FurComs hardware connection. This allows multiple different systems to access different busses remotely, and has the added benefit of allowing Read/Write restrictions using the MQTT built-in authentication.

Instance Method Summary collapse

Methods inherited from Base

#on_message

Constructor Details

#initialize(mqtt, topic = 'FurComs/ttyACM0/') ⇒ MQTT

Initialize a new FurComs MQTT Bridge.

This will subscribe to the topic: topic + ‘Received/#’, and will being relaying messages to the attached callbacks immediately. Messages it wants to send will be sent to topic + ‘Send/#msg_topic’

If no MQTT to FurComs is attached to the bus, the message will be lost!

Parameters:

  • mqtt

    The MQTT handler. Must support subscribe_to and publish_to. A fitting gem would be mqtt-sub_handler.

  • topic (String) (defaults to: 'FurComs/ttyACM0/')

    Topic base. Must end with /



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tef/furcoms/mqtt.rb', line 29

def initialize(mqtt, topic = 'FurComs/ttyACM0/')
	super();

	@mqtt = mqtt;
	@mqtt_topic = topic;

	@mqtt.subscribe_to topic + 'Received/#' do |data, msg_topic|
		msg_topic = msg_topic.join('/')
		next unless msg_topic =~ /^[\w\s\/]*$/

		handout_data(msg_topic, data);
	end

	init_x_log(@mqtt_topic)
end

Instance Method Details

#send_message(topic, data, priority: 0, chip_id: 0) ⇒ Object

Send a message to the FurComs bus.

This will send a message onto topic, using the given priority and chip_id (defaulting both to 0).

Parameters:

  • topic (String)

    Topic to send the message onto

  • message (String)

    Binary string of data to send, expected to be ASCII-8 encoded, can contain any character (including null)



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tef/furcoms/mqtt.rb', line 46

def send_message(topic, data, priority: 0, chip_id: 0)
	unless topic =~ /^[\w\s\/]*$/
		raise ArgumentError, 'Topic includes invalid characters!'
	end
	if (topic.length + data.length) > 250
		raise ArgumentError, 'Message packet length exceeded!'
	end

	x_logd("Sending '#{topic}': '#{data}'")

	@mqtt.publish_to @mqtt_topic + "Send/#{topic}", data
end