diff --git a/README.md b/README.md index d25f1f3..6c032e8 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/lib/pebble.rb b/lib/pebble.rb index 1982364..75dde3d 100644 --- a/lib/pebble.rb +++ b/lib/pebble.rb @@ -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" diff --git a/lib/pebble/protocol.rb b/lib/pebble/protocol.rb index 75da743..8003889 100644 --- a/lib/pebble/protocol.rb +++ b/lib/pebble/protocol.rb @@ -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)) @@ -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 @@ -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 @@ -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) diff --git a/lib/pebble/watch.rb b/lib/pebble/watch.rb index c420b5d..cd455ca 100644 --- a/lib/pebble/watch.rb +++ b/lib/pebble/watch.rb @@ -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 @@ -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 @@ -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>") @@ -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|