2 * Copyright (c) 2010-2023 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 static org.junit.jupiter.api.Assertions.*;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.nio.charset.StandardCharsets;
22 import java.util.HashMap;
23 import java.util.LinkedList;
25 import java.util.Queue;
26 import java.util.stream.Collectors;
28 import org.openhab.binding.loxone.internal.LxBindingConfiguration;
29 import org.openhab.binding.loxone.internal.LxServerHandlerApi;
30 import org.openhab.binding.loxone.internal.types.LxConfig;
31 import org.openhab.binding.loxone.internal.types.LxUuid;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.StateDescription;
37 import com.google.gson.Gson;
38 import com.google.gson.GsonBuilder;
41 * Dummy implementation of thing handler and its API towards controls.
43 * @author Pawel Pieczul - initial contribution
46 public class LxServerHandlerDummy implements LxServerHandlerApi {
50 LxBindingConfiguration bindingConfig = new LxBindingConfiguration();
52 Queue<String> actionQueue = new LinkedList<>();
54 Map<LxUuid, LxControl> controls;
55 Map<LxUuid, LxControl> extraControls = new HashMap<>();
56 Map<ChannelUID, StateDescription> stateDescriptions = new HashMap<>();
58 public LxServerHandlerDummy() {
59 GsonBuilder builder = new GsonBuilder();
60 builder.registerTypeAdapter(LxUuid.class, LxUuid.DESERIALIZER);
61 builder.registerTypeAdapter(LxControl.class, LxControl.DESERIALIZER);
62 gson = builder.create();
65 void loadConfiguration() {
66 InputStream stream = LxServerHandlerDummy.class.getResourceAsStream("LoxAPP3.json");
67 assertNotNull(stream);
68 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
69 assertNotNull(reader);
70 String msg = reader.lines().collect(Collectors.joining(System.lineSeparator()));
73 stateDescriptions.clear();
75 LxConfig config = gson.fromJson(msg, LxConfig.class);
76 config.finalize(this);
77 controls = config.controls;
78 assertNotNull(controls);
82 public void sendAction(LxUuid id, String operation) throws IOException {
83 actionQueue.add(id + "/" + operation);
87 public void addControl(LxControl control) {
88 extraControls.put(control.getUuid(), control);
92 public void removeControl(LxControl control) {
93 LxControl ctrl = extraControls.remove(control.getUuid());
98 public void setChannelState(ChannelUID channelId, State state) {
102 public void setChannelStateDescription(ChannelUID channelId, StateDescription description) {
103 assertNotNull(channelId);
104 assertNotNull(description);
105 stateDescriptions.put(channelId, description);
109 public String getSetting(String name) {
114 public void setSettings(Map<String, String> properties) {
118 public Gson getGson() {
123 public ThingUID getThingId() {
124 return new ThingUID("loxone:miniserver:12345678");