2 * Copyright (c) 2010-2021 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.binding.loxone.internal.controls;
15 import java.io.IOException;
16 import java.lang.reflect.Type;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Objects;
25 import org.openhab.binding.loxone.internal.LxServerHandlerApi;
26 import org.openhab.binding.loxone.internal.types.LxCategory;
27 import org.openhab.binding.loxone.internal.types.LxConfig;
28 import org.openhab.binding.loxone.internal.types.LxContainer;
29 import org.openhab.binding.loxone.internal.types.LxState;
30 import org.openhab.binding.loxone.internal.types.LxUuid;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.thing.Channel;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37 import org.openhab.core.thing.type.ChannelTypeUID;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.State;
40 import org.openhab.core.types.StateDescriptionFragment;
41 import org.openhab.core.types.UnDefType;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.gson.Gson;
46 import com.google.gson.JsonArray;
47 import com.google.gson.JsonDeserializationContext;
48 import com.google.gson.JsonDeserializer;
49 import com.google.gson.JsonElement;
50 import com.google.gson.JsonObject;
51 import com.google.gson.JsonParseException;
52 import com.google.gson.reflect.TypeToken;
55 * A control of Loxone Miniserver.
57 * It represents a control object on the Miniserver. Controls can represent an input, functional block or an output of
58 * the Miniserver, that is marked as visible in the Loxone UI. Controls can belong to a {@link LxContainer} room and a
59 * {@link LxCategory} category.
61 * @author Pawel Pieczul - initial contribution
64 public class LxControl {
67 * This class contains static configuration of the control and is used to make the fields transparent to the child
68 * classes that implement specific controls.
70 * @author Pawel Pieczul - initial contribution
73 public static class LxControlConfig {
74 private final LxServerHandlerApi thingHandler;
75 private final LxContainer room;
76 private final LxCategory category;
78 LxControlConfig(LxControlConfig config) {
79 this(config.thingHandler, config.room, config.category);
82 public LxControlConfig(LxServerHandlerApi thingHandler, LxContainer room, LxCategory category) {
84 this.category = category;
85 this.thingHandler = thingHandler;
90 * This class is used to instantiate a particular control object by the {@link LxControlFactory}
92 * @author Pawel Pieczul - initial contribution
95 abstract static class LxControlInstance {
97 * Creates an instance of a particular control class.
99 * @param uuid UUID of the control object to be created
100 * @return a newly created control object
102 abstract LxControl create(LxUuid uuid);
105 * Return a type name for this control.
107 * @return type name (as used on the Miniserver)
109 abstract String getType();
113 * This class describes additional parameters of a control received from the Miniserver and is used during JSON
116 * @author Pawel Pieczul - initial contribution
119 class LxControlDetails {
126 Boolean increaseOnly;
130 Map<String, String> outputs;
131 Boolean presenceConnected;
132 Integer connectedInputs;
136 * A callback that should be implemented by child classes to process received commands. This callback can be
137 * provided for each channel created by the controls.
139 * @author Pawel Pieczul - initial contribution
143 interface CommandCallback {
144 abstract void handleCommand(Command cmd) throws IOException;
148 * A callback that should be implemented by child classes to return current channel state. This callback can be
149 * provided for each channel created by the controls.
151 * @author Pawel Pieczul - initial contribution
155 interface StateCallback {
156 abstract State getChannelState();
160 * A set of callbacks registered per each channel by the child classes.
162 * @author Pawel Pieczul - initial contribution
165 private class Callbacks {
166 private CommandCallback commandCallback;
167 private StateCallback stateCallback;
169 private Callbacks(CommandCallback cC, StateCallback sC) {
170 commandCallback = cC;
176 * Parameters parsed from the JSON configuration file during deserialization
179 LxControlDetails details;
181 private LxUuid roomUuid;
182 private Boolean isSecured;
183 private LxUuid categoryUuid;
184 private Map<LxUuid, LxControl> subControls;
185 private final Map<String, LxState> states;
188 * Parameters set when finalizing {@link LxConfig} object setup. They will be null right after constructing object.
190 transient String defaultChannelLabel;
191 private transient LxControlConfig config;
194 * Parameters set when object is connected to the openHAB by the binding handler
196 final transient Set<String> tags = new HashSet<>();
197 private final transient List<Channel> channels = new ArrayList<>();
198 private final transient Map<ChannelUID, Callbacks> callbacks = new HashMap<>();
200 private final transient Logger logger;
201 private int numberOfChannels = 0;
204 * JSON deserialization routine, called during parsing configuration by the GSON library
206 public static final JsonDeserializer<LxControl> DESERIALIZER = new JsonDeserializer<LxControl>() {
208 public LxControl deserialize(JsonElement json, Type type, JsonDeserializationContext context)
209 throws JsonParseException {
210 JsonObject parent = json.getAsJsonObject();
211 String controlName = LxConfig.deserializeString(parent, "name");
212 String controlType = LxConfig.deserializeString(parent, "type");
213 LxUuid uuid = LxConfig.deserializeObject(parent, "uuidAction", LxUuid.class, context);
214 if (controlName == null || controlType == null || uuid == null) {
215 throw new JsonParseException("Control name/type/uuid is null.");
217 LxControl control = LxControlFactory.createControl(uuid, controlType);
218 if (control == null) {
221 control.name = controlName;
222 control.isSecured = LxConfig.deserializeObject(parent, "isSecured", Boolean.class, context);
223 control.roomUuid = LxConfig.deserializeObject(parent, "room", LxUuid.class, context);
224 control.categoryUuid = LxConfig.deserializeObject(parent, "cat", LxUuid.class, context);
225 control.details = LxConfig.deserializeObject(parent, "details", LxControlDetails.class, context);
226 control.subControls = LxConfig.deserializeObject(parent, "subControls",
227 new TypeToken<Map<LxUuid, LxControl>>() {
228 }.getType(), context);
230 JsonObject states = parent.getAsJsonObject("states");
231 if (states != null) {
232 states.entrySet().forEach(entry -> {
233 // temperature state of intelligent home controller object is the only
234 // one that has state represented as an array, as this is not implemented
235 // yet, we will skip this state
236 JsonElement element = entry.getValue();
237 if (element != null && !(element instanceof JsonArray)) {
238 String value = element.getAsString();
240 String name = entry.getKey().toLowerCase();
241 control.states.put(name, new LxState(new LxUuid(value), name, control));
250 LxControl(LxUuid uuid) {
251 logger = LoggerFactory.getLogger(LxControl.class);
253 states = new HashMap<>();
257 * A method that executes commands by the control. It delegates command execution to a registered callback method.
259 * @param channelId channel Id for the command
260 * @param command value of the command to perform
261 * @throws IOException in case of communication error with the Miniserver
263 public final void handleCommand(ChannelUID channelId, Command command) throws IOException {
264 Callbacks c = callbacks.get(channelId);
265 if (c != null && c.commandCallback != null) {
266 c.commandCallback.handleCommand(command);
268 logger.debug("Control UUID={} has no command handler", getUuid());
273 * Provides actual state value for the specified channel. It delegates execution to a registered callback method.
275 * @param channelId channel ID to get state for
276 * @return state if the channel value or null if no value available
278 public final State getChannelState(ChannelUID channelId) {
279 Callbacks c = callbacks.get(channelId);
280 if (c != null && c.stateCallback != null) {
282 return c.stateCallback.getChannelState();
283 } catch (NumberFormatException e) {
284 return UnDefType.UNDEF;
291 * Obtain control's name
293 * @return Human readable name of control
295 public String getName() {
300 * Get control's UUID as defined on the Miniserver
302 * @return UUID of the control
304 public LxUuid getUuid() {
309 * Get subcontrols of this control
311 * @return subcontrols of the control
313 public Map<LxUuid, LxControl> getSubControls() {
318 * Get control's channels
322 public List<Channel> getChannels() {
327 * Get control's and its subcontrols' channels
331 public List<Channel> getChannelsWithSubcontrols() {
332 final List<Channel> list = new ArrayList<>(channels);
333 subControls.values().forEach(c -> list.addAll(c.getChannelsWithSubcontrols()));
338 * Get control's Miniserver states
340 * @return control's Miniserver states
342 public Map<String, LxState> getStates() {
347 * Gets information is password is required to operate on this control object
349 * @return true is control is secured
351 public Boolean isSecured() {
352 return isSecured != null && isSecured;
356 * Compare UUID's of two controls -
358 * @param object Object to compare with
359 * @return true if UUID of two objects are equal
362 public boolean equals(Object object) {
363 if (this == object) {
366 if (object == null) {
369 if (object.getClass() != getClass()) {
372 LxControl c = (LxControl) object;
373 return Objects.equals(c.uuid, uuid);
377 * Hash code of the control is equal to its UUID's hash code
380 public int hashCode() {
381 return uuid.hashCode();
385 * Initialize Miniserver's control in runtime. Each class that implements {@link LxControl} should override this
386 * method and call it as a first step in the overridden implementation. Then it should add all runtime data, like
387 * channels and any fields that derive their value from the parsed JSON configuration.
388 * Before this method is called during configuration parsing, the control object must not be used.
390 * @param configToSet control's configuration
392 public void initialize(LxControlConfig configToSet) {
393 logger.debug("Initializing LxControl: {}", uuid);
395 if (config != null) {
396 logger.error("Error, attempt to initialize control that is already initialized: {}", uuid);
399 config = configToSet;
401 if (subControls == null) {
402 subControls = new HashMap<>();
404 subControls.values().removeIf(Objects::isNull);
407 if (config.room != null) {
408 config.room.addControl(this);
411 if (config.category != null) {
412 config.category.addControl(this);
415 String label = getLabel();
417 // Each control on a Miniserver must have a name defined, but in case this is a subject
418 // of some malicious data attack, we'll prevent null pointer exception
419 label = "Undefined name";
421 String roomName = config.room != null ? config.room.getName() : null;
422 if (roomName != null) {
423 label = roomName + " / " + label;
425 defaultChannelLabel = label;
427 // Propagate to all subcontrols of this control object
428 subControls.values().forEach(c -> c.initialize(config));
432 * This method will be called from {@link LxState}, when Miniserver state value is updated.
433 * By default it will query all channels of the control and update their state accordingly.
434 * This method will not handle channel state descriptions, as they must be prepared individually.
435 * It can be overridden in child class to handle particular states differently.
437 * @param state changed Miniserver state or null if not specified (any/all)
439 public void onStateChange(LxState state) {
440 if (config == null) {
441 logger.error("Attempt to change state with not finalized configuration!: {}", state.getUuid());
443 channels.forEach(channel -> {
444 ChannelUID channelId = channel.getUID();
445 State channelState = getChannelState(channelId);
446 if (channelState != null) {
447 config.thingHandler.setChannelState(channelId, channelState);
454 * Gets room UUID after it was deserialized by GSON
458 public LxUuid getRoomUuid() {
463 * Gets category UUID after it was deserialized by GSON
465 * @return category UUID
467 public LxUuid getCategoryUuid() {
472 * Gets a GSON object for reuse
474 * @return GSON object
477 if (config == null) {
478 logger.error("Attempt to get GSON from not finalized configuration!");
481 return config.thingHandler.getGson();
485 * Adds a new control in the framework. Called when a control is dynamically created based on some control's state
486 * changes from the Miniserver.
488 * @param control a new control to be created
490 static void addControl(LxControl control) {
491 control.config.thingHandler.addControl(control);
495 * Removes a control from the framework. Called when a control is dynamically deleted based on some control's state
496 * changes from the Miniserver.
498 * @param control a control to be removed
500 static void removeControl(LxControl control) {
501 control.config.thingHandler.removeControl(control);
506 * Gets control's configuration
508 * @return configuration
510 LxControlConfig getConfig() {
515 * Get control's room.
517 * @return control's room object
519 LxContainer getRoom() {
524 * Get control's category.
526 * @return control's category object
528 LxCategory getCategory() {
529 return config.category;
533 * Changes the channel state in the framework.
535 * @param id channel ID
536 * @param state new state value
538 void setChannelState(ChannelUID id, State state) {
539 if (config == null) {
540 logger.error("Attempt to set channel state with not finalized configuration!: {}", id);
543 config.thingHandler.setChannelState(id, state);
549 * Returns control label that will be used for building channel name. This allows for customizing the label per
550 * control by overriding this method, but keeping {@link LxControl#getName()} intact.
552 * @return control channel label
559 * Gets value of a state object of given name, if exists
561 * @param name name of state object
562 * @return state object's value
564 Double getStateDoubleValue(String name) {
565 LxState state = states.get(name);
567 Object value = state.getStateValue();
568 if (value instanceof Double) {
569 return (Double) value;
576 * Gets value of a state object of given name, if exists, and converts it to decimal type value.
578 * @param name state name
579 * @return state value
581 State getStateDecimalValue(String name) {
582 Double value = getStateDoubleValue(name);
584 return new DecimalType(value);
590 * Gets text value of a state object of given name, if exists
592 * @param name name of state object
593 * @return state object's text value
595 String getStateTextValue(String name) {
596 LxState state = states.get(name);
598 Object value = state.getStateValue();
599 if (value instanceof String) {
600 return (String) value;
607 * Gets text value of a state object of given name, if exists and converts it to string type
609 * @param name name of state object
610 * @return state object's text value
612 State getStateStringValue(String name) {
613 String value = getStateTextValue(name);
615 return new StringType(value);
621 * Gets double value of a state object of given name, if exists and converts it to switch type
623 * @param name name of state object
624 * @return state object's text value
626 State getStateOnOffValue(String name) {
627 Double value = getStateDoubleValue(name);
632 return OnOffType.OFF;
638 * Create a new channel and add it to the control. Channel ID is assigned automatically in the order of calls to
639 * this method, see (@link LxControl#getChannelId}.
641 * @param itemType item type for the channel
642 * @param typeId channel type ID for the channel
643 * @param channelLabel channel label
644 * @param channelDescription channel description
645 * @param tags tags for the channel or null if no tags needed
646 * @param commandCallback {@link LxControl} child class method that will be called when command is received
647 * @param stateCallback {@link LxControl} child class method that will be called to get state value
648 * @return channel ID of the added channel (can be used to later set state description to it)
650 ChannelUID addChannel(String itemType, ChannelTypeUID typeId, String channelLabel, String channelDescription,
651 Set<String> tags, CommandCallback commandCallback, StateCallback stateCallback) {
652 if (channelLabel == null || channelDescription == null) {
653 logger.error("Attempt to add channel with not finalized configuration!: {}", channelLabel);
656 ChannelUID channelId = getChannelId(numberOfChannels++);
657 ChannelBuilder builder = ChannelBuilder.create(channelId, itemType).withType(typeId).withLabel(channelLabel)
658 .withDescription(channelDescription + " : " + channelLabel);
660 builder.withDefaultTags(tags);
662 channels.add(builder.build());
663 if (commandCallback != null || stateCallback != null) {
664 callbacks.put(channelId, new Callbacks(commandCallback, stateCallback));
670 * Adds a new {@link StateDescriptionFragment} for a channel that has multiple options to select from or a custom
673 * @param channelId channel ID to add the description for
674 * @param descriptionFragment channel state description fragment
676 void addChannelStateDescriptionFragment(ChannelUID channelId, StateDescriptionFragment descriptionFragment) {
677 if (config == null) {
678 logger.error("Attempt to set channel state description with not finalized configuration!: {}", channelId);
680 if (channelId != null && descriptionFragment != null) {
681 config.thingHandler.setChannelStateDescription(channelId, descriptionFragment.toStateDescription());
687 * Sends an action command to the Miniserver using active socket connection
689 * @param action string with action command
690 * @throws IOException when communication error with Miniserver occurs
692 void sendAction(String action) throws IOException {
693 if (config == null) {
694 logger.error("Attempt to send command with not finalized configuration!: {}", action);
696 config.thingHandler.sendAction(uuid, action);
701 * Remove all channels from the control. This method is used by child classes that may decide to stop exposing any
702 * channels, for example by {@link LxControlMood}, which is based on {@link LxControlSwitch}, but sometime does not
703 * expose anything to the user.
705 void removeAllChannels() {
711 * Call when control is no more needed - unlink it from containers
713 private void dispose() {
714 if (config.room != null) {
715 config.room.removeControl(this);
717 if (config.category != null) {
718 config.category.removeControl(this);
720 subControls.values().forEach(control -> control.dispose());
724 * Build channel ID for the control, based on control's UUID, thing's UUID and index of the channel for the control
726 * @param index index of a channel within control (0 for primary channel) all indexes greater than 0 will have
727 * -index added to the channel ID
728 * @return channel ID for the control and index
730 private ChannelUID getChannelId(int index) {
731 if (config == null) {
732 logger.error("Attempt to get control's channel ID with not finalized configuration!: {}", index);
735 String controlId = uuid.toString();
737 controlId += "-" + index;
739 return new ChannelUID(config.thingHandler.getThingId(), controlId);