2 * Copyright (c) 2010-2022 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.fineoffsetweatherstation.internal.domain;
15 import java.util.Arrays;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.fineoffsetweatherstation.internal.Utils;
22 * The Commands supported by the gateway.
24 * @author Andreas Berger - Initial contribution
29 * send SSID and Password to WIFI module
31 CMD_WRITE_SSID((byte) 0x11, 1),
34 * UDP cast for device echo,answer back data size is 2 Bytes
36 CMD_BROADCAST((byte) 0x12, 2),
41 CMD_READ_ECOWITT((byte) 0x1E, 1),
44 * write back awt.net setting
46 CMD_WRITE_ECOWITT((byte) 0x1F, 1),
49 * read Wunderground setting
51 CMD_READ_WUNDERGROUND((byte) 0x20, 1),
54 * write back Wunderground setting
56 CMD_WRITE_WUNDERGROUND((byte) 0x21, 1),
59 * read WeatherObservationsWebsite setting
61 CMD_READ_WOW((byte) 0x22, 1),
64 * write back WeatherObservationsWebsite setting
66 CMD_WRITE_WOW((byte) 0x23, 1),
69 * read Weathercloud setting
71 CMD_READ_WEATHERCLOUD((byte) 0x24, 1),
74 * write back Weathercloud setting
76 CMD_WRITE_WEATHERCLOUD((byte) 0x25, 1),
81 CMD_READ_SATION_MAC((byte) 0x26, 1),
84 * read Customized sever setting
86 CMD_READ_CUSTOMIZED((byte) 0x2A, 1),
89 * write back Customized sever setting
91 CMD_WRITE_CUSTOMIZED((byte) 0x2B, 1),
96 CMD_WRITE_UPDATE((byte) 0x43, 1),
99 * read current firmware version number
101 CMD_READ_FIRMWARE_VERSION((byte) 0x50, 1),
103 CMD_READ_USR_PATH((byte) 0x51, 1),
105 CMD_WRITE_USR_PATH((byte) 0x52, 1),
107 // the following command is only valid for GW1000, WH2650 and wn1900
110 * read current data,reply data size is 2bytes.
112 CMD_GW1000_LIVEDATA((byte) 0x27, 2),
115 * read Soilmoisture Sensor calibration parameters
117 CMD_GET_SOILHUMIAD((byte) 0x28, 1),
120 * write back Soilmoisture Sensor calibration parameters
122 CMD_SET_SOILHUMIAD((byte) 0x29, 1),
125 * read multi channel sensor offset value
127 CMD_GET_MulCH_OFFSET((byte) 0x2C, 1),
130 * write back multi channel sensor OFFSET value
132 CMD_SET_MulCH_OFFSET((byte) 0x2D, 1),
135 * read PM2.5OFFSET calibration data
137 CMD_GET_PM25_OFFSET((byte) 0x2E, 1),
140 * writeback PM2.5OFFSET calibration data
142 CMD_SET_PM25_OFFSET((byte) 0x2F, 1),
147 CMD_READ_SSSS((byte) 0x30, 1),
150 * write back system info
152 CMD_WRITE_SSSS((byte) 0x31, 1),
157 CMD_READ_RAINDATA((byte) 0x34, 1),
160 * write back rain data
162 CMD_WRITE_RAINDATA((byte) 0x35, 1),
167 CMD_READ_GAIN((byte) 0x36, 1),
170 * write back rain gain
172 CMD_WRITE_GAIN((byte) 0x37, 1),
175 * read sensor set offset calibration value
177 CMD_READ_CALIBRATION((byte) 0x38, 1),
180 * write back sensor set offset value
182 CMD_WRITE_CALIBRATION((byte) 0x39, 1),
187 CMD_READ_SENSOR_ID((byte) 0x3A, 1),
190 * write back Sensors ID
192 CMD_WRITE_SENSOR_ID((byte) 0x3B, 1),
195 * this is reserved for newly added sensors
197 CMD_READ_SENSOR_ID_NEW((byte) 0x3C, 2),
202 CMD_WRITE_REBOOT((byte) 0x40, 1),
207 CMD_WRITE_RESET((byte) 0x41, 1),
209 CMD_READ_CUSTOMIZED_PATH((byte) 0x51, 1),
211 CMD_WRITE_CUSTOMIZED_PATH((byte) 0x52, 1),
216 CMD_GET_CO2_OFFSET((byte) 0x53, 1),
221 CMD_SET_CO2_OFFSET((byte) 0x54, 1),
224 * read rain reset time
226 CMD_READ_RSTRAIN_TIME((byte) 0x55, 1),
229 * write back rain reset time
231 CMD_WRITE_RSTRAIN_TIME((byte) 0x56, 1);
233 private final byte code;
234 private final int sizeBytes;
236 Command(byte code, int sizeBytes) {
238 this.sizeBytes = sizeBytes;
241 public byte getCode() {
245 public int getSizeBytes() {
249 public byte[] getPayload() {
250 byte size = 3; // + rest of payload / not yet implemented
251 return new byte[] { (byte) 0xff, (byte) 0xff, code, size, (byte) (code + size) };
254 public static @Nullable Command findByCode(byte code) {
255 return Arrays.stream(values()).filter(command -> command.getCode() == code).findFirst().orElse(null);
258 public boolean isHeaderValid(byte[] data) {
259 if (data.length < 4 + sizeBytes) {
262 return data[0] == (byte) 0xff && data[1] == (byte) 0xff && data[2] == code;
265 public boolean isResponseValid(byte[] data) {
266 return isHeaderValid(data) && Utils.validateChecksum(data, sizeBytes);