Add MQTT publish support plugin #304

Merged
merged 4 commits into from May 1, 2012
Jump to file or symbol
Failed to load files and symbols.
+92 −0
Diff settings

Always

Just for now

View
@@ -22,3 +22,4 @@ gem "statsd-ruby", "0.3.0.github.1"
gem "twilio-ruby", "3.4.2"
gem "right_aws", "3.0.3"
gem "right_http_connection", "1.3.0"
+gem "mqtt", "0.0.8"
View
@@ -36,6 +36,7 @@ GEM
treetop (~> 1.4.8)
mash (0.1.1)
mime-types (1.16)
+ mqtt (0.0.8)
multi_json (1.0.3)
multipart-post (1.1.3)
oauth (0.4.4)
@@ -94,6 +95,7 @@ DEPENDENCIES
mail (~> 2.3)
mash (~> 0.1.1)
mime-types (~> 1.15)
+ mqtt (= 0.0.8)
oauth (= 0.4.4)
rake (= 0.8.7)
right_aws (= 3.0.3)
View
@@ -0,0 +1,28 @@
+MQTT
+========
+
+This service lets you publish push and commit messages to a message broker via the MQTT protocol.
+
+
+Install Notes
+-------------
+
+ 1. 'broker' is the hostname of the MQTT broker.
+ 2. 'port' is the port to connect (default is 1883, MQTT standard).
+ 3. 'topic' is the name of the topic to publish on (suggested example: 'github/<github_username>/<repo_name>').
+ 5. 'id' is the unique client ID which publishes the payload message (default is epoc time).
+ 4. The payload of the message is JSON received from Github (refer to docs/github_payload).
+ 5. ruby-mqtt does not support v3.1 of MQTT (username/password), and only supports QoS 0.
+ 6. For testing, you can use mqtt.io as your web MQTT client: (broker: q.m2m.io, port: 1883, id: <github_username>. You will "Subscribe to Topics": 'github/<github_username>/<repo_name>').
+
+Developer Notes
+---------------
+
+data
+ - broker
+ - port
+ - topic
+ - id
+
+payload
+ - refer to docs/github_payload
View
@@ -0,0 +1,45 @@
+class Service::MqttPub < Service
+ string :broker, :port, :topic, :id
+
+ require 'mqtt'
+
+ def receive_push
+
+ if data['broker'].to_s == ''
+ raise_config_error "Invalid broker endpoint."
+ end
+
+ port = data['port'].to_i
+ if data['port'].to_s == ''
+ port = 1883
+ end
+
+ if data['topic'].to_s == ''
+ raise_config_error "Invalid topic. Try github/<github_username>/<repo_name> ."
+ end
+
+ # Generate random epoch for ID if not specified
+ client_id = data['id'].to_s
+ if client_id == ''
+ client_id = Time.now.to_i.to_s
+ end
+
+ begin
+ # Connect to the broker, publish the payload!
+ MQTT::Client.connect(
+ :remote_host => data['broker'].to_s,
+ :remote_port => port,
+ :client_id => client_id
+ ) do |client|
+ client.publish(data['topic'].to_s, payload.to_json)
+ # Disconnect (don't send last will and testament)
+ client.disconnect(false)
+ end
+ rescue SocketError => e
+ warn "SocketError occurred: " + e
+ end
+
+ end
+
+
+end
View
@@ -0,0 +1,16 @@
+require File.expand_path('../helper', __FILE__)
+
+class MqttPubTest < Service::TestCase
+ def setup
+ @stubs = Faraday::Adapter::Test::Stubs.new
+ end
+
+ def test_push
+ svc = service :push, {'broker' => 'q.m2m.io','port' => '1883', 'id' => 'github', 'topic' => 'github/franklovecchio/github-services'}, 'payload'
+ svc.receive
+ end
+
+ def service(*args)
+ super Service::MqttPub, *args
+ end
+end