ruby_home 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -1
- data/lib/ruby_home/hap/server.rb +3 -3
- data/lib/ruby_home/version.rb +1 -1
- data/lib/ruby_home.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 164e411f03ec5168e551384eac1a24c7c95040aaa3e9c1c50fae85d2a357c0a6
|
4
|
+
data.tar.gz: 485d5bb8c361d732fb916f81e164d50a88cdfb44ce6088ec6c67f73d35e19f33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12340d7997f5c78bb160f5f7258595b73a81700c40a3a7de79acb2790290366443c5a03f899f4687487e82b6e7ed45a9c2400342c545d39588c0de14910bd052
|
7
|
+
data.tar.gz: 3c86acf49f8348c1d56f4189faabd222d19141bbfed8f073d69fd701a5bce40f6e65356f4f4ce95e6867da98ce3a18d4e4b279e62413ac65d1fb879952abda7a
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ Or install it yourself as:
|
|
37
37
|
|
38
38
|
$ gem install ruby_home
|
39
39
|
|
40
|
-
## Basic
|
40
|
+
## Basic usage
|
41
41
|
|
42
42
|
Create a fan with an on/off switch.
|
43
43
|
|
@@ -116,6 +116,39 @@ end
|
|
116
116
|
RubyHome.run
|
117
117
|
```
|
118
118
|
|
119
|
+
## Updating a characteristics value
|
120
|
+
|
121
|
+
If you have a service with characteristics that can be changed outside of Ruby Home. You'll want to keep Ruby Home in sync with these modifications. Otherwise, the characteristics current value won't correspond with reality. The simplest way to do this is a background job that periodically polls the devices current status and updates the corresponding characteristics value if it's changed.
|
122
|
+
|
123
|
+
Given a fan which can be switched on / off with a remote control. Which has a JSON API endpoint at http://example.org/fan_status.json that returns its current status `{ "on": true }` or `{ "on": false }`. We can spawn a thread that keeps polling the fans current status and if its changed update our fan service "on" characteristic.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
require 'json'
|
127
|
+
require 'open-uri'
|
128
|
+
require 'ruby_home'
|
129
|
+
|
130
|
+
fan = RubyHome::ServiceFactory.create(:fan)
|
131
|
+
on_characteristic = fan.characteristic(:on)
|
132
|
+
|
133
|
+
Thread.new do
|
134
|
+
def fetch_fan_status
|
135
|
+
json = JSON.load(open("http://example.org/fan_status.json"))
|
136
|
+
json["on"]
|
137
|
+
end
|
138
|
+
|
139
|
+
loop do
|
140
|
+
sleep 10 # seconds
|
141
|
+
|
142
|
+
current_fan_status = fetch_fan_status
|
143
|
+
|
144
|
+
unless on_characteristic.value == current_fan_status
|
145
|
+
on_characteristic.value = current_fan_status
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
RubyHome.run
|
151
|
+
```
|
119
152
|
|
120
153
|
## Development
|
121
154
|
|
data/lib/ruby_home/hap/server.rb
CHANGED
@@ -27,7 +27,6 @@ module RubyHome
|
|
27
27
|
def accept
|
28
28
|
socket = @server.accept
|
29
29
|
_, port, host = socket.peeraddr
|
30
|
-
puts "*** #{host}:#{port} connected"
|
31
30
|
|
32
31
|
monitor = @selector.register(socket, :r)
|
33
32
|
monitor.value = proc { read(socket) }
|
@@ -64,13 +63,14 @@ module RubyHome
|
|
64
63
|
|
65
64
|
return close(socket) unless request.keep_alive?
|
66
65
|
return close(socket) unless response.keep_alive?
|
66
|
+
rescue Errno::ECONNRESET, Errno::ECONNABORTED,
|
67
|
+
Errno::EPROTO, Errno::EINVAL, Errno::EHOSTUNREACH
|
68
|
+
close(socket)
|
67
69
|
rescue EOFError
|
68
70
|
close(socket)
|
69
71
|
end
|
70
72
|
|
71
73
|
def close(socket)
|
72
|
-
_, port, host = socket.peeraddr
|
73
|
-
puts "*** #{host}:#{port} disconnected"
|
74
74
|
@selector.deregister(socket)
|
75
75
|
socket_store.delete(socket)
|
76
76
|
socket.close
|
data/lib/ruby_home/version.rb
CHANGED
data/lib/ruby_home.rb
CHANGED
@@ -34,7 +34,7 @@ module RubyHome
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def threads
|
37
|
-
|
37
|
+
@_threads ||= []
|
38
38
|
end
|
39
39
|
|
40
40
|
def shutdown
|
@@ -42,15 +42,15 @@ module RubyHome
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def dns_service
|
45
|
-
|
45
|
+
@_dns_service ||= DNS::Service.new(hap_server.port)
|
46
46
|
end
|
47
47
|
|
48
48
|
def hap_server
|
49
|
-
|
49
|
+
@_hap_server ||= HAP::Server.new('0.0.0.0', 4567, socket_store)
|
50
50
|
end
|
51
51
|
|
52
52
|
def socket_store
|
53
|
-
|
53
|
+
@_socket_store ||= {}
|
54
54
|
end
|
55
55
|
|
56
56
|
def greet
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_home
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karl Entwistle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|