2 * Copyright (c) 2010-2021 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.automower.internal.things;
15 import static org.openhab.binding.automower.internal.AutomowerBindingConstants.*;
18 import java.util.Collection;
19 import java.util.Collections;
21 import java.util.Optional;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicReference;
27 import javax.measure.quantity.Dimensionless;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.openhab.binding.automower.internal.AutomowerBindingConstants;
32 import org.openhab.binding.automower.internal.actions.AutomowerActions;
33 import org.openhab.binding.automower.internal.bridge.AutomowerBridge;
34 import org.openhab.binding.automower.internal.bridge.AutomowerBridgeHandler;
35 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
36 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.RestrictedReason;
37 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.State;
38 import org.openhab.binding.automower.internal.rest.exceptions.AutomowerCommunicationException;
39 import org.openhab.core.i18n.TimeZoneProvider;
40 import org.openhab.core.library.types.DateTimeType;
41 import org.openhab.core.library.types.DecimalType;
42 import org.openhab.core.library.types.QuantityType;
43 import org.openhab.core.library.types.StringType;
44 import org.openhab.core.library.unit.Units;
45 import org.openhab.core.thing.Bridge;
46 import org.openhab.core.thing.ChannelUID;
47 import org.openhab.core.thing.Thing;
48 import org.openhab.core.thing.ThingStatus;
49 import org.openhab.core.thing.ThingStatusDetail;
50 import org.openhab.core.thing.ThingTypeUID;
51 import org.openhab.core.thing.binding.BaseThingHandler;
52 import org.openhab.core.thing.binding.ThingHandler;
53 import org.openhab.core.thing.binding.ThingHandlerService;
54 import org.openhab.core.types.Command;
55 import org.openhab.core.types.RefreshType;
56 import org.openhab.core.types.Type;
57 import org.openhab.core.types.UnDefType;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 import com.google.gson.Gson;
64 * The {@link AutomowerHandler} is responsible for handling commands, which are
65 * sent to one of the channels.
67 * @author Markus Pfleger - Initial contribution
68 * @author Marcin Czeczko - Added support for planner & calendar data
71 public class AutomowerHandler extends BaseThingHandler {
72 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_AUTOMOWER);
73 private static final String NO_ID = "NO_ID";
74 private static final long DEFAULT_COMMAND_DURATION_MIN = 60;
75 private static final long DEFAULT_POLLING_INTERVAL_S = TimeUnit.MINUTES.toSeconds(10);
77 private final Logger logger = LoggerFactory.getLogger(AutomowerHandler.class);
78 private final TimeZoneProvider timeZoneProvider;
80 private AtomicReference<String> automowerId = new AtomicReference<String>(NO_ID);
81 private long lastQueryTimeMs = 0L;
83 private @Nullable ScheduledFuture<?> automowerPollingJob;
84 private long maxQueryFrequencyNanos = TimeUnit.MINUTES.toNanos(1);
86 private @Nullable Mower mowerState;
88 private Gson gson = new Gson();
90 private Runnable automowerPollingRunnable = () -> {
91 Bridge bridge = getBridge();
92 if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
93 updateAutomowerState();
95 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
99 public AutomowerHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
101 this.timeZoneProvider = timeZoneProvider;
105 public void handleCommand(ChannelUID channelUID, Command command) {
106 if (RefreshType.REFRESH == command) {
107 logger.debug("Refreshing channel '{}'", channelUID);
108 refreshChannels(channelUID);
110 AutomowerCommand.fromChannelUID(channelUID).ifPresent(commandName -> {
111 logger.debug("Sending command '{}'", commandName);
112 getCommandValue(command).ifPresentOrElse(duration -> sendAutomowerCommand(commandName, duration),
113 () -> sendAutomowerCommand(commandName));
118 private Optional<Integer> getCommandValue(Type type) {
119 if (type instanceof DecimalType) {
120 return Optional.of(((DecimalType) type).intValue());
122 return Optional.empty();
125 private void refreshChannels(ChannelUID channelUID) {
126 updateAutomowerState();
130 public Collection<Class<? extends ThingHandlerService>> getServices() {
131 return Collections.singleton(AutomowerActions.class);
135 public void initialize() {
136 Bridge bridge = getBridge();
137 if (bridge != null) {
138 AutomowerConfiguration currentConfig = getConfigAs(AutomowerConfiguration.class);
139 final String configMowerId = currentConfig.getMowerId();
140 final Integer pollingIntervalS = currentConfig.getPollingInterval();
142 if (configMowerId == null) {
143 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
144 "@text/conf-error-no-mower-id");
145 } else if (pollingIntervalS != null && pollingIntervalS < 1) {
146 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
147 "@text/conf-error-invalid-polling-interval");
149 automowerId.set(configMowerId);
150 startAutomowerPolling(pollingIntervalS);
153 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
158 private AutomowerBridge getAutomowerBridge() {
159 Bridge bridge = getBridge();
160 if (bridge != null) {
161 ThingHandler handler = bridge.getHandler();
162 if (handler instanceof AutomowerBridgeHandler) {
163 AutomowerBridgeHandler bridgeHandler = (AutomowerBridgeHandler) handler;
164 return bridgeHandler.getAutomowerBridge();
171 public void dispose() {
172 if (!automowerId.get().equals(NO_ID)) {
173 stopAutomowerPolling();
174 automowerId.set(NO_ID);
178 private void startAutomowerPolling(@Nullable Integer pollingIntervalS) {
179 if (automowerPollingJob == null) {
180 final long pollingIntervalToUse = pollingIntervalS == null ? DEFAULT_POLLING_INTERVAL_S : pollingIntervalS;
181 automowerPollingJob = scheduler.scheduleWithFixedDelay(automowerPollingRunnable, 1, pollingIntervalToUse,
186 private void stopAutomowerPolling() {
187 if (automowerPollingJob != null) {
188 automowerPollingJob.cancel(true);
189 automowerPollingJob = null;
193 private boolean isValidResult(@Nullable Mower mower) {
194 return mower != null && mower.getAttributes() != null && mower.getAttributes().getMetadata() != null
195 && mower.getAttributes().getBattery() != null && mower.getAttributes().getSystem() != null;
198 private boolean isConnected(@Nullable Mower mower) {
199 return mower != null && mower.getAttributes() != null && mower.getAttributes().getMetadata() != null
200 && mower.getAttributes().getMetadata().isConnected();
203 private synchronized void updateAutomowerState() {
204 String id = automowerId.get();
206 AutomowerBridge automowerBridge = getAutomowerBridge();
207 if (automowerBridge != null) {
208 if (mowerState == null || (System.nanoTime() - lastQueryTimeMs > maxQueryFrequencyNanos)) {
209 lastQueryTimeMs = System.nanoTime();
210 mowerState = automowerBridge.getAutomowerStatus(id);
212 if (isValidResult(mowerState)) {
213 initializeProperties(mowerState);
215 updateChannelState(mowerState);
217 if (isConnected(mowerState)) {
218 updateStatus(ThingStatus.ONLINE);
220 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
221 "@text/comm-error-mower-not-connected-to-cloud");
224 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
225 "@text/comm-error-query-mower-failed");
228 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/conf-error-no-bridge");
230 } catch (AutomowerCommunicationException e) {
231 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
232 "@text/comm-error-query-mower-failed");
233 logger.warn("Unable to query automower status for: {}. Error: {}", id, e.getMessage());
238 * Sends a command to the automower with the default duration of 60min
240 * @param command The command that should be sent. Valid values are: "Start", "ResumeSchedule", "Pause", "Park",
241 * "ParkUntilNextSchedule", "ParkUntilFurtherNotice"
243 public void sendAutomowerCommand(AutomowerCommand command) {
244 sendAutomowerCommand(command, DEFAULT_COMMAND_DURATION_MIN);
248 * Sends a command to the automower with the given duration
250 * @param command The command that should be sent. Valid values are: "Start", "ResumeSchedule", "Pause", "Park",
251 * "ParkUntilNextSchedule", "ParkUntilFurtherNotice"
252 * @param commandDurationMinutes The duration of the command in minutes. This is only evaluated for "Start" and
255 public void sendAutomowerCommand(AutomowerCommand command, long commandDurationMinutes) {
256 logger.debug("Sending command '{} {}'", command.getCommand(), commandDurationMinutes);
257 String id = automowerId.get();
259 AutomowerBridge automowerBridge = getAutomowerBridge();
260 if (automowerBridge != null) {
261 automowerBridge.sendAutomowerCommand(id, command, commandDurationMinutes);
263 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/conf-error-no-bridge");
265 } catch (AutomowerCommunicationException e) {
266 logger.warn("Unable to send command to automower: {}, Error: {}", id, e.getMessage());
269 updateAutomowerState();
272 private String restrictedState(RestrictedReason reason) {
273 return "RESTRICTED_" + reason.name();
276 private void updateChannelState(@Nullable Mower mower) {
277 if (isValidResult(mower)) {
278 updateState(CHANNEL_STATUS_NAME, new StringType(mower.getAttributes().getSystem().getName()));
279 updateState(CHANNEL_STATUS_MODE, new StringType(mower.getAttributes().getMower().getMode().name()));
280 updateState(CHANNEL_STATUS_ACTIVITY, new StringType(mower.getAttributes().getMower().getActivity().name()));
282 if (mower.getAttributes().getMower().getState() != State.RESTRICTED) {
283 updateState(CHANNEL_STATUS_STATE, new StringType(mower.getAttributes().getMower().getState().name()));
285 updateState(CHANNEL_STATUS_STATE,
286 new StringType(restrictedState(mower.getAttributes().getPlanner().getRestrictedReason())));
289 updateState(CHANNEL_STATUS_LAST_UPDATE,
290 new DateTimeType(toZonedDateTime(mower.getAttributes().getMetadata().getStatusTimestamp())));
291 updateState(CHANNEL_STATUS_BATTERY, new QuantityType<Dimensionless>(
292 mower.getAttributes().getBattery().getBatteryPercent(), Units.PERCENT));
294 updateState(CHANNEL_STATUS_ERROR_CODE, new DecimalType(mower.getAttributes().getMower().getErrorCode()));
296 long errorCodeTimestamp = mower.getAttributes().getMower().getErrorCodeTimestamp();
297 if (errorCodeTimestamp == 0L) {
298 updateState(CHANNEL_STATUS_ERROR_TIMESTAMP, UnDefType.NULL);
300 updateState(CHANNEL_STATUS_ERROR_TIMESTAMP, new DateTimeType(toZonedDateTime(errorCodeTimestamp)));
303 long nextStartTimestamp = mower.getAttributes().getPlanner().getNextStartTimestamp();
304 // If next start timestamp is 0 it means the mower should start now, so using current timestamp
305 if (nextStartTimestamp == 0L) {
306 updateState(CHANNEL_PLANNER_NEXT_START, UnDefType.NULL);
308 updateState(CHANNEL_PLANNER_NEXT_START, new DateTimeType(toZonedDateTime(nextStartTimestamp)));
310 updateState(CHANNEL_PLANNER_OVERRIDE_ACTION,
311 new StringType(mower.getAttributes().getPlanner().getOverride().getAction()));
313 updateState(CHANNEL_CALENDAR_TASKS,
314 new StringType(gson.toJson(mower.getAttributes().getCalendar().getTasks())));
318 private void initializeProperties(@Nullable Mower mower) {
319 Map<String, String> properties = editProperties();
321 properties.put(AutomowerBindingConstants.AUTOMOWER_ID, mower.getId());
323 if (mower.getAttributes() != null && mower.getAttributes().getSystem() != null) {
324 properties.put(AutomowerBindingConstants.AUTOMOWER_SERIAL_NUMBER,
325 mower.getAttributes().getSystem().getSerialNumber());
326 properties.put(AutomowerBindingConstants.AUTOMOWER_MODEL, mower.getAttributes().getSystem().getModel());
327 properties.put(AutomowerBindingConstants.AUTOMOWER_NAME, mower.getAttributes().getSystem().getName());
330 updateProperties(properties);
334 * Converts timestamp returned by the Automower API into local time-zone.
335 * Timestamp returned by the API doesn't have offset and it always in the current time zone - it can be treated as
337 * Method builds a ZonedDateTime with same hour value but in the current system timezone.
339 * @param timestamp - Automower API timestamp
340 * @return ZonedDateTime in system timezone
342 private ZonedDateTime toZonedDateTime(long timestamp) {
343 Instant timestampInstant = Instant.ofEpochMilli(timestamp);
344 return ZonedDateTime.ofInstant(timestampInstant, timeZoneProvider.getTimeZone());