Search in sources :

Example 1 with ShellyDeviceStats

use of org.openhab.binding.shelly.internal.handler.ShellyDeviceStats in project openhab-addons by openhab.

the class ShellyManagerOverviewPage method getStatusWarnings.

private Map<String, String> getStatusWarnings(ShellyManagerInterface handler) {
    Thing thing = handler.getThing();
    ThingStatus status = handler.getThing().getStatus();
    ShellyDeviceStats stats = handler.getStats();
    ShellyDeviceProfile profile = handler.getProfile();
    ShellyThingConfiguration config = thing.getConfiguration().as(ShellyThingConfiguration.class);
    TreeMap<String, String> result = new TreeMap<>();
    if ((status != ThingStatus.ONLINE) && (status != ThingStatus.UNKNOWN)) {
        result.put("Thing Status", status.toString());
    }
    State wifiSignal = handler.getChannelValue(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_RSSI);
    if ((profile.alwaysOn || (profile.hasBattery && (status == ThingStatus.ONLINE))) && ((wifiSignal != UnDefType.NULL) && (((DecimalType) wifiSignal).intValue() < 2))) {
        result.put("Weak WiFi Signal", wifiSignal.toString());
    }
    if (profile.hasBattery) {
        State lowBattery = handler.getChannelValue(CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LOW);
        if ((lowBattery == OnOffType.ON)) {
            lowBattery = handler.getChannelValue(CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
            result.put("Battery Low", lowBattery.toString());
        }
    }
    if (stats.lastAlarm.equalsIgnoreCase(ALARM_TYPE_RESTARTED)) {
        result.put("Device Alarm", ALARM_TYPE_RESTARTED + " (" + convertTimestamp(stats.lastAlarmTs) + ")");
    }
    if (getBool(profile.status.overtemperature)) {
        result.put("Device Alarm", ALARM_TYPE_OVERTEMP);
    }
    if (getBool(profile.status.overload)) {
        result.put("Device Alarm", ALARM_TYPE_OVERLOAD);
    }
    if (getBool(profile.status.loaderror)) {
        result.put("Device Alarm", ALARM_TYPE_LOADERR);
    }
    if (profile.isSensor) {
        State sensorError = handler.getChannelValue(CHANNEL_GROUP_SENSOR, CHANNEL_SENSOR_ERROR);
        if (sensorError != UnDefType.NULL) {
            if (!sensorError.toString().isEmpty()) {
                result.put("Device Alarm", ALARM_TYPE_SENSOR_ERROR);
            }
        }
    }
    if (profile.alwaysOn && (status == ThingStatus.ONLINE)) {
        if ((config.eventsCoIoT) && (profile.settings.coiot != null)) {
            if ((profile.settings.coiot.enabled != null) && !profile.settings.coiot.enabled) {
                result.put("CoIoT Status", "COIOT_DISABLED");
            } else if (stats.coiotMessages == 0) {
                result.put("CoIoT Discovery", "NO_COIOT_DISCOVERY");
            } else if (stats.coiotMessages < 2) {
                result.put("CoIoT Multicast", "NO_COIOT_MULTICAST");
            }
        }
    }
    return result;
}
Also used : State(org.openhab.core.types.State) ThingStatus(org.openhab.core.thing.ThingStatus) ShellyThingConfiguration(org.openhab.binding.shelly.internal.config.ShellyThingConfiguration) TreeMap(java.util.TreeMap) ShellyDeviceProfile(org.openhab.binding.shelly.internal.api.ShellyDeviceProfile) Thing(org.openhab.core.thing.Thing) ShellyDeviceStats(org.openhab.binding.shelly.internal.handler.ShellyDeviceStats)

Example 2 with ShellyDeviceStats

use of org.openhab.binding.shelly.internal.handler.ShellyDeviceStats in project openhab-addons by openhab.

the class ShellyManagerPage method fillProperties.

protected Map<String, String> fillProperties(Map<String, String> properties, String uid, ShellyManagerInterface th) {
    try {
        Configuration serviceConfig = configurationAdmin.getConfiguration("binding." + BINDING_ID);
        bindingConfig.updateFromProperties(serviceConfig.getProperties());
    } catch (IOException e) {
        logger.debug("ShellyManager: Unable to get bindingConfig");
    }
    properties.putAll(th.getThing().getProperties());
    Thing thing = th.getThing();
    ThingStatus status = thing.getStatus();
    properties.put("thingName", getString(thing.getLabel()));
    properties.put("thingStatus", status.toString());
    ThingStatusDetail detail = thing.getStatusInfo().getStatusDetail();
    properties.put("thingStatusDetail", detail.equals(ThingStatusDetail.NONE) ? "" : getString(detail.toString()));
    properties.put("thingStatusDescr", getString(thing.getStatusInfo().getDescription()));
    properties.put(ATTRIBUTE_UID, uid);
    ShellyDeviceProfile profile = th.getProfile();
    ShellyThingConfiguration config = thing.getConfiguration().as(ShellyThingConfiguration.class);
    ShellyDeviceStats stats = th.getStats();
    properties.putAll(stats.asProperties());
    for (Map.Entry<String, Object> p : thing.getConfiguration().getProperties().entrySet()) {
        String key = p.getKey();
        if (p.getValue() != null) {
            String value = p.getValue().toString();
            properties.put(key, value);
        }
    }
    State state = th.getChannelValue(CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_NAME);
    if (state != UnDefType.NULL) {
        addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_NAME);
    } else {
        // If the Shelly doesn't provide a device name (not configured) we use the service name
        String deviceName = getDeviceName(properties);
        properties.put(PROPERTY_DEV_NAME, !deviceName.isEmpty() ? deviceName : getString(properties.get(PROPERTY_SERVICE_NAME)));
    }
    if (config.userId.isEmpty()) {
        // Get defauls from Binding Config
        properties.put("userId", bindingConfig.defaultUserId);
        properties.put("password", bindingConfig.defaultPassword);
    }
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_RSSI);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_UPTIME);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_HEARTBEAT);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_ITEMP);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_WAKEUP);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_CHARGER);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_UPDATE);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_ALARM);
    addAttribute(properties, th, CHANNEL_GROUP_DEV_STATUS, CHANNEL_DEVST_CHARGER);
    properties.put(ATTRIBUTE_DEBUG_MODE, getOption(profile.settings.debugEnable));
    properties.put(ATTRIBUTE_DISCOVERABLE, String.valueOf(getBool(profile.settings.discoverable)));
    properties.put(ATTRIBUTE_WIFI_RECOVERY, String.valueOf(getBool(profile.settings.wifiRecoveryReboot)));
    properties.put(ATTRIBUTE_APR_MODE, profile.settings.apRoaming != null ? getOption(profile.settings.apRoaming.enabled) : "n/a");
    properties.put(ATTRIBUTE_APR_TRESHOLD, profile.settings.apRoaming != null ? getOption(profile.settings.apRoaming.threshold) : "n/a");
    properties.put(ATTRIBUTE_PWD_PROTECT, profile.auth ? "enabled, user=" + getString(profile.settings.login.username) : "disabled");
    String tz = getString(profile.settings.timezone);
    properties.put(ATTRIBUTE_TIMEZONE, (tz.isEmpty() ? "n/a" : tz) + ", auto-detect: " + getBool(profile.settings.tzautodetect));
    properties.put(ATTRIBUTE_ACTIONS_SKIPPED, profile.status.astats != null ? String.valueOf(profile.status.astats.skipped) : "n/a");
    properties.put(ATTRIBUTE_MAX_ITEMP, stats.maxInternalTemp > 0 ? stats.maxInternalTemp + " °C" : "n/a");
    // Shelly H&T: When external power is connected the battery level is not valid
    if (!profile.isHT || (getInteger(profile.settings.externalPower) == 0)) {
        addAttribute(properties, th, CHANNEL_GROUP_BATTERY, CHANNEL_SENSOR_BAT_LEVEL);
    } else {
        properties.put(CHANNEL_SENSOR_BAT_LEVEL, "USB");
    }
    String wiFiSignal = getString(properties.get(CHANNEL_DEVST_RSSI));
    if (!wiFiSignal.isEmpty()) {
        properties.put("wifiSignalRssi", wiFiSignal + " / " + stats.wifiRssi + " dBm");
        properties.put("imgWiFi", "imgWiFi" + wiFiSignal);
    }
    if (profile.settings.sntp != null) {
        properties.put(ATTRIBUTE_SNTP_SERVER, getString(profile.settings.sntp.server) + ", enabled: " + getBool((profile.settings.sntp.enabled)));
    }
    boolean coiotEnabled = true;
    if ((profile.settings.coiot != null) && (profile.settings.coiot.enabled != null)) {
        coiotEnabled = profile.settings.coiot.enabled;
    }
    properties.put(ATTRIBUTE_COIOT_STATUS, !coiotEnabled ? "Disbaled in settings" : "Events are " + (config.eventsCoIoT ? "enabled" : "disabled"));
    properties.put(ATTRIBUTE_COIOT_PEER, (profile.settings.coiot != null) && !getString(profile.settings.coiot.peer).isEmpty() ? profile.settings.coiot.peer : "Multicast");
    if (profile.status.cloud != null) {
        properties.put(ATTRIBUTE_CLOUD_STATUS, getBool(profile.settings.cloud.enabled) ? getBool(profile.status.cloud.connected) ? "connected" : "enabled" : "disabled");
    } else {
        properties.put(ATTRIBUTE_CLOUD_STATUS, "unknown");
    }
    if (profile.status.mqtt != null) {
        properties.put(ATTRIBUTE_MQTT_STATUS, getBool(profile.settings.mqtt.enable) ? getBool(profile.status.mqtt.connected) ? "connected" : "enabled" : "disabled");
    } else {
        properties.put(ATTRIBUTE_MQTT_STATUS, "unknown");
    }
    String statusIcon = "";
    ThingStatus ts = th.getThing().getStatus();
    switch(ts) {
        case UNINITIALIZED:
        case REMOVED:
        case REMOVING:
            statusIcon = ICON_UNINITIALIZED;
            break;
        case OFFLINE:
            ThingStatusDetail sd = th.getThing().getStatusInfo().getStatusDetail();
            if (uid.contains(THING_TYPE_SHELLYUNKNOWN_STR) || (sd == ThingStatusDetail.CONFIGURATION_ERROR) || (sd == ThingStatusDetail.HANDLER_CONFIGURATION_PENDING)) {
                statusIcon = ICON_CONFIG;
                break;
            }
        default:
            statusIcon = ts.toString();
    }
    properties.put(ATTRIBUTE_STATUS_ICON, statusIcon.toLowerCase());
    return properties;
}
Also used : Configuration(org.osgi.service.cm.Configuration) ShellyBindingConfiguration(org.openhab.binding.shelly.internal.config.ShellyBindingConfiguration) ShellyThingConfiguration(org.openhab.binding.shelly.internal.config.ShellyThingConfiguration) ThingStatus(org.openhab.core.thing.ThingStatus) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) IOException(java.io.IOException) State(org.openhab.core.types.State) ShellyThingConfiguration(org.openhab.binding.shelly.internal.config.ShellyThingConfiguration) ShellyDeviceProfile(org.openhab.binding.shelly.internal.api.ShellyDeviceProfile) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Thing(org.openhab.core.thing.Thing) ShellyDeviceStats(org.openhab.binding.shelly.internal.handler.ShellyDeviceStats)

Aggregations

TreeMap (java.util.TreeMap)2 ShellyDeviceProfile (org.openhab.binding.shelly.internal.api.ShellyDeviceProfile)2 ShellyThingConfiguration (org.openhab.binding.shelly.internal.config.ShellyThingConfiguration)2 ShellyDeviceStats (org.openhab.binding.shelly.internal.handler.ShellyDeviceStats)2 Thing (org.openhab.core.thing.Thing)2 ThingStatus (org.openhab.core.thing.ThingStatus)2 State (org.openhab.core.types.State)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ShellyBindingConfiguration (org.openhab.binding.shelly.internal.config.ShellyBindingConfiguration)1 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)1 Configuration (org.osgi.service.cm.Configuration)1