Class: Hacklet::Dongle

Inherits:
Object
  • Object
show all
Defined in:
lib/hacklet/dongle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = Logger.new(STDOUT)) ⇒ Dongle

logger - Optionally takes a Logger instance, the default is to log to

STDOUT


11
12
13
# File 'lib/hacklet/dongle.rb', line 11

def initialize(logger=Logger.new(STDOUT))
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/hacklet/dongle.rb', line 7

def logger
  @logger
end

Instance Method Details

#commissionObject

Public: Listens for new devices on the network.

This must be executed within an open session.

Returns nothing.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hacklet/dongle.rb', line 38

def commission
  require_session

  response = nil
  begin
    unlock_network
    Timeout.timeout(30) do
      @logger.info("Listening for devices ...")
      loop do
        buffer = receive(4)
        buffer += 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_networkObject

Public: Locks the network, prevents adding new devices.

Returns the BootConfirmResponse



140
141
142
143
144
145
# File 'lib/hacklet/dongle.rb', line 140

def lock_network
  @logger.info("Locking network")
  transmit(LockRequest.new)
  LockResponse.read(receive(6))
  @logger.info("Locking complete")
end

#open_session(port = '/dev/ttyUSB0') ⇒ Object

Public: Initializes a session so the client can request data.

port - Optional string for configuring the serial port device.

Returns nothing.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hacklet/dongle.rb', line 20

def open_session(port='/dev/ttyUSB0')
  @serial = open_serial_port(port)
  begin
    @logger.info("Booting")
    boot
    boot_confirm
    @logger.info("Booting complete")
    yield self
  ensure
    @serial.close
  end
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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hacklet/dongle.rb', line 86

def request_samples(network_id, channel_id)
  require_session

  @logger.info("Requesting samples")
  transmit(SamplesRequest.new(:network_id => network_id, :channel_id => channel_id))
  AckResponse.read(receive(6))
  buffer = receive(4)
  remaining_bytes = buffer.bytes.to_a[3] + 1
  buffer += 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.



72
73
74
75
76
77
# File 'lib/hacklet/dongle.rb', line 72

def select_network(network_id)
  require_session

  transmit(HandshakeRequest.new(:network_id => network_id))
  HandshakeResponse.read(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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hacklet/dongle.rb', line 112

def switch(network_id, channel_id, state)
  require_session

  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
  transmit(request)
  ScheduleResponse.read(receive(6))
end

#unlock_networkObject

Public: Unlocks the network, to add a new device.

Returns the BootConfirmResponse



130
131
132
133
134
135
# File 'lib/hacklet/dongle.rb', line 130

def unlock_network
  @logger.info("Unlocking network")
  transmit(UnlockRequest.new)
  LockResponse.read(receive(6))
  @logger.info("Unlocking complete")
end