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);
}
}
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) {
}
}
}
}
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;
}
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;
}
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);
}
}
}
}
Aggregations