Sha256: cdc63c426e97f80660cde8b8e53b52a0bb38b6c84c49766529687cb26afb9ebb

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module TeslaApi
  module Autopark
    def start_autopark(&handler)
      Async do |task|
        Async::WebSocket::Client.connect(autopark_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

            handler.call(message)
          end
        end
      end
    end

    private

    def autopark_endpoint
      Async::HTTP::Endpoint.parse(autopark_endpoint_url)
    end

    def autopark_endpoint_url
      "wss://streaming.vn.teslamotors.com/connect/#{self['vehicle_id']}"
    end

    def autopark_headers
      {
          'Authorization' => "Basic #{socket_auth}"
      }
    end

    def autopark_socket_auth
      Base64.strict_encode64("#{email}:#{self['tokens'].first}")
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tesla_api-3.1.0 lib/tesla_api/autopark.rb
tesla_api-3.0.7 lib/tesla_api/autopark.rb
tesla_api-3.0.6 lib/tesla_api/autopark.rb
tesla_api-3.0.5 lib/tesla_api/autopark.rb
tesla_api-3.0.4 lib/tesla_api/autopark.rb