]> git.basschouten.com Git - openhab-addons.git/blob
60c5f017de75608b2af8a26837c260b0d8e66015
[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.binding.loxone.internal.controls;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
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;
24 import java.util.Map;
25 import java.util.Queue;
26 import java.util.stream.Collectors;
27
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;
36
37 import com.google.gson.Gson;
38 import com.google.gson.GsonBuilder;
39
40 /**
41  * Dummy implementation of thing handler and its API towards controls.
42  *
43  * @author Pawel Pieczul - initial contribution
44  *
45  */
46 public class LxServerHandlerDummy implements LxServerHandlerApi {
47
48     Gson gson;
49     LxConfig config;
50     LxBindingConfiguration bindingConfig = new LxBindingConfiguration();
51
52     Queue<String> actionQueue = new LinkedList<>();
53
54     Map<LxUuid, LxControl> controls;
55     Map<LxUuid, LxControl> extraControls = new HashMap<>();
56     Map<ChannelUID, StateDescription> stateDescriptions = new HashMap<>();
57
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();
63     }
64
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()));
71         assertNotNull(msg);
72
73         stateDescriptions.clear();
74
75         LxConfig config = gson.fromJson(msg, LxConfig.class);
76         config.finalize(this);
77         controls = config.controls;
78         assertNotNull(controls);
79     }
80
81     @Override
82     public void sendAction(LxUuid id, String operation) throws IOException {
83         actionQueue.add(id + "/" + operation);
84     }
85
86     @Override
87     public void addControl(LxControl control) {
88         extraControls.put(control.getUuid(), control);
89     }
90
91     @Override
92     public void removeControl(LxControl control) {
93         LxControl ctrl = extraControls.remove(control.getUuid());
94         assertNotNull(ctrl);
95     }
96
97     @Override
98     public void setChannelState(ChannelUID channelId, State state) {
99     }
100
101     @Override
102     public void setChannelStateDescription(ChannelUID channelId, StateDescription description) {
103         assertNotNull(channelId);
104         assertNotNull(description);
105         stateDescriptions.put(channelId, description);
106     }
107
108     @Override
109     public String getSetting(String name) {
110         return null;
111     }
112
113     @Override
114     public void setSettings(Map<String, String> properties) {
115     }
116
117     @Override
118     public Gson getGson() {
119         return gson;
120     }
121
122     @Override
123     public ThingUID getThingId() {
124         return new ThingUID("loxone:miniserver:12345678");
125     }
126 }