Class: Cassia::ResponseHandlers::WriteCharByHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/cassia/response_handlers/write_char_by_handle.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_controller, router:, device_mac:, handle:, value:) ⇒ WriteCharByHandle

Returns a new instance of WriteCharByHandle.



4
5
6
7
8
9
10
# File 'lib/cassia/response_handlers/write_char_by_handle.rb', line 4

def initialize(access_controller, router: , device_mac: , handle: , value: )
  @access_controller = access_controller
  @router = router
  @device_mac = device_mac
  @handle = handle
  @value = value
end

Instance Method Details

#handle(response) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/cassia/response_handlers/write_char_by_handle.rb', line 12

def handle(response)
  if response.success?
    return handle_success
  else
    handle_failure(response)
  end
  response.success?
end

#handle_failure(response) ⇒ Object



36
37
38
# File 'lib/cassia/response_handlers/write_char_by_handle.rb', line 36

def handle_failure(response)
  @access_controller.error = response.body
end

#handle_successObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cassia/response_handlers/write_char_by_handle.rb', line 21

def handle_success
  device = @router.connected_devices.detect {|device| device.mac == @device_mac}
  char = device.characteristics.detect {|char| char.handle == @handle || !char.descriptors.detect {|d| d.handle == @handle }.nil? }
  if char
    if @value == "0100"
      char.notification_on = true
    elsif @value == "0000"
      char.notification_on = false
    end
  else
    @access_controller.error = "Characteristic With Given Handle Not Found"
  end
  !(char.nil?)
end