]> git.basschouten.com Git - openhab-addons.git/blob
939aacb8dc0f03151ca79d8db23680df81667d0b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal.device;
14
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.HashMap;
17 import java.util.Map;
18
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;
27
28 /**
29  * A PollHandler creates an Insteon message to query a particular
30  * DeviceFeature of an Insteon device.
31  *
32  * @author Bernd Pfrommer - Initial contribution
33  * @author Rob Nielsen - Port to openHAB 2 insteon binding
34  */
35 @NonNullByDefault
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<>();
40
41     /**
42      * Constructor
43      *
44      * @param feature The device feature being polled
45      */
46     PollHandler(DeviceFeature feature) {
47         this.feature = feature;
48     }
49
50     /**
51      * Creates Insteon message that can be used to poll a feature
52      * via the Insteon network.
53      *
54      * @param device reference to the insteon device to be polled
55      * @return Insteon query message or null if creation failed
56      */
57     public abstract @Nullable Msg makeMsg(InsteonDevice device);
58
59     public void setParameters(Map<String, String> hm) {
60         parameters = hm;
61     }
62
63     /**
64      * Returns parameter as integer
65      *
66      * @param key key of parameter
67      * @param def default
68      * @return value of parameter
69      */
70     protected int getIntParameter(String key, int def) {
71         String val = parameters.get(key);
72         if (val == null) {
73             return (def); // param not found
74         }
75         int ret = def;
76         try {
77             ret = Utils.strToInt(val);
78         } catch (NumberFormatException e) {
79             logger.warn("malformed int parameter in command handler: {}", key);
80         }
81         return ret;
82     }
83
84     /**
85      * A flexible, parameterized poll handler that can generate
86      * most query messages. Provide the suitable parameters in
87      * the device features file.
88      */
89     public static class FlexPollHandler extends PollHandler {
90         FlexPollHandler(DeviceFeature f) {
91             super(f);
92         }
93
94         @Override
95         public @Nullable Msg makeMsg(InsteonDevice d) {
96             Msg m = null;
97             int cmd1 = getIntParameter("cmd1", 0);
98             int cmd2 = getIntParameter("cmd2", 0);
99             int ext = getIntParameter("ext", -1);
100             try {
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 });
107                     if (ext == 1) {
108                         m.setCRC();
109                     } else if (ext == 2) {
110                         m.setCRC2();
111                     }
112                 } else {
113                     m = d.makeStandardMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2);
114                 }
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);
120             }
121             return m;
122         }
123     }
124
125     public static class NoPollHandler extends PollHandler {
126         NoPollHandler(DeviceFeature f) {
127             super(f);
128         }
129
130         @Override
131         public @Nullable Msg makeMsg(InsteonDevice d) {
132             return null;
133         }
134     }
135
136     /**
137      * Factory method for creating handlers of a given name using java reflection
138      *
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
142      */
143     @Nullable
144     public static <T extends PollHandler> T makeHandler(HandlerEntry ph, DeviceFeature f) {
145         String cname = PollHandler.class.getName() + "$" + ph.getName();
146         try {
147             Class<?> c = Class.forName(cname);
148             @SuppressWarnings("unchecked")
149             Class<? extends T> dc = (Class<? extends T>) c;
150             @Nullable
151             T phc = dc.getDeclaredConstructor(DeviceFeature.class).newInstance(f);
152             phc.setParameters(ph.getParams());
153             return phc;
154         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
155                 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
156             logger.warn("error trying to create message handler: {}", ph.getName(), e);
157         }
158         return null;
159     }
160 }