From b51116d2036bd402f6318824b61119abdb3392a1 Mon Sep 17 00:00:00 2001 From: Calvin Yu Date: Sun, 13 Sep 2020 07:46:29 -0400 Subject: [PATCH 1/3] Set stream protocol to HTTP 1.1 --- lib/tesla_api/stream.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tesla_api/stream.rb b/lib/tesla_api/stream.rb index b91c3fac..317917bc 100644 --- a/lib/tesla_api/stream.rb +++ b/lib/tesla_api/stream.rb @@ -47,7 +47,7 @@ def stream(&receiver) TIMEOUT = 30 def streaming_endpoint - Async::HTTP::Endpoint.parse(streaming_endpoint_url) + Async::HTTP::Endpoint.parse(streaming_endpoint_url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names) end def streaming_endpoint_url From 63143945d02d5a64f92cf6496f46708fbfe87443 Mon Sep 17 00:00:00 2001 From: Calvin Yu Date: Sun, 13 Sep 2020 20:11:05 -0400 Subject: [PATCH 2/3] Allow async endpoint to be configured for streams This allows streams to run through a proxy server: proxy_client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse("http://localhost:8888")) original_endpoint = vehicle.streaming_endpoint headers = { "proxy-authorization" => Protocol::HTTP::Header::Authorization.basic('username','password') } proxy_client.proxied_endpoint(endpoint, headers) --- lib/tesla_api/stream.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/tesla_api/stream.rb b/lib/tesla_api/stream.rb index 317917bc..d2b8bdcb 100644 --- a/lib/tesla_api/stream.rb +++ b/lib/tesla_api/stream.rb @@ -1,8 +1,8 @@ module TeslaApi module Stream - def stream(&receiver) + def stream(endpoint: streaming_endpoint, &receiver) Async do |task| - Async::WebSocket::Client.connect(streaming_endpoint) do |connection| + Async::WebSocket::Client.connect(endpoint) do |connection| on_timeout = ->(subtask) do subtask.sleep TIMEOUT task.stop @@ -42,14 +42,14 @@ def stream(&receiver) end end - private - - TIMEOUT = 30 - def streaming_endpoint Async::HTTP::Endpoint.parse(streaming_endpoint_url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names) end + private + + TIMEOUT = 30 + def streaming_endpoint_url 'wss://streaming.vn.teslamotors.com/streaming/' end From 0ad3e6047f3fbede7764277227f23684f77d7190 Mon Sep 17 00:00:00 2001 From: Calvin Yu Date: Wed, 16 Sep 2020 10:49:12 -0400 Subject: [PATCH 3/3] Make endpoints static members on Stream --- lib/tesla_api/stream.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/tesla_api/stream.rb b/lib/tesla_api/stream.rb index d2b8bdcb..c36363cc 100644 --- a/lib/tesla_api/stream.rb +++ b/lib/tesla_api/stream.rb @@ -1,6 +1,14 @@ module TeslaApi module Stream - def stream(endpoint: streaming_endpoint, &receiver) + def self.streaming_endpoint_url + 'wss://streaming.vn.teslamotors.com/streaming/' + end + + def self.streaming_endpoint + Async::HTTP::Endpoint.parse(streaming_endpoint_url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names) + end + + def stream(endpoint: Stream.streaming_endpoint, &receiver) Async do |task| Async::WebSocket::Client.connect(endpoint) do |connection| on_timeout = ->(subtask) do @@ -42,18 +50,10 @@ def stream(endpoint: streaming_endpoint, &receiver) end end - def streaming_endpoint - Async::HTTP::Endpoint.parse(streaming_endpoint_url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names) - end - private TIMEOUT = 30 - def streaming_endpoint_url - 'wss://streaming.vn.teslamotors.com/streaming/' - end - def streaming_connect_message { msg_type: 'data:subscribe',