lightwaverf 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/lightwaverf CHANGED
@@ -1,3 +1,7 @@
1
1
  #!/usr/bin/ruby
2
2
  require 'lightwaverf'
3
- LightWaveRF.new.go ARGV[0], ARGV[1], ARGV[2]
3
+ if ARGV[0] == 'help'
4
+ LightWaveRF.new.help
5
+ else
6
+ LightWaveRF.new.send ARGV[0], ARGV[1], ARGV[2]
7
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/ruby
2
+ require 'lightwaverf'
3
+ p LightWaveRF.new.config_json
data/lib/lightwaverf.rb CHANGED
@@ -1,7 +1,28 @@
1
+ require 'yaml'
2
+ require 'socket'
3
+ include Socket::Constants
4
+
1
5
  class LightWaveRF
2
6
 
3
7
  @config_file = nil
8
+ @config = nil
9
+
10
+ def usage
11
+ rooms = self.class.get_rooms self.get_config
12
+ 'usage: lightwaverf ' + rooms.keys.first + ' ' + rooms.values.first['device'].keys.first.to_s + ' on'
13
+ end
4
14
 
15
+ def help
16
+ help = self.usage + "\n"
17
+ help += 'your rooms, devices, and sequences, as defined in ' + self.get_config_file + ":\n"
18
+ help += YAML.dump self.get_config['room']
19
+ end
20
+
21
+ def config_json
22
+ require 'json'
23
+ JSON self.get_config
24
+ end
25
+
5
26
  def set_config_file file
6
27
  @config_file = file
7
28
  end
@@ -11,13 +32,15 @@ class LightWaveRF
11
32
  end
12
33
 
13
34
  def get_config
14
- require 'yaml'
15
- if ! File.exists? self.get_config_file
16
- File.open( @config_file, 'w' ) do | handle |
17
- handle.write YAML.dump( { 'host' => '192.168.0.14', 'room' => { 'our' => [ 'light', 'lights' ] } } )
35
+ if ! @config
36
+ if ! File.exists? self.get_config_file
37
+ File.open( self.get_config_file, 'w' ) do | handle |
38
+ handle.write YAML.dump( { 'host' => '192.168.0.14', 'room' => { 'our' => [ 'light', 'lights' ] }, 'sequence' => { 'lights' => [ [ 'our', 'light', 'on' ], [ 'our', 'lights', 'on' ] ] }} )
39
+ end
18
40
  end
41
+ @config = YAML.load_file self.get_config_file
19
42
  end
20
- YAML.load_file self.get_config_file
43
+ @config
21
44
  end
22
45
 
23
46
  def self.get_rooms config = { 'room' => { }}
@@ -63,22 +86,41 @@ class LightWaveRF
63
86
  # Turn one of your devices on or off
64
87
  #
65
88
  # Example:
66
- # >> LightWaveRF.new.go 'our', 'light', 'on'
89
+ # >> LightWaveRF.new.send 'our', 'light', 'on'
67
90
  #
68
91
  # Arguments:
69
92
  # room: (String)
70
93
  # device: (String)
71
94
  # 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]
95
+ def send room = nil, device = nil, state = 'on', debug = false
96
+ debug && ( p 'config is ' + self.get_config.to_s )
97
+ rooms = self.class.get_rooms self.get_config
78
98
  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
99
+ if rooms[room] && device && state && rooms[room]['device'][device]
100
+ command = self.command rooms[room], device, state
101
+ debug && ( p 'command is ' + command )
102
+ UDPSocket.new.send command, 0, self.get_config['host'], 9760
103
+ else
104
+ STDERR.puts self.usage
105
+ end
83
106
  end
107
+
108
+ def sequence name, debug = false
109
+ if self.get_config['sequence'][name]
110
+ self.get_config['sequence'][name].each do | task |
111
+ self.send task[0], task[1], task[2], debug
112
+ sleep 1
113
+ end
114
+ end
115
+ end
116
+
117
+ def energy
118
+ listener = UDPSocket.new
119
+ listener.bind '0.0.0.0', 9761
120
+ UDPSocket.new.send "666,@?", 0, self.get_config['host'], 9760
121
+ data, addr = listener.recvfrom 200
122
+ listener.close
123
+ data = /W=(?<usage>\d+),(?<max>\d+),(?<today>\d+),(?<yesterday>\d+)/.match( data )
124
+ end
125
+
84
126
  end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,12 +15,14 @@ description: Interact with lightwaverf wifi link from the command line
15
15
  email: pauly@clarkeology.com
16
16
  executables:
17
17
  - lightwaverf
18
+ - lightwaverf-config-json
18
19
  extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - lib/lightwaverf.rb
22
23
  - bin/lightwaverf
23
- homepage: http://www.clarkeology.com/wiki/lightwaverf+ruby+gem
24
+ - bin/lightwaverf-config-json
25
+ homepage: http://www.clarkeology.com/wiki/lightwaverf+ruby
24
26
  licenses: []
25
27
  post_install_message:
26
28
  rdoc_options: []