hybridgroup-pebblewatch 0.0.4 → 0.1.0

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: 6375eee495aa881fd8f68ee492eb66b58d858882
4
- data.tar.gz: 4d82b1ce5d795c947f8689a53a588acf30488201
3
+ metadata.gz: 54c276fa2e663e29012d6e027950abcfeef02896
4
+ data.tar.gz: ce72a9e9663757673ce68525a9021c469dee3f46
5
5
  SHA512:
6
- metadata.gz: 861a4f51687d2e181db15de7ff446788d4404b4d0312ce21bbdc3642c5c68b734bf095db59c676c8cb998ce3f931fa45f4833b795ea9c4a83dab2f797f3d5976
7
- data.tar.gz: 02db86e888204b22994965b279878f8b362a37a4d49b54962b4d0597fa48ffc288de4de0911af0456c38c057c184b97df1c8c1cd4846b940b03980ddaaf2b418
6
+ metadata.gz: 643c2c113aa4b3d3760b7cf8511394e34932e2e14507e506d5e907f40b382d9232674957c51538d747026d74c5121a8cd2c323fe9f5af52c78e48d49632609f8
7
+ data.tar.gz: cb1f125ad54d773c16d683257bd38c32dc1a6dcfff17ed798f78b0cff363e6749a52c5197b5cfd0c751ae80c263c3ae0ac4e70b3f1b75c9ee8947723395b87cf
@@ -1,5 +1,3 @@
1
- require "serialport"
2
-
3
1
  module Pebble
4
2
  class Protocol
5
3
  module Errors
@@ -10,6 +8,7 @@ module Pebble
10
8
 
11
9
  attr_reader :connected
12
10
  attr_reader :message_handlers
11
+ attr_reader :messages
13
12
 
14
13
  def self.open(port)
15
14
  protocol = new(port)
@@ -29,12 +28,14 @@ module Pebble
29
28
  @connected = false
30
29
  @send_message_mutex = Mutex.new
31
30
  @message_handlers = Hash.new { |hash, key| hash[key] = [] }
31
+ @messages = Queue.new
32
+ @use_message_queue = false
32
33
  end
33
34
 
34
35
  def connect
35
36
  if @port.is_a?(String)
37
+ require 'serialport'
36
38
  @serial_port = SerialPort.new(@port, baudrate: 115200)
37
- @serial_port.read_timeout = 500
38
39
  else
39
40
  @serial_port = @port
40
41
  end
@@ -45,6 +46,8 @@ module Pebble
45
46
  @receive_messages_thread = Thread.new(&method(:receive_messages))
46
47
 
47
48
  true
49
+ rescue LoadError
50
+ puts "Please 'gem install hybridgroup-serialport' for serial port support."
48
51
  end
49
52
 
50
53
  def disconnect
@@ -58,10 +61,15 @@ module Pebble
58
61
  true
59
62
  end
60
63
 
61
- def listen_for_messages
64
+ def listen_for_messages(sync=true)
62
65
  raise Errors::NotConnected unless @connected
63
66
 
64
- @receive_messages_thread.join
67
+ if sync
68
+ @receive_messages_thread.join
69
+ else
70
+ @use_message_queue = true
71
+ @receive_messages_thread.run
72
+ end
65
73
  end
66
74
 
67
75
  def on_receive(endpoint = :any, &handler)
@@ -129,6 +137,7 @@ module Pebble
129
137
  raise Errors::MalformedResponse if header.length < 4
130
138
 
131
139
  size, endpoint = header.unpack("S>S>")
140
+ next if endpoint == 0
132
141
  message = @serial_port.read(size)
133
142
 
134
143
  Pebble.logger.debug "Received #{Endpoints.for_code(endpoint) || endpoint}: #{message.inspect}"
@@ -146,6 +155,8 @@ module Pebble
146
155
  end
147
156
 
148
157
  def trigger_received(endpoint, message)
158
+ @messages.push(event_for(endpoint, message)) if @use_message_queue
159
+
149
160
  @message_handlers[:any].each do |handler|
150
161
  Thread.new(handler) do |handler|
151
162
  handler.call(endpoint, message)
@@ -158,5 +169,16 @@ module Pebble
158
169
  end
159
170
  end
160
171
  end
172
+
173
+ def event_for(endpoint, message)
174
+ events = {
175
+ Pebble::Endpoints::LOGS => [:log, Pebble::Watch::LogEvent],
176
+ Pebble::Endpoints::SYSTEM_MESSAGE => [:system_message, Pebble::Watch::SystemMessageEvent],
177
+ Pebble::Endpoints::MUSIC_CONTROL => [:media_control, Pebble::Watch::MediaControlEvent],
178
+ Pebble::Endpoints::APPLICATION_MESSAGE => [:app_message, Pebble::Watch::AppMessageEvent]
179
+ }
180
+
181
+ [events[endpoint][0], events[endpoint][1].parse(message)]
182
+ end
161
183
  end
162
184
  end
@@ -1,3 +1,3 @@
1
1
  module Pebble
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/pebble/watch.rb CHANGED
@@ -62,11 +62,12 @@ module Pebble
62
62
  @protocol.disconnect
63
63
  end
64
64
 
65
- def listen_for_events
66
- @protocol.listen_for_messages
65
+ def listen_for_events(sync=true)
66
+ @protocol.listen_for_messages(sync)
67
67
  end
68
68
 
69
69
  def on_event(event = :any, &handler)
70
+ Pebble.logger.info "adding handler for #{event}"
70
71
  @event_handlers[event] << handler
71
72
  handler
72
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hybridgroup-pebblewatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douwe Maan
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-26 00:00:00.000000000 Z
12
+ date: 2013-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: serialport
15
+ name: hybridgroup-serialport
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - ~>
19
19
  - !ruby/object:Gem::Version
20
- version: '1.1'
20
+ version: '1.2'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
27
- version: '1.1'
27
+ version: '1.2'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement