2 * Copyright (c) 2010-2020 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.gardena.internal.model.dto;
15 import static org.openhab.binding.gardena.internal.GardenaBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
21 import org.openhab.binding.gardena.internal.exception.GardenaException;
22 import org.openhab.binding.gardena.internal.model.dto.api.*;
23 import org.openhab.binding.gardena.internal.util.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Represents a Gardena device.
30 * @author Gerhard Riegler - Initial contribution
33 private final Logger logger = LoggerFactory.getLogger(Device.class);
35 private transient static final String DEVICE_TYPE_PREFIX = "gardena smart";
36 public boolean active = true;
38 public String deviceType;
39 public String location;
40 public CommonServiceDataItem common;
41 public MowerServiceDataItem mower;
42 public PowerSocketServiceDataItem powerSocket;
43 public SensorServiceDataItem sensor;
44 public ValveServiceDataItem valve;
45 public ValveServiceDataItem valveOne;
46 public ValveServiceDataItem valveTwo;
47 public ValveServiceDataItem valveThree;
48 public ValveServiceDataItem valveFour;
49 public ValveServiceDataItem valveFive;
50 public ValveServiceDataItem valveSix;
51 public ValveSetServiceDataItem valveSet;
53 private Map<String, LocalService> localServices = new HashMap<>();
55 public Device(String id) {
60 * Returns the local service or creates one if it does not exist.
62 public LocalService getLocalService(String key) {
63 LocalService localService = localServices.get(key);
64 if (localService == null) {
65 localService = new LocalService();
66 localServices.put(key, localService);
67 localService.commandDuration = 3600;
73 * Evaluates the device type.
75 public void evaluateDeviceType() {
76 if (deviceType == null) {
77 if (common.attributes.modelType.value.toLowerCase().startsWith(DEVICE_TYPE_PREFIX)) {
78 String modelType = common.attributes.modelType.value.toLowerCase();
79 modelType = modelType.substring(14);
80 deviceType = modelType.replace(" ", "_");
82 // workaround: we have to guess the device type, valves cannot be identified if modeType is wrong
84 deviceType = DEVICE_TYPE_MOWER;
85 } else if (powerSocket != null) {
86 deviceType = DEVICE_TYPE_POWER;
87 } else if (sensor != null) {
88 deviceType = DEVICE_TYPE_SENSOR;
91 if (deviceType == null) {
92 logger.warn("Can't identify device with id {}, wrong modelType sent from the Gardena API", id);
99 * Assigns the dataItem to the corresponding property.
101 public void setDataItem(DataItem<?> dataItem) throws GardenaException {
102 if (dataItem instanceof DeviceDataItem) {
104 } else if (dataItem instanceof LocationDataItem) {
105 LocationDataItem locationDataItem = (LocationDataItem) dataItem;
106 if (locationDataItem.attributes != null) {
107 location = locationDataItem.attributes.name;
109 } else if (dataItem instanceof CommonServiceDataItem) {
110 common = (CommonServiceDataItem) dataItem;
111 } else if (dataItem instanceof MowerServiceDataItem) {
112 mower = (MowerServiceDataItem) dataItem;
113 } else if (dataItem instanceof PowerSocketServiceDataItem) {
114 powerSocket = (PowerSocketServiceDataItem) dataItem;
115 } else if (dataItem instanceof SensorServiceDataItem) {
116 sensor = (SensorServiceDataItem) dataItem;
117 } else if (dataItem instanceof ValveSetServiceDataItem) {
118 valveSet = (ValveSetServiceDataItem) dataItem;
119 } else if (dataItem instanceof ValveServiceDataItem) {
120 String valveNumber = StringUtils.substringAfterLast(dataItem.id, ":");
121 if (valveNumber != null
122 && (valveNumber.equals("") || valveNumber.equals("wc") || valveNumber.equals("0"))) {
123 valve = (ValveServiceDataItem) dataItem;
124 } else if ("1".equals(valveNumber)) {
125 valveOne = (ValveServiceDataItem) dataItem;
126 } else if ("2".equals(valveNumber)) {
127 valveTwo = (ValveServiceDataItem) dataItem;
128 } else if ("3".equals(valveNumber)) {
129 valveThree = (ValveServiceDataItem) dataItem;
130 } else if ("4".equals(valveNumber)) {
131 valveFour = (ValveServiceDataItem) dataItem;
132 } else if ("5".equals(valveNumber)) {
133 valveFive = (ValveServiceDataItem) dataItem;
134 } else if ("6".equals(valveNumber)) {
135 valveSix = (ValveServiceDataItem) dataItem;
137 throw new GardenaException("Unknown valveNumber in dataItem with id: " + dataItem.id);
140 throw new GardenaException("Unknown dataItem with id: " + dataItem.id);
143 if (common != null && common.attributes != null) {
144 common.attributes.lastUpdate.timestamp = new Date();
149 public int hashCode() {
150 return id.hashCode();
154 public boolean equals(Object obj) {
155 if (obj == null || !(obj instanceof Device)) {
158 Device comp = (Device) obj;
159 return comp.id.equals(id);