]> git.basschouten.com Git - openhab-addons.git/blob
2d47269a5a6a26354fd674f7ee5e4ec1d1d8f09b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.fineoffsetweatherstation.internal.domain;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.fineoffsetweatherstation.internal.Utils;
20
21 /**
22  * The Commands supported by the gateway.
23  *
24  * @author Andreas Berger - Initial contribution
25  */
26 @NonNullByDefault
27 public enum Command {
28     /**
29      * send SSID and Password to WIFI module
30      */
31     CMD_WRITE_SSID((byte) 0x11, 1),
32
33     /**
34      * UDP cast for device echo,answer back data size is 2 Bytes
35      */
36     CMD_BROADCAST((byte) 0x12, 2),
37
38     /**
39      * read aw.net setting
40      */
41     CMD_READ_ECOWITT((byte) 0x1E, 1),
42
43     /**
44      * write back awt.net setting
45      */
46     CMD_WRITE_ECOWITT((byte) 0x1F, 1),
47
48     /**
49      * read Wunderground setting
50      */
51     CMD_READ_WUNDERGROUND((byte) 0x20, 1),
52
53     /**
54      * write back Wunderground setting
55      */
56     CMD_WRITE_WUNDERGROUND((byte) 0x21, 1),
57
58     /**
59      * read WeatherObservationsWebsite setting
60      */
61     CMD_READ_WOW((byte) 0x22, 1),
62
63     /**
64      * write back WeatherObservationsWebsite setting
65      */
66     CMD_WRITE_WOW((byte) 0x23, 1),
67
68     /**
69      * read Weathercloud setting
70      */
71     CMD_READ_WEATHERCLOUD((byte) 0x24, 1),
72
73     /**
74      * write back Weathercloud setting
75      */
76     CMD_WRITE_WEATHERCLOUD((byte) 0x25, 1),
77
78     /**
79      * read MAC address
80      */
81     CMD_READ_SATION_MAC((byte) 0x26, 1),
82
83     /**
84      * read Customized sever setting
85      */
86     CMD_READ_CUSTOMIZED((byte) 0x2A, 1),
87
88     /**
89      * write back Customized sever setting
90      */
91     CMD_WRITE_CUSTOMIZED((byte) 0x2B, 1),
92
93     /**
94      * firmware upgrade
95      */
96     CMD_WRITE_UPDATE((byte) 0x43, 1),
97
98     /**
99      * read current firmware version number
100      */
101     CMD_READ_FIRMWARE_VERSION((byte) 0x50, 1),
102
103     CMD_READ_USR_PATH((byte) 0x51, 1),
104
105     CMD_WRITE_USR_PATH((byte) 0x52, 1),
106
107     // the following command is only valid for GW1000, WH2650 and wn1900
108
109     /**
110      * read current data,reply data size is 2bytes.
111      */
112     CMD_GW1000_LIVEDATA((byte) 0x27, 2),
113
114     /**
115      * read Soilmoisture Sensor calibration parameters
116      */
117     CMD_GET_SOILHUMIAD((byte) 0x28, 1),
118
119     /**
120      * write back Soilmoisture Sensor calibration parameters
121      */
122     CMD_SET_SOILHUMIAD((byte) 0x29, 1),
123
124     /**
125      * read multi channel sensor offset value
126      */
127     CMD_GET_MulCH_OFFSET((byte) 0x2C, 1),
128
129     /**
130      * write back multi channel sensor OFFSET value
131      */
132     CMD_SET_MulCH_OFFSET((byte) 0x2D, 1),
133
134     /**
135      * read PM2.5OFFSET calibration data
136      */
137     CMD_GET_PM25_OFFSET((byte) 0x2E, 1),
138
139     /**
140      * writeback PM2.5OFFSET calibration data
141      */
142     CMD_SET_PM25_OFFSET((byte) 0x2F, 1),
143
144     /**
145      * read system info
146      */
147     CMD_READ_SSSS((byte) 0x30, 1),
148
149     /**
150      * write back system info
151      */
152     CMD_WRITE_SSSS((byte) 0x31, 1),
153
154     /**
155      * read rain data
156      */
157     CMD_READ_RAINDATA((byte) 0x34, 1),
158
159     /**
160      * write back rain data
161      */
162     CMD_WRITE_RAINDATA((byte) 0x35, 1),
163
164     /**
165      * read rain gain
166      */
167     CMD_READ_GAIN((byte) 0x36, 1),
168
169     /**
170      * write back rain gain
171      */
172     CMD_WRITE_GAIN((byte) 0x37, 1),
173
174     /**
175      * read sensor set offset calibration value
176      */
177     CMD_READ_CALIBRATION((byte) 0x38, 1),
178
179     /**
180      * write back sensor set offset value
181      */
182     CMD_WRITE_CALIBRATION((byte) 0x39, 1),
183
184     /**
185      * read Sensors ID
186      */
187     CMD_READ_SENSOR_ID((byte) 0x3A, 1),
188
189     /**
190      * write back Sensors ID
191      */
192     CMD_WRITE_SENSOR_ID((byte) 0x3B, 1),
193
194     /**
195      * this is reserved for newly added sensors
196      */
197     CMD_READ_SENSOR_ID_NEW((byte) 0x3C, 2),
198
199     /**
200      * system restart
201      */
202     CMD_WRITE_REBOOT((byte) 0x40, 1),
203
204     /**
205      * reset to default
206      */
207     CMD_WRITE_RESET((byte) 0x41, 1),
208
209     CMD_READ_CUSTOMIZED_PATH((byte) 0x51, 1),
210
211     CMD_WRITE_CUSTOMIZED_PATH((byte) 0x52, 1),
212
213     /**
214      * CO2 OFFSET
215      */
216     CMD_GET_CO2_OFFSET((byte) 0x53, 1),
217
218     /**
219      * CO2 OFFSET
220      */
221     CMD_SET_CO2_OFFSET((byte) 0x54, 1),
222
223     /**
224      * read rain reset time
225      */
226     CMD_READ_RSTRAIN_TIME((byte) 0x55, 1),
227
228     /**
229      * write back rain reset time
230      */
231     CMD_WRITE_RSTRAIN_TIME((byte) 0x56, 1);
232
233     private final byte code;
234     private final int sizeBytes;
235
236     Command(byte code, int sizeBytes) {
237         this.code = code;
238         this.sizeBytes = sizeBytes;
239     }
240
241     public byte getCode() {
242         return code;
243     }
244
245     public int getSizeBytes() {
246         return sizeBytes;
247     }
248
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) };
252     }
253
254     public static @Nullable Command findByCode(byte code) {
255         return Arrays.stream(values()).filter(command -> command.getCode() == code).findFirst().orElse(null);
256     }
257
258     public boolean isHeaderValid(byte[] data) {
259         if (data.length < 4 + sizeBytes) {
260             return false;
261         }
262         return data[0] == (byte) 0xff && data[1] == (byte) 0xff && data[2] == code;
263     }
264
265     public boolean isResponseValid(byte[] data) {
266         return isHeaderValid(data) && Utils.validateChecksum(data, sizeBytes);
267     }
268 }