Skip to content

Commit

Permalink
Moved configuration setting into a YAML configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
njh committed Mar 15, 2012
1 parent 3b3a2c3 commit cec8faa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
16 changes: 16 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
radiovis:
host: vis.musicradio.com
port: 61613

mqtt:
host: test.mosquitto.org
port: 1883

identifiers:
# Capital FM
- fm/ce1/c479/09580
# Heart
- dab/ce1/c185/c460/0
# Classic FM
- dab/ce1/c181/c2a1/0
37 changes: 27 additions & 10 deletions radiovis-to-mqtt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

Bundler.require(:default)

STOMP_HOST = 'vis.musicradio.com'
MQTT_HOST = 'test.mosquitto.org'

# Load the configuration file
CONFIG = YAML::load_file('config.yml')

# Make STDOUT unbuffered
STDOUT.sync = true
Expand All @@ -18,29 +17,47 @@ module StompClient
def connection_completed
connect
end

def message_callback(&block)
@message_callback = block
end

def connected_callback(&block)
@connected_callback = block
end

def receive_msg msg
if msg.command == "CONNECTED"
subscribe '/topic/fm/ce1/c479/09580/image' # Capital FM
subscribe '/topic/fm/ce1/c479/09580/text' # Capital FM
@connected_callback.call(msg) unless @connected_callback.nil?
elsif msg.command == "MESSAGE"
@message_callback.call(msg) unless @message_callback.nil?
end
end
end


EM.run do
mqtt = EventMachine::MQTT::ClientConnection.connect(MQTT_HOST)
stomp = EM.connect(STOMP_HOST, 61613, StompClient)

mqtt = EventMachine::MQTT::ClientConnection.connect(
CONFIG['mqtt']['host'],
CONFIG['mqtt']['port']
)
stomp = EM.connect(
CONFIG['radiovis']['host'],
CONFIG['radiovis']['port'],
StompClient
)

stomp.connected_callback do
CONFIG['identifiers'].each do |identifier|
stomp.subscribe "/topic/#{identifier}/image"
stomp.subscribe "/topic/#{identifier}/text"
end
end

stomp.message_callback do |msg|
topic = msg.header['destination'].sub(%r(^/topic/), 'radiovis/')
body = msg.body.sub(/^([A-Z]+) /, '')
p [topic, body]
mqtt.publish(topic, body, retain=true)
end
end
end

0 comments on commit cec8faa

Please sign in to comment.