Class: Hacklet::Dongle
- Inherits:
-
Object
- Object
- Hacklet::Dongle
- Defined in:
- lib/hacklet/dongle.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.open(logger = Logger.new(STDOUT)) ⇒ Object
Public: Initializes a dongle instance and yields it.
Instance Method Summary collapse
-
#commission ⇒ Object
Public: Listens for new devices on the network.
-
#initialize(serial, logger) ⇒ Dongle
constructor
serial - Serial connection to use with the dongle.
-
#lock_network ⇒ Object
Public: Locks the network, prevents adding new devices.
-
#request_samples(network_id, channel_id) ⇒ Object
Public: Request stored samples.
-
#select_network(network_id) ⇒ Object
Public: Selects the network.
-
#switch(network_id, channel_id, state) ⇒ Object
Public: Used to controls whether a socket is on or off.
-
#unlock_network ⇒ Object
Public: Unlocks the network, to add a new device.
Constructor Details
#initialize(serial, logger) ⇒ Dongle
serial - Serial connection to use with the dongle. logger - The Logger instance to log to.
27 28 29 30 |
# File 'lib/hacklet/dongle.rb', line 27 def initialize(serial, logger) @serial = serial @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/hacklet/dongle.rb', line 6 def logger @logger end |
Class Method Details
.open(logger = Logger.new(STDOUT)) ⇒ Object
Public: Initializes a dongle instance and yields it.
logger - The Logger instance to log to, defaults to STDOUT.
Returns nothing.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/hacklet/dongle.rb', line 13 def self.open(logger=Logger.new(STDOUT)) serial = SerialConnection.new(logger) dongle = Dongle.new(serial, logger) begin dongle.send(:boot) dongle.send(:boot_confirm) yield dongle ensure serial.close end end |
Instance Method Details
#commission ⇒ Object
Public: Listens for new devices on the network.
This must be executed within an open session.
Returns nothing.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/hacklet/dongle.rb', line 37 def commission response = nil begin unlock_network Timeout.timeout(30) do @logger.info("Listening for devices ...") loop do buffer = @serial.receive(4) buffer += @serial.receive(buffer.bytes.to_a[3]+1) if buffer.bytes.to_a[1] == 0xa0 response = BroadcastResponse.read(buffer) @logger.info("Found device 0x%x on network 0x%x" % [response.device_id, response.network_id]) break end end end rescue Timeout::Error ensure lock_network end update_time(response.network_id) if response end |
#lock_network ⇒ Object
Public: Locks the network, prevents adding new devices.
Returns the BootConfirmResponse
131 132 133 134 135 136 |
# File 'lib/hacklet/dongle.rb', line 131 def lock_network @logger.info("Locking network") @serial.transmit(LockRequest.new) LockResponse.read(@serial.receive(6)) @logger.info("Locking complete") end |
#request_samples(network_id, channel_id) ⇒ Object
Public: Request stored samples.
network_id - 2 byte identified for the network. channel_id - 2 byte identified for the channel.
TODO: This needs to return a more usable set of data. Returns the SamplesResponse.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hacklet/dongle.rb', line 81 def request_samples(network_id, channel_id) @logger.info("Requesting samples") @serial.transmit(SamplesRequest.new(:network_id => network_id, :channel_id => channel_id)) AckResponse.read(@serial.receive(6)) buffer = @serial.receive(4) remaining_bytes = buffer.bytes.to_a[3] + 1 buffer += @serial.receive(remaining_bytes) response = SamplesResponse.read(buffer) response.converted_samples.each do |time, wattage| @logger.info("#{wattage}w at #{time}") end @logger.info("#{response.sample_count} returned, #{response.stored_sample_count} remaining") response end |
#select_network(network_id) ⇒ Object
Public: Selects the network.
This must be executed within an open session. I’m guessing it selects the network.
network_id - 2 byte identified for the network.
Returns nothing.
69 70 71 72 |
# File 'lib/hacklet/dongle.rb', line 69 def select_network(network_id) @serial.transmit(HandshakeRequest.new(:network_id => network_id)) HandshakeResponse.read(@serial.receive(6)) end |
#switch(network_id, channel_id, state) ⇒ Object
Public: Used to controls whether a socket is on or off.
network_id - 2 byte identified for the network. channel_id - 1 byte identified for the channel. enabled - true enables the socket and false disables it.
Returns the SwitchResponse.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/hacklet/dongle.rb', line 105 def switch(network_id, channel_id, state) request = ScheduleRequest.new(:network_id => network_id, :channel_id => channel_id) if state request.always_on! @logger.info("Turning on channel #{channel_id} on network 0x#{network_id.to_s(16)}") else request.always_off! @logger.info("Turning off channel #{channel_id} on network 0x#{network_id.to_s(16)}") end @serial.transmit(request) ScheduleResponse.read(@serial.receive(6)) end |
#unlock_network ⇒ Object
Public: Unlocks the network, to add a new device.
Returns the BootConfirmResponse
121 122 123 124 125 126 |
# File 'lib/hacklet/dongle.rb', line 121 def unlock_network @logger.info("Unlocking network") @serial.transmit(UnlockRequest.new) LockResponse.read(@serial.receive(6)) @logger.info("Unlocking complete") end |