rain8net 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +30 -0
  2. data/lib/rain8net.rb +153 -0
  3. data/test/test_rain8net.rb +29 -0
  4. metadata +48 -0
data/README ADDED
@@ -0,0 +1,30 @@
1
+ = Rain8net library for Ruby.
2
+
3
+ Author: Adam Anderson (adam@makeascene.com)
4
+
5
+ NOTE: You must install ruby-serialport in order to use this library.
6
+ As of this writing, it is not available as a gem, but can be downloaded
7
+ here:
8
+ http://ruby-serialport.rubyforge.org
9
+
10
+ The Rain8net device is an RS232 controlled sprinkler controller. The
11
+ "net" in Rain8net refers to the device's ability to be expanded with
12
+ with several networked modules. This class can be used to control
13
+ one device or a whole network of devices.
14
+
15
+ Usage:
16
+
17
+ require 'rain8net'
18
+ r8 = Rain8net.new
19
+ r8.turn_on_zone(1)
20
+ sleep(30) # run for 30 seconds...
21
+ puts r8.zone_status(1) # => true
22
+ puts r8.zone_status(2) # => false
23
+ r8.turn_off_zone(1)
24
+
25
+ More information about the Rain8 series of products can be found on
26
+ the manufacturer's website at:
27
+ http://www.wgldesigns.com
28
+
29
+ Copyright (c) 2008 Make A Scene Media, Inc. All Rights Reserved.
30
+ Licensed under GPL
data/lib/rain8net.rb ADDED
@@ -0,0 +1,153 @@
1
+ # Rain8net library for Ruby.
2
+ #
3
+ # Author: Adam Anderson (adam@makeascene.com)
4
+ #
5
+ # NOTE: You must install ruby-serialport in order to use this library.
6
+ # As of this writing, it is not available as a gem, but can be downloaded
7
+ # here:
8
+ # http://ruby-serialport.rubyforge.org
9
+ #
10
+ # Rain8net is an RS232 controlled sprinkler controller. The "net"
11
+ # in Rain8net refers to the device's ability to be expanded with
12
+ # several networked modules. This class can be used to control
13
+ # one device or a whole network of devices.
14
+ #
15
+ # Usage:
16
+ #
17
+ # require 'rain8net'
18
+ # r8 = Rain8net.new
19
+ # r8.turn_on_zone(1)
20
+ # sleep(30) # run for 30 seconds...
21
+ # puts r8.zone_status(1) # => true
22
+ # puts r8.zone_status(2) # => false
23
+ # r8.turn_off_zone(1)
24
+ #
25
+ # More information about the Rain8 series of products can be found on
26
+ # the manufacturer's website at:
27
+ # http://www.wgldesigns.com
28
+ #
29
+ # Copyright (c) 2008 Make A Scene Media, Inc. All Rights Reserved.
30
+ # Licensed under GPL
31
+ #
32
+ Kernel::require "serialport" # Kernel::require works better for serialport.
33
+ class Rain8net
34
+ attr_reader :sp, :addresses, :tty
35
+
36
+ # Create a new object. Options include:
37
+ # TTY port (COM port) number on the machine connected to the
38
+ # RS232 port of the first module in the network.
39
+ # :tty => 0
40
+ #
41
+ # Address of the Rain8net devices. These are the addresses
42
+ # of the modules in the Rain8 network. The manufacturer default
43
+ # for each module is "01". Configure the address using the
44
+ # manufacturer's configuration program before hooking it up
45
+ # to your Rain8 network.
46
+ # :addresses => ['01', '45', '02']
47
+ # Make sure your addresses are entered in the order they have
48
+ # been built into your Rain8net system. The zones numbers are
49
+ # determined based on the order they are entered in the
50
+ # addresses array. (See Rain8net#module_address_for_zone)
51
+ #
52
+ # Example:
53
+ # r8 = Rain8net.new(:tty => 0, :addresses=>['01'])
54
+ # Note: values above are the defaults. So, the above is equivilant to:
55
+ # r8 = Rain8net.new
56
+ #
57
+ def initialize(options={})
58
+ default_options = {:tty => 0, :addresses => ["01"]}
59
+ options = default_options.merge(options)
60
+ @tty = options[:tty]
61
+ @addresses = options[:addresses]
62
+ @sp = SerialPort.new(@tty, 4800, 8, 1, SerialPort::NONE)
63
+ end
64
+
65
+ # Turn on an individual zone:
66
+ # r8.turn_on_zone(1)
67
+ #
68
+ def turn_on_zone(zone=1)
69
+ send_code("40#{module_address_for_zone(zone)}3#{zone}")
70
+ end
71
+
72
+ # Turn off an individual zone:
73
+ # r8.turn_off_zone(1)
74
+ #
75
+ def turn_off_zone(zone=1)
76
+ send_code("40#{module_address_for_zone(zone)}4#{zone}")
77
+ end
78
+
79
+ # Turn off all zones for the entire system.
80
+ # r8.turn_all_off
81
+ #
82
+ def turn_all_off
83
+ send_code("205555")
84
+ end
85
+
86
+ # Alias for Rain8net#turn_all_off
87
+ #
88
+ def all_off
89
+ turn_all_off
90
+ end
91
+
92
+ # Read all settings from a given module. Returns
93
+ # the raw hex response from the module.
94
+ # settings = r8.read_settings
95
+ # settings.to_s
96
+ # => (a whole bunch of hex info)
97
+ #
98
+ # TODO: Interpret the settings and format them into
99
+ # something usable.
100
+ def read_settings
101
+ send_code("232323")
102
+ response
103
+ end
104
+
105
+ # Get the status of the given zone. Returns true if
106
+ # a zone is running, false if it is not.
107
+ # r8.zone_status(1)
108
+ # => true
109
+ #
110
+ def zone_status(zone=1)
111
+ zone = zone.to_i
112
+ send_code("40#{module_address_for_zone(zone)}F0")
113
+ # Response includes a status of each of the 8 zones for the module.
114
+ r = response.reverse.unpack("B*").to_s.match(/(^.{8})/).to_s.split(//).reverse
115
+ position = (zone - (((zone - 1) / 8).to_i * 8) - 1).to_i
116
+ (r[position].to_i == 1) ? true : false
117
+ end
118
+
119
+ # Determines which address from the @addresses attribute is hooked
120
+ # up to a given zone. Note, there is no fancy way to figure out
121
+ # out what your hardware connections actually are. This method uses
122
+ # the convention that your FIRST 8 zones are connected to the FIRST
123
+ # address in the @addresses array. Zones 9-16 are connected to the
124
+ # next address in the array and so on.
125
+ # r8.module_address_for_zone(5)
126
+ # => "01"
127
+ #
128
+ def module_address_for_zone(zone=1)
129
+ module_position = ((zone.to_f - 0.01) / 8).to_i
130
+ begin
131
+ @addresses[module_position]
132
+ rescue
133
+ raise "No module found for zone #{zone}"
134
+ end
135
+ end
136
+
137
+ # Sends the given code to the Rain8net module.
138
+ # r8.send_code("205555")
139
+ #
140
+ def send_code(code)
141
+ sp.write([code].pack("H*"))
142
+ end
143
+
144
+ # After sending a code that expects a response, call this
145
+ # function to receive the response from the Rain8net module
146
+ # r8.response
147
+ # => "@\001"
148
+ def response
149
+ sleep(1)
150
+ sp.read
151
+ end
152
+
153
+ end
@@ -0,0 +1,29 @@
1
+ #:nodoc: all
2
+ require "test/unit"
3
+ require "../lib/rain8net"
4
+ class TestRain8net < Test::Unit::TestCase
5
+
6
+ def initialize(options)
7
+ super(options)
8
+ end
9
+
10
+ def setup
11
+ # Sets up a 2-module Rain8net system with 16 zones.
12
+ @r8 = Rain8net.new(:tty => 0, :addresses=>['01', '02'])
13
+ end
14
+
15
+ def test_constructor
16
+ assert_instance_of(Rain8net, @r8, message = "@r8 should be an instance of Rain8net")
17
+ end
18
+
19
+ def test_zone_assignments
20
+ assert_equal('01', @r8.module_address_for_zone(4), message = "Zone 4 should be associated with module at address '01'")
21
+ assert_equal('02', @r8.module_address_for_zone(10), message = "Zone 10 should be associated with module at address '02'")
22
+ end
23
+
24
+ def test_turning_on_first_zone
25
+ res = @r8.turn_on_zone 1
26
+ assert_not_nil(res, message = "Should get something back when turning on a zone.")
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: rain8net
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2008-05-30 00:00:00 -06:00
8
+ summary: A library for interfacing with Rain8net irrigation controllers.
9
+ require_paths:
10
+ - lib
11
+ email: adam@makeascene.com
12
+ homepage: http://rain8net.rubyforge.org
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: rain8net
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Adam Anderson
31
+ files:
32
+ - lib/rain8net.rb
33
+ - test/test_rain8net.rb
34
+ - README
35
+ test_files:
36
+ - test/test_rain8net.rb
37
+ rdoc_options: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ requirements: []
46
+
47
+ dependencies: []
48
+