Skip to content

Commit 859a622

Browse files
[shelly] Adapt shelly v1 handler to californium 4.0.0-M6 (#20704)
* [shelly] Adapt shelly v1 handler to californium 4.0.0-M6 Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
1 parent bc8c12f commit 859a622

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1CoapHandler.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.net.SocketException;
2020
import java.net.UnknownHostException;
21+
import java.nio.charset.StandardCharsets;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
2324
import java.util.Locale;
@@ -33,6 +34,7 @@
3334
import org.eclipse.californium.core.coap.OptionNumberRegistry;
3435
import org.eclipse.californium.core.coap.Request;
3536
import org.eclipse.californium.core.coap.Response;
37+
import org.eclipse.californium.core.coap.option.OpaqueOption;
3638
import org.eclipse.californium.core.network.Endpoint;
3739
import org.eclipse.jdt.annotation.NonNullByDefault;
3840
import org.eclipse.jdt.annotation.Nullable;
@@ -155,6 +157,27 @@ public boolean isStarted() {
155157
return statusClient != null;
156158
}
157159

160+
private byte[] readOptionBytes(Option option) {
161+
try {
162+
return option.encode();
163+
} catch (RuntimeException e) {
164+
logger.debug("{}: Failed to encode option value", thingName, e);
165+
return EMPTY_BYTE;
166+
}
167+
}
168+
169+
private String readStringOption(Option option) {
170+
return new String(readOptionBytes(option), StandardCharsets.UTF_8);
171+
}
172+
173+
private int readIntegerOption(Option option) {
174+
int value = 0;
175+
for (byte b : readOptionBytes(option)) {
176+
value = (value << 8) | (b & 0xFF);
177+
}
178+
return value;
179+
}
180+
158181
/**
159182
* Process an inbound Response (or mapped Request): decode CoAP options. handle discovery result or status updates
160183
*
@@ -182,7 +205,7 @@ public void processResponse(@Nullable Response response) {
182205
// We can't identify device by IP, so we need to check the CoAP header's Global Device ID
183206
for (Option opt : options) {
184207
if (opt.getNumber() == COIOT_OPTION_GLOBAL_DEVID) {
185-
String devid = opt.getStringValue();
208+
String devid = readStringOption(opt);
186209
if (devid.contains("#") && profile.device.mac != null) {
187210
// Format: <device type>#<mac address>#<coap version>
188211
String macid = substringBetween(devid, "#", "#");
@@ -226,14 +249,14 @@ public void processResponse(@Nullable Response response) {
226249
for (Option opt : options) {
227250
switch (opt.getNumber()) {
228251
case OptionNumberRegistry.URI_PATH:
229-
uri = COLOIT_URI_BASE + opt.getStringValue();
252+
uri = COLOIT_URI_BASE + readStringOption(opt);
230253
break;
231254
case OptionNumberRegistry.URI_HOST: // ignore
232255
break;
233256
case OptionNumberRegistry.CONTENT_FORMAT: // ignore
234257
break;
235258
case COIOT_OPTION_GLOBAL_DEVID:
236-
devId = opt.getStringValue();
259+
devId = readStringOption(opt);
237260
String sVersion = substringAfterLast(devId, "#");
238261
int iVersion;
239262
try {
@@ -267,7 +290,7 @@ public void processResponse(@Nullable Response response) {
267290
case COIOT_OPTION_STATUS_VALIDITY:
268291
break;
269292
case COIOT_OPTION_STATUS_SERIAL:
270-
serial = opt.getIntegerValue();
293+
serial = readIntegerOption(opt);
271294
break;
272295
default:
273296
logger.debug("{} ({}): CoAP option {} with value {} skipped", thingName, devId, opt.getNumber(),

0 commit comments

Comments
 (0)