2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.novafinedust.internal.sds011protocol;
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;
25 * Factory for creating the specific reply instances for data received from the sensor
27 * @author Stefan Triller - Initial contribution
31 public class ReplyFactory {
33 private static final byte COMMAND_REPLY = (byte) 0xC5;
34 private static final byte DATA_REPLY = (byte) 0xC0;
36 private ReplyFactory() {
40 * Creates the specific reply message according to the commandID and first data byte
42 * @param bytes the received message
43 * @return a specific instance of a sensor reply message
45 public static @Nullable SensorReply create(byte[] bytes) {
46 if (bytes.length != 10) {
50 byte commandID = bytes[1];
51 byte firstDataByte = bytes[2];
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);
60 return new ModeReply(bytes);
62 return new SleepReply(bytes);
64 return new SensorReply(bytes);
66 } else if (commandID == DATA_REPLY) {
67 return new SensorMeasuredDataReply(bytes);