Sha256: b11a1c19cd8824f77710cf7c048c406b00c96ee3976ad6fa230d3cf27a1ed06c

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module TeslaApi
  module Autopark
    def start_autopark(&handler)
      Async do |task|
        Async::WebSocket::Client.connect(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 endpoint
      Async::HTTP::Endpoint.parse(autopark_socket_endpoint)
    end

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tesla_api-3.0.3 lib/tesla_api/autopark.rb