]> git.basschouten.com Git - openhab-addons.git/blob
a29c53d43c7ef5b6a75256ea517314029b6d5b7f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.hdanywhere.internal.handler;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.math.BigDecimal;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24
25 import org.openhab.binding.hdanywhere.internal.HDanywhereBindingConstants.Port;
26 import org.openhab.core.io.net.http.HttpUtil;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.gson.Gson;
38 import com.google.gson.reflect.TypeToken;
39
40 /**
41  * The {@link Mhub4K431Handler} is responsible for handling commands, which are
42  * sent to one of the channels. It supports the MHUB 4K (4×3+1) matrix
43  *
44  * @author Karel Goderis - Initial contribution
45  */
46 public class Mhub4K431Handler extends BaseThingHandler {
47
48     // List of Configurations constants
49     public static final String IP_ADDRESS = "ipAddress";
50     // public static final String PORTS = "ports";
51     public static final String POLLING_INTERVAL = "interval";
52
53     private final Logger logger = LoggerFactory.getLogger(Mhub4K431Handler.class);
54
55     private ScheduledFuture<?> pollingJob;
56     protected final Gson gson = new Gson();
57
58     private final int timeout = 5000;
59     private final int numberOfPorts = 4;
60
61     public Mhub4K431Handler(Thing thing) {
62         super(thing);
63     }
64
65     @Override
66     public void initialize() {
67         logger.debug("Initializing HDanywhere MHUB 4K (4×3+1) matrix handler.");
68
69         if (pollingJob == null || pollingJob.isCancelled()) {
70             int pollingInterval = ((BigDecimal) getConfig().get(POLLING_INTERVAL)).intValue();
71             pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 1, pollingInterval, TimeUnit.SECONDS);
72         }
73         updateStatus(ThingStatus.UNKNOWN);
74     }
75
76     @Override
77     public void dispose() {
78         logger.debug("Disposing HDanywhere matrix handler.");
79         if (pollingJob != null && !pollingJob.isCancelled()) {
80             pollingJob.cancel(true);
81             pollingJob = null;
82         }
83     }
84
85     private Runnable pollingRunnable = () -> {
86         try {
87             String host = (String) getConfig().get(IP_ADDRESS);
88
89             String httpMethod = "POST";
90             String url = "http://" + host + "/cgi-bin/MUH44TP_getsetparams.cgi";
91             String content = "{tag:ptn}";
92             InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
93
94             String response = HttpUtil.executeUrl(httpMethod, url, null, stream, null, timeout);
95             response = response.trim();
96             response = response.substring(1, response.length() - 1);
97
98             if (response != null) {
99                 updateStatus(ThingStatus.ONLINE);
100
101                 java.lang.reflect.Type type = new TypeToken<Map<String, String>>() {
102                 }.getType();
103                 Map<String, String> map = gson.fromJson(response, type);
104
105                 String inputChannel = map.get("Inputchannel");
106
107                 for (int i = 0; i < numberOfPorts; i++) {
108                     DecimalType decimalType = new DecimalType(String.valueOf(inputChannel.charAt(i)));
109                     updateState(new ChannelUID(getThing().getUID(), Port.get(i + 1).channelID()), decimalType);
110                 }
111             } else {
112                 updateStatus(ThingStatus.OFFLINE);
113             }
114         } catch (Exception e) {
115             logger.debug("An exception occurred while polling the HDanwywhere matrix: '{}'", e.getMessage());
116             updateStatus(ThingStatus.OFFLINE);
117         }
118     };
119
120     @Override
121     public void handleCommand(ChannelUID channelUID, Command command) {
122         if (command instanceof RefreshType) {
123             // Simply schedule a single run of the polling runnable to refresh all channels
124             scheduler.schedule(pollingRunnable, 0, TimeUnit.SECONDS);
125         } else {
126             String channelID = channelUID.getId();
127
128             String host = (String) getConfig().get(IP_ADDRESS);
129             int sourcePort = Integer.valueOf(command.toString());
130             int outputPort = Port.get(channelID).toNumber();
131
132             if (sourcePort > numberOfPorts) {
133                 // nice try - we can switch to a port that does not physically exist
134                 logger.warn("Source port {} goes beyond the physical number of {} ports available on the matrix {}",
135                         new Object[] { sourcePort, numberOfPorts, host });
136             } else if (outputPort > numberOfPorts) {
137                 // nice try - we can switch to a port that does not physically exist
138                 logger.warn("Output port {} goes beyond the physical number of {} ports available on the matrix {}",
139                         new Object[] { outputPort, numberOfPorts, host });
140             } else {
141                 String httpMethod = "POST";
142                 String url = "http://" + host + "/cgi-bin/MMX32_Keyvalue.cgi";
143
144                 String content = "{CMD=";
145                 content = content + command.toString() + "B";
146                 content = content + String.valueOf(outputPort) + ".";
147
148                 InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
149
150                 Properties httpHeaders = new Properties();
151                 httpHeaders.setProperty("Cookie", "logintype-88=01");
152
153                 try {
154                     HttpUtil.executeUrl(httpMethod, url, httpHeaders, stream,
155                             "application/x-www-form-urlencoded; charset=UTF-8", timeout);
156                 } catch (IOException e) {
157                     logger.debug("Communication with device failed", e);
158                     updateStatus(ThingStatus.OFFLINE);
159                 }
160             }
161         }
162     }
163 }