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
# File 'lib/hacklet/dongle.rb', line 38

def commission
  require_session

  begin
    unlock_network
    Timeout.timeout(30) do
      loop do
        @logger.info("Listening for devices ...")
        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])
        end
      end
    end
  rescue Timeout::Error
  ensure
    lock_network
  end
end

#lock_networkObject

Public: Locks the network, prevents adding new devices.

Returns the BootConfirmResponse



136
137
138
139
140
141
# File 'lib/hacklet/dongle.rb', line 136

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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hacklet/dongle.rb', line 82

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.



68
69
70
71
72
73
# File 'lib/hacklet/dongle.rb', line 68

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hacklet/dongle.rb', line 108

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



126
127
128
129
130
131
# File 'lib/hacklet/dongle.rb', line 126

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