Class: SpsMqttBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/sps_mqtt_bridge.rb

Instance Method Summary collapse

Constructor Details

#initialize(mqtt: {address: 'mqtt', port: '1883'}, sps: {address: 'sps', port: '59000'}) ⇒ SpsMqttBridge

Returns a new instance of SpsMqttBridge.



12
13
14
15
# File 'lib/sps_mqtt_bridge.rb', line 12

def initialize(mqtt: {address: 'mqtt', port: '1883'}, 
                                      sps:{address: 'sps', port: '59000'})
  @mqtt, @sps = mqtt, sps
end

Instance Method Details

#mqtt_to_sps(topic: '#') ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/sps_mqtt_bridge.rb', line 17

def mqtt_to_sps(topic: '#')

  MQTT::Client.connect(@mqtt[:address], @mqtt[:port]) do |client|

    client.get(topic) do |t, message|
      SPSPub.notice [t, message].join(': '), 
                              address: @sps[:address], port: @sps[:port]
    end
  end
end

#sps_to_mqtt(topic: '#') ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sps_mqtt_bridge.rb', line 28

def sps_to_mqtt(topic: '#')

  SimplePubSub::Client.connect(@sps[:address], @sps[:port]) do |client|

    client.get(topic) do |t, message|

      MQTT::Client.connect(@mqtt[:address], @mqtt[:port]) do |client|
        client.publish(t, message)
      end
    end
  end
end