lightwaverf 0.1.1 → 0.2.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.
Files changed (3) hide show
  1. data/bin/lightwaverf +1 -1
  2. data/lib/lightwaverf.rb +41 -12
  3. metadata +2 -2
@@ -6,7 +6,7 @@ case ARGV[0]
6
6
  when 'sequence'
7
7
  puts LightWaveRF.new.sequence ARGV[1], ARGV[2]
8
8
  when 'energy'
9
- puts LightWaveRF.new.energy
9
+ puts LightWaveRF.new.energy ARGV[1], ARGV[2], ARGV[3]
10
10
  when 'timer'
11
11
  puts LightWaveRF.new.timer ARGV[1], ARGV[2]
12
12
  else
@@ -5,6 +5,7 @@ include Socket::Constants
5
5
  class LightWaveRF
6
6
 
7
7
  @config_file = nil
8
+ @log_file = nil
8
9
  @config = nil
9
10
 
10
11
  # Display usage info
@@ -33,6 +34,11 @@ class LightWaveRF
33
34
  @config_file || File.expand_path('~') + '/lightwaverf-config.yml'
34
35
  end
35
36
 
37
+ # Log file getter
38
+ def get_log_file
39
+ @log_file || File.expand_path('~') + '/lightwaverf.log'
40
+ end
41
+
36
42
  # Get the config file, create it if it does not exist
37
43
  def get_config
38
44
  if ! @config
@@ -111,12 +117,12 @@ class LightWaveRF
111
117
  # device: (String)
112
118
  # state: (String)
113
119
  def send room = nil, device = nil, state = 'on', debug = false
114
- debug && ( puts 'config is ' + self.get_config.to_s )
120
+ debug and ( puts 'config is ' + self.get_config.to_s )
115
121
  rooms = self.class.get_rooms self.get_config
116
122
  state = self.class.get_state state
117
- if rooms[room] && device && state && rooms[room]['device'][device]
123
+ if rooms[room] and device and state and rooms[room]['device'][device]
118
124
  command = self.command rooms[room], device, state
119
- debug && ( p 'command is ' + command )
125
+ debug and ( p 'command is ' + command )
120
126
  self.raw command
121
127
  else
122
128
  STDERR.puts self.usage
@@ -141,11 +147,26 @@ class LightWaveRF
141
147
  end
142
148
  end
143
149
 
144
- def energy
150
+ def energy title = nil, note = nil, debug = false
151
+ debug and note and ( p 'energy: ' + note )
145
152
  data = self.raw '666,@?'
153
+ debug and ( p data )
146
154
  # /W=(?<usage>\d+),(?<max>\d+),(?<today>\d+),(?<yesterday>\d+)/.match data # ruby 1.9 only?
147
155
  match = /W=(\d+),(\d+),(\d+),(\d+)/.match data
148
- match and { 'usage' => match[1], 'max' => match[2], 'today' => match[3], 'yesterday' => match[4] }
156
+ debug and ( p match )
157
+ if match
158
+ data = { 'message' => { 'usage' => match[1].to_i, 'max' => match[2].to_i, 'today' => match[3].to_i }}
159
+ data['timestamp'] = Time.now.to_s
160
+ if note
161
+ data['message']['annotation'] = { 'title' => title.to_s, 'text' => note.to_s }
162
+ end
163
+ debug and ( p data )
164
+ require 'json'
165
+ File.open( self.get_log_file, 'a' ) do |f|
166
+ f.write( data.to_json + "\n" )
167
+ end
168
+ data['message']
169
+ end
149
170
  end
150
171
 
151
172
  def raw command
@@ -186,7 +207,7 @@ class LightWaveRF
186
207
  require 'net/http'
187
208
  require 'rexml/document'
188
209
  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' )
189
- debug && ( p url )
210
+ debug and ( p url )
190
211
  parsed_url = URI.parse url
191
212
  http = Net::HTTP.new parsed_url.host, parsed_url.port
192
213
  http.use_ssl = true
@@ -195,7 +216,7 @@ class LightWaveRF
195
216
  doc = REXML::Document.new response.body
196
217
  now = Time.now.strftime '%H:%M'
197
218
  interval_end_time = ( Time.now + interval.to_i * 60 ).strftime '%H:%M'
198
- triggered = 0
219
+ triggered = []
199
220
  doc.elements.each 'feed/entry' do | e |
200
221
  command = /(\w+) (\w+)( (\w+))?/.match e.elements['title'].text # look for events with a title like 'lounge light on'
201
222
  if command
@@ -216,17 +237,25 @@ class LightWaveRF
216
237
  event_times = { event_time => 'on', event_end_time => 'off' }
217
238
  end
218
239
  event_times.each do | t, s |
219
- debug && ( p e.elements['title'].text + ' - ' + now + ' < ' + t + ' < ' + interval_end_time + ' ?' )
220
- if t >= now && t < interval_end_time
221
- debug && ( p 'so going to turn the ' + room + ' ' + device + ' ' + s.to_s + ' now!' )
240
+ debug and ( p e.elements['title'].text + ' - ' + now + ' < ' + t + ' < ' + interval_end_time + ' ?' )
241
+ if t >= now and t < interval_end_time
242
+ debug and ( p 'so going to turn the ' + room + ' ' + device + ' ' + s.to_s + ' now!' )
222
243
  self.send room, device, s.to_s
223
244
  sleep 1
224
- triggered += 1
245
+ triggered << [ room, device, s ]
225
246
  end
226
247
  end
227
248
  end
228
249
  end
229
- triggered.to_s + " events triggered"
250
+ triggered.length.to_s + " events triggered"
251
+ title = nil
252
+ text = nil
253
+ if triggered.length > 0
254
+ debug and ( p triggered.length.to_s + ' events so annotating energy log too...' )
255
+ title = 'timer'
256
+ text = triggered.map { |e| e.join " " }.join ", "
257
+ end
258
+ self.energy title, text, debug
230
259
  end
231
260
  end
232
261
 
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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,7 +12,7 @@ cert_chain: []
12
12
  date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Interact with lightwaverf wifi link from code or the command line. Control
15
- your lights, heating, sockets etc.
15
+ your lights, heating, sockets etc. Also logs energy usage.
16
16
  email: pauly@clarkeology.com
17
17
  executables:
18
18
  - lightwaverf