use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class AlarmDecoderBinding method parseKeypadMessage.
private void parseKeypadMessage(String msg) throws MessageParseException {
List<String> parts = splitMsg(msg);
if (parts.size() != 4) {
throw new MessageParseException("got invalid keypad msg");
}
if (parts.get(0).length() != 22) {
throw new MessageParseException("bad keypad status length : " + parts.get(0).length());
}
try {
int numeric = 0;
try {
numeric = Integer.parseInt(parts.get(1));
} catch (NumberFormatException e) {
numeric = Integer.parseInt(parts.get(1), 16);
}
int upper = Integer.parseInt(parts.get(0).substring(1, 6), 2);
int nbeeps = Integer.parseInt(parts.get(0).substring(6, 7));
int lower = Integer.parseInt(parts.get(0).substring(7, 17), 2);
int status = ((upper & 0x1F) << 13) | ((nbeeps & 0x3) << 10) | lower;
ArrayList<AlarmDecoderBindingConfig> bcl = getItems(ADMsgType.KPM, null, null);
for (AlarmDecoderBindingConfig c : bcl) {
if (c.hasFeature("zone")) {
updateItem(c, new DecimalType(numeric));
} else if (c.hasFeature("text")) {
updateItem(c, new StringType(parts.get(3)));
} else if (c.hasFeature("beeps")) {
updateItem(c, new DecimalType(nbeeps));
} else if (c.hasFeature("status")) {
int bit = c.getIntParameter("bit", 0, 17, -1);
if (bit >= 0) {
// only pick a single bit
int v = (status >> bit) & 0x1;
updateItem(c, new DecimalType(v));
} else {
// pick all bits
updateItem(c, new DecimalType(status));
}
} else if (c.hasFeature("contact")) {
int bit = c.getIntParameter("bit", 0, 17, -1);
if (bit >= 0) {
// only pick a single bit
int v = (status >> bit) & 0x1;
updateItem(c, (v == 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
} else {
// pick all bits
logger.warn("ignoring item {}: it has contact without bit field", c.getItemName());
}
}
}
if ((status & (1 << 17)) != 0) {
// the panel is clear, so we can assume that all contacts that we
// have not heard from are open
setUnupdatedItemsToDefault();
}
} catch (NumberFormatException e) {
throw new MessageParseException("keypad msg contains invalid number: " + e.getMessage());
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class AlarmDecoderBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
if (!m_acceptCommands) {
logger.warn("sending commands is disabled, enable it in openhab.cfg!");
return;
}
String param = "INVALID";
if (command instanceof OnOffType) {
OnOffType cmd = (OnOffType) command;
param = cmd.equals(OnOffType.ON) ? "ON" : "OFF";
} else if (command instanceof DecimalType) {
param = ((DecimalType) command).toString();
} else {
logger.error("item {} only accepts DecimalType and OnOffType", itemName);
return;
}
try {
ArrayList<AlarmDecoderBindingConfig> bcl = getItems(itemName);
for (AlarmDecoderBindingConfig bc : bcl) {
String sendStr = bc.getParameters().get(param);
if (sendStr == null) {
logger.error("{} has no mapping for command {}!", itemName, param);
} else {
String s = sendStr.replace("POUND", "#");
m_writer.write(s);
m_writer.flush();
}
}
} catch (IOException e) {
logger.error("write to serial port failed: ", e);
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class AlarmDecoderBinding method parseRFMessage.
private void parseRFMessage(String msg) throws MessageParseException {
String[] parts = splitMessage(msg);
if (parts.length != 2) {
throw new MessageParseException("need 2 comma separated fields in msg");
}
try {
int numeric = Integer.parseInt(parts[1], 16);
ArrayList<AlarmDecoderBindingConfig> bcl = getItems(ADMsgType.RFX, parts[0], null);
for (AlarmDecoderBindingConfig c : bcl) {
if (c.hasFeature("data")) {
int bit = c.getIntParameter("bit", 0, 7, -1);
// apply bitmask if requested, else publish raw number
int v = (bit == -1) ? numeric : ((numeric >> bit) & 0x00000001);
updateItem(c, new DecimalType(v));
} else if (c.hasFeature("contact")) {
// if no loop indicator bitmask is set, default to 0x80
int bit = c.getIntParameter("bitmask", 0, 255, 0x80);
int v = numeric & bit;
updateItem(c, v == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
}
}
} catch (NumberFormatException e) {
throw new MessageParseException("msg contains invalid state number: " + e.getMessage());
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class DataTypeNumber method convertToState.
/**
* {@inheritDoc}
*/
public State convertToState(byte[] data, DavisValueType valueType) {
ByteBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
DecimalType res = null;
switch(valueType.getDataSize()) {
case 1:
res = new DecimalType(bb.get(valueType.getDataOffset()));
break;
case 2:
res = new DecimalType(bb.getShort(valueType.getDataOffset()));
break;
case 4:
res = new DecimalType(bb.getInt(valueType.getDataOffset()));
break;
default:
res = null;
break;
}
return res;
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class AutelisBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.trace("internalReceiveCommand({},{}) is called!", itemName, command);
for (AutelisBindingProvider provider : providers) {
Item item = provider.getItem(itemName);
String config = provider.getAutelisBindingConfigString(itemName);
Matcher m = commandPattern.matcher(config);
if (m.find() && m.groupCount() > 1) {
String type = m.group(1);
String name = m.group(2);
if (type.equals(AUTELIS_TYPES_EQUIP)) {
String cmd = AUTELIS_CMD_VALUE;
int value;
if (command == OnOffType.OFF) {
value = 0;
} else if (command == OnOffType.ON) {
value = 1;
} else if (command instanceof DecimalType) {
value = ((DecimalType) item.getStateAs(DecimalType.class)).intValue();
if (value >= 3) {
// this is a dim type. not sure what 2 does
cmd = AUTELIS_CMD_DIM;
}
} else {
logger.error("Equipment commands must be of Decimal type not {}", command);
break;
}
String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?name=" + name + "&" + cmd + "=" + value, TIMEOUT);
logger.trace("equipment set {} {} {} : result {}", name, cmd, value, response);
} else if (type.equals(AUTELIS_TYPES_TEMP)) {
String value;
if (command == IncreaseDecreaseType.INCREASE) {
value = AUTELIS_CMD_UP;
} else if (command == IncreaseDecreaseType.DECREASE) {
value = AUTELIS_CMD_DOWN;
} else {
value = command.toString();
}
String cmd;
// name ending in sp are setpoints, ht are heat types?
if (name.endsWith(AUTELIS_SETPOINT)) {
cmd = AUTELIS_TYPES_TEMP;
} else if (name.endsWith(AUTELIS_HEATTYPE)) {
cmd = AUTELIS_CMD_HEAT;
} else {
logger.error("Unknown temp type {}", name);
break;
}
String response = HttpUtil.executeUrl("GET", baseURL + "/set.cgi?wait=1&name=" + name + "&" + cmd + "=" + value, TIMEOUT);
logger.trace("temp set {} {} : result {}", cmd, value, response);
}
} else if (config.equals(AUTELIS_TYPES_LIGHTS)) {
/*
* lighting command
* possible values, but we will let anything through.
* alloff, allon, csync, cset, cswim, party, romance, caribbean, american,
* sunset, royalty, blue, green, red, white, magenta, hold, recall
*/
String response = HttpUtil.executeUrl("GET", baseURL + "lights.cgi?val=" + command.toString(), TIMEOUT);
logger.trace("lights set {} : result {}", command.toString(), response);
} else {
logger.error("Unsupported set config {}", config);
}
}
scheduleClearTime(UPDATE_CLEARTIME);
}
Aggregations