]> git.basschouten.com Git - openhab-addons.git/blob
14464b221411780711e90596e48dca68f1621278
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 @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<>();
41
42     /**
43      * Constructor
44      *
45      * @param feature The device feature being polled
46      */
47     PollHandler(DeviceFeature feature) {
48         this.feature = feature;
49     }
50
51     /**
52      * Creates Insteon message that can be used to poll a feature
53      * via the Insteon network.
54      *
55      * @param device reference to the insteon device to be polled
56      * @return Insteon query message or null if creation failed
57      */
58     public abstract @Nullable Msg makeMsg(InsteonDevice device);
59
60     public void setParameters(Map<String, String> hm) {
61         parameters = hm;
62     }
63
64     /**
65      * Returns parameter as integer
66      *
67      * @param key key of parameter
68      * @param def default
69      * @return value of parameter
70      */
71     protected int getIntParameter(String key, int def) {
72         String val = parameters.get(key);
73         if (val == null) {
74             return (def); // param not found
75         }
76         int ret = def;
77         try {
78             ret = Utils.strToInt(val);
79         } catch (NumberFormatException e) {
80             logger.warn("malformed int parameter in command handler: {}", key);
81         }
82         return ret;
83     }
84
85     /**
86      * A flexible, parameterized poll handler that can generate
87      * most query messages. Provide the suitable parameters in
88      * the device features file.
89      */
90     @NonNullByDefault
91     public static class FlexPollHandler extends PollHandler {
92         FlexPollHandler(DeviceFeature f) {
93             super(f);
94         }
95
96         @Override
97         public @Nullable Msg makeMsg(InsteonDevice d) {
98             Msg m = null;
99             int cmd1 = getIntParameter("cmd1", 0);
100             int cmd2 = getIntParameter("cmd2", 0);
101             int ext = getIntParameter("ext", -1);
102             try {
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 });
109                     if (ext == 1) {
110                         m.setCRC();
111                     } else if (ext == 2) {
112                         m.setCRC2();
113                     }
114                 } else {
115                     m = d.makeStandardMessage((byte) 0x0f, (byte) cmd1, (byte) cmd2);
116                 }
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);
122             }
123             return m;
124         }
125     }
126
127     @NonNullByDefault
128     public static class NoPollHandler extends PollHandler {
129         NoPollHandler(DeviceFeature f) {
130             super(f);
131         }
132
133         @Override
134         public @Nullable Msg makeMsg(InsteonDevice d) {
135             return null;
136         }
137     }
138
139     /**
140      * Factory method for creating handlers of a given name using java reflection
141      *
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
145      */
146     @Nullable
147     public static <T extends PollHandler> T makeHandler(@Nullable HandlerEntry ph, DeviceFeature f) {
148         String cname = PollHandler.class.getName() + "$" + ph.getName();
149         try {
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());
155             return phc;
156         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
157                 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
158             logger.warn("error trying to create message handler: {}", ph.getName(), e);
159         }
160         return null;
161     }
162 }