tesla_api 3.0.2 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4379e187bc321aa06bf2675be264b8f7dba76d33d08ae2088b5491e718aeb387
4
- data.tar.gz: d0efa66690fa5ed2edb6b624c77ea1fef140e88c7a4aa177cb6f68c82fe4e88a
3
+ metadata.gz: 36800b87fde594b317de4423717ddddde20b02a89a29dcc3030da421c857961d
4
+ data.tar.gz: a7578ddecc3e79faaf4b91eec431196fdd42cc6b2697d8a47c115a9839beb13d
5
5
  SHA512:
6
- metadata.gz: d705d155f086cbd9e8476575973d45b346f29dd3b77f7df29295e88bf1dc0563841e745e6f3e79fb33db59fa2d03bec4ce8f3cd6b9e06d849309a6c9709b0575
7
- data.tar.gz: ea8910775ee4536f95249ffe0fedb1a8468d9996ebaaab01fab5569955053e515a937c920cff817d76c508d4b3c32b682b5a24b4863a1487f46f01c0cba15339
6
+ metadata.gz: 4cefac51f88134ddb5cf93567046c63249c48aac76c19ea95006f7a8f98e97ad2258c1d4969be3cedc3dd8e3ec8ad21513cc1b29913982f11b2263ca44e2bc2e
7
+ data.tar.gz: 3724b8215a83bd514ab27ddc02165793052f54dfb1bfc45cfe3f90c18fd50395dcedadd4e7070c3469d2f5557fb3354c02e97a3850e33c2d1ed8c397b4e8cef9
@@ -3,8 +3,10 @@ require 'base64'
3
3
 
4
4
  require 'faraday'
5
5
  require 'faraday_middleware'
6
- require 'eventmachine'
7
- require 'faye/websocket'
6
+
7
+ require 'async'
8
+ require 'async/http/endpoint'
9
+ require 'async/websocket/client'
8
10
 
9
11
  require 'tesla_api/version'
10
12
  require 'tesla_api/client'
@@ -1,60 +1,42 @@
1
1
  module TeslaApi
2
2
  module Autopark
3
3
  def start_autopark(&handler)
4
- EventMachine.run do
5
- autopark_socket.on(:message) do |event|
6
- message = if event.data.is_a?(Array)
7
- JSON.parse(event.data.map(&:chr).join)
8
- else
9
- JSON.parse(event.data)
10
- end
11
-
12
- default_handler(message)
13
- handler.call(message.delete('msg_type'), message)
14
- end
4
+ Async do |task|
5
+ Async::WebSocket::Client.connect(endpoint, headers: headers) do |connection|
6
+ while message = connection.read
7
+ case message[:msg_type]
8
+ when 'control:hello'
9
+ interval = message[:autopark][:heartbeat_frequency] / 1000.0
10
+ task.async do |subtask|
11
+ subtask.sleep interval
12
+ connection.write({ msg_type: 'autopark:heartbeat_app', timestamp: Time.now.to_i }.to_json)
13
+ end
14
+ end
15
15
 
16
- autopark_socket.on(:close) do |_|
17
- @autopark_socket = nil
18
- @heartbeat && @heartbeat.cancel
19
- EventMachine.stop
16
+ handler.call(message)
17
+ end
20
18
  end
21
19
  end
22
20
  end
23
21
 
24
22
  private
25
23
 
26
- def default_handler(message)
27
- case message['msg_type']
28
- when 'control:hello'
29
- interval = message['autopark']['heartbeat_frequency'] / 1000.0
30
- @heartbeat = EventMachine::Timer.new(interval) do
31
- beat = {
32
- msg_type: 'autopark:heartbeat_app',
33
- timestamp: Time.now.to_i
34
- }
35
- autopark_socket.send(beat.to_json)
36
- end
37
- end
24
+ def endpoint
25
+ Async::HTTP::Endpoint.parse(autopark_socket_endpoint)
26
+ end
27
+
28
+ def autopark_socket_endpoint
29
+ "wss://streaming.vn.teslamotors.com/connect/#{self['vehicle_id']}"
38
30
  end
39
31
 
40
- def autopark_socket
41
- @autopark_socket ||= Faye::WebSocket::Client.new(
42
- autopark_socket_endpoint,
43
- nil,
44
- {
45
- headers: {
46
- 'Authorization' => "Basic #{socket_auth}"
47
- }
48
- }
49
- )
32
+ def headers
33
+ {
34
+ 'Authorization' => "Basic #{socket_auth}"
35
+ }
50
36
  end
51
37
 
52
38
  def socket_auth
53
39
  Base64.strict_encode64("#{email}:#{self['tokens'].first}")
54
40
  end
55
-
56
- def autopark_socket_endpoint
57
- "wss://streaming.vn.teslamotors.com/connect/#{self['vehicle_id']}"
58
- end
59
41
  end
60
42
  end
@@ -1,47 +1,53 @@
1
1
  module TeslaApi
2
2
  module Stream
3
3
  def stream(&receiver)
4
- EventMachine.run do
5
- socket = create_streaming_socket
4
+ Async do |task|
5
+ Async::WebSocket::Client.connect(endpoint) do |connection|
6
+ on_timeout = ->(subtask) do
7
+ subtask.sleep TIMEOUT
8
+ task.stop
9
+ end
6
10
 
7
- socket.on(:open) do |event|
8
- socket.send(JSON.generate(stream_connect_message))
9
- end
11
+ connection.write(stream_connect_message)
12
+ timeout = task.async(&on_timeout)
10
13
 
11
- socket.on(:message) do |event|
12
- data = JSON.parse(event.data.pack('c*'))
13
-
14
- if data['msg_type'] == 'data:update'
15
- attributes = data['value'].split(',')
16
-
17
- receiver.call({
18
- time: DateTime.strptime((attributes[0].to_i/1000).to_s, '%s'),
19
- speed: attributes[1].to_f,
20
- odometer: attributes[2].to_f,
21
- soc: attributes[3].to_f,
22
- elevation: attributes[4].to_f,
23
- est_heading: attributes[5].to_f,
24
- est_lat: attributes[6].to_f,
25
- est_lng: attributes[7].to_f,
26
- power: attributes[8].to_f,
27
- shift_state: attributes[9].to_s,
28
- range: attributes[10].to_f,
29
- est_range: attributes[11].to_f,
30
- heading: attributes[12].to_f
31
- })
32
- end
33
- end
14
+ while message = connection.read
15
+ timeout.stop
16
+ timeout = task.async(&on_timeout)
34
17
 
35
- socket.on(:close) do |event|
36
- EventMachine.stop
18
+ case message[:msg_type]
19
+ when 'data:update'
20
+ attributes = message[:value].split(',')
21
+
22
+ receiver.call({
23
+ time: DateTime.strptime((attributes[0].to_i/1000).to_s, '%s'),
24
+ speed: attributes[1].to_f,
25
+ odometer: attributes[2].to_f,
26
+ soc: attributes[3].to_f,
27
+ elevation: attributes[4].to_f,
28
+ est_heading: attributes[5].to_f,
29
+ est_lat: attributes[6].to_f,
30
+ est_lng: attributes[7].to_f,
31
+ power: attributes[8].to_f,
32
+ shift_state: attributes[9].to_s,
33
+ range: attributes[10].to_f,
34
+ est_range: attributes[11].to_f,
35
+ heading: attributes[12].to_f
36
+ })
37
+ when 'data:error'
38
+ task.stop
39
+ end
40
+ end
37
41
  end
38
42
  end
39
43
  end
40
44
 
41
45
  private
42
46
 
43
- def create_streaming_socket
44
- Faye::WebSocket::Client.new(streaming_endpoint)
47
+ TIMEOUT = 30
48
+
49
+ def endpoint
50
+ Async::HTTP::Endpoint.parse(streaming_endpoint)
45
51
  end
46
52
 
47
53
  def streaming_endpoint
@@ -1,3 +1,3 @@
1
1
  module TeslaApi
2
- VERSION = '3.0.2'
2
+ VERSION = '3.0.3'
3
3
  end
@@ -14,9 +14,9 @@ Gem::Specification.new do |spec|
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ['lib']
16
16
 
17
+ spec.add_dependency 'async-websocket'
17
18
  spec.add_dependency 'faraday'
18
19
  spec.add_dependency 'faraday_middleware'
19
- spec.add_dependency 'faye-websocket'
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 2.0'
22
22
  spec.add_development_dependency 'rake', '~> 12.0'
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tesla_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Dorr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-04 00:00:00.000000000 Z
11
+ date: 2019-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: faraday
14
+ name: async-websocket
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: faraday_middleware
28
+ name: faraday
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: faye-websocket
42
+ name: faraday_middleware
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="