Class: Flic::SimpleClient

Inherits:
Object
  • Object
show all
Defined in:
lib/flic/simple_client.rb

Defined Under Namespace

Classes: ConnectionChannelRemoved, Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*client_args) ⇒ SimpleClient

Returns a new instance of SimpleClient.



12
13
14
# File 'lib/flic/simple_client.rb', line 12

def initialize(*client_args)
  @client = Client.new(*client_args)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/flic/simple_client.rb', line 10

def client
  @client
end

Instance Method Details

#buttonsObject



21
22
23
24
25
26
27
# File 'lib/flic/simple_client.rb', line 21

def buttons
  server_info = process_events_until do |callback|
    client.get_info(&callback)
  end

  server_info.verified_buttons_bluetooth_addresses
end

#connect_buttonObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flic/simple_client.rb', line 29

def connect_button
  begin
    scan_wizard = Client::ScanWizard.new

    process_events_until do |callback|
      scan_wizard.removed do |result, bluetooth_address, *|
        if result == :success
          callback.call(bluetooth_address)
        else
          callback.call(nil)
        end
      end

      client.add_scan_wizard(scan_wizard)
    end
  ensure
    client.remove_scan_wizard(scan_wizard)
  end
end

#disconnect_button(button_bluetooth_address) ⇒ Object



49
50
51
# File 'lib/flic/simple_client.rb', line 49

def disconnect_button(button_bluetooth_address)
  client.force_disconnect(button_bluetooth_address)
end

#listen(latency_mode, *button_bluetooth_addresses) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flic/simple_client.rb', line 53

def listen(latency_mode, *button_bluetooth_addresses)
  connection_channels = []
  button_events = []
  broken = false

  begin
    button_bluetooth_addresses.each do |button_bluetooth_addresses|
      connection_channel = Client::ConnectionChannel.new(button_bluetooth_addresses, latency_mode)

      connection_channel.button_up_or_down do |click_type, latency|
        button_events << [button_bluetooth_addresses, click_type, latency]
      end

      connection_channel.button_single_click_or_double_click_or_hold do |click_type, latency|
        button_events << [button_bluetooth_addresses, click_type, latency]
      end

      connection_channel.removed do
        broken = true
      end

      connection_channels << connection_channel

      client.add_connection_channel connection_channel
    end


    loop do
      client.handle_next_event while !broken && button_events.empty?

      button_events.each do |button_event|
        yield *button_event
      end

      button_events.clear

      raise ConnectionChannelRemoved, 'A connection channel was removed' if broken
    end
  ensure
    connection_channels.each do |connection_channel|
      client.remove_connection_channel connection_channel
    end
  end
end

#shutdownObject



16
17
18
19
# File 'lib/flic/simple_client.rb', line 16

def shutdown
  client.shutdown
  event_dispatch_thread.join
end