]> git.basschouten.com Git - openhab-addons.git/blob
6ecdfcf1973a2a1d37f7e4c7381aa6b21018c314
[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 static final transient 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 locationDataItem) {
117             Location locationAttributes = locationDataItem.attributes;
118             if (locationAttributes != null) {
119                 location = locationAttributes.name;
120             }
121         } else if (dataItem instanceof CommonServiceDataItem commonServiceItem) {
122             common = commonServiceItem;
123         } else if (dataItem instanceof MowerServiceDataItem mowerServiceItemm) {
124             mower = mowerServiceItemm;
125         } else if (dataItem instanceof PowerSocketServiceDataItem powerSocketItem) {
126             powerSocket = powerSocketItem;
127         } else if (dataItem instanceof SensorServiceDataItem sensorServiceItem) {
128             sensor = sensorServiceItem;
129         } else if (dataItem instanceof ValveSetServiceDataItem valveSetServiceItem) {
130             valveSet = valveSetServiceItem;
131         } else if (dataItem instanceof ValveServiceDataItem valveServiceItem) {
132             String valveNumber = StringUtils.substringAfterLast(dataItem.id, ":");
133             if ("".equals(valveNumber) || "wc".equals(valveNumber) || "0".equals(valveNumber)) {
134                 valve = valveServiceItem;
135             } else if ("1".equals(valveNumber)) {
136                 valveOne = valveServiceItem;
137             } else if ("2".equals(valveNumber)) {
138                 valveTwo = valveServiceItem;
139             } else if ("3".equals(valveNumber)) {
140                 valveThree = valveServiceItem;
141             } else if ("4".equals(valveNumber)) {
142                 valveFour = valveServiceItem;
143             } else if ("5".equals(valveNumber)) {
144                 valveFive = valveServiceItem;
145             } else if ("6".equals(valveNumber)) {
146                 valveSix = valveServiceItem;
147             } else {
148                 throw new GardenaException("Unknown valveNumber in dataItem with id: " + dataItem.id);
149             }
150         } else {
151             throw new GardenaException("Unknown dataItem with id: " + dataItem.id);
152         }
153
154         if (common != null) {
155             CommonService attributes = common.attributes;
156             if (attributes != null) {
157                 attributes.lastUpdate.timestamp = new Date();
158             }
159             common.attributes = attributes;
160         }
161     }
162
163     @Override
164     public int hashCode() {
165         return id.hashCode();
166     }
167
168     @Override
169     public boolean equals(Object obj) {
170         if (obj == null || !(obj instanceof Device)) {
171             return false;
172         }
173         Device comp = (Device) obj;
174         return comp.id.equals(id);
175     }
176 }