2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tesla.internal.handler;
15 import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.math.BigDecimal;
22 import java.math.RoundingMode;
23 import java.text.SimpleDateFormat;
24 import java.util.Arrays;
25 import java.util.Date;
26 import java.util.HashMap;
29 import java.util.concurrent.ScheduledFuture;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.locks.ReentrantLock;
32 import java.util.stream.Collectors;
34 import javax.measure.quantity.Temperature;
35 import javax.ws.rs.ProcessingException;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.ClientBuilder;
38 import javax.ws.rs.client.WebTarget;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
42 import org.eclipse.jdt.annotation.Nullable;
43 import org.openhab.binding.tesla.internal.TeslaBindingConstants;
44 import org.openhab.binding.tesla.internal.TeslaBindingConstants.EventKeys;
45 import org.openhab.binding.tesla.internal.TeslaChannelSelectorProxy;
46 import org.openhab.binding.tesla.internal.TeslaChannelSelectorProxy.TeslaChannelSelector;
47 import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler.Authenticator;
48 import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler.Request;
49 import org.openhab.binding.tesla.internal.protocol.ChargeState;
50 import org.openhab.binding.tesla.internal.protocol.ClimateState;
51 import org.openhab.binding.tesla.internal.protocol.DriveState;
52 import org.openhab.binding.tesla.internal.protocol.GUIState;
53 import org.openhab.binding.tesla.internal.protocol.Vehicle;
54 import org.openhab.binding.tesla.internal.protocol.VehicleState;
55 import org.openhab.binding.tesla.internal.throttler.QueueChannelThrottler;
56 import org.openhab.binding.tesla.internal.throttler.Rate;
57 import org.openhab.core.library.types.DecimalType;
58 import org.openhab.core.library.types.IncreaseDecreaseType;
59 import org.openhab.core.library.types.OnOffType;
60 import org.openhab.core.library.types.PercentType;
61 import org.openhab.core.library.types.QuantityType;
62 import org.openhab.core.library.types.StringType;
63 import org.openhab.core.library.unit.SIUnits;
64 import org.openhab.core.library.unit.Units;
65 import org.openhab.core.thing.ChannelUID;
66 import org.openhab.core.thing.Thing;
67 import org.openhab.core.thing.ThingStatus;
68 import org.openhab.core.thing.ThingStatusDetail;
69 import org.openhab.core.thing.binding.BaseThingHandler;
70 import org.openhab.core.types.Command;
71 import org.openhab.core.types.RefreshType;
72 import org.openhab.core.types.State;
73 import org.openhab.core.types.UnDefType;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 import com.google.gson.Gson;
78 import com.google.gson.JsonElement;
79 import com.google.gson.JsonObject;
80 import com.google.gson.JsonParser;
83 * The {@link TeslaVehicleHandler} is responsible for handling commands, which are sent
84 * to one of the channels of a specific vehicle.
86 * @author Karel Goderis - Initial contribution
87 * @author Kai Kreuzer - Refactored to use separate account handler and improved configuration options
89 public class TeslaVehicleHandler extends BaseThingHandler {
91 private static final int EVENT_STREAM_PAUSE = 5000;
92 private static final int EVENT_TIMESTAMP_AGE_LIMIT = 3000;
93 private static final int EVENT_TIMESTAMP_MAX_DELTA = 10000;
94 private static final int FAST_STATUS_REFRESH_INTERVAL = 15000;
95 private static final int SLOW_STATUS_REFRESH_INTERVAL = 60000;
96 private static final int EVENT_MAXIMUM_ERRORS_IN_INTERVAL = 10;
97 private static final int EVENT_ERROR_INTERVAL_SECONDS = 15;
98 private static final int API_SLEEP_INTERVAL_MINUTES = 20;
99 private static final int MOVE_THRESHOLD_INTERVAL_MINUTES = 5;
101 private final Logger logger = LoggerFactory.getLogger(TeslaVehicleHandler.class);
103 protected WebTarget eventTarget;
105 // Vehicle state variables
106 protected Vehicle vehicle;
107 protected String vehicleJSON;
108 protected DriveState driveState;
109 protected GUIState guiState;
110 protected VehicleState vehicleState;
111 protected ChargeState chargeState;
112 protected ClimateState climateState;
114 protected boolean allowWakeUp;
115 protected boolean enableEvents = false;
116 protected long lastTimeStamp;
117 protected long apiIntervalTimestamp;
118 protected int apiIntervalErrors;
119 protected long eventIntervalTimestamp;
120 protected int eventIntervalErrors;
121 protected ReentrantLock lock;
123 protected double lastLongitude;
124 protected double lastLatitude;
125 protected long lastLocationChangeTimestamp;
127 protected long lastStateTimestamp = System.currentTimeMillis();
128 protected String lastState = "";
129 protected boolean isInactive = false;
131 protected TeslaAccountHandler account;
133 protected QueueChannelThrottler stateThrottler;
134 protected ClientBuilder clientBuilder;
135 protected Client eventClient;
136 protected TeslaChannelSelectorProxy teslaChannelSelectorProxy = new TeslaChannelSelectorProxy();
137 protected Thread eventThread;
138 protected ScheduledFuture<?> fastStateJob;
139 protected ScheduledFuture<?> slowStateJob;
141 private final Gson gson = new Gson();
143 public TeslaVehicleHandler(Thing thing, ClientBuilder clientBuilder) {
145 this.clientBuilder = clientBuilder;
148 @SuppressWarnings("null")
150 public void initialize() {
151 logger.trace("Initializing the Tesla handler for {}", getThing().getUID());
152 updateStatus(ThingStatus.UNKNOWN);
153 allowWakeUp = (boolean) getConfig().get(TeslaBindingConstants.CONFIG_ALLOWWAKEUP);
155 // the streaming API seems to be broken - let's keep the code, if it comes back one day
156 // enableEvents = (boolean) getConfig().get(TeslaBindingConstants.CONFIG_ENABLEEVENTS);
158 account = (TeslaAccountHandler) getBridge().getHandler();
159 lock = new ReentrantLock();
160 scheduler.execute(() -> queryVehicleAndUpdate());
164 Map<Object, Rate> channels = new HashMap<>();
165 channels.put(DATA_THROTTLE, new Rate(1, 1, TimeUnit.SECONDS));
166 channels.put(COMMAND_THROTTLE, new Rate(20, 1, TimeUnit.MINUTES));
168 Rate firstRate = new Rate(20, 1, TimeUnit.MINUTES);
169 Rate secondRate = new Rate(200, 10, TimeUnit.MINUTES);
170 stateThrottler = new QueueChannelThrottler(firstRate, scheduler, channels);
171 stateThrottler.addRate(secondRate);
173 if (fastStateJob == null || fastStateJob.isCancelled()) {
174 fastStateJob = scheduler.scheduleWithFixedDelay(fastStateRunnable, 0, FAST_STATUS_REFRESH_INTERVAL,
175 TimeUnit.MILLISECONDS);
178 if (slowStateJob == null || slowStateJob.isCancelled()) {
179 slowStateJob = scheduler.scheduleWithFixedDelay(slowStateRunnable, 0, SLOW_STATUS_REFRESH_INTERVAL,
180 TimeUnit.MILLISECONDS);
187 if (eventThread == null) {
188 eventThread = new Thread(eventRunnable, "openHAB-Tesla-Events-" + getThing().getUID());
195 public void dispose() {
196 logger.trace("Disposing the Tesla handler for {}", getThing().getUID());
199 if (fastStateJob != null && !fastStateJob.isCancelled()) {
200 fastStateJob.cancel(true);
204 if (slowStateJob != null && !slowStateJob.isCancelled()) {
205 slowStateJob.cancel(true);
209 if (eventThread != null && !eventThread.isInterrupted()) {
210 eventThread.interrupt();
217 if (eventClient != null) {
223 * Retrieves the unique vehicle id this handler is associated with
225 * @return the vehicle id
227 public String getVehicleId() {
228 if (vehicle != null) {
236 public void handleCommand(ChannelUID channelUID, Command command) {
237 logger.debug("handleCommand {} {}", channelUID, command);
238 String channelID = channelUID.getId();
239 TeslaChannelSelector selector = TeslaChannelSelector.getValueSelectorFromChannelID(channelID);
241 if (command instanceof RefreshType) {
243 logger.debug("Waking vehicle to refresh all data");
249 // Request the state of all known variables. This is sub-optimal, but the requests get scheduled and
250 // throttled so we are safe not to break the Tesla SLA
253 if (selector != null) {
256 case CHARGE_LIMIT_SOC: {
257 if (command instanceof PercentType) {
258 setChargeLimit(((PercentType) command).intValue());
259 } else if (command instanceof OnOffType && command == OnOffType.ON) {
261 } else if (command instanceof OnOffType && command == OnOffType.OFF) {
263 } else if (command instanceof IncreaseDecreaseType
264 && command == IncreaseDecreaseType.INCREASE) {
265 setChargeLimit(Math.min(chargeState.charge_limit_soc + 1, 100));
266 } else if (command instanceof IncreaseDecreaseType
267 && command == IncreaseDecreaseType.DECREASE) {
268 setChargeLimit(Math.max(chargeState.charge_limit_soc - 1, 0));
274 if (command instanceof DecimalType) {
275 amps = ((DecimalType) command).intValue();
277 if (command instanceof QuantityType<?>) {
278 QuantityType<?> qamps = ((QuantityType<?>) command).toUnit(Units.AMPERE);
280 amps = qamps.intValue();
284 if (amps < 5 || amps > 32) {
285 logger.warn("Charging amps can only be set in a range of 5-32A, but not to {}A.",
289 setChargingAmps(amps);
292 case COMBINED_TEMP: {
293 QuantityType<Temperature> quantity = commandToQuantityType(command);
294 if (quantity != null) {
295 setCombinedTemperature(quanityToRoundedFloat(quantity));
300 QuantityType<Temperature> quantity = commandToQuantityType(command);
301 if (quantity != null) {
302 setDriverTemperature(quanityToRoundedFloat(quantity));
306 case PASSENGER_TEMP: {
307 QuantityType<Temperature> quantity = commandToQuantityType(command);
308 if (quantity != null) {
309 setPassengerTemperature(quanityToRoundedFloat(quantity));
314 if (command instanceof OnOffType) {
315 setSentryMode(command == OnOffType.ON);
319 case SUN_ROOF_STATE: {
320 if (command instanceof StringType) {
321 setSunroof(command.toString());
325 case CHARGE_TO_MAX: {
326 if (command instanceof OnOffType) {
327 if (((OnOffType) command) == OnOffType.ON) {
328 setMaxRangeCharging(true);
330 setMaxRangeCharging(false);
336 if (command instanceof OnOffType) {
337 if (((OnOffType) command) == OnOffType.ON) {
346 if (command instanceof OnOffType) {
347 if (((OnOffType) command) == OnOffType.ON) {
354 if (command instanceof OnOffType) {
355 if (((OnOffType) command) == OnOffType.ON) {
362 if (command instanceof OnOffType) {
363 if (((OnOffType) command) == OnOffType.ON) {
370 if (command instanceof OnOffType) {
371 if (((OnOffType) command) == OnOffType.ON) {
380 if (command instanceof OnOffType) {
381 if (((OnOffType) command) == OnOffType.ON) {
382 autoConditioning(true);
384 autoConditioning(false);
390 if (command instanceof OnOffType) {
391 if (((OnOffType) command) == OnOffType.ON) {
398 if (command instanceof OnOffType) {
399 if (((OnOffType) command) == OnOffType.ON) {
406 if (command instanceof OnOffType) {
407 if (((OnOffType) command) == OnOffType.ON) {
408 if (vehicleState.rt == 0) {
412 if (vehicleState.rt == 1) {
420 if (command instanceof OnOffType) {
421 int valetpin = ((BigDecimal) getConfig().get(VALETPIN)).intValue();
422 if (((OnOffType) command) == OnOffType.ON) {
423 setValetMode(true, valetpin);
425 setValetMode(false, valetpin);
430 case RESET_VALET_PIN: {
431 if (command instanceof OnOffType) {
432 if (((OnOffType) command) == OnOffType.ON) {
442 } catch (IllegalArgumentException e) {
444 "An error occurred while trying to set the read-only variable associated with channel '{}' to '{}'",
445 channelID, command.toString());
451 public void sendCommand(String command, String payLoad, WebTarget target) {
452 if (command.equals(COMMAND_WAKE_UP) || isAwake()) {
453 Request request = account.newRequest(this, command, payLoad, target);
454 if (stateThrottler != null) {
455 stateThrottler.submit(COMMAND_THROTTLE, request);
460 public void sendCommand(String command) {
461 sendCommand(command, "{}");
464 public void sendCommand(String command, String payLoad) {
465 if (command.equals(COMMAND_WAKE_UP) || isAwake()) {
466 Request request = account.newRequest(this, command, payLoad, account.commandTarget);
467 if (stateThrottler != null) {
468 stateThrottler.submit(COMMAND_THROTTLE, request);
473 public void sendCommand(String command, WebTarget target) {
474 if (command.equals(COMMAND_WAKE_UP) || isAwake()) {
475 Request request = account.newRequest(this, command, "{}", target);
476 if (stateThrottler != null) {
477 stateThrottler.submit(COMMAND_THROTTLE, request);
482 public void requestData(String command, String payLoad) {
483 if (command.equals(COMMAND_WAKE_UP) || isAwake()) {
484 Request request = account.newRequest(this, command, payLoad, account.dataRequestTarget);
485 if (stateThrottler != null) {
486 stateThrottler.submit(DATA_THROTTLE, request);
492 protected void updateStatus(ThingStatus status) {
493 super.updateStatus(status);
497 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
498 super.updateStatus(status, statusDetail);
502 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
503 super.updateStatus(status, statusDetail, description);
506 public void requestData(String command) {
507 requestData(command, null);
510 public void queryVehicle(String parameter) {
511 WebTarget target = account.vehicleTarget.path(parameter);
512 sendCommand(parameter, null, target);
515 public void requestAllData() {
516 requestData(DRIVE_STATE);
517 requestData(VEHICLE_STATE);
518 requestData(CHARGE_STATE);
519 requestData(CLIMATE_STATE);
520 requestData(GUI_STATE);
523 protected boolean isAwake() {
524 return vehicle != null && "online".equals(vehicle.state) && vehicle.vehicle_id != null;
527 protected boolean isInMotion() {
528 if (driveState != null) {
529 if (driveState.speed != null && driveState.shift_state != null) {
530 return !"Undefined".equals(driveState.speed)
531 && (!"P".equals(driveState.shift_state) || !"Undefined".equals(driveState.shift_state));
537 protected boolean isInactive() {
538 // vehicle is inactive in case
539 // - it does not charge
540 // - it has not moved in the observation period
541 return isInactive && !isCharging() && !hasMovedInSleepInterval();
544 protected boolean isCharging() {
545 return chargeState != null && "Charging".equals(chargeState.charging_state);
548 protected boolean hasMovedInSleepInterval() {
549 return lastLocationChangeTimestamp > (System.currentTimeMillis()
550 - (MOVE_THRESHOLD_INTERVAL_MINUTES * 60 * 1000));
553 protected boolean allowQuery() {
554 return (isAwake() && !isInactive());
557 protected void setActive() {
559 lastLocationChangeTimestamp = System.currentTimeMillis();
564 protected boolean checkResponse(Response response, boolean immediatelyFail) {
565 if (response != null && response.getStatus() == 200) {
569 if (immediatelyFail || apiIntervalErrors >= TeslaAccountHandler.API_MAXIMUM_ERRORS_IN_INTERVAL) {
570 if (immediatelyFail) {
571 logger.warn("Got an unsuccessful result, setting vehicle to offline and will try again");
573 logger.warn("Reached the maximum number of errors ({}) for the current interval ({} seconds)",
574 TeslaAccountHandler.API_MAXIMUM_ERRORS_IN_INTERVAL,
575 TeslaAccountHandler.API_ERROR_INTERVAL_SECONDS);
578 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
579 if (eventClient != null) {
582 } else if ((System.currentTimeMillis() - apiIntervalTimestamp) > 1000
583 * TeslaAccountHandler.API_ERROR_INTERVAL_SECONDS) {
584 logger.trace("Resetting the error counter. ({} errors in the last interval)", apiIntervalErrors);
585 apiIntervalTimestamp = System.currentTimeMillis();
586 apiIntervalErrors = 0;
593 public void setChargeLimit(int percent) {
594 JsonObject payloadObject = new JsonObject();
595 payloadObject.addProperty("percent", percent);
596 sendCommand(COMMAND_SET_CHARGE_LIMIT, gson.toJson(payloadObject), account.commandTarget);
597 requestData(CHARGE_STATE);
600 public void setChargingAmps(int amps) {
601 JsonObject payloadObject = new JsonObject();
602 payloadObject.addProperty("charging_amps", amps);
603 sendCommand(COMMAND_SET_CHARGING_AMPS, gson.toJson(payloadObject), account.commandTarget);
604 requestData(CHARGE_STATE);
607 public void setSentryMode(boolean b) {
608 JsonObject payloadObject = new JsonObject();
609 payloadObject.addProperty("on", b);
610 sendCommand(COMMAND_SET_SENTRY_MODE, gson.toJson(payloadObject), account.commandTarget);
611 requestData(VEHICLE_STATE);
614 public void setSunroof(String state) {
615 if (state.equals("vent") || state.equals("close")) {
616 JsonObject payloadObject = new JsonObject();
617 payloadObject.addProperty("state", state);
618 sendCommand(COMMAND_SUN_ROOF, gson.toJson(payloadObject), account.commandTarget);
619 requestData(VEHICLE_STATE);
621 logger.warn("Ignoring invalid command '{}' for sunroof.", state);
626 * Sets the driver and passenger temperatures.
628 * While setting different temperature values is supported by the API, in practice this does not always work
629 * reliably, possibly if the the
630 * only reliable method is to set the driver and passenger temperature to the same value
632 * @param driverTemperature in Celsius
633 * @param passenegerTemperature in Celsius
635 public void setTemperature(float driverTemperature, float passenegerTemperature) {
636 JsonObject payloadObject = new JsonObject();
637 payloadObject.addProperty("driver_temp", driverTemperature);
638 payloadObject.addProperty("passenger_temp", passenegerTemperature);
639 sendCommand(COMMAND_SET_TEMP, gson.toJson(payloadObject), account.commandTarget);
640 requestData(CLIMATE_STATE);
643 public void setCombinedTemperature(float temperature) {
644 setTemperature(temperature, temperature);
647 public void setDriverTemperature(float temperature) {
648 setTemperature(temperature, climateState != null ? climateState.passenger_temp_setting : temperature);
651 public void setPassengerTemperature(float temperature) {
652 setTemperature(climateState != null ? climateState.driver_temp_setting : temperature, temperature);
655 public void openFrunk() {
656 JsonObject payloadObject = new JsonObject();
657 payloadObject.addProperty("which_trunk", "front");
658 sendCommand(COMMAND_ACTUATE_TRUNK, gson.toJson(payloadObject), account.commandTarget);
659 requestData(VEHICLE_STATE);
662 public void openTrunk() {
663 JsonObject payloadObject = new JsonObject();
664 payloadObject.addProperty("which_trunk", "rear");
665 sendCommand(COMMAND_ACTUATE_TRUNK, gson.toJson(payloadObject), account.commandTarget);
666 requestData(VEHICLE_STATE);
669 public void closeTrunk() {
673 public void setValetMode(boolean b, Integer pin) {
674 JsonObject payloadObject = new JsonObject();
675 payloadObject.addProperty("on", b);
677 payloadObject.addProperty("password", String.format("%04d", pin));
679 sendCommand(COMMAND_SET_VALET_MODE, gson.toJson(payloadObject), account.commandTarget);
680 requestData(VEHICLE_STATE);
683 public void resetValetPin() {
684 sendCommand(COMMAND_RESET_VALET_PIN, account.commandTarget);
685 requestData(VEHICLE_STATE);
688 public void setMaxRangeCharging(boolean b) {
689 sendCommand(b ? COMMAND_CHARGE_MAX : COMMAND_CHARGE_STD, account.commandTarget);
690 requestData(CHARGE_STATE);
693 public void charge(boolean b) {
694 sendCommand(b ? COMMAND_CHARGE_START : COMMAND_CHARGE_STOP, account.commandTarget);
695 requestData(CHARGE_STATE);
698 public void flashLights() {
699 sendCommand(COMMAND_FLASH_LIGHTS, account.commandTarget);
702 public void honkHorn() {
703 sendCommand(COMMAND_HONK_HORN, account.commandTarget);
706 public void openChargePort() {
707 sendCommand(COMMAND_OPEN_CHARGE_PORT, account.commandTarget);
708 requestData(CHARGE_STATE);
711 public void lockDoors(boolean b) {
712 sendCommand(b ? COMMAND_DOOR_LOCK : COMMAND_DOOR_UNLOCK, account.commandTarget);
713 requestData(VEHICLE_STATE);
716 public void autoConditioning(boolean b) {
717 sendCommand(b ? COMMAND_AUTO_COND_START : COMMAND_AUTO_COND_STOP, account.commandTarget);
718 requestData(CLIMATE_STATE);
721 public void wakeUp() {
722 sendCommand(COMMAND_WAKE_UP, account.wakeUpTarget);
725 protected Vehicle queryVehicle() {
726 String authHeader = account.getAuthHeader();
728 if (authHeader != null) {
730 // get a list of vehicles
731 Response response = account.vehiclesTarget.request(MediaType.APPLICATION_JSON_TYPE)
732 .header("Authorization", authHeader).get();
734 logger.debug("Querying the vehicle : Response : {}:{}", response.getStatus(), response.getStatusInfo());
736 if (!checkResponse(response, true)) {
737 logger.error("An error occurred while querying the vehicle");
741 JsonObject jsonObject = JsonParser.parseString(response.readEntity(String.class)).getAsJsonObject();
742 Vehicle[] vehicleArray = gson.fromJson(jsonObject.getAsJsonArray("response"), Vehicle[].class);
744 for (Vehicle vehicle : vehicleArray) {
745 logger.debug("Querying the vehicle: VIN {}", vehicle.vin);
746 if (vehicle.vin.equals(getConfig().get(VIN))) {
747 vehicleJSON = gson.toJson(vehicle);
748 parseAndUpdate("queryVehicle", null, vehicleJSON);
749 if (logger.isTraceEnabled()) {
750 logger.trace("Vehicle is id {}/vehicle_id {}/tokens {}", vehicle.id, vehicle.vehicle_id,
756 } catch (ProcessingException e) {
757 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
763 protected void queryVehicleAndUpdate() {
764 vehicle = queryVehicle();
765 if (vehicle != null) {
766 parseAndUpdate("queryVehicle", null, vehicleJSON);
770 public void parseAndUpdate(String request, String payLoad, String result) {
771 final Double LOCATION_THRESHOLD = .0000001;
773 JsonObject jsonObject = null;
776 if (request != null && result != null && !"null".equals(result)) {
777 updateStatus(ThingStatus.ONLINE);
778 // first, update state objects
781 driveState = gson.fromJson(result, DriveState.class);
783 if (Math.abs(lastLatitude - driveState.latitude) > LOCATION_THRESHOLD
784 || Math.abs(lastLongitude - driveState.longitude) > LOCATION_THRESHOLD) {
785 logger.debug("Vehicle moved, resetting last location timestamp");
787 lastLatitude = driveState.latitude;
788 lastLongitude = driveState.longitude;
789 lastLocationChangeTimestamp = System.currentTimeMillis();
795 guiState = gson.fromJson(result, GUIState.class);
798 case VEHICLE_STATE: {
799 vehicleState = gson.fromJson(result, VehicleState.class);
803 chargeState = gson.fromJson(result, ChargeState.class);
805 updateState(CHANNEL_CHARGE, OnOffType.ON);
807 updateState(CHANNEL_CHARGE, OnOffType.OFF);
812 case CLIMATE_STATE: {
813 climateState = gson.fromJson(result, ClimateState.class);
814 BigDecimal avgtemp = roundBigDecimal(new BigDecimal(
815 (climateState.driver_temp_setting + climateState.passenger_temp_setting) / 2.0f));
816 updateState(CHANNEL_COMBINED_TEMP, new QuantityType<>(avgtemp, SIUnits.CELSIUS));
819 case "queryVehicle": {
820 if (vehicle != null && !lastState.equals(vehicle.state)) {
821 lastState = vehicle.state;
823 // in case vehicle changed to awake, refresh all data
825 logger.debug("Vehicle is now awake, updating all data");
826 lastLocationChangeTimestamp = System.currentTimeMillis();
833 // reset timestamp if elapsed and set inactive to false
834 if (isInactive && lastStateTimestamp + (API_SLEEP_INTERVAL_MINUTES * 60 * 1000) < System
835 .currentTimeMillis()) {
836 logger.debug("Vehicle did not fall asleep within sleep period, checking again");
839 boolean wasInactive = isInactive;
840 isInactive = !isCharging() && !hasMovedInSleepInterval();
842 if (!wasInactive && isInactive) {
843 lastStateTimestamp = System.currentTimeMillis();
844 logger.debug("Vehicle is inactive");
852 // secondly, reformat the response string to a JSON compliant
853 // object for some specific non-JSON compatible requests
855 case MOBILE_ENABLED_STATE: {
856 jsonObject = new JsonObject();
857 jsonObject.addProperty(MOBILE_ENABLED_STATE, result);
861 jsonObject = JsonParser.parseString(result).getAsJsonObject();
867 // process the result
868 if (jsonObject != null && result != null && !"null".equals(result)) {
869 // deal with responses for "set" commands, which get confirmed
870 // positively, or negatively, in which case a reason for failure
872 if (jsonObject.get("reason") != null && jsonObject.get("reason").getAsString() != null) {
873 boolean requestResult = jsonObject.get("result").getAsBoolean();
874 logger.debug("The request ({}) execution was {}, and reported '{}'", new Object[] { request,
875 requestResult ? "successful" : "not successful", jsonObject.get("reason").getAsString() });
877 Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
879 long resultTimeStamp = 0;
880 for (Map.Entry<String, JsonElement> entry : entrySet) {
881 if ("timestamp".equals(entry.getKey())) {
882 resultTimeStamp = Long.valueOf(entry.getValue().getAsString());
883 if (logger.isTraceEnabled()) {
884 Date date = new Date(resultTimeStamp);
885 SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
886 logger.trace("The request result timestamp is {}", dateFormatter.format(date));
895 boolean proceed = true;
896 if (resultTimeStamp < lastTimeStamp && request == DRIVE_STATE) {
901 for (Map.Entry<String, JsonElement> entry : entrySet) {
903 TeslaChannelSelector selector = TeslaChannelSelector
904 .getValueSelectorFromRESTID(entry.getKey());
905 if (!selector.isProperty()) {
906 if (!entry.getValue().isJsonNull()) {
907 updateState(selector.getChannelID(), teslaChannelSelectorProxy.getState(
908 entry.getValue().getAsString(), selector, editProperties()));
909 if (logger.isTraceEnabled()) {
911 "The variable/value pair '{}':'{}' is successfully processed",
912 entry.getKey(), entry.getValue());
915 updateState(selector.getChannelID(), UnDefType.UNDEF);
918 if (!entry.getValue().isJsonNull()) {
919 Map<String, String> properties = editProperties();
920 properties.put(selector.getChannelID(), entry.getValue().getAsString());
921 updateProperties(properties);
922 if (logger.isTraceEnabled()) {
924 "The variable/value pair '{}':'{}' is successfully used to set property '{}'",
925 entry.getKey(), entry.getValue(), selector.getChannelID());
929 } catch (IllegalArgumentException e) {
930 logger.trace("The variable/value pair '{}':'{}' is not (yet) supported",
931 entry.getKey(), entry.getValue());
932 } catch (ClassCastException | IllegalStateException e) {
933 logger.trace("An exception occurred while converting the JSON data : '{}'",
938 logger.warn("The result for request '{}' is discarded due to an out of sync timestamp",
946 } catch (Exception p) {
947 logger.error("An exception occurred while parsing data received from the vehicle: '{}'", p.getMessage());
951 @SuppressWarnings("unchecked")
952 protected QuantityType<Temperature> commandToQuantityType(Command command) {
953 if (command instanceof QuantityType) {
954 return ((QuantityType<Temperature>) command).toUnit(SIUnits.CELSIUS);
956 return new QuantityType<>(new BigDecimal(command.toString()), SIUnits.CELSIUS);
959 protected float quanityToRoundedFloat(QuantityType<Temperature> quantity) {
960 return roundBigDecimal(quantity.toBigDecimal()).floatValue();
963 protected BigDecimal roundBigDecimal(BigDecimal value) {
964 return value.setScale(1, RoundingMode.HALF_EVEN);
967 protected Runnable slowStateRunnable = () -> {
968 queryVehicleAndUpdate();
970 boolean allowQuery = allowQuery();
973 requestData(CHARGE_STATE);
974 requestData(CLIMATE_STATE);
975 requestData(GUI_STATE);
976 queryVehicle(MOBILE_ENABLED_STATE);
982 logger.debug("Vehicle is neither charging nor moving, skipping updates to allow it to sleep");
988 protected Runnable fastStateRunnable = () -> {
989 if (getThing().getStatus() == ThingStatus.ONLINE) {
990 boolean allowQuery = allowQuery();
993 requestData(DRIVE_STATE);
994 requestData(VEHICLE_STATE);
1000 logger.debug("Vehicle is neither charging nor moving, skipping updates to allow it to sleep");
1007 protected Runnable eventRunnable = new Runnable() {
1008 Response eventResponse;
1009 BufferedReader eventBufferedReader;
1010 InputStreamReader eventInputStreamReader;
1011 boolean isEstablished = false;
1013 protected boolean establishEventStream() {
1015 if (!isEstablished) {
1016 eventBufferedReader = null;
1018 eventClient = clientBuilder.build()
1019 .register(new Authenticator((String) getConfig().get(CONFIG_USERNAME), vehicle.tokens[0]));
1020 eventTarget = eventClient.target(URI_EVENT).path(vehicle.vehicle_id + "/").queryParam("values",
1021 Arrays.asList(EventKeys.values()).stream().skip(1).map(Enum::toString)
1022 .collect(Collectors.joining(",")));
1023 eventResponse = eventTarget.request(MediaType.TEXT_PLAIN_TYPE).get();
1025 logger.debug("Event Stream: Establishing the event stream: Response: {}:{}",
1026 eventResponse.getStatus(), eventResponse.getStatusInfo());
1028 if (eventResponse.getStatus() == 200) {
1029 InputStream dummy = (InputStream) eventResponse.getEntity();
1030 eventInputStreamReader = new InputStreamReader(dummy);
1031 eventBufferedReader = new BufferedReader(eventInputStreamReader);
1032 isEstablished = true;
1033 } else if (eventResponse.getStatus() == 401) {
1034 isEstablished = false;
1036 isEstablished = false;
1039 if (!isEstablished) {
1040 eventIntervalErrors++;
1041 if (eventIntervalErrors >= EVENT_MAXIMUM_ERRORS_IN_INTERVAL) {
1043 "Reached the maximum number of errors ({}) for the current interval ({} seconds)",
1044 EVENT_MAXIMUM_ERRORS_IN_INTERVAL, EVENT_ERROR_INTERVAL_SECONDS);
1045 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
1046 eventClient.close();
1049 if ((System.currentTimeMillis() - eventIntervalTimestamp) > 1000
1050 * EVENT_ERROR_INTERVAL_SECONDS) {
1051 logger.trace("Resetting the error counter. ({} errors in the last interval)",
1052 eventIntervalErrors);
1053 eventIntervalTimestamp = System.currentTimeMillis();
1054 eventIntervalErrors = 0;
1058 } catch (Exception e) {
1060 "Event stream: An exception occurred while establishing the event stream for the vehicle: '{}'",
1062 isEstablished = false;
1065 return isEstablished;
1072 if (getThing().getStatus() == ThingStatus.ONLINE) {
1074 if (establishEventStream()) {
1075 String line = eventBufferedReader.readLine();
1077 while (line != null) {
1078 logger.debug("Event stream: Received an event: '{}'", line);
1079 String vals[] = line.split(",");
1080 long currentTimeStamp = Long.valueOf(vals[0]);
1081 long systemTimeStamp = System.currentTimeMillis();
1082 if (logger.isDebugEnabled()) {
1083 SimpleDateFormat dateFormatter = new SimpleDateFormat(
1084 "yyyy-MM-dd'T'HH:mm:ss.SSS");
1085 logger.debug("STS {} CTS {} Delta {}",
1086 dateFormatter.format(new Date(systemTimeStamp)),
1087 dateFormatter.format(new Date(currentTimeStamp)),
1088 systemTimeStamp - currentTimeStamp);
1090 if (systemTimeStamp - currentTimeStamp < EVENT_TIMESTAMP_AGE_LIMIT) {
1091 if (currentTimeStamp > lastTimeStamp) {
1092 lastTimeStamp = Long.valueOf(vals[0]);
1093 if (logger.isDebugEnabled()) {
1094 SimpleDateFormat dateFormatter = new SimpleDateFormat(
1095 "yyyy-MM-dd'T'HH:mm:ss.SSS");
1096 logger.debug("Event Stream: Event stamp is {}",
1097 dateFormatter.format(new Date(lastTimeStamp)));
1099 for (int i = 0; i < EventKeys.values().length; i++) {
1100 TeslaChannelSelector selector = TeslaChannelSelector
1101 .getValueSelectorFromRESTID((EventKeys.values()[i]).toString());
1102 if (!selector.isProperty()) {
1103 State newState = teslaChannelSelectorProxy.getState(vals[i],
1104 selector, editProperties());
1105 if (newState != null && !"".equals(vals[i])) {
1106 updateState(selector.getChannelID(), newState);
1108 updateState(selector.getChannelID(), UnDefType.UNDEF);
1111 Map<String, String> properties = editProperties();
1112 properties.put(selector.getChannelID(),
1113 (selector.getState(vals[i])).toString());
1114 updateProperties(properties);
1118 if (logger.isDebugEnabled()) {
1119 SimpleDateFormat dateFormatter = new SimpleDateFormat(
1120 "yyyy-MM-dd'T'HH:mm:ss.SSS");
1122 "Event stream: Discarding an event with an out of sync timestamp {} (last is {})",
1123 dateFormatter.format(new Date(currentTimeStamp)),
1124 dateFormatter.format(new Date(lastTimeStamp)));
1128 if (logger.isDebugEnabled()) {
1129 SimpleDateFormat dateFormatter = new SimpleDateFormat(
1130 "yyyy-MM-dd'T'HH:mm:ss.SSS");
1132 "Event Stream: Discarding an event that differs {} ms from the system time: {} (system is {})",
1133 systemTimeStamp - currentTimeStamp,
1134 dateFormatter.format(currentTimeStamp),
1135 dateFormatter.format(systemTimeStamp));
1137 if (systemTimeStamp - currentTimeStamp > EVENT_TIMESTAMP_MAX_DELTA) {
1138 logger.trace("Event stream: The event stream will be reset");
1139 isEstablished = false;
1142 line = eventBufferedReader.readLine();
1144 logger.trace("Event stream: The end of stream was reached");
1145 isEstablished = false;
1148 logger.debug("Event stream: The vehicle is not awake");
1149 if (vehicle != null) {
1151 // wake up the vehicle until streaming token <> 0
1152 logger.debug("Event stream: Waking up the vehicle");
1156 vehicle = queryVehicle();
1158 Thread.sleep(EVENT_STREAM_PAUSE);
1161 } catch (IOException | NumberFormatException e) {
1162 logger.debug("Event stream: An exception occurred while reading events: '{}'", e.getMessage());
1163 isEstablished = false;
1164 } catch (InterruptedException e) {
1165 isEstablished = false;
1168 if (Thread.interrupted()) {
1169 logger.debug("Event stream: the event stream was interrupted");