-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch over the autopark code to async.
- Loading branch information
Showing
3 changed files
with
23 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,42 @@ | ||
module TeslaApi | ||
module Autopark | ||
def start_autopark(&handler) | ||
EventMachine.run do | ||
autopark_socket.on(:message) do |event| | ||
message = if event.data.is_a?(Array) | ||
JSON.parse(event.data.map(&:chr).join) | ||
else | ||
JSON.parse(event.data) | ||
end | ||
|
||
default_handler(message) | ||
handler.call(message.delete('msg_type'), message) | ||
end | ||
Async do |task| | ||
Async::WebSocket::Client.connect(endpoint, headers: headers) do |connection| | ||
while message = connection.read | ||
case message[:msg_type] | ||
when 'control:hello' | ||
interval = message[:autopark][:heartbeat_frequency] / 1000.0 | ||
task.async do |subtask| | ||
subtask.sleep interval | ||
connection.write({ msg_type: 'autopark:heartbeat_app', timestamp: Time.now.to_i }.to_json) | ||
end | ||
end | ||
|
||
autopark_socket.on(:close) do |_| | ||
@autopark_socket = nil | ||
@heartbeat && @heartbeat.cancel | ||
EventMachine.stop | ||
handler.call(message) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def default_handler(message) | ||
case message['msg_type'] | ||
when 'control:hello' | ||
interval = message['autopark']['heartbeat_frequency'] / 1000.0 | ||
@heartbeat = EventMachine::Timer.new(interval) do | ||
beat = { | ||
msg_type: 'autopark:heartbeat_app', | ||
timestamp: Time.now.to_i | ||
} | ||
autopark_socket.send(beat.to_json) | ||
end | ||
end | ||
def endpoint | ||
Async::HTTP::Endpoint.parse(autopark_socket_endpoint) | ||
end | ||
|
||
def autopark_socket_endpoint | ||
"wss://streaming.vn.teslamotors.com/connect/#{self['vehicle_id']}" | ||
end | ||
|
||
def autopark_socket | ||
@autopark_socket ||= Faye::WebSocket::Client.new( | ||
autopark_socket_endpoint, | ||
nil, | ||
{ | ||
headers: { | ||
'Authorization' => "Basic #{socket_auth}" | ||
} | ||
} | ||
) | ||
def headers | ||
{ | ||
'Authorization' => "Basic #{socket_auth}" | ||
} | ||
end | ||
|
||
def socket_auth | ||
Base64.strict_encode64("#{email}:#{self['tokens'].first}") | ||
end | ||
|
||
def autopark_socket_endpoint | ||
"wss://streaming.vn.teslamotors.com/connect/#{self['vehicle_id']}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters