use of org.openhab.binding.mystromecopower.MyStromEcoPowerBindingProvider in project openhab1-addons by openhab.
the class MyStromEcoPowerBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// the code being executed when a command was sent on the openHAB
// event bus goes here. This method is only called if one of the
// BindingProviders provide a binding for the given 'itemName'.
logger.debug("internalReceiveCommand() is called!");
String deviceId = null;
for (MyStromEcoPowerBindingProvider provider : providers) {
String switchFriendlyName = provider.getMystromFriendlyName(itemName);
deviceId = this.devicesMap.get(switchFriendlyName);
logger.debug("item '{}' is configured as '{}'", itemName, switchFriendlyName);
if (deviceId != null) {
if (provider.getIsSwitch(itemName)) {
try {
logger.debug("Command '{}' is about to be sent to item '{}'", command, itemName);
if (OnOffType.ON.equals(command) || OnOffType.OFF.equals(command)) {
// on/off command
boolean onOff = OnOffType.ON.equals(command);
logger.debug("command '{}' transformed to '{}'", command, onOff ? "on" : "off");
boolean actualState = this.mystromClient.getDeviceInfo(deviceId).state.equals("on");
if (onOff == actualState) {
// mystrom state is the same, may be due to
// change state on/off too
// rapidly, so postpone change state
String scheduledCommand = deviceId + ";" + onOff;
logger.debug("Schedule command: " + scheduledCommand);
JobDetail job = JobBuilder.newJob(org.openhab.binding.mystromecopower.internal.util.ChangeStateJob.class).usingJobData(org.openhab.binding.mystromecopower.internal.util.ChangeStateJob.JOB_DATA_CONTENT_KEY, scheduledCommand).withIdentity(itemName, "MYSTROMECOPOWER").build();
Date dateTrigger = new Date(System.currentTimeMillis() + 5000L);
Trigger trigger = newTrigger().forJob(job).withIdentity(itemName + "_" + dateTrigger + "_trigger", "MYSTROMECOPOWER").startAt(dateTrigger).build();
this.scheduler.scheduleJob(job, trigger);
} else {
if (this.masterDevice == null || (this.masterDevice != null && deviceId != this.masterDevice.id)) {
// This is not the master device.
if (!this.mystromClient.ChangeState(deviceId, onOff)) {
// Unsuccessful state change, inform bus
// that the good
// state is the old one.
eventPublisher.postUpdate(itemName, onOff ? OnOffType.OFF : OnOffType.ON);
}
} else {
// This is the mater device.
if (this.masterDevice != null && OnOffType.OFF.equals(command)) {
// Do a reset if try to set OFF the
// master device.
logger.debug("Restart master device");
this.mystromClient.RestartMaster(deviceId);
}
}
}
}
} catch (Exception e) {
logger.error("Failed to send '{}' command", command, e);
}
}
} else {
logger.error("Unable to send command to '{}'. Device is not in discovery table", itemName);
}
}
}
use of org.openhab.binding.mystromecopower.MyStromEcoPowerBindingProvider 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