use of org.openhab.binding.pioneeravr.PioneerAvrBindingProvider in project openhab1-addons by openhab.
the class PioneerAvrBinding method updated.
/**
* @{inheritDoc
*/
@Override
public void updated(Dictionary<String, ?> config) throws ConfigurationException {
logger.debug("Configuration updated, config {}", config != null ? true : false);
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|checkconn>'");
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 ("checkconn".equals(configKey)) {
if (value.equals("0")) {
deviceConfig.connectionCheckActive = false;
} else {
deviceConfig.connectionCheckActive = true;
}
} else {
throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' is unknown");
}
}
// open connection to all receivers
for (String device : deviceConfigCache.keySet()) {
PioneerAvrConnection connection = deviceConfigCache.get(device).getConnection();
if (connection != null) {
connection.openConnection();
connection.addEventListener(this);
}
}
for (PioneerAvrBindingProvider provider : this.providers) {
for (String itemName : provider.getItemNames()) {
initializeItem(itemName);
}
}
}
}
use of org.openhab.binding.pioneeravr.PioneerAvrBindingProvider in project openhab1-addons by openhab.
the class PioneerAvrBinding method initializeItem.
/**
* Initialize item value. Method send query to receiver if init query is configured to binding item configuration
*
* @param itemType
*
*/
private void initializeItem(String itemName) {
for (PioneerAvrBindingProvider provider : providers) {
String initCmd = provider.getItemInitCommand(itemName);
if (initCmd != null) {
logger.debug("Initialize item {}", itemName);
String[] commandParts = initCmd.split(":");
String deviceId = commandParts[0];
String deviceCmd = commandParts[1];
DeviceConfig device = deviceConfigCache.get(deviceId);
PioneerAvrConnection remoteController = device.getConnection();
if (device != null && remoteController != null) {
if (deviceCmd.startsWith(ADVANCED_COMMAND_KEY)) {
deviceCmd = deviceCmd.replace(ADVANCED_COMMAND_KEY, "");
} else {
IpControlCommand cmd = IpControlCommand.valueOf(deviceCmd);
deviceCmd = cmd.getCommand();
}
remoteController.send(deviceCmd);
} else {
logger.warn("Cannot find connection details for device id '{}'", deviceId);
}
}
}
}
use of org.openhab.binding.pioneeravr.PioneerAvrBindingProvider in project openhab1-addons by openhab.
the class PioneerAvrBinding method statusUpdateReceived.
@Override
public void statusUpdateReceived(EventObject event, String ip, String data) {
// find correct device from device cache
DeviceConfig deviceConfig = findDevice(ip);
if (deviceConfig != null) {
logger.debug("Received status update '{}' from device {}", data, deviceConfig.host);
for (PioneerAvrBindingProvider 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 = false;
if (deviceCmd.startsWith(ADVANCED_COMMAND_KEY)) {
// of a user configured "advanced" command, so skip this item
continue;
}
try {
String commandResponse = IpControlCommand.valueOf(deviceCmd).getResponse();
// when no respone is expected, the response string is empty
if (!commandResponse.isEmpty()) {
// compare response from network with response in enum
if (data.startsWith(commandResponse)) {
match = true;
}
}
} catch (Exception e) {
logger.error("Unregonized command '" + deviceCmd + "'", e);
}
if (match) {
Class<? extends Item> itemType = provider.getItemType(itemName);
State v = convertDeviceValueToOpenHabState(itemType, data, IpControlCommand.valueOf(deviceCmd));
eventPublisher.postUpdate(itemName, v);
break;
}
}
}
}
}
}
use of org.openhab.binding.pioneeravr.PioneerAvrBindingProvider in project openhab1-addons by openhab.
the class PioneerAvrBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
if (itemName != null) {
PioneerAvrBindingProvider provider = findFirstMatchingBindingProvider(itemName, command.toString());
if (provider == null) {
logger.warn("Doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
logger.debug("Received command (item='{}', state='{}', class='{}')", new Object[] { itemName, command.toString(), command.getClass().toString() });
String tmp = provider.getDeviceCommand(itemName, command.toString());
if (tmp == null) {
tmp = provider.getDeviceCommand(itemName, WILDCARD_COMMAND_KEY);
}
String[] commandParts = tmp.split(":");
String deviceId = commandParts[0];
String deviceCmd = commandParts[1];
DeviceConfig device = deviceConfigCache.get(deviceId);
PioneerAvrConnection remoteController = device.getConnection();
if (device != null && remoteController != null) {
if (deviceCmd.startsWith(ADVANCED_COMMAND_KEY)) {
// advanced command
deviceCmd = deviceCmd.replace(ADVANCED_COMMAND_KEY, "");
if (deviceCmd.contains("%")) {
deviceCmd = convertOpenHabCommandToDeviceCommand(command, deviceCmd);
}
} else {
// normal command
IpControlCommand cmd = IpControlCommand.valueOf(deviceCmd);
deviceCmd = cmd.getCommand();
if (deviceCmd.contains("%")) {
deviceCmd = convertOpenHabCommandToDeviceCommand(command, deviceCmd);
}
}
if (deviceCmd != null) {
remoteController.send(deviceCmd);
} else {
logger.warn("Cannot convert value '{}' to IpControl format", command);
}
} else {
logger.warn("Cannot find connection details for device id '{}'", deviceId);
}
}
}
Aggregations