use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class Zone method handleEvent.
/**
* {@inheritDoc}
*/
@Override
public void handleEvent(Item item, DSCAlarmBindingConfig config, EventPublisher publisher, DSCAlarmEvent event) {
int apiCode = -1;
APIMessage apiMessage = null;
String str = "Status Unknown!";
if (event != null) {
apiMessage = event.getAPIMessage();
apiCode = Integer.parseInt(apiMessage.getAPICode());
str = apiMessage.getAPIName();
logger.debug("handleEvent(): Zone Item Name: {}", item.getName());
if (config != null) {
if (config.getDSCAlarmItemType() != null) {
switch(config.getDSCAlarmItemType()) {
case ZONE_ALARM_STATUS:
publisher.postUpdate(item.getName(), new StringType(str));
break;
case ZONE_TAMPER_STATUS:
publisher.postUpdate(item.getName(), new StringType(str));
break;
case ZONE_FAULT_STATUS:
publisher.postUpdate(item.getName(), new StringType(str));
break;
case ZONE_GENERAL_STATUS:
publisher.postUpdate(item.getName(), (apiCode == 609) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
break;
default:
logger.debug("handleEvent(): Zone item not updated.");
break;
}
}
}
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class DDWRTBinding method execute.
@Override
protected void execute() {
logger.trace("execute");
if (password == null) {
return;
} else if (StringUtils.isBlank(password)) {
logger.error("Password mustn't be empty!");
return;
}
try {
TelnetClient client = null;
for (DDWRTBindingProvider provider : providers) {
for (String item : provider.getItemNames()) {
String query = null;
String type = provider.getType(item);
if (queryMap.containsKey(type)) {
if (type.startsWith(DDWRTBindingProvider.TYPE_ROUTER_TYPE)) {
query = queryMap.get(type);
} else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_24) && !interface_24.isEmpty()) {
query = queryMap.get(type) + " " + interface_24 + " | grep UP";
} else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_50) && !interface_50.isEmpty()) {
query = queryMap.get(type) + " " + interface_50 + " | grep UP";
} else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_GUEST) && !interface_guest.isEmpty()) {
query = queryMap.get(type) + " " + interface_guest + " | grep UP";
}
} else {
continue;
}
if (query == null) {
continue;
}
logger.trace("execute query ({}) ({}) ({})", query, ip, username);
if (client == null) {
client = new TelnetClient();
client.connect(ip);
if (username != null) {
receive(client);
send(client, username);
}
receive(client);
send(client, password);
receive(client);
}
send(client, query);
String answer = receive(client);
String[] lines = answer.split("\r\n");
if (lines.length >= 2) {
answer = lines[1].trim();
}
Class<? extends Item> itemType = provider.getItemType(item);
State state = null;
if (itemType.isAssignableFrom(SwitchItem.class)) {
if (lines.length > 2) {
if (lines[1].contains("UP")) {
state = OnOffType.ON;
} else {
state = OnOffType.OFF;
}
} else {
state = OnOffType.OFF;
}
} else if (itemType.isAssignableFrom(NumberItem.class)) {
state = new DecimalType(answer);
} else if (itemType.isAssignableFrom(StringItem.class)) {
state = new StringType(answer);
}
if (state != null) {
eventPublisher.postUpdate(item, state);
}
}
}
if (client != null) {
client.disconnect();
}
} catch (Exception e) {
logger.warn("Could not get item state ", e);
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class DenonConnector method processUpdate.
/**
* This method tries to parse information received over the telnet connection.
* It's quite unreliable. Some chars go missing or turn into other chars. That's
* why each command is validated using a regex.
*
* @param commandString The received command (one line)
*/
private void processUpdate(String commandString) {
if (COMMAND_PATTERN.matcher(commandString).matches()) {
/*
* This splits the commandString into the command and the parameter. SICD
* for example has SI as the command and CD as the parameter.
*/
String command = commandString.substring(0, 2);
String value = commandString.substring(2, commandString.length()).trim();
// Secondary zone commands with a parameter
if (ZONE_SUBCOMMAND_PATTERN.matcher(commandString).matches()) {
command = commandString.substring(0, 4);
value = commandString.substring(4, commandString.length()).trim();
}
logger.debug("Command: {}, value: {}", command, value);
if (value.equals("ON") || value.equals("OFF")) {
sendUpdate(command, OnOffType.valueOf(value));
} else if (value.equals("STANDBY")) {
sendUpdate(command, OnOffType.OFF);
} else if (StringUtils.isNumeric(value)) {
PercentType percent = new PercentType(fromDenonValue(value));
command = translateVolumeCommand(command);
sendUpdate(command, percent);
} else if (command.equals("SI")) {
sendUpdate(DenonProperty.INPUT.getCode(), new StringType(value));
sendUpdate(commandString, OnOffType.ON);
} else if (command.equals("MS")) {
sendUpdate(DenonProperty.SURROUND_MODE.getCode(), new StringType(value));
} else if (command.equals("NS")) {
processTitleCommand(command, value);
}
} else {
logger.debug("Invalid command: " + commandString);
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class DenonConnector method updateMainZone.
private void updateMainZone() {
String url = statusUrl + URL_ZONE_MAIN;
logger.trace("Refreshing URL: {}", url);
ZoneStatus mainZone = getDocument(url, ZoneStatus.class);
if (mainZone != null) {
stateCache.put(DenonProperty.INPUT.getCode(), new StringType(mainZone.getInputFuncSelect().getValue()));
stateCache.put("SI" + mainZone.getInputFuncSelect().getValue(), OnOffType.ON);
stateCache.put(DenonProperty.MASTER_VOLUME.getCode(), new PercentType(mainZone.getMasterVolume().getValue()));
stateCache.put(DenonProperty.POWER_MAINZONE.getCode(), mainZone.getPower().getValue() ? OnOffType.ON : OnOffType.OFF);
stateCache.put(DenonProperty.MUTE.getCode(), mainZone.getMute().getValue() ? OnOffType.ON : OnOffType.OFF);
if (mainZone.getSurrMode() == null) {
logger.debug("Unable to get the SURROUND_MODE. MainZone update may not be correct.");
} else {
stateCache.put(DenonProperty.SURROUND_MODE.getCode(), new StringType(mainZone.getSurrMode().getValue()));
}
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class AnelDataParser method parseData.
/**
* Parse data package from Anel NET-PwrCtrl device and update
* {@link AnelState}. For all changes, {@link AnelCommandType}s are created
* and returned. The expected format is as follows, separated with colons:
* <ul>
* <li>0. 'NET-PwrCtrl'
* <li>1. <name> (may contain trailing spaces)
* <li>2. <ip>
* <li>3. <netmask>
* <li>4. <gateway>
* <li>5. <mac addess>
* <li>6-13. <name of switch n>,<state 0 or 1>
* <li>14. <locked switches>
* <li>15. <http port>
* <li>16-23. <name of IO n>,<direction in=1 or out=0>,<state
* 0 or 1>
* <li>24. <temperature>
* <li>25. <firmware version> (may contain trailing line break)
* </ul>
* Source: <a
* href="http://www.anel-elektronik.de/forum_new/viewtopic.php?f=16&t=207"
* >Anel forum (German)</a>
* <p>
* It turned out that the HOME variant has a different format which contains
* only the first 16 segments. If that is the case, the remaining fields of
* {@link AnelState} are simply ignored (and remain unset).
* </p>
* Source: <a href="https://github.com/openhab/openhab/issues/2068">Issue
* 2068</a>
*
* @param data
* The data received from {@link AnelUDPConnector}.
* @param state
* The internal (cached) state of the device.
* @return A map of commands to the new openHAB {@link State}s.
* @throws Exception
* If the data is invalid or corrupt.
*/
public static Map<AnelCommandType, State> parseData(byte[] data, AnelState state) throws Exception {
final String string = new String(data);
final String[] arr = string.split(":");
if (arr.length != 28 && arr.length != 26 && arr.length != 16) {
throw new IllegalArgumentException("Data with 16, 26, or 28 values expected but " + arr.length + " received: " + string);
}
if (!arr[0].equals("NET-PwrCtrl")) {
throw new IllegalArgumentException("Data must start with 'NET-PwrCtrl' but it didn't: " + arr[0]);
}
if (!state.host.equals(arr[2]) && !state.host.equalsIgnoreCase(arr[1].trim())) {
// this came from another device
return Collections.emptyMap();
}
final Map<AnelCommandType, State> result = new LinkedHashMap<AnelCommandType, State>();
// check for switch changes, update cached state, and prepare command if
// needed
final int locked = Integer.parseInt(arr[14]);
for (int nr = 0; nr < 8; nr++) {
final String[] swState = arr[6 + nr].split(",");
if (swState.length == 2) {
// expected format
addCommand(state.switchName, nr, swState[0], "F" + (nr + 1) + "NAME", result);
addCommand(state.switchState, nr, "1".equals(swState[1]), "F" + (nr + 1), result);
} else {
// unexpected format, set states to null
addCommand(state.switchName, nr, null, "F" + (nr + 1) + "NAME", result);
addCommand(state.switchState, nr, null, "F" + (nr + 1), result);
}
addCommand(state.switchLocked, nr, (locked & (1 << nr)) > 0, "F" + (nr + 1) + "LOCKED", result);
}
// IO and temperature is only available if array has length 24
if (arr.length > 16) {
// if needed
for (int nr = 0; nr < 8; nr++) {
final String[] ioState = arr[16 + nr].split(",");
if (ioState.length == 3) {
// expected format
addCommand(state.ioName, nr, ioState[0], "IO" + (nr + 1) + "NAME", result);
addCommand(state.ioIsInput, nr, "1".equals(ioState[1]), "IO" + (nr + 1) + "ISINPUT", result);
addCommand(state.ioState, nr, "1".equals(ioState[2]), "IO" + (nr + 1), result);
} else {
// unexpected format, set states to null
addCommand(state.ioName, nr, null, "IO" + (nr + 1) + "NAME", result);
addCommand(state.ioIsInput, nr, null, "IO" + (nr + 1) + "ISINPUT", result);
addCommand(state.ioState, nr, null, "IO" + (nr + 1), result);
}
}
// example temperature string: '26.4°C'
// '°' is caused by some different encoding, so cut last 2 chars
final String temperature = arr[24].substring(0, arr[24].length() - 2);
if (hasTemperaturChanged(state, temperature)) {
result.put(AnelCommandType.TEMPERATURE, new DecimalType(temperature));
state.temperature = temperature;
}
}
// maybe the device's name changed?!
final String name = arr[1];
if (!name.equals(state.name)) {
result.put(AnelCommandType.NAME, new StringType(name));
state.name = name;
}
return result;
}
Aggregations