lightwaverf 0.0.8 → 0.1.0

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.
@@ -3,8 +3,12 @@ require 'lightwaverf'
3
3
  case ARGV[0]
4
4
  when 'help'
5
5
  puts LightWaveRF.new.help
6
+ when 'sequence'
7
+ puts LightWaveRF.new.sequence ARGV[1], ARGV[2]
6
8
  when 'energy'
7
9
  puts LightWaveRF.new.energy
10
+ when 'timer'
11
+ puts LightWaveRF.new.timer ARGV[1], ARGV[2]
8
12
  else
9
13
  LightWaveRF.new.send ARGV[0], ARGV[1], ARGV[2], ARGV[4]
10
14
  end
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/ruby
2
2
  require 'lightwaverf'
3
- puts LightWaveRF.new.config_json
3
+ require 'json'
4
+ puts JSON.generate LightWaveRF.new.get_config
@@ -21,12 +21,6 @@ class LightWaveRF
21
21
  help += "\n\nso to turn on " + room + " " + device + " type \"lightwaverf " + room + " " + device + " on\"\n"
22
22
  end
23
23
 
24
- def config_json
25
- require 'json'
26
- require 'pp'
27
- JSON.generate self.get_config
28
- end
29
-
30
24
  def set_config_file file
31
25
  @config_file = file
32
26
  end
@@ -63,6 +57,14 @@ class LightWaveRF
63
57
  rooms
64
58
  end
65
59
 
60
+ # Translate the "state" we pass in to one the wifi link understands
61
+ #
62
+ # Example:
63
+ # >> LightWaveRF.new.state 'on' # 'F1'
64
+ # >> LightWaveRF.new.state 'off' # 'F0'
65
+ #
66
+ # Arguments:
67
+ # state: (String)
66
68
  def self.get_state state = 'on'
67
69
  case state
68
70
  when 'off'
@@ -103,7 +105,7 @@ class LightWaveRF
103
105
  # device: (String)
104
106
  # state: (String)
105
107
  def send room = nil, device = nil, state = 'on', debug = false
106
- debug && ( p 'config is ' + self.get_config.to_s )
108
+ debug && ( puts 'config is ' + self.get_config.to_s )
107
109
  rooms = self.class.get_rooms self.get_config
108
110
  state = self.class.get_state state
109
111
  if rooms[room] && device && state && rooms[room]['device'][device]
@@ -135,10 +137,9 @@ class LightWaveRF
135
137
 
136
138
  def energy
137
139
  data = self.raw '666,@?'
138
- p data
139
- # /W=(?<usage>\d+),(?<max>\d+),(?<today>\d+),(?<yesterday>\d+)/.match( data ) # ruby 1.9 only?
140
- match = /W=(\d+),(\d+),(\d+),(\d+)/.match( data )
141
- { 'usage' => match[0], 'max' => match[1], 'today' => match[2], 'yesterday' => match[3] }
140
+ # /W=(?<usage>\d+),(?<max>\d+),(?<today>\d+),(?<yesterday>\d+)/.match data # ruby 1.9 only?
141
+ match = /W=(\d+),(\d+),(\d+),(\d+)/.match data
142
+ match and { 'usage' => match[1], 'max' => match[2], 'today' => match[3], 'yesterday' => match[4] }
142
143
  end
143
144
 
144
145
  def raw command
@@ -157,4 +158,67 @@ class LightWaveRF
157
158
  response
158
159
  end
159
160
 
161
+ # Use a google calendar as a timer?
162
+ # Needs a google calendar, with its url in your config file, with events like "lounge light on" etc
163
+ # Only the start time of the event is used right now.
164
+ #
165
+ # Run this as a cron job every 5 mins, ie
166
+ # */5 * * * * /usr/local/bin/lightwaverf timer 5 > /tmp/timer.out 2>&1
167
+ #
168
+ # Example:
169
+ # >> LightWaveRF.new.timer
170
+ # >> LightWaveRF.new.state 10
171
+ #
172
+ # Sample calendar:
173
+ # https://www.google.com/calendar/feeds/aar79qh62fej54nprq6334s7ck%40group.calendar.google.com/public/basic
174
+ # https://www.google.com/calendar/embed?src=aar79qh62fej54nprq6334s7ck%40group.calendar.google.com&ctz=Europe/London
175
+ #
176
+ # Arguments:
177
+ # interval: (Integer)
178
+ # debug: (Boolean)
179
+ #
180
+ # @todo actually use the interval we said...
181
+ def timer interval = 5, debug = false
182
+ require 'net/http'
183
+ require 'rexml/document'
184
+ url = LightWaveRF.new.get_config['calendar'] + '?singleevents=true&start-min=' + Date.today.strftime( '%Y-%m-%d' ) + '&start-max=' + Date.today.next.strftime( '%Y-%m-%d' )
185
+ debug && ( p url )
186
+ parsed_url = URI.parse url
187
+ http = Net::HTTP.new parsed_url.host, parsed_url.port
188
+ http.use_ssl = true
189
+ request = Net::HTTP::Get.new parsed_url.request_uri
190
+ response = http.request request
191
+ doc = REXML::Document.new response.body
192
+ now = Time.now.strftime '%H:%M'
193
+ interval_end_time = ( Time.now + interval.to_i * 60 ).strftime '%H:%M'
194
+ triggered = 0
195
+ doc.elements.each 'feed/entry' do | e |
196
+ command = /(\w+) (\w+)( (\w+))?/.match e.elements['title'].text # look for events with a title like 'lounge light on'
197
+ if command
198
+ room = command[1].to_s
199
+ device = command[2].to_s
200
+ status = command[4]
201
+ timer = /When: ([\w ]+) (\d\d:\d\d) to ([\w ]+)?(\d\d:\d\d)/.match e.elements['summary'].text
202
+ if timer
203
+ event_time = timer[2].to_s
204
+ event_end_time = timer[4]
205
+ else
206
+ STDERR.puts 'did not get When: in ' + e.elements['summary'].text
207
+ end
208
+ if ! status
209
+ event_time = event_end_time
210
+ status = 'off'
211
+ end
212
+ debug && ( p e.elements['title'].text + ' - ' + now + ' < ' + event_time + ' < ' + interval_end_time + ' ?' )
213
+ if event_time >= now && event_time < interval_end_time
214
+ debug && ( p 'so going to turn the ' + room + ' ' + device + ' ' + status.to_s + ' now!' )
215
+ self.send room, device, status.to_s
216
+ sleep 1
217
+ triggered += 1
218
+ end
219
+ end
220
+ end
221
+ triggered.to_s + " events triggered"
222
+ end
160
223
  end
224
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightwaverf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-30 00:00:00.000000000 Z
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Interact with lightwaverf wifi link from code or the command line
14
+ description: Interact with lightwaverf wifi link from code or the command line. Control
15
+ your lights, heating, sockets etc.
15
16
  email: pauly@clarkeology.com
16
17
  executables:
17
18
  - lightwaverf