-
-
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.
See #16 for more details.
- Loading branch information
Showing
5 changed files
with
91 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
require "httparty" | ||
require "em-http-request" | ||
require 'httparty' | ||
require 'em-http-request' | ||
|
||
require "tesla_api/version" | ||
require "tesla_api/client" | ||
require "tesla_api/stream" | ||
require "tesla_api/vehicle" | ||
require 'tesla_api/version' | ||
require 'tesla_api/client' | ||
require 'tesla_api/stream' | ||
require 'tesla_api/autopark' | ||
require 'tesla_api/vehicle' |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'faye/websocket' | ||
|
||
module TeslaApi | ||
module Autopark | ||
def start_autopark(&handler) | ||
EventMachine.run do | ||
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 | ||
|
||
socket.on(:close) do |_| | ||
@socket = nil | ||
@heartbeat && @heartbeat.cancel | ||
EventMachine.stop | ||
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 | ||
} | ||
socket.send(beat.to_json) | ||
end | ||
end | ||
end | ||
|
||
def socket | ||
@socket ||= Faye::WebSocket::Client.new( | ||
socket_endpoint, | ||
nil, | ||
{ | ||
headers: { | ||
'Authorization' => "Basic #{socket_auth}" | ||
} | ||
} | ||
) | ||
end | ||
|
||
def socket_auth | ||
Base64.strict_encode64("#{email}:#{self['tokens'].first}") | ||
end | ||
|
||
def 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
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,3 +1,3 @@ | ||
module TeslaApi | ||
VERSION = "1.2.0" | ||
VERSION = '1.2.0' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'tesla_api/version' | ||
require File.expand_path('../lib/tesla_api/version', __FILE__) | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "tesla_api" | ||
spec.name = 'tesla_api' | ||
spec.version = TeslaApi::VERSION | ||
spec.authors = ["Tim Dorr"] | ||
spec.email = ["[email protected]"] | ||
spec.authors = ['Tim Dorr'] | ||
spec.email = ['[email protected]'] | ||
spec.summary = %q{A wrapper for the Tesla API} | ||
spec.description = %q{Check the state of your Tesla Model S and issue basic commands. Stream data from the car's telematics system.} | ||
spec.homepage = "https://github.com/timdorr/model-s-api" | ||
spec.license = "MIT" | ||
spec.homepage = 'https://github.com/timdorr/model-s-api' | ||
spec.license = 'MIT' | ||
|
||
spec.files = `git ls-files -z`.split("\x0") | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.files = `git ls-files -z`.split("\0") | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_dependency "httparty" | ||
spec.add_dependency "em-http-request" | ||
spec.add_dependency 'httparty' | ||
spec.add_dependency 'faye-websocket' | ||
spec.add_dependency 'em-http-request' | ||
|
||
spec.add_development_dependency "bundler", "~> 1.7" | ||
spec.add_development_dependency "rake", "~> 10.0" | ||
spec.add_development_dependency "rspec", "~> 3.1" | ||
spec.add_development_dependency "faker", "~> 1.4" | ||
spec.add_development_dependency "vcr", "~> 2.9" | ||
spec.add_development_dependency "webmock", "~> 1.20" | ||
spec.add_development_dependency "dotenv", "~> 1.0" | ||
spec.add_development_dependency "simplecov", "~> 0.9" | ||
spec.add_development_dependency "coveralls" | ||
spec.add_development_dependency 'bundler', '~> 1.7' | ||
spec.add_development_dependency 'rake', '~> 10.0' | ||
spec.add_development_dependency 'rspec', '~> 3.1' | ||
spec.add_development_dependency 'faker', '~> 1.4' | ||
spec.add_development_dependency 'vcr', '~> 2.9' | ||
spec.add_development_dependency 'webmock', '~> 1.20' | ||
spec.add_development_dependency 'dotenv', '~> 1.0' | ||
spec.add_development_dependency 'simplecov', '~> 0.9' | ||
spec.add_development_dependency 'coveralls' | ||
end |