]> git.basschouten.com Git - openhab-addons.git/blob
559496a7a456098e3dbd96a5816a06907168c182
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nibeheatpump.internal.message;
14
15 import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
16 import org.openhab.binding.nibeheatpump.internal.message.NibeHeatPumpBaseMessage.MessageType;
17 import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocol;
18
19 /**
20  * The {@link MessageFactory} implements factory class to create Nibe protocol messages.
21  *
22  *
23  * @author Pauli Anttila - Initial contribution
24  */
25 public class MessageFactory {
26
27     public static NibeHeatPumpMessage getMessage(byte[] message) throws NibeHeatPumpException {
28         if (message != null) {
29             byte messageTypeByte = NibeHeatPumpProtocol.getMessageType(message);
30             MessageType messageType = NibeHeatPumpBaseMessage.getMessageType(messageTypeByte);
31
32             switch (messageType) {
33                 case MODBUS_DATA_READ_OUT_MSG:
34                     return new ModbusDataReadOutMessage(message);
35                 case MODBUS_READ_REQUEST_MSG:
36                     return new ModbusReadRequestMessage(message);
37                 case MODBUS_READ_RESPONSE_MSG:
38                     return new ModbusReadResponseMessage(message);
39                 case MODBUS_WRITE_REQUEST_MSG:
40                     return new ModbusWriteRequestMessage(message);
41                 case MODBUS_WRITE_RESPONSE_MSG:
42                     return new ModbusWriteResponseMessage(message);
43                 default:
44                     return null;
45             }
46         }
47
48         throw new NibeHeatPumpException("Illegal message (null)");
49     }
50 }