flic 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d98133ba87184c44c442f4368915fff6a872f27
4
- data.tar.gz: 92ec00a30aed285f08d4f4bf5bcfd646ee8c6e75
3
+ metadata.gz: ae762531f46f32f4d88e0d006bf08c7c804e1a9f
4
+ data.tar.gz: 470cd52b6b01ee89bb423df22d6847d6bfdfef9c
5
5
  SHA512:
6
- metadata.gz: 84f52fc2f06133df1fb3cae5000e463a01378261136ae972a38a987a4f481e77e997812d0b7591523e59bb71c2fbeab9d631a1cfa9d9b6289c3bc8578cdcffa1
7
- data.tar.gz: 52bb33a48508e0fbcfdb7054f5aea28bd7c28dbff5e1aa9ead156772fa6095e60d063d0220451752754c63e4765170068d7cb960199f799be4886f779d2090f3
6
+ metadata.gz: bffa799520a877ab2924f84af76a1ae91acb5c860c1881c383ba7c8755c359d448ec0fdd7b02843dc44616e3cbc73778244da06253859ff8b32cdc7e8134ee2d
7
+ data.tar.gz: 24481590f130a83cca420f172a0cf43d6832e45f2aa81bee9bea94d1b7d5dcc084915f4e072f991a05a37bd5ed43da2cc9cf5aaa77b7b554e73ff80bca849f1b
data/README.md CHANGED
@@ -1,9 +1,7 @@
1
1
  # Flic
2
-
3
- Flic is a Ruby implementation of the [Fliclib](https://github.com/50ButtonsEach/fliclib-linux-hci/blob/master/ProtocolDocumentation.md).
2
+ Flic is a lightweight but thread-safe Ruby implementation of the [Fliclib](https://github.com/50ButtonsEach/fliclib-linux-hci/blob/master/ProtocolDocumentation.md). It interfaces with [flicd](https://github.com/50ButtonsEach/fliclib-linux-hci), and allows [Flic buttons](https://flic.io/) to be used as an input for Ruby applications.
4
3
 
5
4
  ## Installation
6
-
7
5
  Add this line to your application's Gemfile:
8
6
 
9
7
  ```ruby
@@ -18,13 +16,108 @@ Or install it yourself as:
18
16
 
19
17
  $ gem install flic
20
18
 
19
+ ## Simple Usage
20
+ The easiest way to get up an running is to use `Flic::SimpleClient`. It is a thin wrapper around `Flic::Client` that provides a simplified, synchronous API for the most intuative uses of a Flic button.
21
21
 
22
- ## Contributing
22
+ ### Establishing a connection to `flicd`
23
+ By default, `flicd` binds to `localhost` on port `5551`. These are also the defaults for `Filc::SimpleClient`. This means that if you are running `flicd` on your local machine, establishing a connection is as easy as creating a new instance of `Flic::SimpleClient`. For other configurations, you can initialize `Flic::SimpleCient` with host and port. For example, `Flic::SimpleClient.new('192.168.1.200', 5553)` will open a connection to `flicd` on `192.168.1.200` listening on port `5553`.
23
24
 
24
- Bug reports and pull requests are welcome on GitHub at https://github.com/anarchocurious/flic.
25
+ ### Getting a list of buttons
26
+ To get a list of verified buttons (buttons associated and ready to be used with a given `flicd`), call `Flic::SimpleClient#buttons`. This will return a list of button's bluetooth addresses.
25
27
 
28
+ ### Connecting a new button
29
+ A button must be in public mode before it can be added. To put a button in public mode, press and hold it for at least 7 seconds. `Flic::SimpleClient#connect_button` will return the bluetooh address of the button that was added or `nil` if no button was added.
26
30
 
27
- ## License
31
+ ### Disconnecting a button
32
+ Similarly, a button may be disconnected by passing `Flic::SimpleClient#disconnect_button` a button's bluetooth address.
28
33
 
29
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
34
+ ### Listening for button events
35
+ `Flic::SimpleClient#listen` accepts a latency mode (`:low`, `:normal`, or `:high`) as it's first argument and button bluetooth addresses as its other arguments. For each event that occurs to those buttons, it yields the bluetooth address of the button responsible for a given event, the type of click involved in the event (`:button_down`, `:button_up`, `:button_single_click`, `:button_double_click`, or `:button_hold`), the time in milliseconds since the event occured, and whether the event was queued. **It will block until the connection is closed or the block raises some other exception.**
36
+
37
+ ### Closing the connection to `flicd`
38
+ To gracefully cleanup all connection channels and close the socket connection, call `Flic::SimpleClient#shutdown`. Once a `Flic::SimpleClient` has been shutdown it will close the underlying socket and cannot be used anymore.
39
+
40
+ ### Example
41
+ This is the script that I wrote to allow some of my Flic buttons to control Wink-enabled smart devices in home.
42
+ ```ruby
43
+ #!/usr/bin/env ruby
44
+
45
+ require 'bundler/setup'
46
+ require 'flic'
47
+ require 'httparty'
48
+
49
+ # Obtained from https://winkbearertoken.appspot.com/
50
+ WINK_ACCESS_TOKEN = 'YOUR ACCESS TOKEN HERE'
51
+
52
+ # These bluetooth addresses were obtained from SimpleClient#connect_button
53
+ LIVING_ROOM_BUTTON = 'XX:XX:XX:XX:XX:XX'
54
+ BEDROOM_BUTTON = 'XX:XX:XX:XX:XX:XX'
55
+ NIGHTSTAND_BUTTON = 'XX:XX:XX:XX:XX:XX'
56
+
57
+ begin
58
+ puts "[*] Opening a connection to flicd..."
59
+ client = Flic::SimpleClient.new
60
+
61
+ puts "[*] Entering main loop"
62
+ client.listen(:low, LIVING_ROOM_BUTTON, BEDROOM_BUTTON, NIGHTSTAND_BUTTON) do |button, event, latency_ms|
63
+ if latency_ms > 2000
64
+ puts "[*] [#{button}] Ignoring #{event} because the latency is #{latency_ms} ms"
65
+ else
66
+ puts "[*] [#{button}] Handling #{event}"
30
67
 
68
+ case button
69
+ when LIVING_ROOM_BUTTON
70
+ case event
71
+ when :button_single_click
72
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
73
+ when :button_double_click
74
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
75
+ when :button_hold
76
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
77
+ end
78
+
79
+ when BEDROOM_BUTTON
80
+ case event
81
+ when :button_single_click
82
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
83
+ when :button_double_click
84
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
85
+ when :button_hold
86
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
87
+ end
88
+
89
+ when NIGHTSTAND_BUTTON
90
+ case event
91
+ when :button_single_click
92
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
93
+ when :button_double_click
94
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
95
+ when :button_hold
96
+ puts HTTParty.post('https://api.wink.com/scenes/SCENE_ID/activate', headers: { Authorization: "Bearer #{WINK_ACCESS_TOKEN}" }).inspect
97
+ end
98
+ end
99
+ end
100
+ end
101
+ rescue StandardError => error
102
+ puts "[!] Whoops! #{error.inspect} occured. Wait for a second and restart everything."
103
+ sleep 1
104
+
105
+ retry
106
+ rescue Interrupt
107
+ puts "[*] Shutting down gracefully because of an interrupt"
108
+
109
+ client.shutdown
110
+ end
111
+
112
+ puts "[*] Goodbye cruel world!"
113
+ ```
114
+
115
+ ## Advanced Usage
116
+ The interface of `Flic::Client` closely mirrors [the official Java implimentation](https://github.com/50ButtonsEach/fliclib-linux-hci/tree/master/clientlib/java). Unlike `Flic::SimpleClient`, none of it's methods are blocking except `Flic::Client#handle_next_event` which blocks until the next event is recevied and dispatches it to the approprate handlers and `Flic::Client#enter_main_loop` which blocks until the client is shutdown and dispatches events as they are received. Ideally, `Flic::Client#enter_main_loop` should be run in a dedicated networking / event dispatch thread.
117
+
118
+
119
+ ## Contributing
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/anarchocurious/flic.
121
+
122
+ ## License
123
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -19,7 +19,7 @@ module Flic
19
19
  end
20
20
 
21
21
  def recv_command
22
- Protocol.parse_event(recv_command)
22
+ Protocol.parse_command(recv_packet)
23
23
  end
24
24
 
25
25
  def send_event(event)
@@ -1,3 +1,3 @@
1
1
  module Flic
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alec Larsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-14 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata