2 * Copyright (c) 2010-2020 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.insteon.internal.device;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.insteon.internal.message.FieldException;
22 import org.openhab.binding.insteon.internal.message.InvalidMessageTypeException;
23 import org.openhab.binding.insteon.internal.message.Msg;
24 import org.openhab.binding.insteon.internal.utils.Utils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * A PollHandler creates an Insteon message to query a particular
30 * DeviceFeature of an Insteon device.
32 * @author Bernd Pfrommer - Initial contribution
33 * @author Rob Nielsen - Port to openHAB 2 insteon binding
36 @SuppressWarnings("null")
37 public abstract class PollHandler {
38 private static final Logger logger = LoggerFactory.getLogger(PollHandler.class);
39 DeviceFeature feature;
40 Map<String, String> parameters = new HashMap<>();
45 * @param feature The device feature being polled
47 PollHandler(DeviceFeature feature) {
48 this.feature = feature;
52 * Creates Insteon message that can be used to poll a feature
53 * via the Insteon network.
55 * @param device reference to the insteon device to be polled
56 * @return Insteon query message or null if creation failed
58 public abstract @Nullable Msg makeMsg(InsteonDevice device);
60 public void setParameters(Map<String, String> hm) {
65 * Returns parameter as integer
67 * @param key key of parameter
69 * @return value of parameter
71 protected int getIntParameter(String key, int def) {
72 String val = parameters.get(key);
74 return (def); // param not found
78 ret = Utils.strToInt(val);
79 } catch (NumberFormatException e) {
80 logger.warn("malformed int parameter in command handler: {}", key);
86 * A flexible, parameterized poll handler that can generate
87 * most query messages. Provide the suitable parameters in
88 * the device features file.
91 public static class FlexPollHandler extends PollHandler {
92 FlexPollHandler(DeviceFeature f) {
97 public @Nullable Msg makeMsg(InsteonDevice d) {
99 int cmd1 = getIntParameter("cmd1", 0);
100 int cmd2 = getIntParameter("cmd2", 0);
101 int ext = getIntParameter("ext", -1);
103 if (ext == 1 || ext == 2) {
104 int d1 = getIntParameter("d1", 0);
105 int d2 = getIntParameter("d2", 0);
106 int d3 = getIntParameter("d3", 0);
107 m = d.makeExtendedMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2,
108 new byte[] { (byte) d1, (byte) d2, (byte) d3 });
111 } else if (ext == 2) {
115 m = d.makeStandardMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2);
117 m.setQuietTime(500L);
118 } catch (FieldException e) {
119 logger.warn("error setting field in msg: ", e);
120 } catch (InvalidMessageTypeException e) {
121 logger.warn("invalid message ", e);
128 public static class NoPollHandler extends PollHandler {
129 NoPollHandler(DeviceFeature f) {
134 public @Nullable Msg makeMsg(InsteonDevice d) {
140 * Factory method for creating handlers of a given name using java reflection
142 * @param ph the name of the handler to create
143 * @param f the feature for which to create the handler
144 * @return the handler which was created
147 public static <T extends PollHandler> T makeHandler(@Nullable HandlerEntry ph, DeviceFeature f) {
148 String cname = PollHandler.class.getName() + "$" + ph.getName();
150 Class<?> c = Class.forName(cname);
151 @SuppressWarnings("unchecked")
152 Class<? extends T> dc = (Class<? extends T>) c;
153 T phc = dc.getDeclaredConstructor(DeviceFeature.class).newInstance(f);
154 phc.setParameters(ph.getParams());
156 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
157 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
158 logger.warn("error trying to create message handler: {}", ph.getName(), e);