2 * Copyright (c) 2010-2022 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.io.imperihome.internal.model.device;
15 import java.util.HashMap;
16 import java.util.List;
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;
35 * Abstract parent of all devices. Sets up and tears down state listeners and contains parameter and link data.
37 * @author Pepijn de Geus - Initial contribution
39 public abstract class AbstractDevice implements StateChangeListener {
41 protected final transient Logger logger = LoggerFactory.getLogger(getClass());
46 private DeviceType type;
47 private String defaultIcon;
48 private final DeviceParameters params;
50 private transient boolean inverted;
51 private transient String roomName;
52 private transient Item item;
54 private final transient Map<String, String> links;
55 private transient Map<String, String> mapping;
57 private transient DeviceRegistry deviceRegistry;
58 private transient ActionRegistry actionRegistry;
60 public AbstractDevice(DeviceType type, Item item) {
63 params = new DeviceParameters();
64 links = new HashMap<>();
66 if (item instanceof GenericItem) {
67 ((GenericItem) item).addStateChangeListener(this);
71 public void destroy() {
72 if (item instanceof GenericItem) {
73 ((GenericItem) item).removeStateChangeListener(this);
76 deviceRegistry = null;
77 actionRegistry = null;
81 public String getId() {
85 public void setId(String id) {
89 public String getName() {
93 public void setName(String name) {
97 public String getRoom() {
101 public void setRoom(String room) {
105 public String getRoomName() {
109 public void setRoomName(String roomName) {
110 this.roomName = roomName;
113 public DeviceType getType() {
117 public void setType(DeviceType type) {
121 public boolean isInverted() {
125 public void setInverted(boolean inverted) {
126 this.inverted = inverted;
129 public String getDefaultIcon() {
133 public void setDefaultIcon(String defaultIcon) {
134 this.defaultIcon = defaultIcon;
137 public DeviceParameters getParams() {
141 public void addParam(DeviceParam param) {
142 logger.trace("Setting param for device {}: {}", this, param);
146 public Map<String, String> getLinks() {
150 public void addLink(String linkType, String deviceId) {
151 links.put(linkType, deviceId);
154 public void setMapping(Map<String, String> mapping) {
155 this.mapping = mapping;
158 public Map<String, String> getMapping() {
162 public void setDeviceRegistry(DeviceRegistry deviceRegistry) {
163 this.deviceRegistry = deviceRegistry;
166 protected DeviceRegistry getDeviceRegistry() {
167 return deviceRegistry;
170 public void setActionRegistry(ActionRegistry actionRegistry) {
171 this.actionRegistry = actionRegistry;
174 protected ActionRegistry getActionRegistry() {
175 return actionRegistry;
178 public Item getItem() {
182 public String getItemName() {
183 return item.getName();
187 * Process any device-specific ISS tags.
189 * @param issTags ISS tags map.
191 public void processCustomTags(Map<TagType, List<String>> issTags) {
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.
199 public void updateParams() {
200 logger.trace("updateParams on {}", this);
204 * Performs an action on this device.
206 * @param action Action name.
207 * @param value Action value.
209 public void performAction(String action, String value) {
210 Action actionInst = actionRegistry.get(action);
211 if (actionInst == null) {
212 logger.warn("Unknown action: {}", action);
216 Item item = getItem();
217 if (!actionInst.supports(this, item)) {
218 logger.warn("Action '{}' not supported on this device ({})", action, this);
222 actionInst.perform(this, item, value);
226 public void stateChanged(Item item, State oldState, State newState) {
230 public void stateUpdated(Item item, State newState) {
231 logger.debug("Device item {} state changed to {}", item, newState);
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");
242 public String toString() {
243 return getClass().getSimpleName() + "{id='" + id + '\'' + ", name='" + name + '\'' + ", room='" + room + '\''
244 + ", type=" + type + ", invert=" + inverted + ", icon=" + defaultIcon + ", links=" + links + '}';