]> git.basschouten.com Git - openhab-addons.git/blob
0a3ec7d47bea66bc5828d57a09c0794341d108f2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.rfxcom.internal.messages;
14
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComMessageNotImplementedException;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
24
25 /**
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 public class RFXComMessageFactory {
30
31     @SuppressWarnings("serial")
32     private static final Map<PacketType, Class<? extends RFXComMessage>> MESSAGE_CLASSES = Collections
33             .unmodifiableMap(new HashMap<PacketType, Class<? extends RFXComMessage>>() {
34                 {
35                     put(PacketType.INTERFACE_CONTROL, RFXComInterfaceControlMessage.class);
36                     put(PacketType.INTERFACE_MESSAGE, RFXComInterfaceMessage.class);
37                     put(PacketType.TRANSMITTER_MESSAGE, RFXComTransmitterMessage.class);
38                     put(PacketType.UNDECODED_RF_MESSAGE, RFXComUndecodedRFMessage.class);
39                     put(PacketType.LIGHTING1, RFXComLighting1Message.class);
40                     put(PacketType.LIGHTING2, RFXComLighting2Message.class);
41                     // put(PacketType.LIGHTING3, RFXComLighting3Message.class);
42                     put(PacketType.LIGHTING4, RFXComLighting4Message.class);
43                     put(PacketType.LIGHTING5, RFXComLighting5Message.class);
44                     put(PacketType.LIGHTING6, RFXComLighting6Message.class);
45                     put(PacketType.CHIME, RFXComChimeMessage.class);
46                     put(PacketType.FAN, RFXComFanMessage.class);
47                     // put(PacketType.FAN_SF01, RFXComFanMessage.class);
48                     // put(PacketType.FAN_ITHO, RFXComFanMessage.class);
49                     // put(PacketType.FAN_SEAV, RFXComFanMessage.class);
50                     put(PacketType.FAN_LUCCI_DC, RFXComFanMessage.class);
51                     // put(PacketType.FAN_FT1211R, RFXComFanMessage.class);
52                     put(PacketType.FAN_FALMEC, RFXComFanMessage.class);
53                     put(PacketType.FAN_LUCCI_DC_II, RFXComFanMessage.class);
54                     put(PacketType.CURTAIN1, RFXComCurtain1Message.class);
55                     put(PacketType.BLINDS1, RFXComBlinds1Message.class);
56                     put(PacketType.RFY, RFXComRfyMessage.class);
57                     put(PacketType.HOME_CONFORT, RFXComHomeConfortMessage.class);
58                     put(PacketType.SECURITY1, RFXComSecurity1Message.class);
59                     put(PacketType.SECURITY2, RFXComSecurity2Message.class);
60                     // put(PacketType.CAMERA1, RFXComCamera1Message.class);
61                     // put(PacketType.REMOTE_CONTROL, RFXComRemoteControlMessage.class);
62                     put(PacketType.THERMOSTAT1, RFXComThermostat1Message.class);
63                     // put(PacketType.THERMOSTAT2, RFXComThermostat2Message.class);
64                     put(PacketType.THERMOSTAT3, RFXComThermostat3Message.class);
65                     // put(PacketType.RADIATOR1, RFXComRadiator1Message.class);
66                     put(PacketType.BBQ, RFXComBBQTemperatureMessage.class);
67                     put(PacketType.TEMPERATURE_RAIN, RFXComTemperatureRainMessage.class);
68                     put(PacketType.TEMPERATURE, RFXComTemperatureMessage.class);
69                     put(PacketType.HUMIDITY, RFXComHumidityMessage.class);
70                     put(PacketType.TEMPERATURE_HUMIDITY, RFXComTemperatureHumidityMessage.class);
71                     // put(PacketType.BAROMETRIC, RFXComBarometricMessage.class);
72                     put(PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC, RFXComTemperatureHumidityBarometricMessage.class);
73                     put(PacketType.RAIN, RFXComRainMessage.class);
74                     put(PacketType.WIND, RFXComWindMessage.class);
75                     put(PacketType.UV, RFXComUVMessage.class);
76                     put(PacketType.DATE_TIME, RFXComDateTimeMessage.class);
77                     put(PacketType.CURRENT, RFXComCurrentMessage.class);
78                     put(PacketType.ENERGY, RFXComEnergyMessage.class);
79                     put(PacketType.CURRENT_ENERGY, RFXComCurrentEnergyMessage.class);
80                     // put(PacketType.POWER, RFXComPowerMessage.class);
81                     // put(PacketType.WEIGHT, RFXComWeightMessage.class);
82                     // put(PacketType.GAS, RFXComGasMessage.class);
83                     // put(PacketType.WATER, RFXComWaterMessage.class);
84                     put(PacketType.RFXSENSOR, RFXComRFXSensorMessage.class);
85                     // put(PacketType.RFXMETER, RFXComRFXMeterMessage.class);
86                     // put(PacketType.FS20, RFXComFS20Message.class);
87                     put(PacketType.RAW, RFXComRawMessage.class);
88                     // put(PacketType.IO_LINES, RFXComIOLinesMessage.class);
89                 }
90             });
91
92     /**
93      * Command to reset RFXCOM controller.
94      *
95      */
96     public static final byte[] CMD_RESET = new byte[] { 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97             0x00, 0x00, 0x00, 0x00 };
98
99     /**
100      * Command to get RFXCOM controller status.
101      *
102      */
103     public static final byte[] CMD_GET_STATUS = new byte[] { 0x0D, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
104             0x00, 0x00, 0x00, 0x00 };
105
106     /**
107      * Command to save RFXCOM controller configuration.
108      *
109      */
110     public static final byte[] CMD_SAVE = new byte[] { 0x0D, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111             0x00, 0x00, 0x00 };
112
113     /**
114      * Command to start RFXCOM receiver.
115      *
116      */
117     public static final byte[] CMD_START_RECEIVER = new byte[] { 0x0D, 0x00, 0x00, 0x03, 0x07, 0x00, 0x00, 0x00, 0x00,
118             0x00, 0x00, 0x00, 0x00, 0x00 };
119
120     public static RFXComMessage createMessage(PacketType packetType) throws RFXComException {
121         try {
122             Class<? extends RFXComMessage> cl = MESSAGE_CLASSES.get(packetType);
123             if (cl == null) {
124                 throw new RFXComMessageNotImplementedException("Message " + packetType + " not implemented");
125             }
126             return cl.newInstance();
127         } catch (IllegalAccessException | InstantiationException e) {
128             throw new RFXComException(e);
129         }
130     }
131
132     public static RFXComMessage createMessage(byte[] packet) throws RFXComException {
133         PacketType packetType = ByteEnumUtil.fromByte(PacketType.class, packet[1]);
134
135         try {
136             Class<? extends RFXComMessage> cl = MESSAGE_CLASSES.get(packetType);
137             if (cl == null) {
138                 throw new RFXComMessageNotImplementedException("Message " + packetType + " not implemented");
139             }
140             Constructor<?> c = cl.getConstructor(byte[].class);
141             return (RFXComMessage) c.newInstance(packet);
142         } catch (InvocationTargetException e) {
143             if (e.getCause() instanceof RFXComException) {
144                 throw (RFXComException) e.getCause();
145             } else {
146                 throw new RFXComException(e);
147             }
148         } catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
149             throw new RFXComException(e);
150         }
151     }
152
153     public static PacketType convertPacketType(String packetType) throws IllegalArgumentException {
154         for (PacketType p : PacketType.values()) {
155             if (p.toString().replace("_", "").equals(packetType.replace("_", ""))) {
156                 return p;
157             }
158         }
159
160         throw new IllegalArgumentException("Unknown packet type " + packetType);
161     }
162 }