]> git.basschouten.com Git - openhab-addons.git/blob
71504dfdb61f74b77761a80d3d44366b70457e05
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.io.imperihome.internal.model.device;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.openhab.core.items.GenericItem;
20 import org.openhab.core.items.Item;
21 import org.openhab.core.items.StateChangeListener;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.types.State;
24 import org.openhab.io.imperihome.internal.action.Action;
25 import org.openhab.io.imperihome.internal.action.ActionRegistry;
26 import org.openhab.io.imperihome.internal.model.param.DeviceParam;
27 import org.openhab.io.imperihome.internal.model.param.DeviceParameters;
28 import org.openhab.io.imperihome.internal.model.param.ParamType;
29 import org.openhab.io.imperihome.internal.processor.DeviceRegistry;
30 import org.openhab.io.imperihome.internal.processor.TagType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Abstract parent of all devices. Sets up and tears down state listeners and contains parameter and link data.
36  *
37  * @author Pepijn de Geus - Initial contribution
38  */
39 public abstract class AbstractDevice implements StateChangeListener {
40
41     protected final transient Logger logger = LoggerFactory.getLogger(getClass());
42
43     private String id;
44     private String name;
45     private String room;
46     private DeviceType type;
47     private String defaultIcon;
48     private final DeviceParameters params;
49
50     private transient boolean inverted;
51     private transient String roomName;
52     private transient Item item;
53
54     private final transient Map<String, String> links;
55     private transient Map<String, String> mapping;
56
57     private transient DeviceRegistry deviceRegistry;
58     private transient ActionRegistry actionRegistry;
59
60     public AbstractDevice(DeviceType type, Item item) {
61         this.type = type;
62         this.item = item;
63         params = new DeviceParameters();
64         links = new HashMap<>();
65
66         if (item instanceof GenericItem) {
67             ((GenericItem) item).addStateChangeListener(this);
68         }
69     }
70
71     public void destroy() {
72         if (item instanceof GenericItem) {
73             ((GenericItem) item).removeStateChangeListener(this);
74         }
75
76         deviceRegistry = null;
77         actionRegistry = null;
78         item = null;
79     }
80
81     public String getId() {
82         return id;
83     }
84
85     public void setId(String id) {
86         this.id = id;
87     }
88
89     public String getName() {
90         return name;
91     }
92
93     public void setName(String name) {
94         this.name = name;
95     }
96
97     public String getRoom() {
98         return room;
99     }
100
101     public void setRoom(String room) {
102         this.room = room;
103     }
104
105     public String getRoomName() {
106         return roomName;
107     }
108
109     public void setRoomName(String roomName) {
110         this.roomName = roomName;
111     }
112
113     public DeviceType getType() {
114         return type;
115     }
116
117     public void setType(DeviceType type) {
118         this.type = type;
119     }
120
121     public boolean isInverted() {
122         return inverted;
123     }
124
125     public void setInverted(boolean inverted) {
126         this.inverted = inverted;
127     }
128
129     public String getDefaultIcon() {
130         return defaultIcon;
131     }
132
133     public void setDefaultIcon(String defaultIcon) {
134         this.defaultIcon = defaultIcon;
135     }
136
137     public DeviceParameters getParams() {
138         return params;
139     }
140
141     public void addParam(DeviceParam param) {
142         logger.trace("Setting param for device {}: {}", this, param);
143         params.set(param);
144     }
145
146     public Map<String, String> getLinks() {
147         return links;
148     }
149
150     public void addLink(String linkType, String deviceId) {
151         links.put(linkType, deviceId);
152     }
153
154     public void setMapping(Map<String, String> mapping) {
155         this.mapping = mapping;
156     }
157
158     public Map<String, String> getMapping() {
159         return mapping;
160     }
161
162     public void setDeviceRegistry(DeviceRegistry deviceRegistry) {
163         this.deviceRegistry = deviceRegistry;
164     }
165
166     protected DeviceRegistry getDeviceRegistry() {
167         return deviceRegistry;
168     }
169
170     public void setActionRegistry(ActionRegistry actionRegistry) {
171         this.actionRegistry = actionRegistry;
172     }
173
174     protected ActionRegistry getActionRegistry() {
175         return actionRegistry;
176     }
177
178     public Item getItem() {
179         return item;
180     }
181
182     public String getItemName() {
183         return item.getName();
184     }
185
186     /**
187      * Process any device-specific ISS tags.
188      * 
189      * @param issTags ISS tags map.
190      */
191     public void processCustomTags(Map<TagType, List<String>> issTags) {
192     }
193
194     /**
195      * Can be implemented by Devices that require their state to be updated manually, instead of relying (only) on Item
196      * state change events.
197      * This method is called just before serializing the device to JSON.
198      */
199     public void updateParams() {
200         logger.trace("updateParams on {}", this);
201     }
202
203     /**
204      * Performs an action on this device.
205      * 
206      * @param action Action name.
207      * @param value Action value.
208      */
209     public void performAction(String action, String value) {
210         Action actionInst = actionRegistry.get(action);
211         if (actionInst == null) {
212             logger.warn("Unknown action: {}", action);
213             return;
214         }
215
216         Item item = getItem();
217         if (!actionInst.supports(this, item)) {
218             logger.warn("Action '{}' not supported on this device ({})", action, this);
219             return;
220         }
221
222         actionInst.perform(this, item, value);
223     }
224
225     @Override
226     public void stateChanged(Item item, State oldState, State newState) {
227     }
228
229     @Override
230     public void stateUpdated(Item item, State newState) {
231         logger.debug("Device item {} state changed to {}", item, newState);
232
233         OnOffType onOffState = (OnOffType) item.getStateAs(OnOffType.class);
234         if (onOffState != null) {
235             boolean isOn = onOffState == OnOffType.ON;
236             DeviceParam param = new DeviceParam(ParamType.STATUS, isOn ^ isInverted() ? "1" : "0");
237             addParam(param);
238         }
239     }
240
241     @Override
242     public String toString() {
243         return getClass().getSimpleName() + "{id='" + id + '\'' + ", name='" + name + '\'' + ", room='" + room + '\''
244                 + ", type=" + type + ", invert=" + inverted + ", icon=" + defaultIcon + ", links=" + links + '}';
245     }
246 }