use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.
the class DigitalSTROMBinding method handleOpenhabEvent.
/**
* only works on openhabEvent! please copy "openhab/openhab.js" to your dSS
* server (/usr/share/dss/add-ons/) and "openhab.xml" to
* /usr/share/dss/data/subscriptions.d/ than you need to restart your dSS
*
* If you don't, you will not get detailed infos about, what exactly
* happened (for example: which device was turned on by a browser or handy
* app )
*
* @param eventItem
*/
private void handleOpenhabEvent(EventItem eventItem) {
if (eventItem != null) {
int zoneId = -1;
short groupId = -1;
short sceneId = -1;
boolean isDeviceCall = false;
String dsidStr = null;
String zoneIDStr = eventItem.getProperties().get(EventPropertyEnum.ZONEID);
if (zoneIDStr != null) {
try {
zoneId = Integer.parseInt(zoneIDStr);
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by handling event at parsing zoneId");
}
}
String sceneStr = eventItem.getProperties().get(EventPropertyEnum.SCENEID);
if (sceneStr != null) {
try {
sceneId = Short.parseShort(sceneStr);
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by handling event at parsing sceneId: " + sceneStr);
}
}
String groupStr = eventItem.getProperties().get(EventPropertyEnum.GROUPID);
if (groupStr != null) {
try {
groupId = Short.parseShort(groupStr);
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by handling event at parsing groupId");
}
}
dsidStr = eventItem.getProperties().get(EventPropertyEnum.DSID);
String deviceCallStr = eventItem.getProperties().get(EventPropertyEnum.IS_DEVICE_CALL);
if (deviceCallStr != null) {
isDeviceCall = deviceCallStr.equals("true");
}
if (sceneId != -1) {
if (!isEcho(dsidStr, sceneId)) {
if (isDeviceCall) {
if (dsidStr != null) {
Device device = getDsidToDeviceMap().get(dsidStr);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (isDimmScene(sceneId)) {
if (!device.doIgnoreScene(sceneId)) {
handleDimmScene(device, sceneId, (short) -1, true);
}
} else if (stateMapper.isMappable(sceneId)) {
boolean shouldBeOn = stateMapper.getMapping(sceneId);
if (!device.doIgnoreScene(sceneId)) {
if (shouldBeOn) {
device.setOutputValue(device.getMaxOutPutValue());
} else {
device.setOutputValue(0);
}
}
} else {
if (!device.doIgnoreScene(sceneId)) {
short value = device.getSceneOutputValue(sceneId);
if (value != -1) {
device.setOutputValue(value);
} else {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
initSceneOutputValue(device, sceneId);
}
}
}
}
}
} else {
if (isApartmentScene(sceneId)) {
handleApartmentScene(sceneId, groupId);
} else {
if (zoneId == 0) {
if (isDimmScene(sceneId)) {
Map<String, Device> deviceMap = getDsidToDeviceMap();
if (groupId == 0) {
Set<String> dsidSet = deviceMap.keySet();
if (dsidSet != null) {
for (String dsid : dsidSet) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
handleDimmScene(deviceMap.get(dsid), sceneId, groupId, false);
}
}
}
}
} else if (groupId != -1) {
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
if (map != null) {
List<String> dsidList = map.get(groupId);
if (dsidList != null) {
for (String dsid : dsidList) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
handleDimmScene(deviceMap.get(dsid), sceneId, groupId, false);
}
}
}
}
}
}
} else if (stateMapper.isMappable(sceneId)) {
boolean shouldBeOn = stateMapper.getMapping(sceneId);
if (groupId == 0) {
Map<String, Device> deviceMap = getDsidToDeviceMap();
Set<String> dsidSet = deviceMap.keySet();
if (dsidSet != null) {
for (String dsid : dsidSet) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
if (shouldBeOn) {
device.setOutputValue(device.getMaxOutPutValue());
} else {
device.setOutputValue(0);
}
}
}
}
}
} else if (groupId != -1) {
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
Map<String, Device> deviceMap = getDsidToDeviceMap();
if (map != null) {
List<String> dsidList = map.get(groupId);
if (dsidList != null) {
for (String dsid : dsidList) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
if (shouldBeOn) {
device.setOutputValue(device.getMaxOutPutValue());
} else {
device.setOutputValue(0);
}
}
}
}
}
}
}
} else {
Map<String, Device> deviceMap = getDsidToDeviceMap();
if (groupId != -1) {
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
if (map != null) {
List<String> dsidList = map.get(groupId);
if (dsidList != null) {
for (String dsid : dsidList) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
short sceneValue = device.getSceneOutputValue(sceneId);
if (sceneValue == -1) {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
initSceneOutputValue(device, sceneId);
} else {
device.setOutputValue(sceneValue);
}
}
}
}
}
}
}
}
} else {
if (isDimmScene(sceneId)) {
if (groupId != -1) {
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
if (map != null) {
List<String> devicesInGroup = map.get(groupId);
if (devicesInGroup != null) {
Map<String, Device> deviceMap = getDsidToDeviceMap();
for (String dsid : devicesInGroup) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
handleDimmScene(deviceMap.get(dsid), sceneId, groupId, false);
}
}
}
}
}
}
} else if (stateMapper.isMappable(sceneId)) {
boolean shouldBeOn = stateMapper.getMapping(sceneId);
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
Map<String, Device> deviceMap = getDsidToDeviceMap();
if (map != null) {
if (groupId != -1) {
List<String> devicesInGroup = map.get(groupId);
if (devicesInGroup != null) {
for (String dsid : devicesInGroup) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
if (shouldBeOn) {
device.setOutputValue(device.getMaxOutPutValue());
} else {
device.setOutputValue(0);
}
}
}
}
}
}
}
} else {
Map<Short, List<String>> map = getDigitalSTROMZoneGroupMap().get(zoneId);
Map<String, Device> deviceMap = getDsidToDeviceMap();
if (map != null) {
if (groupId != -1) {
List<String> devicesInGroup = map.get(groupId);
if (devicesInGroup != null) {
for (String dsid : devicesInGroup) {
Device device = deviceMap.get(dsid);
if (device != null) {
if (!device.containsSceneConfig(sceneId)) {
getSceneSpec(device, sceneId);
}
if (!device.doIgnoreScene(sceneId)) {
short outputValue = device.getSceneOutputValue(sceneId);
if (outputValue == -1) {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
initSceneOutputValue(device, sceneId);
} else {
device.setOutputValue(outputValue);
}
}
}
}
}
}
}
}
}
}
}
}
} else {
logger.error("an event without a sceneID; groupID:" + groupId + ", zoneID:" + zoneId + ", isDeviceCall:" + deviceCallStr + ", dsid:" + dsidStr);
}
}
}
use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.
the class DigitalSTROMBinding method deviceCall.
private void deviceCall(String itemName, Command cm) {
Device device = deviceMap.get(itemName);
if (device != null) {
if (cm instanceof org.openhab.core.library.types.OnOffType) {
if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.ON)) {
boolean transmitted = digitalSTROM.turnDeviceOn(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(device.getMaxOutPutValue());
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MAXIMUM.getSceneNumber());
}
} else if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.OFF)) {
boolean transmitted = digitalSTROM.turnDeviceOff(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(0);
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MINIMUM.getSceneNumber());
}
}
} else if (cm instanceof org.openhab.core.library.types.IncreaseDecreaseType) {
if (!device.isDimmable()) {
logger.warn("device is not in dimm mode: " + itemName + " outputMode: " + device.getOutputMode().getMode());
return;
}
if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.INCREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.INCREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.INCREMENT.getSceneNumber());
if (device.getOutputValue() == 0) {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
} else {
device.increase();
}
} else {
logger.error("transmitting increase command FAILED " + itemName);
}
} else if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.DECREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.DECREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.DECREMENT.getSceneNumber());
device.decrease();
} else {
logger.error("transmitting decrease command FAILED " + itemName);
}
}
} else if (cm instanceof org.openhab.core.library.types.PercentType) {
int percent = -1;
try {
percent = (int) Float.parseFloat(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException on a PercentType with command: " + cm.toString());
}
if (percent != -1) {
if (percent > -1 && percent < 101) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int old = device.getSlatPosition();
device.setSlatPosition(fromPercentToValue(percent, device.getMaxSlatPosition()));
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, fromPercentToValue(percent, device.getMaxSlatPosition()));
if (!transmitted) {
device.setSlatPosition(old);
logger.error("could NOT successfully set new value for slats ..." + cm.toString());
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
}
} else if (cm instanceof org.openhab.core.library.types.StopMoveType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
logger.warn("stop and move command NOT possible for slats, use PercentType command or up and down please");
} else {
handleStopMoveForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleStopMoveForRollershutter(device, cm);
}
} else if (cm instanceof org.openhab.core.library.types.UpDownType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
// 255 is max open, 0 is closed
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.UP)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition + DeviceConstants.MOVE_STEP_SLAT;
if (newPosition > device.getMaxSlatPosition()) {
newPosition = device.getMaxSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
} else if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.DOWN)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition - DeviceConstants.MOVE_STEP_SLAT;
if (newPosition < device.getMinSlatPosition()) {
newPosition = device.getMinSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
}
} else {
handleUpDownForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleUpDownForRollershutter(device, cm);
} else {
logger.warn("Wrong item configuration ... this hardware is not a rollershutter: " + itemName);
}
}
} else {
if (cm instanceof DecimalType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(((DecimalType) cm).intValue()), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(((DecimalType) cm).intValue()), false);
}
}
} else if (cm instanceof StringType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
int scene = -1;
try {
scene = Integer.parseInt(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by parsing " + cm.toString() + " for " + confItem.itemName);
}
if (scene != -1) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(scene), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(scene), false);
}
}
}
} else {
logger.warn("couldn't find digitalstrom device for " + itemName);
}
}
}
use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.
the class DigitalSTROMBinding method bindingChanged.
@Override
public void bindingChanged(BindingProvider provider, String itemName) {
if (provider instanceof DigitalSTROMBindingProvider) {
// remove device associated with the item
Device device = deviceMap.get(itemName);
if (device != null) {
List<String> itemNamesForDsid = getItemNamesForDsid(device.getDSID().getValue());
if (itemNamesForDsid.size() == 1) {
device.removeDeviceListener(this);
removeSensorJobs(device.getDSID());
}
}
deviceMap.remove(itemName);
// initialize the device
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.dsid != null) {
if (rawDsidToDeviceMap.size() == 0 && serverIsFound()) {
rawDsidToDeviceMap = getAllDigitalSTROMDevicesMap();
}
device = rawDsidToDeviceMap.get(confItem.dsid.getValue());
if (device != null) {
addDevice(itemName, device);
updateItemState(confItem.item);
handleStructure(digitalSTROM.getApartmentStructure(getSessionToken()));
}
}
}
}
use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.
the class DigitalSTROMBinding method updateItemState.
private void updateItemState(Item item) {
if (item != null) {
Device device = deviceMap.get(item.getName());
if (device != null) {
State state = null;
if (item instanceof DimmerItem) {
state = new PercentType(getPercent(device.getMaxOutPutValue(), device.getOutputValue()));
} else if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
state = device.getOutputValue() > 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof NumberItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
} else if (item instanceof RollershutterItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int output = getPercent(device.getMaxSlatPosition(), device.getSlatPosition());
state = new PercentType(100 - output);
} else if (confItem.context != null && confItem.context.equals(ContextConfig.awning)) {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(output);
} else {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(100 - output);
}
}
} else if (item instanceof StringItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
if (value != -1) {
state = new DecimalType(value);
}
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
}
eventPublisher.postUpdate(item.getName(), state);
} else {
logger.error("couldn't update item state, because device is null: " + item.getName());
}
} else {
logger.error("couldn't update item state, because item is null");
}
}
use of org.openhab.binding.digitalstrom.internal.client.entity.Device in project openhab1-addons by openhab.
the class DigitalSTROMJSONImpl method getApartmentDevices.
@Override
public List<Device> getApartmentDevices(String token, boolean unassigned) {
String response = null;
if (unassigned) {
response = transport.execute(JSONRequestConstants.JSON_APARTMENT_GET_DEVICES + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_UNASSIGNED_TRUE);
} else {
response = transport.execute(JSONRequestConstants.JSON_APARTMENT_GET_DEVICES + JSONRequestConstants.PARAMETER_TOKEN + token);
}
JSONObject responseObj = handler.toJSONObject(response);
if (handler.checkResponse(responseObj) && responseObj.get(JSONApiResponseKeysEnum.APARTMENT_GET_DEVICES.getKey()) instanceof JSONArray) {
JSONArray array = (JSONArray) responseObj.get(JSONApiResponseKeysEnum.APARTMENT_GET_DEVICES.getKey());
List<Device> deviceList = new LinkedList<Device>();
for (int i = 0; i < array.size(); i++) {
if (array.get(i) instanceof org.json.simple.JSONObject) {
deviceList.add(new JSONDeviceImpl((JSONObject) array.get(i)));
}
}
return deviceList;
}
return new LinkedList<Device>();
}
Aggregations