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 +4 -4
- data/lib/tesla_api.rb +4 -2
- data/lib/tesla_api/autopark.rb +23 -41
- data/lib/tesla_api/stream.rb +38 -32
- data/lib/tesla_api/version.rb +1 -1
- data/tesla_api.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36800b87fde594b317de4423717ddddde20b02a89a29dcc3030da421c857961d
|
4
|
+
data.tar.gz: a7578ddecc3e79faaf4b91eec431196fdd42cc6b2697d8a47c115a9839beb13d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cefac51f88134ddb5cf93567046c63249c48aac76c19ea95006f7a8f98e97ad2258c1d4969be3cedc3dd8e3ec8ad21513cc1b29913982f11b2263ca44e2bc2e
|
7
|
+
data.tar.gz: 3724b8215a83bd514ab27ddc02165793052f54dfb1bfc45cfe3f90c18fd50395dcedadd4e7070c3469d2f5557fb3354c02e97a3850e33c2d1ed8c397b4e8cef9
|
data/lib/tesla_api.rb
CHANGED
@@ -3,8 +3,10 @@ require 'base64'
|
|
3
3
|
|
4
4
|
require 'faraday'
|
5
5
|
require 'faraday_middleware'
|
6
|
-
|
7
|
-
require '
|
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'
|
data/lib/tesla_api/autopark.rb
CHANGED
@@ -1,60 +1,42 @@
|
|
1
1
|
module TeslaApi
|
2
2
|
module Autopark
|
3
3
|
def start_autopark(&handler)
|
4
|
-
|
5
|
-
|
6
|
-
message =
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
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
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
data/lib/tesla_api/stream.rb
CHANGED
@@ -1,47 +1,53 @@
|
|
1
1
|
module TeslaApi
|
2
2
|
module Stream
|
3
3
|
def stream(&receiver)
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
-
|
9
|
-
end
|
11
|
+
connection.write(stream_connect_message)
|
12
|
+
timeout = task.async(&on_timeout)
|
10
13
|
|
11
|
-
|
12
|
-
|
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
|
-
|
36
|
-
|
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
|
-
|
44
|
-
|
47
|
+
TIMEOUT = 30
|
48
|
+
|
49
|
+
def endpoint
|
50
|
+
Async::HTTP::Endpoint.parse(streaming_endpoint)
|
45
51
|
end
|
46
52
|
|
47
53
|
def streaming_endpoint
|
data/lib/tesla_api/version.rb
CHANGED
data/tesla_api.gemspec
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2019-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
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:
|
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:
|
42
|
+
name: faraday_middleware
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|