Module: TeslaApi::Autopark

Included in:
Vehicle
Defined in:
lib/tesla_api/autopark.rb

Instance Method Summary collapse

Instance Method Details

#start_autopark(&handler) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tesla_api/autopark.rb', line 3

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