lita-sonos-commander 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/client.rb +0 -1
- data/lib/lita-sonos-commander.rb +1 -0
- data/lib/lita/commander_middleware.rb +47 -0
- data/lib/lita/handlers/sonos_commander.rb +41 -38
- data/lita-sonos-commander.gemspec +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71a47b802acd8b74a12165728bd3b0fded035ab5
|
4
|
+
data.tar.gz: 49ecca4439098ff7827f4d2b6ac78ddde816e4e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dae726c2765371f5459269bc7c95a6468f224d32cf0f64de0656c932241e3bc24c36028959114135512adcfa876a26ed2d390fb85acd500fe9bb22f8f9f106fd
|
7
|
+
data.tar.gz: 4d22468f6a65ea06c68d426628d1908d68c53606c2cbc301ad0b26e3f6e776593a55099ce71eeea8d04e518a09f0e5dd016d8d1784720d4aa0e6353f6e5a926d
|
data/example/client.rb
CHANGED
data/lib/lita-sonos-commander.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
Lita::CommanderMiddleware = lambda do |env|
|
2
|
+
|
3
|
+
if Faye::WebSocket.websocket?(env)
|
4
|
+
ws = Faye::WebSocket.new(env)
|
5
|
+
|
6
|
+
Lita::Handlers::SonosCommander.add_socket(ws)
|
7
|
+
|
8
|
+
ws.on :open do |event|
|
9
|
+
end
|
10
|
+
|
11
|
+
ws.on :connect do |event|
|
12
|
+
end
|
13
|
+
|
14
|
+
ws.on :message do |event|
|
15
|
+
ws.send({ message: event.data }.to_json)
|
16
|
+
|
17
|
+
sleep 0.5
|
18
|
+
ws.send({ message: 'WE DID IT TWITCH', command: 'echo' }.to_json)
|
19
|
+
end
|
20
|
+
|
21
|
+
ws.on :close do |event|
|
22
|
+
Lita::Handlers::SonosCommander.drop_socket(ws)
|
23
|
+
|
24
|
+
p [:close, event.code, event.reason]
|
25
|
+
ws = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return async Rack response
|
29
|
+
ws.rack_response
|
30
|
+
|
31
|
+
else
|
32
|
+
# Normal HTTP request
|
33
|
+
[200, {'Content-Type' => 'text/plain'}, ['Hello']]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def register_faye(arg)
|
38
|
+
@@_sockets ||= []
|
39
|
+
middleware = robot.registry.config.http.middleware
|
40
|
+
result = middleware.use App
|
41
|
+
end
|
42
|
+
|
43
|
+
def sonos_connector(request, response)
|
44
|
+
middlewares.each do |mw|
|
45
|
+
mw.middleware.call(request.env)
|
46
|
+
end
|
47
|
+
end
|
@@ -1,68 +1,71 @@
|
|
1
|
-
require '
|
1
|
+
require 'json'
|
2
|
+
require 'faye/websocket'
|
3
|
+
require 'uri'
|
4
|
+
|
2
5
|
module Lita
|
3
6
|
module Handlers
|
4
7
|
class SonosCommander < Handler
|
5
|
-
# insert handler code here
|
6
|
-
|
7
|
-
require 'faye/websocket'
|
8
|
-
|
9
8
|
|
10
9
|
http.get '/sonos/listen', :sonos_connector
|
11
10
|
|
12
|
-
route /^
|
13
|
-
route /^
|
11
|
+
route /^play_url (.+)/, :sonos_play_url
|
12
|
+
route /^say_text (.+)/, :sonos_say_text
|
14
13
|
|
15
14
|
on :loaded, :register_faye
|
16
15
|
|
17
|
-
def
|
18
|
-
|
16
|
+
def sonos_play_url(message)
|
17
|
+
text = message.matches.last.last
|
18
|
+
emit_message command: 'play_url', data: URI.escape(text)
|
19
19
|
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
ws.on :message do |event|
|
26
|
-
ws.send(event.data)
|
21
|
+
def sonos_say_text(message)
|
22
|
+
text = message.matches.last.last
|
23
|
+
emit_message command: 'play_text', data: text
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
def emit_message(command:, data:)
|
27
|
+
puts "emitting #{command} \t #{data}"
|
28
|
+
sockets.each do |ws|
|
29
|
+
ws.send(
|
30
|
+
{ command: command, data: { text: data, volume: 20 } }.to_json
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
35
|
+
def middlewares
|
36
|
+
robot.registry.config.http.middleware
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
+
def sockets
|
40
|
+
self.class.sockets
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
[200, {'Content-Type' => 'text/plain'}, ['Hello']]
|
43
|
-
end
|
43
|
+
def self.sockets
|
44
|
+
@@_sockets ||= []
|
44
45
|
end
|
45
46
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
def self.add_socket(socket)
|
48
|
+
puts "Tracking socket #{socket}"
|
49
|
+
@@_sockets ||= []
|
50
|
+
@@_sockets << socket
|
50
51
|
end
|
51
52
|
|
52
|
-
def
|
53
|
-
#
|
53
|
+
def self.drop_socket(socket)
|
54
|
+
puts "Forgetting socket #{socket}"
|
55
|
+
sockets.delete_if { |s| s == socket }
|
54
56
|
end
|
55
57
|
|
56
|
-
def
|
58
|
+
def self.serialize(message)
|
59
|
+
end
|
57
60
|
|
58
|
-
def
|
61
|
+
def register_faye(arg)
|
62
|
+
@@_sockets ||= []
|
59
63
|
middleware = robot.registry.config.http.middleware
|
64
|
+
result = middleware.use Lita::CommanderMiddleware
|
60
65
|
end
|
61
66
|
|
62
67
|
def sonos_connector(request, response)
|
63
|
-
#binding.pry
|
64
68
|
middlewares.each do |mw|
|
65
|
-
# binding.pry
|
66
69
|
mw.middleware.call(request.env)
|
67
70
|
end
|
68
71
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = 'lita-sonos-commander'
|
3
|
-
spec.version = '
|
3
|
+
spec.version = '1.0.0'
|
4
4
|
spec.authors = ['Daniel J. Pritchett']
|
5
5
|
spec.email = ['dpritchett@gmail.com']
|
6
6
|
spec.description = 'Control your Sonos with Lita chatbot commands'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-sonos-commander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel J. Pritchett
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- Rakefile
|
153
153
|
- example/client.rb
|
154
154
|
- lib/lita-sonos-commander.rb
|
155
|
+
- lib/lita/commander_middleware.rb
|
155
156
|
- lib/lita/handlers/sonos_commander.rb
|
156
157
|
- lita-sonos-commander.gemspec
|
157
158
|
- locales/en.yml
|