use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.
the class IhcResourceInteractionService method resourceQuery.
/**
* Query resource value from controller.
*
*
* @param resoureId
* Resource Identifier.
* @return Resource value.
*/
public WSResourceValue resourceQuery(int resoureId) throws IhcExecption {
final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soapenv:Body>" + " <ns1:getRuntimeValue1 xmlns:ns1=\"utcs\">%s</ns1:getRuntimeValue1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
String query = String.format(soapQuery, String.valueOf(resoureId));
openConnection(url);
String response = sendQuery(query, timeout);
closeConnection();
NodeList nodeList;
try {
nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getRuntimeValue2");
if (nodeList.getLength() == 1) {
WSResourceValue val = parseResourceValue(nodeList.item(0), 2);
if (val.getResourceID() == resoureId) {
return val;
} else {
throw new IhcExecption("No resource id found");
}
} else {
throw new IhcExecption("No resource value found");
}
} catch (XPathExpressionException e) {
throw new IhcExecption(e);
} catch (UnsupportedEncodingException e) {
throw new IhcExecption(e);
}
}
use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.
the class IhcClient method waitResourceValueNotifications.
/**
* Wait runtime value notifications.
*
* Runtime value notification should firstly be activated by
* enableRuntimeValueNotifications function.
*
* @param timeoutInSeconds
* How many seconds to wait notifications.
* @return List of received runtime value notifications.
* @throws SocketTimeoutException
*/
private List<? extends WSResourceValue> waitResourceValueNotifications(int timeoutInSeconds) throws IhcExecption, SocketTimeoutException {
IhcResourceInteractionService service = new IhcResourceInteractionService(ip, timeout);
List<? extends WSResourceValue> list = service.waitResourceValueNotifications(timeoutInSeconds);
for (WSResourceValue val : list) {
resourceValues.put(val.getResourceID(), val);
}
return list;
}
use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.
the class IhcBinding method execute.
/**
* @{inheritDoc
*/
@Override
public void execute() {
if (ihc == null || isReconnectRequestActivated()) {
try {
if (ihc != null) {
disconnect();
}
connect();
setReconnectRequest(false);
enableResourceValueNotifications();
} catch (IhcExecption e) {
logger.warn("Can't open connection to controller", e);
return;
}
}
if (ihc != null) {
if (isValueNotificationRequestActivated()) {
try {
enableResourceValueNotifications();
} catch (IhcExecption e) {
logger.warn("Can't enable resource value notifications from controller", e);
}
}
// Poll all requested resources from controller
for (IhcBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
int resourceId = provider.getResourceIdForInBinding(itemName);
int itemRefreshInterval = provider.getRefreshInterval(itemName) * 1000;
if (resourceId > 0 && itemRefreshInterval > 0) {
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate = age >= itemRefreshInterval;
if (needsUpdate) {
logger.debug("Item '{}' is about to be refreshed now", itemName);
try {
WSResourceValue resourceValue = null;
try {
resourceValue = ihc.resourceQuery(resourceId);
} catch (IhcExecption e) {
logger.warn("Value could not be read from controller - retrying one time.", e);
try {
resourceValue = ihc.resourceQuery(resourceId);
} catch (IhcExecption ex) {
logger.error("Communication error", ex);
logger.debug("Reconnection request");
setReconnectRequest(true);
}
}
if (resourceValue != null) {
Class<? extends Item> itemType = provider.getItemType(itemName);
State value = IhcDataConverter.convertResourceValueToState(itemType, resourceValue);
eventPublisher.postUpdate(itemName, value);
}
} catch (Exception e) {
logger.error("Error occured during resource query", e);
}
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
}
}
} else {
logger.warn("Controller is not initialized => refresh cycle aborted!");
}
}
use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.
the class IhcBinding method updateResource.
/**
* Update resource value to IHC controller.
*/
private void updateResource(String itemName, Type type, boolean updateOnlyExclusiveOutBinding) {
if (itemName != null) {
Command cmd = null;
try {
cmd = (Command) type;
} catch (Exception e) {
}
IhcBindingProvider provider = findFirstMatchingBindingProvider(itemName, cmd);
if (provider == null) {
// command not configured, skip
return;
}
if (updateOnlyExclusiveOutBinding && provider.hasInBinding(itemName)) {
logger.trace("Ignore in binding update for item '{}'", itemName);
return;
}
logger.debug("Received update/command (item='{}', state='{}', class='{}')", new Object[] { itemName, type.toString(), type.getClass().toString() });
if (ihc == null) {
logger.warn("Controller is not initialized, abort resource value update for item '{}'!", itemName);
return;
}
if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
logger.warn("Connection to controller is not ok, abort resource value update for item '{}'!", itemName);
return;
}
try {
int resourceId = provider.getResourceId(itemName, (Command) type);
logger.trace("found resourceId {} (item='{}', state='{}', class='{}')", new Object[] { new Integer(resourceId).toString(), itemName, type.toString(), type.getClass().toString() });
if (resourceId > 0) {
WSResourceValue value = ihc.getResourceValueInformation(resourceId);
ArrayList<IhcEnumValue> enumValues = null;
if (value instanceof WSEnumValue) {
enumValues = ihc.getEnumValues(((WSEnumValue) value).getDefinitionTypeID());
}
// check if configuration has a custom value defined
// (0->OFF, 1->ON, >1->trigger)
// if that is the case, the type will be overridden with a
// new type
Integer val = provider.getValue(itemName, (Command) type);
boolean trigger = false;
if (val != null) {
if (val == 0) {
type = OnOffType.OFF;
} else if (val == 1) {
type = OnOffType.ON;
} else {
trigger = true;
}
} else {
// the original type is kept
}
if (!trigger) {
value = IhcDataConverter.convertCommandToResourceValue(type, value, enumValues);
boolean result = updateResource(value);
if (result == true) {
logger.debug("Item updated '{}' succesfully sent", itemName);
} else {
logger.error("Item '{}' update failed", itemName);
}
} else {
value = IhcDataConverter.convertCommandToResourceValue(OnOffType.ON, value, enumValues);
boolean result = updateResource(value);
if (result == true) {
logger.debug("Item '{}' trigger started", itemName);
Thread.sleep(val);
value = IhcDataConverter.convertCommandToResourceValue(OnOffType.OFF, value, enumValues);
result = updateResource(value);
if (result == true) {
logger.debug("Item '{}' trigger completed", itemName);
} else {
logger.error("Item '{}' trigger stop failed", itemName);
}
} else {
logger.error("Item '{}' update failed", itemName);
}
}
} else {
logger.error("resourceId invalid");
}
} catch (IhcExecption e) {
logger.error("Can't update Item '{}' value ", itemName, e);
} catch (Exception e) {
logger.error("Error occured during item update", e);
}
}
}
use of org.openhab.binding.ihc.ws.datatypes.WSResourceValue in project openhab1-addons by openhab.
the class IhcResourceInteractionService method waitResourceValueNotifications.
/**
* Wait runtime value notifications.
*
* Runtime value notification should firstly be activated by
* enableRuntimeValueNotifications function.
*
* @param timeoutInSeconds
* How many seconds to wait notifications.
* @return List of received runtime value notifications.
* @throws SocketTimeoutException
*/
public List<? extends WSResourceValue> waitResourceValueNotifications(int timeoutInSeconds) throws IhcExecption, SocketTimeoutException {
final String soapQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:utcs=\"utcs\">" + "<soapenv:Header/>" + "<soapenv:Body>" + " <utcs:waitForResourceValueChanges1>%s</utcs:waitForResourceValueChanges1>" + "</soapenv:Body>" + "</soapenv:Envelope>";
String query = String.format(soapQuery, timeoutInSeconds);
openConnection(url);
String response = sendQuery(query, timeout + timeoutInSeconds * 1000);
closeConnection();
List<WSResourceValue> resourceValueList = new ArrayList<WSResourceValue>();
NodeList nodeList;
try {
nodeList = parseList(response, "/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:waitForResourceValueChanges2/ns1:arrayItem");
if (nodeList.getLength() == 1) {
String resourceId = getValue(nodeList.item(0), "ns1:resourceID");
if (resourceId == null || resourceId == "") {
throw new SocketTimeoutException();
}
}
for (int i = 0; i < nodeList.getLength(); i++) {
int index = i + 2;
WSResourceValue newVal = parseResourceValue(nodeList.item(i), index);
resourceValueList.add(newVal);
}
return resourceValueList;
} catch (XPathExpressionException e) {
throw new IhcExecption(e);
} catch (UnsupportedEncodingException e) {
throw new IhcExecption(e);
}
}
Aggregations