]> git.basschouten.com Git - openhab-addons.git/blob
0c05fe52fe233b4a7d6f9041d2a93cbef810e8a7
[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.novafinedust.internal.sds011protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.ModeReply;
18 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.SensorFirmwareReply;
19 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.SensorMeasuredDataReply;
20 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.SensorReply;
21 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.SleepReply;
22 import org.openhab.binding.novafinedust.internal.sds011protocol.messages.WorkingPeriodReply;
23
24 /**
25  * Factory for creating the specific reply instances for data received from the sensor
26  *
27  * @author Stefan Triller - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class ReplyFactory {
32
33     private static final byte COMMAND_REPLY = (byte) 0xC5;
34     private static final byte DATA_REPLY = (byte) 0xC0;
35
36     private ReplyFactory() {
37     }
38
39     /**
40      * Creates the specific reply message according to the commandID and first data byte
41      *
42      * @param bytes the received message
43      * @return a specific instance of a sensor reply message
44      */
45     public static @Nullable SensorReply create(byte[] bytes) {
46         if (bytes.length != 10) {
47             return null;
48         }
49
50         byte commandID = bytes[1];
51         byte firstDataByte = bytes[2];
52
53         if (commandID == COMMAND_REPLY) {
54             switch (firstDataByte) {
55                 case Command.FIRMWARE:
56                     return new SensorFirmwareReply(bytes);
57                 case Command.WORKING_PERIOD:
58                     return new WorkingPeriodReply(bytes);
59                 case Command.MODE:
60                     return new ModeReply(bytes);
61                 case Command.SLEEP:
62                     return new SleepReply(bytes);
63                 default:
64                     return new SensorReply(bytes);
65             }
66         } else if (commandID == DATA_REPLY) {
67             return new SensorMeasuredDataReply(bytes);
68         }
69         return null;
70     }
71 }