Class: Pebble::Watch
- Inherits:
-
Object
- Object
- Pebble::Watch
- Defined in:
- lib/pebble/watch.rb,
lib/pebble/watch/event.rb,
lib/pebble/watch/log_event.rb,
lib/pebble/watch/media_control_event.rb,
lib/pebble/watch/system_message_event.rb
Defined Under Namespace
Modules: Errors Classes: Event, LogEvent, MediaControlEvent, SystemMessageEvent
Instance Attribute Summary collapse
-
#client_capabilities ⇒ Object
Returns the value of attribute client_capabilities.
-
#event_handlers ⇒ Object
readonly
Returns the value of attribute event_handlers.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#session_capabilities ⇒ Object
Returns the value of attribute session_capabilities.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
- #get_installed_apps(&async_response_handler) ⇒ Object
- #get_time(&async_response_handler) ⇒ Object
- #get_versions(&async_response_handler) ⇒ Object
-
#initialize(id, port) ⇒ Watch
constructor
A new instance of Watch.
- #listen_for_events ⇒ Object
- #notification_email(sender, subject, body) ⇒ Object
- #notification_sms(sender, body) ⇒ Object
- #on_event(event = :any, &handler) ⇒ Object
- #ping(cookie = 0xDEADBEEF, &async_response_handler) ⇒ Object
- #remove_app(app_id, app_index) ⇒ Object
- #reset ⇒ Object
- #set_nowplaying_metadata(artist, album, track) ⇒ Object
- #set_time(time) ⇒ Object
- #stop_listening(*params) ⇒ Object
- #system_message(code) ⇒ Object
Constructor Details
#initialize(id, port) ⇒ Watch
Returns a new instance of Watch.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pebble/watch.rb', line 42 def initialize(id, port) @id = id @protocol = Protocol.new(port) @event_handlers = Hash.new { |hash, key| hash[key] = [] } # We're mirroring Android here. @session_capabilities = Capabilities::Session::GAMMA_RAY @client_capabilities = Capabilities::Client::TELEPHONY | Capabilities::Client::SMS | Capabilities::Client::ANDROID log_log_events end |
Instance Attribute Details
#client_capabilities ⇒ Object
Returns the value of attribute client_capabilities.
40 41 42 |
# File 'lib/pebble/watch.rb', line 40 def client_capabilities @client_capabilities end |
#event_handlers ⇒ Object (readonly)
Returns the value of attribute event_handlers.
38 39 40 |
# File 'lib/pebble/watch.rb', line 38 def event_handlers @event_handlers end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
36 37 38 |
# File 'lib/pebble/watch.rb', line 36 def id @id end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
37 38 39 |
# File 'lib/pebble/watch.rb', line 37 def protocol @protocol end |
#session_capabilities ⇒ Object
Returns the value of attribute session_capabilities.
39 40 41 |
# File 'lib/pebble/watch.rb', line 39 def session_capabilities @session_capabilities end |
Class Method Details
.autodetect ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pebble/watch.rb', line 9 def self.autodetect return nil unless RUBY_PLATFORM =~ /darwin/ watches = Dir.glob("/dev/tty.Pebble????-SerialPortSe") raise Errors::NoWatchesFound if watches.length == 0 Pebble.logger.debug "Found multiple watches: #{watches}" if watches.length > 1 port = watches.first id = port[15, 4] Pebble.logger.debug "Detected watch with ID #{id}" return new(id, port) end |
.open(id, port) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pebble/watch.rb', line 24 def self.open(id, port) watch = new(id, port) begin watch.connect yield watch ensure watch.disconnect end nil end |
Instance Method Details
#connect ⇒ Object
57 58 59 |
# File 'lib/pebble/watch.rb', line 57 def connect @protocol.connect end |
#disconnect ⇒ Object
61 62 63 |
# File 'lib/pebble/watch.rb', line 61 def disconnect @protocol.disconnect end |
#get_installed_apps(&async_response_handler) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/pebble/watch.rb', line 136 def get_installed_apps(&async_response_handler) @protocol.(Endpoints::APP_INSTALL_MANAGER, "\x01", async_response_handler) do || response = {} response[:banks_count], apps_count = [1, 8].unpack("L>L>") response[:apps] = [] size = 78 apps_count.times do |index| offset = index * size + 9 app = {} app[:id], app[:index], app[:name], app[:author], app[:flags], app[:version] = [offset, size].unpack("L>L>A32A32L>S>") response[:apps] << app end response end end |
#get_time(&async_response_handler) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/pebble/watch.rb', line 165 def get_time(&async_response_handler) @protocol.(Endpoints::TIME, "\x00", async_response_handler) do || restype, = .unpack("CL>") Time.at() end end |
#get_versions(&async_response_handler) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pebble/watch.rb', line 106 def get_versions(&async_response_handler) @protocol.(Endpoints::VERSION, "\x00", async_response_handler) do || response = {} response[:firmwares] = {} size = 47 [:normal, :recovery].each_with_index do |type, index| offset = index * size + 1 fw = {} fw[:timestamp], fw[:version], fw[:commit], fw[:is_recovery], fw[:hardware_platform], fw[:metadata_version] = [offset, size].unpack("L>A32A8ccc") fw[:is_recovery] = (fw[:is_recovery] == 1) response[:firmwares][type] = fw end response[:bootloader_timestamp], response[:hardware_version], response[:serial] = [95, 25].unpack("L>A9A12") response[:btmac] = [120, 6].unpack("H*").first.scan(/../).reverse.map { |c| c.upcase }.join(":") response end end |
#listen_for_events ⇒ Object
65 66 67 |
# File 'lib/pebble/watch.rb', line 65 def listen_for_events @protocol. end |
#notification_email(sender, subject, body) ⇒ Object
95 96 97 |
# File 'lib/pebble/watch.rb', line 95 def notification_email(sender, subject, body) notification(:email, sender, body, subject) end |
#notification_sms(sender, body) ⇒ Object
91 92 93 |
# File 'lib/pebble/watch.rb', line 91 def notification_sms(sender, body) notification(:sms, sender, body) end |
#on_event(event = :any, &handler) ⇒ Object
69 70 71 72 |
# File 'lib/pebble/watch.rb', line 69 def on_event(event = :any, &handler) @event_handlers[event] << handler handler end |
#ping(cookie = 0xDEADBEEF, &async_response_handler) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/pebble/watch.rb', line 82 def ping( = 0xDEADBEEF, &async_response_handler) = [0, ].pack("CL>") @protocol.(Endpoints::PING, , async_response_handler) do || restype, = .unpack("CL>") end end |
#remove_app(app_id, app_index) ⇒ Object
159 160 161 162 163 |
# File 'lib/pebble/watch.rb', line 159 def remove_app(app_id, app_index) = [2, app_id, app_index].pack("cL>L>") @protocol.(Endpoints::APP_INSTALL_MANAGER, ) end |
#reset ⇒ Object
187 188 189 |
# File 'lib/pebble/watch.rb', line 187 def reset @protocol.(Endpoints::RESET, "\x00") end |
#set_nowplaying_metadata(artist, album, track) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/pebble/watch.rb', line 99 def (artist, album, track) = [16].pack("C") << package_strings(artist, album, track, 30) @protocol.(Endpoints::MUSIC_CONTROL, ) end |
#set_time(time) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/pebble/watch.rb', line 172 def set_time(time) = time.to_i = [2, ].pack("CL>") @protocol.(Endpoints::TIME, ) end |
#stop_listening(*params) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/pebble/watch.rb', line 74 def stop_listening(*params) handler = params.pop event = params.pop || :any @event_handlers[event].delete(handler) end |
#system_message(code) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/pebble/watch.rb', line 179 def (code) Pebble.logger.debug "Sending system message #{SystemMessages.for_code(code)}" = [code].pack("S>") @protocol.(Endpoints::SYSTEM_MESSAGE, ) end |