Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 74 lines (59 sloc) 1.962 kb
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
1 class Service::MqttPub < Service
45a2e6f @shawndavenport Mark a few service hook's password fields as such.
shawndavenport authored
2 string :broker, :port, :topic, :clientid, :user
3 password :pass
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
4
5 require 'mqtt'
a989abf @franklovecchio Add initial MQTT hook.
franklovecchio authored
6
7 def receive_push
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
8
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
9 # Optional - use m2m.io public broker if not specified
6acb24d @franklovecchio Forgot to commit.
franklovecchio authored
10 broker = data['broker'].to_s
11 if broker == ''
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
12 broker = 'q.m2m.io'
a989abf @franklovecchio Add initial MQTT hook.
franklovecchio authored
13 end
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
14
36e77f5 @franklovecchio Add username/password support.
franklovecchio authored
15 # Optional - use standard MQTT port if not specified
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
16 port = data['port'].to_i
17 if data['port'].to_s == ''
18 port = 1883
a989abf @franklovecchio Add initial MQTT hook.
franklovecchio authored
19 end
20
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
21 if data['topic'].to_s == ''
22 raise_config_error "Invalid topic. Try github/<github_username>/<repo_name> ."
a989abf @franklovecchio Add initial MQTT hook.
franklovecchio authored
23 end
24
36e77f5 @franklovecchio Add username/password support.
franklovecchio authored
25 # Optional - generate random epoch for ID if not specified
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
26 clientid = data['clientid'].to_s
27 if clientid == ''
28 # Random ID doesn't make sense, but use prefix like MQTT::generate_client_id
29 clientid = 'github_' + Time.now.to_i.to_s
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
30 end
36e77f5 @franklovecchio Add username/password support.
franklovecchio authored
31
32 # Optional, specify nil if not specified (per http://rubydoc.info/gems/mqtt/MQTT/Client)
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
33 user = data['user'].to_s
34 if user == ''
35 user = nil
36e77f5 @franklovecchio Add username/password support.
franklovecchio authored
36 end
37
38 # Optional, specify nil if not specified
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
39 pass = data['pass'].to_s
40 if pass == ''
41 pass = nil
36e77f5 @franklovecchio Add username/password support.
franklovecchio authored
42 end
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
43
44 # Handle specifying a username or a password, but not both
45 if user != nil and pass == nil
46 raise_config_error "You specified a username without a password."
47 end
48
49 if pass != nil and user == nil
50 raise_config_error "You specified a password without a username."
51 end
52
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
53 begin
54 # Connect to the broker, publish the payload!
55 MQTT::Client.connect(
6acb24d @franklovecchio Forgot to commit.
franklovecchio authored
56 :remote_host => broker,
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
57 :remote_port => port,
dd5a271 @franklovecchio Add 3.1 support (username/password), add prefix to clientid "github_"…
franklovecchio authored
58 :client_id => clientid,
59 :username => user,
60 :password => pass
c1fbb7f @franklovecchio Add working MQTT publish. Update gem file for dependency. Remove requ…
franklovecchio authored
61 ) do |client|
62 client.publish(data['topic'].to_s, payload.to_json)
63 # Disconnect (don't send last will and testament)
64 client.disconnect(false)
65 end
66 rescue SocketError => e
67 warn "SocketError occurred: " + e
a989abf @franklovecchio Add initial MQTT hook.
franklovecchio authored
68 end
69
70 end
71
72
73 end
Something went wrong with that request. Please try again.