Search in sources :

Example 1 with ShellyManagerInterface

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

the class ShellyManagerActionPage method generateContent.

@Override
public ShellyMgrResponse generateContent(String path, Map<String, String[]> parameters) throws ShellyApiException {
    String action = getUrlParm(parameters, URLPARM_ACTION);
    String uid = getUrlParm(parameters, URLPARM_UID);
    String update = getUrlParm(parameters, URLPARM_UPDATE);
    if (uid.isEmpty() || action.isEmpty()) {
        return new ShellyMgrResponse("Invalid URL parameters: " + parameters.toString(), HttpStatus.BAD_REQUEST_400);
    }
    Map<String, String> properties = new HashMap<>();
    properties.put(ATTRIBUTE_METATAG, "");
    properties.put(ATTRIBUTE_CSS_HEADER, "");
    properties.put(ATTRIBUTE_CSS_FOOTER, "");
    String html = loadHTML(HEADER_HTML, properties);
    ShellyManagerInterface th = getThingHandler(uid);
    if (th != null) {
        fillProperties(properties, uid, th);
        Map<String, String> actions = getActions(th.getProfile());
        String actionUrl = SHELLY_MGR_OVERVIEW_URI;
        // Default
        String actionButtonLabel = "OK";
        String serviceName = getValue(properties, PROPERTY_SERVICE_NAME);
        String message = "";
        ShellyThingConfiguration config = getThingConfig(th, properties);
        ShellyDeviceProfile profile = th.getProfile();
        ShellyHttpApi api = th.getApi();
        new ShellyHttpApi(uid, config, httpClient);
        int refreshTimer = 0;
        switch(action) {
            case ACTION_RES_STATS:
                th.resetStats();
                message = getMessageP("action.resstats.confirm", MCINFO);
                refreshTimer = 3;
                break;
            case ACTION_RESTART:
                if ("yes".equalsIgnoreCase(update)) {
                    message = getMessageP("action.restart.info", MCINFO);
                    actionButtonLabel = "Ok";
                    new Thread(() -> {
                        // schedule asynchronous reboot
                        try {
                            api.deviceReboot();
                        } catch (ShellyApiException e) {
                        // maybe the device restarts before returning the http response
                        }
                        // refresh after reboot
                        setRestarted(th, uid);
                    }).start();
                    refreshTimer = profile.isMotion ? 60 : 30;
                } else {
                    message = getMessageS("action.restart.confirm", MCINFO);
                    actionUrl = buildActionUrl(uid, action);
                }
                break;
            case ACTION_PROTECT:
                // Get device settings
                if (config.userId.isEmpty() || config.password.isEmpty()) {
                    message = getMessageP("action.protect.id-missing", MCWARNING);
                    break;
                }
                if (!"yes".equalsIgnoreCase(update)) {
                    ShellySettingsLogin status = api.getLoginSettings();
                    message = getMessage("action.protect.status", getBool(status.enabled) ? "enabled" : "disabled", status.username) + getMessageP("action.protect.new", MCINFO, config.userId, config.password);
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    api.setLoginCredentials(config.userId, config.password);
                    message = getMessageP("action.protect.confirm", MCINFO, config.userId, config.password);
                    refreshTimer = 3;
                }
                break;
            case ACTION_SETCOIOT_MCAST:
            case ACTION_SETCOIOT_PEER:
                if ((profile.settings.coiot == null) || (profile.settings.coiot.peer == null)) {
                    // feature not available
                    message = getMessage("coiot.mode-not-suppored", MCWARNING, action);
                    break;
                }
                String peer = getString(profile.settings.coiot.peer);
                boolean mcast = peer.isEmpty() || SHELLY_COIOT_MCAST.equalsIgnoreCase(peer);
                String newPeer = mcast ? localIp + ":" + ShellyCoapJSonDTO.COIOT_PORT : SHELLY_COIOT_MCAST;
                String displayPeer = mcast ? newPeer : "Multicast";
                if (profile.isMotion && action.equalsIgnoreCase(ACTION_SETCOIOT_MCAST)) {
                    // feature not available
                    message = getMessageP("coiot.multicast-not-supported", "warning", displayPeer);
                    break;
                }
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessageP("coiot.current-peer", MCMESSAGE, mcast ? "Multicast" : peer) + getMessageP("coiot.new-peer", MCINFO, displayPeer) + getMessageP(mcast ? "coiot.mode-peer" : "coiot.mode-mcast", MCMESSAGE);
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    new Thread(() -> {
                        // schedule asynchronous reboot
                        try {
                            api.setCoIoTPeer(newPeer);
                            api.deviceReboot();
                        } catch (ShellyApiException e) {
                        // maybe the device restarts before returning the http response
                        }
                        // refresh after reboot
                        setRestarted(th, uid);
                    }).start();
                    // The device needs a restart after changing the peer mode
                    message = getMessageP("action.restart.info", MCINFO);
                    refreshTimer = 30;
                }
                break;
            case ACTION_ENCLOUD:
            case ACTION_DISCLOUD:
                boolean enabled = action.equals(ACTION_ENCLOUD);
                api.setCloud(enabled);
                message = getMessageP("action.setcloud.config", MCINFO, enabled ? "enabled" : "disabled");
                refreshTimer = 20;
                break;
            case ACTION_RESET:
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessageP("action.reset.warning", MCWARNING, serviceName);
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    new Thread(() -> {
                        // schedule asynchronous reboot
                        try {
                            api.factoryReset();
                            setRestarted(th, uid);
                        } catch (ShellyApiException e) {
                        // maybe the device restarts before returning the http response
                        }
                    }).start();
                    message = getMessageP("action.reset.confirm", MCINFO, serviceName);
                    refreshTimer = 5;
                }
                break;
            case ACTION_OTACHECK:
                try {
                    ShellyOtaCheckResult result = api.checkForUpdate();
                    message = getMessage("action.checkupd." + result.status);
                } catch (ShellyApiException e) {
                    // maybe the device restarts before returning the http response
                    message = getMessageP("action.checkupd.failed", e.toString());
                }
                refreshTimer = 3;
                break;
            case ACTION_ENDEBUG:
            case ACTION_DISDEBUG:
                boolean enable = ACTION_ENDEBUG.equalsIgnoreCase(action);
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessage(enable ? "action.debug-enable" : "action.debug-disable");
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    new Thread(() -> {
                        // schedule asynchronous reboot
                        try {
                            api.setDebug(enable);
                        } catch (ShellyApiException e) {
                        // maybe the device restarts before returning the http response
                        }
                    }).start();
                    message = getMessage("action.debug-confirm", enable ? "enabled" : "disabled");
                    refreshTimer = 3;
                }
                break;
            case ACTION_RESSTA:
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessage("action.resetsta-info");
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    try {
                        api.resetStaCache();
                        message = getMessage("action.resetsta-confirm");
                    } catch (ShellyApiException e) {
                        message = getMessageP("action.resetsta-failed", e.toString());
                    }
                    refreshTimer = 10;
                }
                break;
            case ACTION_ENWIFIREC:
            case ACTION_DISWIFIREC:
                enable = ACTION_ENWIFIREC.equalsIgnoreCase(action);
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessage(enable ? "action.setwifirec-enable" : "action.setwifirec-disable");
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    try {
                        api.setWiFiRecovery(enable);
                        message = getMessage("action.setwifirec-confirm", enable ? "enabled" : "disabled");
                    } catch (ShellyApiException e) {
                        message = getMessage("action.setwifirec-failed", e.toString());
                    }
                    refreshTimer = 3;
                }
                break;
            case ACTION_ENAPROAMING:
            case ACTION_DISAPROAMING:
                enable = ACTION_ENAPROAMING.equalsIgnoreCase(action);
                if (!"yes".equalsIgnoreCase(update)) {
                    message = getMessage(enable ? "action.aproaming-enable" : "action.aproaming-disable");
                    actionUrl = buildActionUrl(uid, action);
                } else {
                    try {
                        api.setApRoaming(enable);
                        message = getMessage("action.aproaming-confirm", enable ? "enabled" : "disabled");
                    } catch (ShellyApiException e) {
                        message = getMessage("action.aproaming-failed", e.toString());
                    }
                    refreshTimer = 3;
                }
                break;
            case ACTION_GETDEB:
            case ACTION_GETDEB1:
                try {
                    message = api.getDebugLog(ACTION_GETDEB.equalsIgnoreCase(action) ? "log" : "log1");
                    message = message.replaceAll("[\r]", "").replaceAll("[\r\n]", "<br>");
                } catch (ShellyApiException e) {
                    message = getMessage("action.getdebug-failed", e.toString());
                }
                break;
            case ACTION_NONE:
                break;
            default:
                logger.warn("{}: {}", LOG_PREFIX, getMessage("action.unknown", action));
        }
        // get description for command
        properties.put(ATTRIBUTE_ACTION, getString(actions.get(action)));
        properties.put(ATTRIBUTE_ACTION_BUTTON, actionButtonLabel);
        properties.put(ATTRIBUTE_ACTION_URL, actionUrl);
        message = fillAttributes(message, properties);
        properties.put(ATTRIBUTE_MESSAGE, message);
        properties.put(ATTRIBUTE_REFRESH, String.valueOf(refreshTimer));
        html += loadHTML(ACTION_HTML, properties);
        // trigger background update
        th.requestUpdates(1, refreshTimer > 0);
    }
    properties.clear();
    html += loadHTML(FOOTER_HTML, properties);
    return new ShellyMgrResponse(html, HttpStatus.OK_200);
}
Also used : ShellyManagerInterface(org.openhab.binding.shelly.internal.handler.ShellyManagerInterface) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ShellyApiException(org.openhab.binding.shelly.internal.api.ShellyApiException) ShellyOtaCheckResult(org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyOtaCheckResult) ShellyThingConfiguration(org.openhab.binding.shelly.internal.config.ShellyThingConfiguration) ShellyHttpApi(org.openhab.binding.shelly.internal.api.ShellyHttpApi) ShellyDeviceProfile(org.openhab.binding.shelly.internal.api.ShellyDeviceProfile) ShellySettingsLogin(org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsLogin)

Example 2 with ShellyManagerInterface

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

the class ShellyManagerOverviewPage method generateContent.

@Override
public ShellyMgrResponse generateContent(String path, Map<String, String[]> parameters) throws ShellyApiException {
    String filter = getUrlParm(parameters, URLPARM_FILTER).toLowerCase();
    String action = getUrlParm(parameters, URLPARM_ACTION).toLowerCase();
    String uidParm = getUrlParm(parameters, URLPARM_UID).toLowerCase();
    logger.debug("Generating overview for {} devices", getThingHandlers().size());
    String html = "";
    Map<String, String> properties = new HashMap<>();
    properties.put(ATTRIBUTE_METATAG, "<meta http-equiv=\"refresh\" content=\"60\" />");
    properties.put(ATTRIBUTE_CSS_HEADER, loadHTML(OVERVIEW_HEADER, properties));
    String deviceHtml = "";
    TreeMap<String, ShellyManagerInterface> sortedMap = new TreeMap<>();
    for (Map.Entry<String, ShellyManagerInterface> th : getThingHandlers().entrySet()) {
        // sort by Device Name
        ShellyManagerInterface handler = th.getValue();
        sortedMap.put(getDisplayName(handler.getThing().getProperties()), handler);
    }
    html = loadHTML(HEADER_HTML, properties);
    html += loadHTML(OVERVIEW_HTML, properties);
    int filteredDevices = 0;
    for (Map.Entry<String, ShellyManagerInterface> handler : sortedMap.entrySet()) {
        try {
            ShellyManagerInterface th = handler.getValue();
            ThingStatus status = th.getThing().getStatus();
            ShellyDeviceProfile profile = th.getProfile();
            // handler.getKey();
            String uid = getString(th.getThing().getUID().getAsString());
            if (action.equals(ACTION_REFRESH) && (uidParm.isEmpty() || uidParm.equals(uid))) {
                // Refresh thing status, this is asynchronosly and takes 0-3sec
                th.requestUpdates(1, true);
            } else if (action.equals(ACTION_RES_STATS) && (uidParm.isEmpty() || uidParm.equals(uid))) {
                th.resetStats();
            } else if (action.equals(ACTION_OTACHECK) && (uidParm.isEmpty() || uidParm.equals(uid))) {
                th.resetStats();
            }
            Map<String, String> warnings = getStatusWarnings(th);
            if (applyFilter(th, filter) || (filter.equals(FILTER_ATTENTION) && !warnings.isEmpty())) {
                filteredDevices++;
                properties.clear();
                fillProperties(properties, uid, handler.getValue());
                String deviceType = getDeviceType(properties);
                properties.put(ATTRIBUTE_DISPLAY_NAME, getDisplayName(properties));
                properties.put(ATTRIBUTE_DEV_STATUS, fillDeviceStatus(warnings));
                if (!warnings.isEmpty() && (status != ThingStatus.UNKNOWN)) {
                    properties.put(ATTRIBUTE_STATUS_ICON, ICON_ATTENTION);
                }
                if (!"unknown".equalsIgnoreCase(deviceType) && (status == ThingStatus.ONLINE)) {
                    properties.put(ATTRIBUTE_FIRMWARE_SEL, fillFirmwareHtml(uid, deviceType, profile.mode));
                    properties.put(ATTRIBUTE_ACTION_LIST, fillActionHtml(th, uid));
                } else {
                    properties.put(ATTRIBUTE_FIRMWARE_SEL, "");
                    properties.put(ATTRIBUTE_ACTION_LIST, "");
                }
                html += loadHTML(OVERVIEW_DEVICE, properties);
            }
        } catch (ShellyApiException e) {
            logger.debug("{}: Exception", LOG_PREFIX, e);
        }
    }
    properties.clear();
    properties.put("numberDevices", "<span class=\"footerDevices\">" + "Number of devices: " + filteredDevices + " of " + String.valueOf(getThingHandlers().size()) + "&nbsp;</span>");
    properties.put(ATTRIBUTE_CSS_FOOTER, loadHTML(OVERVIEW_FOOTER, properties));
    html += deviceHtml + loadHTML(FOOTER_HTML, properties);
    return new ShellyMgrResponse(fillAttributes(html, properties), HttpStatus.OK_200);
}
Also used : ShellyManagerInterface(org.openhab.binding.shelly.internal.handler.ShellyManagerInterface) HashMap(java.util.HashMap) ThingStatus(org.openhab.core.thing.ThingStatus) ShellyApiException(org.openhab.binding.shelly.internal.api.ShellyApiException) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ShellyDeviceProfile(org.openhab.binding.shelly.internal.api.ShellyDeviceProfile)

Example 3 with ShellyManagerInterface

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

the class ShellyManagerOtaPage method generatePage.

public ShellyMgrResponse generatePage(String path, Map<String, String[]> parameters) throws ShellyApiException {
    String uid = getUrlParm(parameters, URLPARM_UID);
    String version = getUrlParm(parameters, URLPARM_VERSION);
    String update = getUrlParm(parameters, URLPARM_UPDATE);
    String connection = getUrlParm(parameters, URLPARM_CONNECTION);
    String url = getUrlParm(parameters, URLPARM_URL);
    if (uid.isEmpty() || (version.isEmpty() && connection.isEmpty()) || !getThingHandlers().containsKey(uid)) {
        return new ShellyMgrResponse("Invalid URL parameters: " + parameters, HttpStatus.BAD_REQUEST_400);
    }
    Map<String, String> properties = new HashMap<>();
    String html = loadHTML(HEADER_HTML, properties);
    ShellyManagerInterface th = getThingHandlers().get(uid);
    if (th != null) {
        properties = fillProperties(new HashMap<>(), uid, th);
        ShellyThingConfiguration config = getThingConfig(th, properties);
        ShellyDeviceProfile profile = th.getProfile();
        String deviceType = getDeviceType(properties);
        String uri = !url.isEmpty() && connection.equals(CONNECTION_TYPE_CUSTOM) ? url : getFirmwareUrl(config.deviceIp, deviceType, profile.mode, version, connection.equals(CONNECTION_TYPE_LOCAL));
        if (connection.equalsIgnoreCase(CONNECTION_TYPE_INTERNET)) {
            // - otherwise qualify full url with ?url=xxxx
            if (uri.contains("update=") || uri.contains("beta=")) {
                url = uri;
            } else {
                url = URLPARM_URL + "=" + uri;
            }
        } else if (connection.equalsIgnoreCase(CONNECTION_TYPE_LOCAL)) {
            // redirect to local server -> http://<oh-ip>:<oh-port>/shelly/manager/ota?deviceType=xxx&version=xxx
            String modeParm = !profile.mode.isEmpty() ? "&" + URLPARM_DEVMODE + "=" + profile.mode : "";
            url = URLPARM_URL + "=http://" + localIp + ":" + localPort + SHELLY_MGR_OTA_URI + urlEncode("?" + URLPARM_DEVTYPE + "=" + deviceType + modeParm + "&" + URLPARM_VERSION + "=" + version);
        } else if (connection.equalsIgnoreCase(CONNECTION_TYPE_CUSTOM)) {
            // else custom -> don't modify url
            uri = url;
            url = URLPARM_URL + "=" + uri;
        }
        String updateUrl = url;
        properties.put(ATTRIBUTE_VERSION, version);
        properties.put(ATTRIBUTE_FW_URL, uri);
        properties.put(ATTRIBUTE_UPDATE_URL, "http://" + getDeviceIp(properties) + "/ota?" + updateUrl);
        properties.put(URLPARM_CONNECTION, connection);
        if ("yes".equalsIgnoreCase(update)) {
            // do the update
            th.setThingOffline(ThingStatusDetail.FIRMWARE_UPDATING, "offline.status-error-fwupgrade");
            html += loadHTML(FWUPDATE2_HTML, properties);
            new Thread(() -> {
                // schedule asynchronous reboot
                try {
                    ShellyHttpApi api = new ShellyHttpApi(uid, config, httpClient);
                    ShellySettingsUpdate result = api.firmwareUpdate(updateUrl);
                    String status = getString(result.status);
                    logger.info("{}: {}", th.getThingName(), getMessage("fwupdate.initiated", status));
                    // Shelly Motion needs almost 2min for upgrade
                    scheduleUpdate(th, uid + "_upgrade", profile.isMotion ? 110 : 30);
                } catch (ShellyApiException e) {
                    // maybe the device restarts before returning the http response
                    logger.warn("{}: {}", th.getThingName(), getMessage("fwupdate.initiated", e.toString()));
                }
            }).start();
        } else {
            String message = getMessageP("fwupdate.confirm", MCINFO);
            properties.put(ATTRIBUTE_MESSAGE, message);
            html += loadHTML(FWUPDATE1_HTML, properties);
        }
    }
    html += loadHTML(FOOTER_HTML, properties);
    return new ShellyMgrResponse(html, HttpStatus.OK_200);
}
Also used : ShellyManagerInterface(org.openhab.binding.shelly.internal.handler.ShellyManagerInterface) HashMap(java.util.HashMap) ShellySettingsUpdate(org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsUpdate) ShellyThingConfiguration(org.openhab.binding.shelly.internal.config.ShellyThingConfiguration) ShellyHttpApi(org.openhab.binding.shelly.internal.api.ShellyHttpApi) ShellyApiException(org.openhab.binding.shelly.internal.api.ShellyApiException) ShellyDeviceProfile(org.openhab.binding.shelly.internal.api.ShellyDeviceProfile)

Aggregations

HashMap (java.util.HashMap)3 ShellyApiException (org.openhab.binding.shelly.internal.api.ShellyApiException)3 ShellyDeviceProfile (org.openhab.binding.shelly.internal.api.ShellyDeviceProfile)3 ShellyManagerInterface (org.openhab.binding.shelly.internal.handler.ShellyManagerInterface)3 ShellyHttpApi (org.openhab.binding.shelly.internal.api.ShellyHttpApi)2 ShellyThingConfiguration (org.openhab.binding.shelly.internal.config.ShellyThingConfiguration)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ShellyOtaCheckResult (org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellyOtaCheckResult)1 ShellySettingsLogin (org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsLogin)1 ShellySettingsUpdate (org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.ShellySettingsUpdate)1 ThingStatus (org.openhab.core.thing.ThingStatus)1