Search in sources :

Example 1 with MystromDevice

use of org.openhab.binding.mystromecopower.internal.api.model.MystromDevice in project openhab1-addons by openhab.

the class MyStromEcoPowerBinding method mystromDiscovery.

/**
     * Do a discovery to find all devices on the mystrom server. Logs device's
     * name and id.
     *
     * @throws MalformedURLException
     * @throws IOException
     */
private void mystromDiscovery() throws MalformedURLException, IOException {
    List<MystromDevice> devices;
    logger.info("Do mystrom discovery");
    this.devicesMap.clear();
    if (this.mystromClient.login() == false) {
        logger.info("Invalid user or password");
        return;
    }
    devices = this.mystromClient.getDevices();
    for (MystromDevice mystromDevice : devices) {
        this.devicesMap.put(mystromDevice.name, mystromDevice.id);
        if (mystromDevice.type.equals("mst")) {
            this.masterDevice = mystromDevice;
        }
        logger.info("Mystrom device name: '{}', mystrom device id:'{}'", mystromDevice.name, mystromDevice.id);
    }
}
Also used : MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)

Example 2 with MystromDevice

use of org.openhab.binding.mystromecopower.internal.api.model.MystromDevice in project openhab1-addons by openhab.

the class MystromClient method getDevices.

private List<MystromDevice> getDevices(boolean minimalMode) {
    Reader reader = null;
    logger.debug("get all devices state");
    try {
        String url = API_URL + "devices" + "?authToken=" + this.authToken;
        if (minimalMode) {
            url = url + "&minimal=true";
        }
        HttpURLConnection httpURLConnection;
        httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
        httpURLConnection.connect();
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            logger.error("Get devices http code: '{}'", responseCode);
            return new ArrayList<MystromDevice>();
        }
        InputStream inputStream = httpURLConnection.getInputStream();
        reader = new InputStreamReader(inputStream, "UTF-8");
        JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
        String status = jsonObject.get("status").getAsString();
        if (!status.equals("ok")) {
            logger.error("Error while getting devices: '{}'", status);
            return new ArrayList<MystromDevice>();
        }
        GetDevicesResult result = gson.fromJson(jsonObject, GetDevicesResult.class);
        logger.debug("Devices discovery sucessfull, found '{}' devices", result.devices.size());
        return result.devices;
    } catch (Exception ex) {
        logger.error("Error getting devices: '{}'", ex.toString());
        return new ArrayList<MystromDevice>();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice) HttpURLConnection(java.net.HttpURLConnection) GetDevicesResult(org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult)

Example 3 with MystromDevice

use of org.openhab.binding.mystromecopower.internal.api.model.MystromDevice in project openhab1-addons by openhab.

the class MockMystromClient method getDeviceInfo.

@Override
public MystromDevice getDeviceInfo(String deviceId) {
    MystromDevice device = this.devices.get(deviceId);
    device.power = Double.toString(Math.random() * 100);
    if (device.id == "3") {
        device.state = "on";
    }
    return device;
}
Also used : MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)

Example 4 with MystromDevice

use of org.openhab.binding.mystromecopower.internal.api.model.MystromDevice in project openhab1-addons by openhab.

the class MockMystromClient method getDevices.

@Override
public List<MystromDevice> getDevices() {
    ArrayList<MystromDevice> foundDevices = new ArrayList<MystromDevice>();
    this.devices.clear();
    MystromDevice device1 = new MystromDevice();
    device1.name = "Halogène/Multi prises";
    device1.id = "1";
    device1.state = "on";
    devices.put(device1.id, device1);
    foundDevices.add(device1);
    MystromDevice device2 = new MystromDevice();
    device2.name = "Tv/HomeCinéma";
    device2.id = "2";
    device2.state = "off";
    devices.put(device2.id, device2);
    foundDevices.add(device2);
    MystromDevice device3 = new MystromDevice();
    device3.name = "Chauffe eau";
    device3.id = "3";
    device3.state = "offline";
    devices.put(device3.id, device3);
    foundDevices.add(device3);
    return foundDevices;
}
Also used : ArrayList(java.util.ArrayList) MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)

Example 5 with MystromDevice

use of org.openhab.binding.mystromecopower.internal.api.model.MystromDevice in project openhab1-addons by openhab.

the class MyStromEcoPowerBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    // the frequently executed code (polling) goes here ...
    logger.debug("execute() method is called!");
    if (this.devicesMap.isEmpty()) {
        return;
    }
    List<MystromDevice> devices = this.mystromClient.getDevicesState();
    for (MyStromEcoPowerBindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            logger.debug("Mystrom eco power switch '{}' state will be updated", itemName);
            String friendlyName = provider.getMystromFriendlyName(itemName);
            String id = this.devicesMap.get(friendlyName);
            if (id != null) {
                MystromDevice device = null;
                for (MystromDevice searchDevice : devices) {
                    if (searchDevice.id.equals(id)) {
                        device = searchDevice;
                        break;
                    }
                }
                if (device != null) {
                    if (provider.getIsSwitch(itemName)) {
                        State state = device.state.equals("on") ? OnOffType.ON : OnOffType.OFF;
                        eventPublisher.postUpdate(itemName, state);
                    }
                    if (provider.getIsStringItem(itemName)) {
                        // publish state of device, on/off/offline
                        eventPublisher.postUpdate(itemName, new StringType(device.state));
                    }
                    if (provider.getIsNumberItem(itemName)) {
                        eventPublisher.postUpdate(itemName, new DecimalType(device.power));
                    }
                }
            } else {
                logger.warn("The device itemName '{}' not found on discovery. Verify device is not offline", itemName);
            }
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) MyStromEcoPowerBindingProvider(org.openhab.binding.mystromecopower.MyStromEcoPowerBindingProvider) DecimalType(org.openhab.core.library.types.DecimalType) MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)

Aggregations

MystromDevice (org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)5 ArrayList (java.util.ArrayList)2 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 MyStromEcoPowerBindingProvider (org.openhab.binding.mystromecopower.MyStromEcoPowerBindingProvider)1 GetDevicesResult (org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult)1 DecimalType (org.openhab.core.library.types.DecimalType)1 StringType (org.openhab.core.library.types.StringType)1 State (org.openhab.core.types.State)1