use of org.openhab.binding.primare.PrimareBindingProvider in project openhab1-addons by openhab.
the class PrimareBinding method initializeItem.
/**
* Initialize item value. Method sends a query to receiver if init query is
* configured to binding item configuration
*
* @param itemType
*
*/
private void initializeItem(String itemName) {
logger.debug("Called initializeItem for {}, ignored", itemName);
for (PrimareBindingProvider provider : providers) {
String initCmd = provider.getItemInitCommand(itemName);
if (initCmd == null) {
logger.debug("No init command found for item {}", itemName);
continue;
}
logger.debug("Initialize item {} with {}", itemName, initCmd);
String[] commandParts = initCmd.split(":");
String deviceId = commandParts[0];
String deviceCmd = commandParts[1];
if (deviceId == null || deviceCmd == null) {
logger.warn("Initializing - ignore item:{} initCmd:{} - failed to find both device id and command in {}", itemName, initCmd, initCmd);
continue;
}
DeviceConfig deviceConfig = deviceConfigCache.get(deviceId);
if (deviceConfig == null) {
logger.warn("Ignore item:{} initCmd:{} - no configuration found for device {}", itemName, initCmd, deviceId);
continue;
}
PrimareConnector connector = deviceConfig.getInitializedConnector();
if (connector == null) {
logger.warn("Ignore device:{} item:{} initCmd:{} - no connector (IP or serial) found for device {}", deviceId, itemName, initCmd, deviceId);
continue;
}
if (!connector.isConnected()) {
logger.warn("Ignore device:{} item:{} initCmd:{} - {} not connected", deviceId, itemName, initCmd, deviceConfig.toString());
continue;
}
try {
// There are no arguments for a device init command
connector.sendCommand(null, deviceCmd);
} catch (Exception e) {
logger.warn("Ignore device:{} item:{} initCmd:{} Message send error {}", deviceId, itemName, initCmd, e.getMessage());
}
}
return;
}
use of org.openhab.binding.primare.PrimareBindingProvider in project openhab1-addons by openhab.
the class PrimareBinding method updated.
/**
* {@inheritDoc}
*/
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
logger.debug("Configuration updated, valid configuration {}", (config != null ? "exists" : "does not exist"));
if (config != null) {
Enumeration<String> keys = config.keys();
if (deviceConfigCache == null) {
deviceConfigCache = new HashMap<String, DeviceConfig>();
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
// don't want to process here ...
if ("service.pid".equals(key)) {
continue;
}
Matcher matcher = EXTRACT_CONFIG_PATTERN.matcher(key);
if (!matcher.matches()) {
logger.debug("given config key '" + key + "' does not follow the expected pattern '<id>.<host|port|model>'");
continue;
}
matcher.reset();
matcher.find();
String deviceId = matcher.group(1);
DeviceConfig deviceConfig = deviceConfigCache.get(deviceId);
if (deviceConfig == null) {
deviceConfig = new DeviceConfig(deviceId);
deviceConfigCache.put(deviceId, deviceConfig);
}
String configKey = matcher.group(2);
String value = (String) config.get(key);
if ("host".equals(configKey)) {
deviceConfig.host = value;
} else if ("port".equals(configKey)) {
deviceConfig.port = Integer.valueOf(value);
} else if ("serial".equals(configKey)) {
deviceConfig.serialport = value;
} else if ("model".equals(configKey)) {
String model = value.toUpperCase();
deviceConfig.model = value.toUpperCase();
} else {
throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' is unknown");
}
}
// open connection to all receivers
for (String device : deviceConfigCache.keySet()) {
PrimareConnector connector = deviceConfigCache.get(device).getInitializedConnector();
if (connector != null) {
try {
connector.connect();
connector.addEventListener(this);
} catch (Exception e) {
logger.warn("failed to connect to {} after configuration update", device.toString());
}
}
}
for (PrimareBindingProvider provider : this.providers) {
for (String itemName : provider.getItemNames()) {
initializeItem(itemName);
}
}
}
}
use of org.openhab.binding.primare.PrimareBindingProvider in project openhab1-addons by openhab.
the class PrimareBinding method statusUpdateReceived.
/**
* {@inheritDoc}
*/
@Override
public void statusUpdateReceived(EventObject event, String deviceId, byte[] data) {
logger.trace("statusUpdateReceived for device {}: {}", deviceId, PrimareUtils.byteArrayToHex(data));
DeviceConfig deviceConfig = deviceConfigCache.get(deviceId);
if (deviceConfig != null) {
logger.trace("Received status update '{}' from device {}", PrimareUtils.byteArrayToHex(data), deviceConfig.toString());
PrimareResponse deviceResponse = null;
deviceResponse = deviceConfig.connector.getResponseFactory().getResponse(data);
for (PrimareBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
// Update all items which refer to command
HashMap<String, String> values = provider.getDeviceCommands(itemName);
for (String cmd : values.keySet()) {
String[] commandParts = values.get(cmd).split(":");
String deviceCmd = commandParts[1];
boolean match = deviceResponse.isRelevantFor(deviceCmd);
if (match) {
Class<? extends Item> itemType = provider.getItemType(itemName);
State v = deviceResponse.openHabState(itemType);
logger.trace("Updating itemName {} to {} based on device command {}", itemName, v, deviceCmd);
eventPublisher.postUpdate(itemName, v);
break;
}
}
}
}
}
}
use of org.openhab.binding.primare.PrimareBindingProvider in project openhab1-addons by openhab.
the class PrimareBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
if (itemName == null) {
logger.warn("ignore command {} - no item name", command);
return;
}
logger.debug("Received command [item:{} command:{}, class:{}]", itemName, command.toString(), command.getClass().toString());
PrimareBindingProvider provider = findFirstMatchingBindingProvider(itemName, command.toString());
if (provider == null) {
logger.warn("Ignore item:{} command:{} - no matching binding provider", itemName, command);
return;
}
String tmp = provider.getDeviceCommand(itemName, command.toString());
if (tmp == null) {
tmp = provider.getDeviceCommand(itemName, WILDCARD_COMMAND_KEY);
}
if (tmp == null) {
logger.warn("Ignore item:{} command {} - no matching command", itemName, command);
return;
}
String[] commandParts = tmp.split(":");
String deviceId = commandParts[0];
String deviceCmd = commandParts[1];
if (deviceId == null || deviceCmd == null) {
logger.warn("Ignore item:{} command:{} - failed to find both device id and command in {}", itemName, command, tmp);
return;
}
DeviceConfig deviceConfig = deviceConfigCache.get(deviceId);
if (deviceConfig == null) {
logger.warn("Ignore item:{} command:{} - no configuration found for device {}", itemName, command, deviceId);
return;
}
PrimareConnector deviceConnector = deviceConfig.getInitializedConnector();
if (deviceConnector == null) {
logger.warn("Ignore {} item:{} command:{} - no connector found", deviceConfig.toString(), itemName, command);
return;
}
if (deviceConnector.isConnected()) {
try {
deviceConnector.sendCommand(command, deviceCmd);
} catch (Exception e) {
logger.warn("Ignore {} item:{} command:{} - message send error {}", deviceConfig.toString(), itemName, command, e.getMessage());
}
} else {
logger.warn("Ignore {} item:{} command:{} - device disconnected", deviceConfig.toString(), itemName, command);
}
}
Aggregations