]> git.basschouten.com Git - openhab-addons.git/blob
8d8dea8355da059bc78e0ce9ac0f26f9916f6031
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.*;
23 import org.openhab.binding.gardena.internal.util.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Represents a Gardena device.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  */
32 public class Device {
33     private final Logger logger = LoggerFactory.getLogger(Device.class);
34
35     private transient static final String DEVICE_TYPE_PREFIX = "gardena smart";
36     public boolean active = true;
37     public String id;
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;
52
53     private Map<String, LocalService> localServices = new HashMap<>();
54
55     public Device(String id) {
56         this.id = id;
57     }
58
59     /**
60      * Returns the local service or creates one if it does not exist.
61      */
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;
68         }
69         return localService;
70     }
71
72     /**
73      * Evaluates the device type.
74      */
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(" ", "_");
81             } else {
82                 // workaround: we have to guess the device type, valves cannot be identified if modeType is wrong
83                 if (mower != null) {
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;
89                 }
90             }
91             if (deviceType == null) {
92                 logger.warn("Can't identify device with id {}, wrong modelType sent from the Gardena API", id);
93                 active = false;
94             }
95         }
96     }
97
98     /**
99      * Assigns the dataItem to the corresponding property.
100      */
101     public void setDataItem(DataItem<?> dataItem) throws GardenaException {
102         if (dataItem instanceof DeviceDataItem) {
103             // ignore
104         } else if (dataItem instanceof LocationDataItem) {
105             LocationDataItem locationDataItem = (LocationDataItem) dataItem;
106             if (locationDataItem.attributes != null) {
107                 location = locationDataItem.attributes.name;
108             }
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;
136             } else {
137                 throw new GardenaException("Unknown valveNumber in dataItem with id: " + dataItem.id);
138             }
139         } else {
140             throw new GardenaException("Unknown dataItem with id: " + dataItem.id);
141         }
142
143         if (common != null && common.attributes != null) {
144             common.attributes.lastUpdate.timestamp = new Date();
145         }
146     }
147
148     @Override
149     public int hashCode() {
150         return id.hashCode();
151     }
152
153     @Override
154     public boolean equals(Object obj) {
155         if (obj == null || !(obj instanceof Device)) {
156             return false;
157         }
158         Device comp = (Device) obj;
159         return comp.id.equals(id);
160     }
161 }