Skip to content

Commit e140913

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 0e00ec4 commit e140913

1 file changed

Lines changed: 27 additions & 5 deletions

File tree

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

Lines changed: 27 additions & 5 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;
@@ -155,6 +156,27 @@ public boolean isStarted() {
155156
return statusClient != null;
156157
}
157158

159+
private byte[] readOptionBytes(Option option) {
160+
try {
161+
return option.encode();
162+
} catch (RuntimeException e) {
163+
logger.debug("{}: Failed to encode option value", thingName, e);
164+
return EMPTY_BYTE;
165+
}
166+
}
167+
168+
private String readStringOption(Option option) {
169+
return new String(readOptionBytes(option), StandardCharsets.UTF_8);
170+
}
171+
172+
private int readIntegerOption(Option option) {
173+
int value = 0;
174+
for (byte b : readOptionBytes(option)) {
175+
value = (value << 8) | (b & 0xFF);
176+
}
177+
return value;
178+
}
179+
158180
/**
159181
* Process an inbound Response (or mapped Request): decode CoAP options. handle discovery result or status updates
160182
*
@@ -182,7 +204,7 @@ public void processResponse(@Nullable Response response) {
182204
// We can't identify device by IP, so we need to check the CoAP header's Global Device ID
183205
for (Option opt : options) {
184206
if (opt.getNumber() == COIOT_OPTION_GLOBAL_DEVID) {
185-
String devid = opt.getStringValue();
207+
String devid = readStringOption(opt);
186208
if (devid.contains("#") && profile.device.mac != null) {
187209
// Format: <device type>#<mac address>#<coap version>
188210
String macid = substringBetween(devid, "#", "#");
@@ -226,14 +248,14 @@ public void processResponse(@Nullable Response response) {
226248
for (Option opt : options) {
227249
switch (opt.getNumber()) {
228250
case OptionNumberRegistry.URI_PATH:
229-
uri = COLOIT_URI_BASE + opt.getStringValue();
251+
uri = COLOIT_URI_BASE + readStringOption(opt);
230252
break;
231253
case OptionNumberRegistry.URI_HOST: // ignore
232254
break;
233255
case OptionNumberRegistry.CONTENT_FORMAT: // ignore
234256
break;
235257
case COIOT_OPTION_GLOBAL_DEVID:
236-
devId = opt.getStringValue();
258+
devId = readStringOption(opt);
237259
String sVersion = substringAfterLast(devId, "#");
238260
int iVersion;
239261
try {
@@ -267,11 +289,11 @@ public void processResponse(@Nullable Response response) {
267289
case COIOT_OPTION_STATUS_VALIDITY:
268290
break;
269291
case COIOT_OPTION_STATUS_SERIAL:
270-
serial = opt.getIntegerValue();
292+
serial = readIntegerOption(opt);
271293
break;
272294
default:
273295
logger.debug("{} ({}): CoAP option {} with value {} skipped", thingName, devId, opt.getNumber(),
274-
opt.getValue());
296+
opt.toValueString());
275297
}
276298
}
277299

0 commit comments

Comments
 (0)