Skip to content

Commit

Permalink
Add Pebble.logger with .level set to Logger::INFO by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
DouweM committed Apr 1, 2013
1 parent c06fe21 commit f0f03e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gem install pebblewatch

## Usage

Make sure your Pebble is paired to your computer and set up as a serial port. We're going to need the path or index of the port, which in the case of OS X looks like `/dev/tty.Pebble7F30-SerialPortSe` for Pebble ID `7F30`.
Make sure your Pebble is paired with your computer and set up as a serial port. We're going to need the path or index of the port, which in the case of OS X looks like `/dev/tty.Pebble7F30-SerialPortSe` for Pebble ID `7F30`.

```ruby
require "pebble"
Expand Down
19 changes: 19 additions & 0 deletions lib/pebble.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
require "logger"

module Pebble
def self.logger=(new_logger)
@@logger = new_logger
end

def self.logger
return @@logger if defined?(@@logger)
@@logger = default_logger
end

def self.default_logger
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
logger
end
end

require "pebble/version"
require "pebble/endpoints"
require "pebble/capabilities"
Expand Down
14 changes: 6 additions & 8 deletions lib/pebble/protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ def initialize(port)
end

def connect
puts "Connecting with port #{@port}"

@serial_port = SerialPort.new(@port, baudrate: 115200)
@serial_port.read_timeout = 500

@connected = true
puts "Connected"
Pebble.logger.debug "Connected to port #{@port}"

@receive_messages_thread = Thread.new(&method(:receive_messages))

Expand Down Expand Up @@ -79,7 +77,7 @@ def send_message(endpoint, message, async_response_handler = nil, &response_pars

message ||= ""

puts "Sending #{Endpoints.for_code(endpoint)}: #{message.inspect}"
Pebble.logger.debug "Sending #{Endpoints.for_code(endpoint)}: #{message.inspect}"

data = [message.size, endpoint].pack("S>S>") + message

Expand Down Expand Up @@ -119,7 +117,7 @@ def send_message(endpoint, message, async_response_handler = nil, &response_pars

private
def receive_messages
puts "Waiting for messages"
Pebble.logger.debug "Waiting for messages"
while @connected
header = @serial_port.read(4)
next unless header
Expand All @@ -129,18 +127,18 @@ def receive_messages
size, endpoint = header.unpack("S>S>")
message = @serial_port.read(size)

puts "Received #{Endpoints.for_code(endpoint)}: #{message.inspect}"
Pebble.logger.debug "Received #{Endpoints.for_code(endpoint)}: #{message.inspect}"

trigger_received(endpoint, message)
end
rescue IOError => e
if @connected
puts "Lost connection"
Pebble.logger.debug "Lost connection"
@connected = false
raise Errors::LostConnection
end
ensure
puts "Finished waiting for messages"
Pebble.logger.debug "Finished waiting for messages"
end

def trigger_received(endpoint, message)
Expand Down
33 changes: 25 additions & 8 deletions lib/pebble/watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class NoWatchesFound < StandardError; end
def self.autodetect
return nil unless RUBY_PLATFORM =~ /darwin/

devices = Dir.glob("/dev/tty.Pebble????-SerialPortSe")
watches = Dir.glob("/dev/tty.Pebble????-SerialPortSe")

raise Errors::NoWatchesFound if devices.length == 0
puts "Found multiple watches" if devices.length > 1
raise Errors::NoWatchesFound if watches.length == 0
Pebble.logger.debug "Found multiple watches: #{watches}" if watches.length > 1

port = devices.first
port = watches.first
id = port[15, 4]
puts "Found watch with ID #{id}"
Pebble.logger.debug "Detected watch with ID #{id}"

return new(id, port)
end
Expand Down Expand Up @@ -50,8 +50,8 @@ def initialize(id, port)
@client_capabilities = Capabilities::Client::TELEPHONY | Capabilities::Client::SMS | Capabilities::Client::ANDROID

answer_phone_version_message

receive_event_messages
log_log_events
end

def connect
Expand Down Expand Up @@ -177,7 +177,7 @@ def set_time(time)
end

def system_message(code)
puts "Sending system message #{SystemMessages.for_code(code)}"
Pebble.logger.debug "Sending system message #{SystemMessages.for_code(code)}"

message = [code].pack("S>")

Expand Down Expand Up @@ -213,8 +213,25 @@ def receive_event_messages
end
end

def log_log_events
on_event(:log) do |event|
case event.level
when :error
Pebble.logger.error event.message
when :warning
Pebble.logger.warn event.message
when :info
Pebble.logger.info event.message
when :debug, :verbose
Pebble.logger.debug event.message
else
Pebble.logger.info event.message
end
end
end

def trigger_event(name, event)
puts "Event '#{name}': #{event.inspect}"
Pebble.logger.debug "Triggering event '#{name}': #{event.inspect}"

@event_handlers[:any].each do |handler|
Thread.new(handler) do |handler|
Expand Down

0 comments on commit f0f03e1

Please sign in to comment.