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.hdanywhere.internal.handler;
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;
21 import java.util.Properties;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
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;
37 import com.google.gson.Gson;
38 import com.google.gson.reflect.TypeToken;
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
44 * @author Karel Goderis - Initial contribution
46 public class Mhub4K431Handler extends BaseThingHandler {
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";
53 private final Logger logger = LoggerFactory.getLogger(Mhub4K431Handler.class);
55 private ScheduledFuture<?> pollingJob;
56 protected final Gson gson = new Gson();
58 private final int timeout = 5000;
59 private final int numberOfPorts = 4;
61 public Mhub4K431Handler(Thing thing) {
66 public void initialize() {
67 logger.debug("Initializing HDanywhere MHUB 4K (4×3+1) matrix handler.");
69 if (pollingJob == null || pollingJob.isCancelled()) {
70 int pollingInterval = ((BigDecimal) getConfig().get(POLLING_INTERVAL)).intValue();
71 pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 1, pollingInterval, TimeUnit.SECONDS);
73 updateStatus(ThingStatus.UNKNOWN);
77 public void dispose() {
78 logger.debug("Disposing HDanywhere matrix handler.");
79 if (pollingJob != null && !pollingJob.isCancelled()) {
80 pollingJob.cancel(true);
85 private Runnable pollingRunnable = () -> {
87 String host = (String) getConfig().get(IP_ADDRESS);
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));
94 String response = HttpUtil.executeUrl(httpMethod, url, null, stream, null, timeout);
95 response = response.trim();
96 response = response.substring(1, response.length() - 1);
98 if (response != null) {
99 updateStatus(ThingStatus.ONLINE);
101 java.lang.reflect.Type type = new TypeToken<Map<String, String>>() {
103 Map<String, String> map = gson.fromJson(response, type);
105 String inputChannel = map.get("Inputchannel");
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);
112 updateStatus(ThingStatus.OFFLINE);
114 } catch (Exception e) {
115 logger.debug("An exception occurred while polling the HDanwywhere matrix: '{}'", e.getMessage());
116 updateStatus(ThingStatus.OFFLINE);
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);
126 String channelID = channelUID.getId();
128 String host = (String) getConfig().get(IP_ADDRESS);
129 int sourcePort = Integer.valueOf(command.toString());
130 int outputPort = Port.get(channelID).toNumber();
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 });
141 String httpMethod = "POST";
142 String url = "http://" + host + "/cgi-bin/MMX32_Keyvalue.cgi";
144 String content = "{CMD=";
145 content = content + command.toString() + "B";
146 content = content + String.valueOf(outputPort) + ".";
148 InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
150 Properties httpHeaders = new Properties();
151 httpHeaders.setProperty("Cookie", "logintype-88=01");
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);