]> git.basschouten.com Git - openhab-addons.git/blob
63f5033f8d2a9b9ccc20a03d54b42d7075a52150
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.gardena.internal.model.dto;
14
15 import static org.openhab.binding.gardena.internal.GardenaBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.openhab.binding.gardena.internal.exception.GardenaException;
22 import org.openhab.binding.gardena.internal.model.dto.api.CommonService;
23 import org.openhab.binding.gardena.internal.model.dto.api.CommonServiceDataItem;
24 import org.openhab.binding.gardena.internal.model.dto.api.DataItem;
25 import org.openhab.binding.gardena.internal.model.dto.api.DeviceDataItem;
26 import org.openhab.binding.gardena.internal.model.dto.api.Location;
27 import org.openhab.binding.gardena.internal.model.dto.api.LocationDataItem;
28 import org.openhab.binding.gardena.internal.model.dto.api.MowerServiceDataItem;
29 import org.openhab.binding.gardena.internal.model.dto.api.PowerSocketServiceDataItem;
30 import org.openhab.binding.gardena.internal.model.dto.api.SensorServiceDataItem;
31 import org.openhab.binding.gardena.internal.model.dto.api.ValveServiceDataItem;
32 import org.openhab.binding.gardena.internal.model.dto.api.ValveSetServiceDataItem;
33 import org.openhab.binding.gardena.internal.util.StringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Represents a Gardena device.
39  *
40  * @author Gerhard Riegler - Initial contribution
41  */
42 public class Device {
43     private final Logger logger = LoggerFactory.getLogger(Device.class);
44
45     private transient static final String DEVICE_TYPE_PREFIX = "gardena smart";
46     public boolean active = true;
47     public String id;
48     public String deviceType;
49     public String location;
50     public CommonServiceDataItem common;
51     public MowerServiceDataItem mower;
52     public PowerSocketServiceDataItem powerSocket;
53     public SensorServiceDataItem sensor;
54     public ValveServiceDataItem valve;
55     public ValveServiceDataItem valveOne;
56     public ValveServiceDataItem valveTwo;
57     public ValveServiceDataItem valveThree;
58     public ValveServiceDataItem valveFour;
59     public ValveServiceDataItem valveFive;
60     public ValveServiceDataItem valveSix;
61     public ValveSetServiceDataItem valveSet;
62
63     private Map<String, LocalService> localServices = new HashMap<>();
64
65     public Device(String id) {
66         this.id = id;
67     }
68
69     /**
70      * Returns the local service or creates one if it does not exist.
71      */
72     public LocalService getLocalService(String key) {
73         LocalService localService = localServices.get(key);
74         if (localService == null) {
75             localService = new LocalService();
76             localServices.put(key, localService);
77             localService.commandDuration = 3600;
78         }
79         return localService;
80     }
81
82     /**
83      * Evaluates the device type.
84      */
85     public void evaluateDeviceType() {
86         if (deviceType == null) {
87             CommonService commonServiceAttributes = common.attributes;
88             if (commonServiceAttributes != null
89                     && commonServiceAttributes.modelType.value.toLowerCase().startsWith(DEVICE_TYPE_PREFIX)) {
90                 String modelType = commonServiceAttributes.modelType.value.toLowerCase();
91                 modelType = modelType.substring(14);
92                 deviceType = modelType.replace(" ", "_");
93             } else {
94                 // workaround: we have to guess the device type, valves cannot be identified if modeType is wrong
95                 if (mower != null) {
96                     deviceType = DEVICE_TYPE_MOWER;
97                 } else if (powerSocket != null) {
98                     deviceType = DEVICE_TYPE_POWER;
99                 } else if (sensor != null) {
100                     deviceType = DEVICE_TYPE_SENSOR;
101                 }
102             }
103             if (deviceType == null) {
104                 logger.warn("Can't identify device with id {}, wrong modelType sent from the Gardena API", id);
105                 active = false;
106             }
107         }
108     }
109
110     /**
111      * Assigns the dataItem to the corresponding property.
112      */
113     public void setDataItem(DataItem<?> dataItem) throws GardenaException {
114         if (dataItem instanceof DeviceDataItem) {
115             // ignore
116         } else if (dataItem instanceof LocationDataItem) {
117             LocationDataItem locationDataItem = (LocationDataItem) dataItem;
118             Location locationAttributes = locationDataItem.attributes;
119             if (locationAttributes != null) {
120                 location = locationAttributes.name;
121             }
122         } else if (dataItem instanceof CommonServiceDataItem) {
123             common = (CommonServiceDataItem) dataItem;
124         } else if (dataItem instanceof MowerServiceDataItem) {
125             mower = (MowerServiceDataItem) dataItem;
126         } else if (dataItem instanceof PowerSocketServiceDataItem) {
127             powerSocket = (PowerSocketServiceDataItem) dataItem;
128         } else if (dataItem instanceof SensorServiceDataItem) {
129             sensor = (SensorServiceDataItem) dataItem;
130         } else if (dataItem instanceof ValveSetServiceDataItem) {
131             valveSet = (ValveSetServiceDataItem) dataItem;
132         } else if (dataItem instanceof ValveServiceDataItem) {
133             String valveNumber = StringUtils.substringAfterLast(dataItem.id, ":");
134             if ("".equals(valveNumber) || "wc".equals(valveNumber) || "0".equals(valveNumber)) {
135                 valve = (ValveServiceDataItem) dataItem;
136             } else if ("1".equals(valveNumber)) {
137                 valveOne = (ValveServiceDataItem) dataItem;
138             } else if ("2".equals(valveNumber)) {
139                 valveTwo = (ValveServiceDataItem) dataItem;
140             } else if ("3".equals(valveNumber)) {
141                 valveThree = (ValveServiceDataItem) dataItem;
142             } else if ("4".equals(valveNumber)) {
143                 valveFour = (ValveServiceDataItem) dataItem;
144             } else if ("5".equals(valveNumber)) {
145                 valveFive = (ValveServiceDataItem) dataItem;
146             } else if ("6".equals(valveNumber)) {
147                 valveSix = (ValveServiceDataItem) dataItem;
148             } else {
149                 throw new GardenaException("Unknown valveNumber in dataItem with id: " + dataItem.id);
150             }
151         } else {
152             throw new GardenaException("Unknown dataItem with id: " + dataItem.id);
153         }
154
155         if (common != null) {
156             CommonService attributes = common.attributes;
157             if (attributes != null) {
158                 attributes.lastUpdate.timestamp = new Date();
159             }
160             common.attributes = attributes;
161         }
162     }
163
164     @Override
165     public int hashCode() {
166         return id.hashCode();
167     }
168
169     @Override
170     public boolean equals(Object obj) {
171         if (obj == null || !(obj instanceof Device)) {
172             return false;
173         }
174         Device comp = (Device) obj;
175         return comp.id.equals(id);
176     }
177 }