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.shelly.internal.coap;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.*;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
19 import java.net.SocketException;
20 import java.net.UnknownHostException;
21 import java.util.LinkedHashMap;
22 import java.util.List;
24 import java.util.TreeMap;
26 import org.eclipse.californium.core.CoapClient;
27 import org.eclipse.californium.core.coap.CoAP.Code;
28 import org.eclipse.californium.core.coap.CoAP.ResponseCode;
29 import org.eclipse.californium.core.coap.CoAP.Type;
30 import org.eclipse.californium.core.coap.MessageObserverAdapter;
31 import org.eclipse.californium.core.coap.Option;
32 import org.eclipse.californium.core.coap.OptionNumberRegistry;
33 import org.eclipse.californium.core.coap.Request;
34 import org.eclipse.californium.core.coap.Response;
35 import org.eclipse.californium.core.network.Endpoint;
36 import org.eclipse.jdt.annotation.NonNullByDefault;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.openhab.binding.shelly.internal.api.ShellyApiException;
39 import org.openhab.binding.shelly.internal.api.ShellyDeviceProfile;
40 import org.openhab.binding.shelly.internal.api.ShellyHttpApi;
41 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDescrBlk;
42 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDescrSen;
43 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDevDescrTypeAdapter;
44 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotDevDescription;
45 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotGenericSensorList;
46 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotSensor;
47 import org.openhab.binding.shelly.internal.coap.ShellyCoapJSonDTO.CoIotSensorTypeAdapter;
48 import org.openhab.binding.shelly.internal.config.ShellyThingConfiguration;
49 import org.openhab.binding.shelly.internal.handler.ShellyBaseHandler;
50 import org.openhab.binding.shelly.internal.handler.ShellyColorUtils;
51 import org.openhab.core.library.unit.Units;
52 import org.openhab.core.types.State;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.gson.Gson;
57 import com.google.gson.GsonBuilder;
58 import com.google.gson.JsonSyntaxException;
61 * The {@link ShellyCoapHandler} handles the CoIoT/CoAP registration and events.
63 * @author Markus Michels - Initial contribution
66 public class ShellyCoapHandler implements ShellyCoapListener {
67 private static final byte[] EMPTY_BYTE = new byte[0];
69 private final Logger logger = LoggerFactory.getLogger(ShellyCoapHandler.class);
70 private final ShellyBaseHandler thingHandler;
71 private ShellyThingConfiguration config = new ShellyThingConfiguration();
72 private final GsonBuilder gsonBuilder = new GsonBuilder();
73 private final Gson gson;
74 private String thingName;
76 private boolean coiotBound = false;
77 private ShellyCoIoTInterface coiot;
78 private int coiotVers = -1;
80 private final ShellyCoapServer coapServer;
81 private @Nullable CoapClient statusClient;
82 private Request reqDescription = new Request(Code.GET, Type.CON);
83 private Request reqStatus = new Request(Code.GET, Type.CON);
84 private boolean updatesRequested = false;
85 private int coiotPort = COIOT_PORT;
87 private long coiotMessages = 0;
88 private long coiotErrors = 0;
89 private int lastSerial = -1;
90 private String lastPayload = "";
91 private Map<String, CoIotDescrBlk> blkMap = new LinkedHashMap<>();
92 private Map<String, CoIotDescrSen> sensorMap = new LinkedHashMap<>();
93 private ShellyDeviceProfile profile;
94 private ShellyHttpApi api;
96 public ShellyCoapHandler(ShellyBaseHandler thingHandler, ShellyCoapServer coapServer) {
97 this.thingHandler = thingHandler;
98 this.thingName = thingHandler.thingName;
99 this.profile = thingHandler.getProfile();
100 this.api = thingHandler.getApi();
101 this.coapServer = coapServer;
102 this.coiot = new ShellyCoIoTVersion2(thingName, thingHandler, blkMap, sensorMap); // Default: V2
104 gsonBuilder.registerTypeAdapter(CoIotDevDescription.class, new CoIotDevDescrTypeAdapter());
105 gsonBuilder.registerTypeAdapter(CoIotGenericSensorList.class, new CoIotSensorTypeAdapter());
106 gson = gsonBuilder.create();
110 * Initialize CoAP access, send discovery packet and start Status server
112 * @parm thingName Thing name derived from Thing Type/hostname
113 * @parm config ShellyThingConfiguration
114 * @thows ShellyApiException
116 public synchronized void start(String thingName, ShellyThingConfiguration config) throws ShellyApiException {
118 this.thingName = thingName;
119 this.config = config;
120 this.profile = thingHandler.getProfile();
122 logger.trace("{}: CoAP Listener was already started", thingName);
126 logger.debug("{}: Starting CoAP Listener", thingName);
127 if (!profile.coiotEndpoint.isEmpty() && profile.coiotEndpoint.contains(":")) {
128 String ps = substringAfter(profile.coiotEndpoint, ":");
129 coiotPort = Integer.parseInt(ps);
131 coapServer.start(config.localIp, coiotPort, this);
132 statusClient = new CoapClient(completeUrl(config.deviceIp, coiotPort, COLOIT_URI_DEVSTATUS))
133 .setTimeout((long) SHELLY_API_TIMEOUT_MS).useNONs().setEndpoint(coapServer.getEndpoint());
135 Endpoint endpoint = null;
136 CoapClient client = statusClient;
137 if (client != null) {
138 endpoint = client.getEndpoint();
140 if ((endpoint == null) || !endpoint.isStarted()) {
141 logger.warn("{}: Unable to initialize CoAP access (network error)", thingName);
142 throw new ShellyApiException("Network initialization failed");
146 } catch (SocketException e) {
147 logger.warn("{}: Unable to initialize CoAP access (socket exception) - {}", thingName, e.getMessage());
148 throw new ShellyApiException("Network error", e);
149 } catch (UnknownHostException e) {
150 logger.info("{}: CoAP Exception (Unknown Host)", thingName, e);
151 throw new ShellyApiException("Unknown Host: " + config.deviceIp, e);
155 public boolean isStarted() {
156 return statusClient != null;
160 * Process an inbound Response (or mapped Request): decode CoAP options. handle discovery result or status updates
162 * @param response The Response packet
165 public void processResponse(@Nullable Response response) {
166 if (response == null) {
168 return; // other device instance
170 ResponseCode code = response.getCode();
171 if (code != ResponseCode.CONTENT) {
173 logger.debug("{}: Unknown Response Code {} received, payload={}", thingName, code,
174 response.getPayloadString());
179 List<Option> options = response.getOptions().asSortedList();
180 String ip = response.getSourceContext().getPeerAddress().toString();
181 boolean match = ip.contains(config.deviceIp);
183 // We can't identify device by IP, so we need to check the CoAP header's Global Device ID
184 for (Option opt : options) {
185 if (opt.getNumber() == COIOT_OPTION_GLOBAL_DEVID) {
186 String devid = opt.getStringValue();
187 if (devid.contains("#")) {
188 // Format: <device type>#<mac address>#<coap version>
189 String macid = substringBetween(devid, "#", "#");
190 if (profile.mac.toUpperCase().contains(macid.toUpperCase())) {
209 if (logger.isDebugEnabled()) {
210 logger.debug("{}: CoIoT Message from {} (MID={}): {}", thingName,
211 response.getSourceContext().getPeerAddress(), response.getMID(), response.getPayloadString());
213 if (response.isCanceled() || response.isDuplicate() || response.isRejected()) {
214 logger.debug("{} ({}): Packet was canceled, rejected or is a duplicate -> discard", thingName, devId);
219 payload = response.getPayloadString();
220 for (Option opt : options) {
221 switch (opt.getNumber()) {
222 case OptionNumberRegistry.URI_PATH:
223 uri = COLOIT_URI_BASE + opt.getStringValue();
225 case OptionNumberRegistry.URI_HOST: // ignore
227 case OptionNumberRegistry.CONTENT_FORMAT: // ignore
229 case COIOT_OPTION_GLOBAL_DEVID:
230 devId = opt.getStringValue();
231 String sVersion = substringAfterLast(devId, "#");
232 int iVersion = Integer.parseInt(sVersion);
233 if (coiotBound && (coiotVers != iVersion)) {
234 logger.debug("{}: CoIoT versopm has changed from {} to {}, maybe the firmware was upgraded",
235 thingName, coiotVers, iVersion);
236 thingHandler.reinitializeThing();
240 thingHandler.updateProperties(PROPERTY_COAP_VERSION, sVersion);
241 logger.debug("{}: CoIoT Version {} detected", thingName, iVersion);
242 if (iVersion == COIOT_VERSION_1) {
243 coiot = new ShellyCoIoTVersion1(thingName, thingHandler, blkMap, sensorMap);
244 } else if (iVersion == COIOT_VERSION_2) {
245 coiot = new ShellyCoIoTVersion2(thingName, thingHandler, blkMap, sensorMap);
247 logger.warn("{}: Unsupported CoAP version detected: {}", thingName, sVersion);
250 coiotVers = iVersion;
254 case COIOT_OPTION_STATUS_VALIDITY:
256 case COIOT_OPTION_STATUS_SERIAL:
257 serial = opt.getIntegerValue();
260 logger.debug("{} ({}): COAP option {} with value {} skipped", thingName, devId, opt.getNumber(),
265 // If we received a CoAP message successful the thing must be online
266 thingHandler.setThingOnline();
268 // The device changes the serial on every update, receiving a message with the same serial is a
269 // duplicate, excep for battery devices! Those reset the serial every time when they wake-up
270 if ((serial == lastSerial) && payload.equals(lastPayload) && (!profile.hasBattery
271 || coiot.getLastWakeup().equalsIgnoreCase("ext_power") || ((serial & 0xFF) != 0))) {
272 logger.debug("{}: Serial {} was already processed, ignore update", thingName, serial);
276 // fixed malformed JSON :-(
277 payload = fixJSON(payload);
280 if (uri.equalsIgnoreCase(COLOIT_URI_DEVDESC) || (uri.isEmpty() && payload.contains(COIOT_TAG_BLK))) {
281 handleDeviceDescription(devId, payload);
282 } else if (uri.equalsIgnoreCase(COLOIT_URI_DEVSTATUS)
283 || (uri.isEmpty() && payload.contains(COIOT_TAG_GENERIC))) {
284 handleStatusUpdate(devId, payload, serial);
286 } catch (ShellyApiException e) {
287 logger.debug("{}: Unable to process CoIoT message: {}", thingName, e.toString());
291 if (!updatesRequested) {
292 // Observe Status Updates
293 reqStatus = sendRequest(reqStatus, config.deviceIp, COLOIT_URI_DEVSTATUS, Type.NON);
294 updatesRequested = true;
296 } catch (JsonSyntaxException | IllegalArgumentException | NullPointerException e) {
297 logger.debug("{}: Unable to process CoIoT Message for payload={}", thingName, payload, e);
304 * Process a CoIoT device description message. This includes definitions on device units (Relay0, Relay1, Sensors
305 * etc.) as well as a definition of sensors and actors. This information needs to be stored allowing to map ids from
306 * status updates to the device units and matching the correct thing channel.
308 * @param devId The device id reported in the CoIoT message.
309 * @param payload Device desciption in JSon format, example:
310 * {"blk":[{"I":0,"D":"Relay0"}],"sen":[{"I":112,"T":"Switch","R":"0/1","L":0}],"act":[{"I":211,"D":"Switch","L":0,"P":[{"I":2011,"D":"ToState","R":"0/1"}]}]}
312 private void handleDeviceDescription(String devId, String payload) throws ShellyApiException {
313 logger.debug("{}: CoIoT Device Description for {}: {}", thingName, devId, payload);
316 boolean valid = true;
319 CoIotDevDescription descr = fromJson(gson, payload, CoIotDevDescription.class);
320 for (int i = 0; i < descr.blk.size(); i++) {
321 CoIotDescrBlk blk = descr.blk.get(i);
322 logger.debug("{}: id={}: {}", thingName, blk.id, blk.desc);
323 if (!blkMap.containsKey(blk.id)) {
324 blkMap.put(blk.id, blk);
326 blkMap.replace(blk.id, blk);
328 if ((blk.type != null) && !blk.type.isEmpty()) {
329 // in fact it is a sen entry - that's vioaling the Spec
330 logger.trace("{}: fix: auto-create sensor definition for id {}/{}!", thingName, blk.id,
332 CoIotDescrSen sen = new CoIotDescrSen();
336 sen.range = blk.range;
337 sen.links = blk.links;
338 valid &= addSensor(sen);
342 // Save to thing properties
343 thingHandler.updateProperties(PROPERTY_COAP_DESCR, payload);
345 logger.debug("{}: Adding {} sensor definitions", thingName, descr.sen.size());
346 if (descr.sen != null) {
347 for (int i = 0; i < descr.sen.size(); i++) {
348 valid &= addSensor(descr.sen.get(i));
351 coiot.completeMissingSensorDefinition(sensorMap);
355 "{}: Incompatible device description detected for CoIoT version {} (id length mismatch), discarding!",
356 thingName, coiot.getVersion());
361 } catch (JsonSyntaxException e) {
362 logger.warn("{}: Unable to parse CoAP Device Description! JSON={}", thingName, payload);
363 } catch (NullPointerException | IllegalArgumentException e) {
364 logger.warn("{}: Unable to parse CoAP Device Description! JSON={}", thingName, payload, e);
369 * Add a new sensor to the sensor table
371 * @param sen CoIotDescrSen of the sensor
373 private synchronized boolean addSensor(CoIotDescrSen sen) {
374 logger.debug("{}: id {}: {}, Type={}, Range={}, Links={}", thingName, sen.id, sen.desc, sen.type, sen.range,
376 // CoIoT version 2 changes from 3 digit IDs to 4 digit IDs
377 // We need to make sure that the persisted device description matches,
378 // otherwise the stored one is discarded and a new discovery is triggered
379 // This happens on firmware up/downgrades (version 1.8 brings CoIoT v2 with 4 digit IDs)
380 int vers = coiot.getVersion();
381 if (((vers == COIOT_VERSION_1) && (sen.id.length() > 3))
382 || ((vers >= COIOT_VERSION_2) && (sen.id.length() < 4))) {
383 logger.debug("{}: Invalid format for sensor defition detected, id={}", thingName, sen.id);
388 CoIotDescrSen fixed = coiot.fixDescription(sen, blkMap);
389 if (!sensorMap.containsKey(fixed.id)) {
390 sensorMap.put(sen.id, fixed);
392 sensorMap.replace(sen.id, fixed);
394 } catch (NullPointerException | IllegalArgumentException e) { // depending on firmware release the CoAP device
395 // description is buggy
396 logger.debug("{}: Unable to decode sensor definition -> skip", thingName, e);
403 * Process CoIoT status update message. If a status update is received, but the device description has not been
404 * received yet a GET is send to query device description.
406 * @param devId device id included in the status packet
407 * @param payload CoAP payload (Json format), example: {"G":[[0,112,0]]}
408 * @param serial Serial for this request. If this the the same as last serial
409 * the update was already sent and processed so this one gets
411 * @throws ShellyApiException
413 private void handleStatusUpdate(String devId, String payload, int serial) throws ShellyApiException {
414 logger.debug("{}: CoIoT Sensor data {} (serial={})", thingName, payload, serial);
415 if (blkMap.isEmpty()) {
416 // send discovery packet
420 // try to uses description from last initialization
421 String savedDescr = thingHandler.getProperty(PROPERTY_COAP_DESCR);
422 if (savedDescr.isEmpty()) {
423 logger.debug("{}: Device description not yet received, trigger auto-initialization", thingName);
427 // simulate received device description to create element table
428 logger.debug("{}: Device description for {} restored: {}", thingName, devId, savedDescr);
429 handleDeviceDescription(devId, savedDescr);
433 CoIotGenericSensorList list = fromJson(gson, fixJSON(payload), CoIotGenericSensorList.class);
434 if (list.generic == null) {
435 logger.debug("{}: Sensor list has invalid format! Payload: {}", devId, payload);
439 List<CoIotSensor> sensorUpdates = list.generic;
440 Map<String, State> updates = new TreeMap<String, State>();
441 logger.debug("{}: {} CoAP sensor updates received", thingName, sensorUpdates.size());
443 ShellyColorUtils col = new ShellyColorUtils();
444 for (int i = 0; i < sensorUpdates.size(); i++) {
446 CoIotSensor s = sensorUpdates.get(i);
447 CoIotDescrSen sen = sensorMap.get(s.id);
449 logger.debug("{}: Unable to sensor definition for id={}, payload={}", thingName, s.id, payload);
452 // find matching sensor definition from device description, use the Link ID as index
453 CoIotDescrBlk element = null;
454 sen = coiot.fixDescription(sen, blkMap);
455 element = blkMap.get(sen.links);
456 if (element == null) {
457 logger.debug("{}: Unable to find BLK for link {} from sen.id={}, payload={}", thingName, sen.links,
461 logger.trace("{}: Sensor value[{}]: id={}, Value={} ({}, Type={}, Range={}, Link={}: {})", thingName,
462 i, s.id, getString(s.valueStr).isEmpty() ? s.value : s.valueStr, sen.desc, sen.type, sen.range,
463 sen.links, element.desc);
465 if (!coiot.handleStatusUpdate(sensorUpdates, sen, serial, s, updates, col)) {
466 logger.debug("{}: CoIoT data for id {}, type {}/{} not processed, value={}; payload={}", thingName,
467 sen.id, sen.type, sen.desc, s.value, payload);
469 } catch (NullPointerException | IllegalArgumentException e) {
470 // even the processing of one value failed we continue with the next one (sometimes this is caused by
471 // buggy formats provided by the device
472 logger.debug("{}: Unable to process data from sensor[{}], devId={}, payload={}", thingName, i, devId,
477 if (!updates.isEmpty()) {
479 for (Map.Entry<String, State> u : updates.entrySet()) {
480 String key = u.getKey();
481 updated += thingHandler.updateChannel(key, u.getValue(), false) ? 1 : 0;
484 logger.debug("{}: {} channels updated from CoIoT status, serial={}", thingName, updated, serial);
485 if (profile.isSensor || profile.isRoller) {
486 // CoAP is currently lacking the lastUpdate info, so we use host timestamp
487 thingHandler.updateChannel(profile.getControlGroup(0), CHANNEL_LAST_UPDATE, getTimestamp());
491 if (profile.isLight && profile.inColor && col.isRgbValid()) {
492 // Update color picker from single values
493 if (col.isRgbValid()) {
494 thingHandler.updateChannel(mkChannelId(CHANNEL_GROUP_COLOR_CONTROL, CHANNEL_COLOR_PICKER),
499 if ((profile.isRGBW2 && !profile.inColor) || profile.isRoller) {
500 // Aggregate Meter Data from different Coap updates
502 double totalCurrent = 0.0;
503 double totalKWH = 0.0;
504 boolean updateMeter = false;
505 while (i <= thingHandler.getProfile().numMeters) {
506 String meter = CHANNEL_GROUP_METER + i;
507 double current = thingHandler.getChannelDouble(meter, CHANNEL_METER_CURRENTWATTS);
508 double total = thingHandler.getChannelDouble(meter, CHANNEL_METER_TOTALKWH);
509 logger.debug("{}: {}#{}={}, total={}", thingName, meter, CHANNEL_METER_CURRENTWATTS, current,
511 totalCurrent += current >= 0 ? current : 0;
512 totalKWH += total >= 0 ? total : 0;
513 updateMeter |= current >= 0 | total >= 0;
516 logger.debug("{}: totalCurrent={}, totalKWH={}, update={}", thingName, totalCurrent, totalKWH,
519 thingHandler.updateChannel(CHANNEL_GROUP_METER, CHANNEL_METER_CURRENTWATTS,
520 toQuantityType(totalCurrent, DIGITS_WATT, Units.WATT));
521 thingHandler.updateChannel(CHANNEL_GROUP_METER, CHANNEL_LAST_UPDATE, getTimestamp());
525 // Old firmware release are lacking various status values, which are not updated using CoIoT.
526 // In this case we keep a refresh so it gets polled using REST. Beginning with Firmware 1.6 most
527 // of the values are available
528 if ((!thingHandler.autoCoIoT && (thingHandler.scheduledUpdates < 1))
529 || (thingHandler.autoCoIoT && !profile.isLight && !profile.hasBattery)) {
530 thingHandler.requestUpdates(1, false);
533 if (failed == sensorUpdates.size()) {
534 logger.debug("{}: Device description problem detected, re-discover", thingName);
540 // Remember serial, new packets with same serial will be ignored
542 lastPayload = payload;
545 private void discover() {
546 if (coiot.getVersion() >= 2) {
549 // Try to device description using http request (FW 1.10+)
550 String payload = api.getCoIoTDescription();
551 if (!payload.isEmpty()) {
552 logger.debug("{}: Using CoAP device description from successful HTTP /cit/d", thingName);
553 handleDeviceDescription(thingName, payload);
556 } catch (ShellyApiException e) {
557 // ignore if not supported by device
561 reqDescription = sendRequest(reqDescription, config.deviceIp, COLOIT_URI_DEVDESC, Type.CON);
565 * Fix malformed JSON - stupid, but the devices sometimes return malformed JSON with then causes a
566 * JsonSyntaxException
568 * @param json to be checked/fixed
570 private static String fixJSON(String payload) {
571 String json = payload;
572 json = json.replace("}{", "},{");
573 json = json.replace("][", "],[");
574 json = json.replace("],,[", "],[");
579 * Send a new request (Discovery to get Device Description). Before a pending
580 * request will be canceled.
582 * @param request The current request (this will be canceled an a new one will
584 * @param ipAddress Device's IP address
585 * @param uri The URI we are calling (CoIoT = /cit/d or /cit/s)
586 * @param con true: send as CON, false: send as NON
589 private Request sendRequest(@Nullable Request request, String ipAddress, String uri, Type con) {
590 if ((request != null) && !request.isCanceled()) {
595 return newRequest(ipAddress, coiotPort, uri, con).send();
599 * Allocate a new Request structure. A message observer will be added to get the
600 * callback when a response has been received.
602 * @param ipAddress IP address of the device
603 * @param uri URI to be addressed
604 * @param uri The URI we are calling (CoIoT = /cit/d or /cit/s)
605 * @param con true: send as CON, false: send as NON
609 private Request newRequest(String ipAddress, int port, String uri, Type con) {
610 // We need to build our own Request to set an empty Token
611 Request request = new Request(Code.GET, con);
612 request.setURI(completeUrl(ipAddress, port, uri));
613 request.setToken(EMPTY_BYTE);
614 request.addMessageObserver(new MessageObserverAdapter() {
616 public void onResponse(@Nullable Response response) {
617 processResponse(response);
621 public void onCancel() {
622 logger.debug("{}: CoAP Request was canceled", thingName);
626 public void onTimeout() {
627 logger.debug("{}: CoAP Request timed out", thingName);
634 * Reset serial and payload used to detect duplicate messages, which have to be ignored.
635 * We can't rely that the device manages serials correctly all the time. There are firmware releases sending updated
636 * sensor information with the serial from the last packet, which is wrong. We bypass this problem by comparing also
639 private void resetSerial() {
644 public int getVersion() {
649 * Cancel pending requests and shutdown the client
651 public synchronized void stop() {
653 logger.debug("{}: Stopping CoAP Listener", thingName);
654 coapServer.stop(this);
655 CoapClient cclient = statusClient;
656 if (cclient != null) {
660 Request request = reqDescription;
661 if (!request.isCanceled()) {
665 if (!request.isCanceled()) {
673 public long getMessageCount() {
674 return coiotMessages;
677 public long getErrorCount() {
681 public void dispose() {
685 private static String completeUrl(String ipAddress, int port, String uri) {
686 return "coap://" + ipAddress + ":" + port + uri;