[shelly] Fix "duplicate id" in device log#20094
[shelly] Fix "duplicate id" in device log#20094markus7017 wants to merge 4 commits intoopenhab:mainfrom
Conversation
Signed-off-by: Markus Michels <markus7017@gmail.com>
requested id includes "ohshelly-<id>-<request id>" Signed-off-by: Markus Michels <markus7017@gmail.com>
Signed-off-by: Markus Michels <markus7017@gmail.com>
Signed-off-by: Markus Michels <markus7017@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes "duplicate id" warnings in Shelly device logs by ensuring unique RPC message IDs and source identifiers across concurrent HTTP requests from different threads (thing handlers and mDNS discovery).
Changes:
- Modified RPC message ID generation to use thread-safe atomic counters with random starting values
- Changed RPC source field format to include unique identifiers (thing UID or local IP) plus request ID
- Updated API initialization to consistently pass configuration parameters
- Fixed config initialization to properly set serviceName and localIp during discovery
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Shelly2ApiClient.java | Implements thread-safe request ID generation using AtomicInteger with random base, builds unique RPC source identifiers |
| Shelly2ApiRpc.java | Updates initialize method signature to accept config, regenerates request on auth retry |
| ShellyBaseHandler.java | Calls api.initialize with config parameters, optimizes setConfig calls |
| ShellyBasicDiscoveryService.java | Initializes config with serviceName and localIp, removes unused IOException, fixes empty hostname handling |
| ShellyApiInterface.java | Updates initialize method signature to require thingName and config parameters |
| Shelly1HttpApi.java | Implements new initialize signature |
| ShellyBluApi.java | Delegates to parent initialize implementation |
| Shelly2ApiJsonDTO.java | Adds jsonrpc field to RPC message structure |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| request.src = RPC_SRC_PREFIX + suffix + "-" + request.id; // use a unique identifier; request.method = | ||
| // !method.contains(".") ? | ||
| // SHELLYRPC_METHOD_CLASS_SHELLY + "." + method : | ||
| // method; |
There was a problem hiding this comment.
The request.method field is not being assigned. The code in the comment on lines 1012-1015 shows the intended logic but it's been commented out rather than properly implemented. The method parameter should be assigned to request.method, with the logic: request.method = !method.contains(".") ? SHELLYRPC_METHOD_CLASS_SHELLY + "." + method : method;. Without this assignment, all RPC requests will have a null method field, causing them to fail.
| request.src = RPC_SRC_PREFIX + suffix + "-" + request.id; // use a unique identifier; request.method = | |
| // !method.contains(".") ? | |
| // SHELLYRPC_METHOD_CLASS_SHELLY + "." + method : | |
| // method; | |
| request.src = RPC_SRC_PREFIX + suffix + "-" + request.id; // use a unique identifier | |
| request.method = !method.contains(".") ? SHELLYRPC_METHOD_CLASS_SHELLY + "." + method : method; |
| String uid = thing.getThing().getUID().getAsString(); | ||
| suffix = substringAfterLast(uid, ":"); | ||
| } else { | ||
| suffix = config.localIp; // use a unique identifier; |
There was a problem hiding this comment.
If config.localIp is empty when thing is null (during discovery), the suffix variable will be an empty string, resulting in request.src being formatted as "ohshelly--{requestId}" with a double dash. While this should still function, it would be clearer to handle this case explicitly, perhaps by using a default placeholder like "discovery" or "unknown" when localIp is empty.
| suffix = config.localIp; // use a unique identifier; | |
| String localIp = config.localIp; | |
| if (localIp == null || localIp.isEmpty()) { | |
| suffix = "discovery"; // fallback identifier when localIp is not available | |
| } else { | |
| suffix = localIp; // use a unique identifier; | |
| } |
|
This pull request has been mentioned on openHAB Community. There might be relevant details there: |
|
Replace by PR #20248 |
Users do report "duplicate id" in the device log. This relates to the fact that the device might receive overlapping http requests from thing and mDNS discovery threads. Each thread started with id 1, which is inconsistent (each id should be different).
The following changes were made
the request ID as a suffix makes it unique across threads.
With those changes I don't see anymore "duplicate id" in the device log and RPC.src field is correctly build.
Awaiting #20069
Closing #19223