Skip to content

Commit

Permalink
[shelly] Add check and ThingStatus for local IP issue (APIPA) (openha…
Browse files Browse the repository at this point in the history
…b#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>
Signed-off-by: René Ulbricht <rene_ulbricht@outlook.com>
  • Loading branch information
2 people authored and ulbi committed Jan 28, 2024
1 parent ca938ec commit 63104d3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ protected void deactivate() {
* Servlet handler. Shelly1: http request, Shelly2: WebSocket call
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse resp)
protected void service(@Nullable HttpServletRequest request, @Nullable HttpServletResponse resp)
throws ServletException, IOException, IllegalArgumentException {
String path = getString(request.getRequestURI()).toLowerCase();

if (path.equals(SHELLY2_CALLBACK_URI)) { // Shelly2 WebSocket
super.service(request, resp);
if (request != null && resp != null) {
super.service(request, resp);
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,12 @@ protected void initializeThingConfig() {
config.serviceName = getString(properties.get(PROPERTY_SERVICE_NAME));
config.localIp = bindingConfig.localIP;
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()) {
// Gen2 has hard coded user "admin"
config.userId = bindingConfig.defaultUserId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* {@link ShellyManagerCache} implements a cache with expiring times of the entries
*
* @author Markus Michels - Initial contribution
*/
@NonNullByDefault
public class ShellyManagerCache<K, V> extends ConcurrentHashMap<K, V> {

private static final long serialVersionUID = 1L;
Expand All @@ -46,7 +44,7 @@ void initialize() {
}

@Override
public @Nullable V put(K key, V value) {
public V put(K key, V value) {
Date date = new Date();
timeMap.put(key, date.getTime());
return super.put(key, value);
Expand All @@ -66,7 +64,7 @@ public void putAll(@Nullable Map<? extends K, ? extends V> m) {
}

@Override
public @Nullable V putIfAbsent(K key, V value) {
public V putIfAbsent(K key, V value) {
if (!containsKey(key)) {
return put(key, value);
} else {
Expand All @@ -86,7 +84,6 @@ public void run() {
}
}

@SuppressWarnings("null")
private void cleanMap() {
long currentTime = new Date().getTime();
for (K key : timeMap.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

# 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-userid = No user ID in the Thing configuration

Expand Down

0 comments on commit 63104d3

Please sign in to comment.