]> git.basschouten.com Git - openhab-addons.git/blob
231d79aa2ad07b7b4fbb406a6a06595b6f5829c3
[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.FAN_NOVY, RFXComFanMessage.class);
55                     put(PacketType.CURTAIN1, RFXComCurtain1Message.class);
56                     put(PacketType.BLINDS1, RFXComBlinds1Message.class);
57                     put(PacketType.RFY, RFXComRfyMessage.class);
58                     put(PacketType.HOME_CONFORT, RFXComHomeConfortMessage.class);
59                     put(PacketType.SECURITY1, RFXComSecurity1Message.class);
60                     put(PacketType.SECURITY2, RFXComSecurity2Message.class);
61                     // put(PacketType.CAMERA1, RFXComCamera1Message.class);
62                     // put(PacketType.REMOTE_CONTROL, RFXComRemoteControlMessage.class);
63                     put(PacketType.THERMOSTAT1, RFXComThermostat1Message.class);
64                     // put(PacketType.THERMOSTAT2, RFXComThermostat2Message.class);
65                     put(PacketType.THERMOSTAT3, RFXComThermostat3Message.class);
66                     // put(PacketType.RADIATOR1, RFXComRadiator1Message.class);
67                     put(PacketType.BBQ, RFXComBBQTemperatureMessage.class);
68                     put(PacketType.TEMPERATURE_RAIN, RFXComTemperatureRainMessage.class);
69                     put(PacketType.TEMPERATURE, RFXComTemperatureMessage.class);
70                     put(PacketType.HUMIDITY, RFXComHumidityMessage.class);
71                     put(PacketType.TEMPERATURE_HUMIDITY, RFXComTemperatureHumidityMessage.class);
72                     // put(PacketType.BAROMETRIC, RFXComBarometricMessage.class);
73                     put(PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC, RFXComTemperatureHumidityBarometricMessage.class);
74                     put(PacketType.RAIN, RFXComRainMessage.class);
75                     put(PacketType.WIND, RFXComWindMessage.class);
76                     put(PacketType.UV, RFXComUVMessage.class);
77                     put(PacketType.DATE_TIME, RFXComDateTimeMessage.class);
78                     put(PacketType.CURRENT, RFXComCurrentMessage.class);
79                     put(PacketType.ENERGY, RFXComEnergyMessage.class);
80                     put(PacketType.CURRENT_ENERGY, RFXComCurrentEnergyMessage.class);
81                     // put(PacketType.POWER, RFXComPowerMessage.class);
82                     // put(PacketType.WEIGHT, RFXComWeightMessage.class);
83                     // put(PacketType.GAS, RFXComGasMessage.class);
84                     // put(PacketType.WATER, RFXComWaterMessage.class);
85                     put(PacketType.RFXSENSOR, RFXComRFXSensorMessage.class);
86                     // put(PacketType.RFXMETER, RFXComRFXMeterMessage.class);
87                     // put(PacketType.FS20, RFXComFS20Message.class);
88                     put(PacketType.RAW, RFXComRawMessage.class);
89                     // put(PacketType.IO_LINES, RFXComIOLinesMessage.class);
90                 }
91             });
92
93     /**
94      * Command to reset RFXCOM controller.
95      *
96      */
97     public static final byte[] CMD_RESET = new byte[] { 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98             0x00, 0x00, 0x00, 0x00 };
99
100     /**
101      * Command to get RFXCOM controller status.
102      *
103      */
104     public static final byte[] CMD_GET_STATUS = new byte[] { 0x0D, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
105             0x00, 0x00, 0x00, 0x00 };
106
107     /**
108      * Command to save RFXCOM controller configuration.
109      *
110      */
111     public static final byte[] CMD_SAVE = new byte[] { 0x0D, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112             0x00, 0x00, 0x00 };
113
114     /**
115      * Command to start RFXCOM receiver.
116      *
117      */
118     public static final byte[] CMD_START_RECEIVER = new byte[] { 0x0D, 0x00, 0x00, 0x03, 0x07, 0x00, 0x00, 0x00, 0x00,
119             0x00, 0x00, 0x00, 0x00, 0x00 };
120
121     public static RFXComMessage createMessage(PacketType packetType) throws RFXComException {
122         try {
123             Class<? extends RFXComMessage> cl = MESSAGE_CLASSES.get(packetType);
124             if (cl == null) {
125                 throw new RFXComMessageNotImplementedException("Message " + packetType + " not implemented");
126             }
127             return cl.newInstance();
128         } catch (IllegalAccessException | InstantiationException e) {
129             throw new RFXComException(e);
130         }
131     }
132
133     public static RFXComMessage createMessage(byte[] packet) throws RFXComException {
134         PacketType packetType = ByteEnumUtil.fromByte(PacketType.class, packet[1]);
135
136         try {
137             Class<? extends RFXComMessage> cl = MESSAGE_CLASSES.get(packetType);
138             if (cl == null) {
139                 throw new RFXComMessageNotImplementedException("Message " + packetType + " not implemented");
140             }
141             Constructor<?> c = cl.getConstructor(byte[].class);
142             return (RFXComMessage) c.newInstance(packet);
143         } catch (InvocationTargetException e) {
144             if (e.getCause() instanceof RFXComException) {
145                 throw (RFXComException) e.getCause();
146             } else {
147                 throw new RFXComException(e);
148             }
149         } catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
150             throw new RFXComException(e);
151         }
152     }
153
154     public static PacketType convertPacketType(String packetType) throws IllegalArgumentException {
155         for (PacketType p : PacketType.values()) {
156             if (p.toString().replace("_", "").equals(packetType.replace("_", ""))) {
157                 return p;
158             }
159         }
160
161         throw new IllegalArgumentException("Unknown packet type " + packetType);
162     }
163 }