2 * Copyright (c) 2010-2024 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 public abstract class PollHandler {
37 private static final Logger logger = LoggerFactory.getLogger(PollHandler.class);
38 DeviceFeature feature;
39 Map<String, String> parameters = new HashMap<>();
44 * @param feature The device feature being polled
46 PollHandler(DeviceFeature feature) {
47 this.feature = feature;
51 * Creates Insteon message that can be used to poll a feature
52 * via the Insteon network.
54 * @param device reference to the insteon device to be polled
55 * @return Insteon query message or null if creation failed
57 public abstract @Nullable Msg makeMsg(InsteonDevice device);
59 public void setParameters(Map<String, String> hm) {
64 * Returns parameter as integer
66 * @param key key of parameter
68 * @return value of parameter
70 protected int getIntParameter(String key, int def) {
71 String val = parameters.get(key);
73 return (def); // param not found
77 ret = Utils.strToInt(val);
78 } catch (NumberFormatException e) {
79 logger.warn("malformed int parameter in command handler: {}", key);
85 * A flexible, parameterized poll handler that can generate
86 * most query messages. Provide the suitable parameters in
87 * the device features file.
89 public static class FlexPollHandler extends PollHandler {
90 FlexPollHandler(DeviceFeature f) {
95 public @Nullable Msg makeMsg(InsteonDevice d) {
97 int cmd1 = getIntParameter("cmd1", 0);
98 int cmd2 = getIntParameter("cmd2", 0);
99 int ext = getIntParameter("ext", -1);
101 if (ext == 1 || ext == 2) {
102 int d1 = getIntParameter("d1", 0);
103 int d2 = getIntParameter("d2", 0);
104 int d3 = getIntParameter("d3", 0);
105 m = d.makeExtendedMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2,
106 new byte[] { (byte) d1, (byte) d2, (byte) d3 });
109 } else if (ext == 2) {
113 m = d.makeStandardMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2);
115 m.setQuietTime(500L);
116 } catch (FieldException e) {
117 logger.warn("error setting field in msg: ", e);
118 } catch (InvalidMessageTypeException e) {
119 logger.warn("invalid message ", e);
125 public static class NoPollHandler extends PollHandler {
126 NoPollHandler(DeviceFeature f) {
131 public @Nullable Msg makeMsg(InsteonDevice d) {
137 * Factory method for creating handlers of a given name using java reflection
139 * @param ph the name of the handler to create
140 * @param f the feature for which to create the handler
141 * @return the handler which was created
144 public static <T extends PollHandler> T makeHandler(HandlerEntry ph, DeviceFeature f) {
145 String cname = PollHandler.class.getName() + "$" + ph.getName();
147 Class<?> c = Class.forName(cname);
148 @SuppressWarnings("unchecked")
149 Class<? extends T> dc = (Class<? extends T>) c;
151 T phc = dc.getDeclaredConstructor(DeviceFeature.class).newInstance(f);
152 phc.setParameters(ph.getParams());
154 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
155 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
156 logger.warn("error trying to create message handler: {}", ph.getName(), e);