Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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