lightwaverf 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/lightwaverf.rb +58 -28
- metadata +1 -1
data/lib/lightwaverf.rb
CHANGED
@@ -1,34 +1,30 @@
|
|
1
1
|
class LightWaveRF
|
2
|
-
|
2
|
+
|
3
|
+
@config_file = nil
|
4
|
+
|
5
|
+
def set_config_file file
|
6
|
+
@config_file = file
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_config_file
|
10
|
+
@config_file || File.expand_path('~') + '/lightwaverf-config.yml'
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_config
|
3
14
|
require 'yaml'
|
4
|
-
|
5
|
-
|
6
|
-
File.open( file, 'w' ) do | handle |
|
15
|
+
if ! File.exists? self.get_config_file
|
16
|
+
File.open( @config_file, 'w' ) do | handle |
|
7
17
|
handle.write YAML.dump( { 'host' => '192.168.0.14', 'room' => { 'our' => [ 'light', 'lights' ] } } )
|
8
18
|
end
|
9
19
|
end
|
10
|
-
YAML.load_file
|
20
|
+
YAML.load_file self.get_config_file
|
11
21
|
end
|
12
22
|
|
13
|
-
|
14
|
-
#
|
15
|
-
# Example:
|
16
|
-
# >> LightWaveRF.new.go 'our', 'light', 'on'
|
17
|
-
#
|
18
|
-
# Arguments:
|
19
|
-
# room: (String)
|
20
|
-
# device: (String)
|
21
|
-
# state: (String)
|
22
|
-
def go room, device, state
|
23
|
-
require 'socket'
|
24
|
-
config = self.config
|
23
|
+
def self.get_rooms config = { 'room' => { }}
|
25
24
|
rooms = { }
|
26
25
|
r = 1
|
27
26
|
config['room'].each do | name, devices |
|
28
|
-
rooms[name] = {
|
29
|
-
'id' => 'R' + r.to_s,
|
30
|
-
'device' => { }
|
31
|
-
}
|
27
|
+
rooms[name] = { 'id' => 'R' + r.to_s, 'device' => { }}
|
32
28
|
d = 1
|
33
29
|
devices.each do | device |
|
34
30
|
rooms[name]['device'][device] = 'D' + d.to_s
|
@@ -36,19 +32,53 @@ class LightWaveRF
|
|
36
32
|
end
|
37
33
|
r += 1
|
38
34
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
35
|
+
rooms
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.get_state state = 'on'
|
42
39
|
case state
|
43
40
|
when 'off'
|
44
41
|
state = 'F0'
|
45
42
|
when 'on'
|
46
43
|
state = 'F1'
|
47
|
-
when 'setup'
|
48
|
-
state = 'F1'
|
49
44
|
when 1..99
|
50
|
-
|
45
|
+
state = 'FdP' + ( state * 0.32 ).round.to_s
|
51
46
|
end
|
52
|
-
|
47
|
+
state
|
48
|
+
end
|
49
|
+
|
50
|
+
# Get the command to send to the wifi link
|
51
|
+
#
|
52
|
+
# Example:
|
53
|
+
# >> LightWaveRF.new.command 'our', 'light', 'on'
|
54
|
+
#
|
55
|
+
# Arguments:
|
56
|
+
# room: (String)
|
57
|
+
# device: (String)
|
58
|
+
# state: (String)
|
59
|
+
def command room, device, state
|
60
|
+
"666,!" + room['id'] + room['device'][device] + state + "|"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Turn one of your devices on or off
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
# >> LightWaveRF.new.go 'our', 'light', 'on'
|
67
|
+
#
|
68
|
+
# Arguments:
|
69
|
+
# room: (String)
|
70
|
+
# device: (String)
|
71
|
+
# state: (String)
|
72
|
+
def go room, device, state = 'on', debug = false
|
73
|
+
require 'socket'
|
74
|
+
config = self.get_config
|
75
|
+
debug && ( p 'config is ' + config.to_s )
|
76
|
+
rooms = self.class.get_rooms config
|
77
|
+
room = rooms[room]
|
78
|
+
state = self.class.get_state state
|
79
|
+
room && device && state && room['device'][device] || abort( "usage: #{__FILE__} [" + rooms.keys.join( "|" ) + "] light on" )
|
80
|
+
command = self.command room, device, state
|
81
|
+
debug && ( p 'command is ' + command )
|
82
|
+
UDPSocket.new.send command, 0, config['host'], 9760
|
53
83
|
end
|
54
84
|
end
|