From 2494bc9c82ce33ec5c8ef746ddf8393cd5aef928 Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Fri, 11 Oct 2019 12:04:24 -0400 Subject: [PATCH] Switch over the autopark code to async. --- lib/tesla_api.rb | 2 -- lib/tesla_api/autopark.rb | 64 ++++++++++++++------------------------- tesla_api.gemspec | 1 - 3 files changed, 23 insertions(+), 44 deletions(-) diff --git a/lib/tesla_api.rb b/lib/tesla_api.rb index bc0f6b69..882d7735 100644 --- a/lib/tesla_api.rb +++ b/lib/tesla_api.rb @@ -3,8 +3,6 @@ require 'faraday' require 'faraday_middleware' -require 'eventmachine' -require 'faye/websocket' require 'async' require 'async/http/endpoint' diff --git a/lib/tesla_api/autopark.rb b/lib/tesla_api/autopark.rb index 2e59c783..8f1c074d 100644 --- a/lib/tesla_api/autopark.rb +++ b/lib/tesla_api/autopark.rb @@ -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 diff --git a/tesla_api.gemspec b/tesla_api.gemspec index 6b828511..8369d66b 100644 --- a/tesla_api.gemspec +++ b/tesla_api.gemspec @@ -17,7 +17,6 @@ Gem::Specification.new do |spec| spec.add_dependency 'async-websocket' spec.add_dependency 'faraday' spec.add_dependency 'faraday_middleware' - spec.add_dependency 'faye-websocket' spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'rake', '~> 12.0'