[shelly] Add check and ThingStatus for local IP issue (APIPA) (#16306)

* Check for local_ip == 169.254.x.xm, which is the result when DHCP IP
assignment failed. Avoid that the binding passes this address to the
device as part of the callback url.

Signed-off-by: Markus Michels <markus7017@gmail.com>

Co-authored-by: markus7017 <markus7017@gmail..com>
This commit is contained in:
Markus Michels 2024-01-27 21:58:44 +01:00 committed by GitHub
parent 1716d41b28
commit bed592c3a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 8 deletions

View File

@ -75,12 +75,13 @@ public class ShellyEventServlet extends WebSocketServlet {
* Servlet handler. Shelly1: http request, Shelly2: WebSocket call * Servlet handler. Shelly1: http request, Shelly2: WebSocket call
*/ */
@Override @Override
protected void service(HttpServletRequest request, HttpServletResponse resp) protected void service(@Nullable HttpServletRequest request, @Nullable HttpServletResponse resp)
throws ServletException, IOException, IllegalArgumentException { throws ServletException, IOException, IllegalArgumentException {
String path = getString(request.getRequestURI()).toLowerCase(); String path = getString(request.getRequestURI()).toLowerCase();
if (path.equals(SHELLY2_CALLBACK_URI)) { // Shelly2 WebSocket if (path.equals(SHELLY2_CALLBACK_URI)) { // Shelly2 WebSocket
if (request != null && resp != null) {
super.service(request, resp); super.service(request, resp);
}
return; return;
} }

View File

@ -1008,6 +1008,12 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
config.serviceName = getString(properties.get(PROPERTY_SERVICE_NAME)); config.serviceName = getString(properties.get(PROPERTY_SERVICE_NAME));
config.localIp = bindingConfig.localIP; config.localIp = bindingConfig.localIP;
config.localPort = String.valueOf(bindingConfig.httpPort); config.localPort = String.valueOf(bindingConfig.httpPort);
if (config.localIp.startsWith("169.254")) {
setThingOffline(ThingStatusDetail.COMMUNICATION_ERROR, "config-status.error.network-config",
config.localIp);
return;
}
if (!profile.isGen2 && config.userId.isEmpty() && !bindingConfig.defaultUserId.isEmpty()) { if (!profile.isGen2 && config.userId.isEmpty() && !bindingConfig.defaultUserId.isEmpty()) {
// Gen2 has hard coded user "admin" // Gen2 has hard coded user "admin"
config.userId = bindingConfig.defaultUserId; config.userId = bindingConfig.defaultUserId;

View File

@ -16,7 +16,6 @@ import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
/** /**
@ -24,7 +23,6 @@ import org.eclipse.jdt.annotation.Nullable;
* *
* @author Markus Michels - Initial contribution * @author Markus Michels - Initial contribution
*/ */
@NonNullByDefault
public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> { public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -46,7 +44,7 @@ public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
} }
@Override @Override
public @Nullable V put(K key, V value) { public V put(K key, V value) {
Date date = new Date(); Date date = new Date();
timeMap.put(key, date.getTime()); timeMap.put(key, date.getTime());
return super.put(key, value); return super.put(key, value);
@ -66,7 +64,7 @@ public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
} }
@Override @Override
public @Nullable V putIfAbsent(K key, V value) { public V putIfAbsent(K key, V value) {
if (!containsKey(key)) { if (!containsKey(key)) {
return put(key, value); return put(key, value);
} else { } else {
@ -86,7 +84,6 @@ public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {
} }
} }
@SuppressWarnings("null")
private void cleanMap() { private void cleanMap() {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
for (K key : timeMap.keySet()) { for (K key : timeMap.keySet()) {

View File

@ -11,6 +11,7 @@ addon.shelly.config.autoCoIoT.label = Auto-CoIoT
addon.shelly.config.autoCoIoT.description = If enabled CoIoT will be automatically used when the devices runs a firmware version 1.6 or newer; false: Use thing configuration to enabled/disable CoIoT events. addon.shelly.config.autoCoIoT.description = If enabled CoIoT will be automatically used when the devices runs a firmware version 1.6 or newer; false: Use thing configuration to enabled/disable CoIoT events.
# Config status messages # Config status messages
message.config-status.error.network-config = Invalid system or openHAB network configuration was detected (local IP {0}).
message.config-status.error.missing-device-address = IP/MAC Address of the Shelly device is missing. message.config-status.error.missing-device-address = IP/MAC Address of the Shelly device is missing.
message.config-status.error.missing-userid = No user ID in the Thing configuration message.config-status.error.missing-userid = No user ID in the Thing configuration