use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class DataTypeBoolean method convertToState.
/**
* {@inheritDoc}
*/
@Override
public State convertToState(int[] data, ComfoAirCommandType commandType) {
int[] get_reply_data_pos = commandType.getGetReplyDataPos();
int get_reply_data_bits = commandType.getGetReplyDataBits();
boolean result = (data[get_reply_data_pos[0]] & get_reply_data_bits) == get_reply_data_bits;
return (result) ? new DecimalType(1) : new DecimalType(0);
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ComfoAirConnector method open.
/**
* Open and initialize a serial port.
*
* @param portName
* e.g. /dev/ttyS0
* @param listener
* the listener which is informed after a successful response
* read
* @throws InitializationException
*/
public void open(String portName) throws InitializationException {
logger.debug("Open ComfoAir connection");
port = portName;
CommPortIdentifier portIdentifier;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(port);
try {
serialPort = portIdentifier.open("openhab", 3000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(1000);
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(new CPUWorkaroundThread());
serialPort.notifyOnDataAvailable(true);
inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();
ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(1));
sendCommand(command);
} catch (PortInUseException e) {
throw new InitializationException(e);
} catch (UnsupportedCommOperationException e) {
throw new InitializationException(e);
} catch (IOException e) {
throw new InitializationException(e);
} catch (TooManyListenersException e) {
throw new InitializationException(e);
}
} catch (NoSuchPortException e) {
StringBuilder sb = new StringBuilder();
@SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
sb.append(id.getName() + "\n");
}
}
throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
}
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class ComfoAirConnector method close.
/**
* Close the serial port.
*/
public void close() {
logger.debug("Close ComfoAir connection");
ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(0));
sendCommand(command);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
serialPort.close();
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class BenqProjectorBinding method sendCommandToProjector.
/**
* Send the command to the projector via configured transport and return the
* response string
*
* @param cfg
* Item binding configuration
* @param c
* command to be sent
* @return Response string from projector
*/
private String sendCommandToProjector(BenqProjectorBindingConfig cfg, Command c) {
Boolean cmdSent = false;
String response = "";
switch(cfg.mode) {
case POWER:
case MUTE:
if (c instanceof OnOffType) {
if ((OnOffType) c == OnOffType.ON) {
response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("ON"));
cmdSent = true;
} else if ((OnOffType) c == OnOffType.OFF) {
response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("OFF"));
cmdSent = true;
}
}
break;
case VOLUME:
if (c instanceof DecimalType) {
/* get current volume */
State currentVolState = queryProjector(cfg);
int currentVol = ((DecimalType) currentVolState).intValue();
int volLevel = ((DecimalType) c).intValue();
if (volLevel > this.MAX_VOLUME) {
volLevel = this.MAX_VOLUME;
} else if (volLevel < this.MIN_VOLUME) {
volLevel = this.MIN_VOLUME;
}
if (currentVol == volLevel) {
cmdSent = true;
}
while (currentVol != volLevel) {
if (currentVol < volLevel) {
transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
currentVol++;
cmdSent = true;
} else {
transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
currentVol--;
cmdSent = true;
}
}
} else if (c instanceof IncreaseDecreaseType) {
if ((IncreaseDecreaseType) c == IncreaseDecreaseType.INCREASE) {
transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
cmdSent = true;
} else if ((IncreaseDecreaseType) c == IncreaseDecreaseType.DECREASE) {
transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
cmdSent = true;
}
}
/* get final volume */
response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandQueryString());
break;
case LAMP_HOURS:
logger.warn("Cannot send command to set lamp hours - not a valid operation!");
break;
case SOURCE_NUMBER:
if (c instanceof DecimalType) {
DecimalType sourceIdx = (DecimalType) c;
String cmd = BenqProjectorSourceMapping.getStringFromMapping(sourceIdx.intValue());
if (cmd.isEmpty() == false) {
response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(cmd));
cmdSent = true;
}
}
break;
case SOURCE_STRING:
if (c instanceof StringType) {
StringType sourceStr = (StringType) c;
int mappingIdx = BenqProjectorSourceMapping.getMappingFromString(sourceStr.toString());
if (// double check this is a valid mapping
mappingIdx != -1) {
response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(sourceStr.toString()));
cmdSent = true;
}
}
break;
default:
logger.error("Unexpected Item Mode!");
break;
}
if (cmdSent == false) {
logger.error("Unable to convert item command to projector state: Command=" + c);
}
return response;
}
use of org.openhab.core.library.types.DecimalType in project openhab1-addons by openhab.
the class BluetoothBinding method handleAllDevicesInRange.
/**
* {@inheritDoc}
*/
@Override
public void handleAllDevicesInRange(Iterable<BluetoothDevice> devices) {
if (eventPublisher != null) {
// build a comma separated list of all devices in range
StringBuilder authSb = new StringBuilder();
StringBuilder unauthSb = new StringBuilder();
int noOfAuthDevices = 0;
int noOfUnauthDevices = 0;
for (BluetoothDevice device : devices) {
handleDeviceInRange(device);
if (device.isPaired()) {
authSb.append(getName(device));
authSb.append(", ");
noOfAuthDevices++;
} else {
unauthSb.append(getName(device));
unauthSb.append(", ");
noOfUnauthDevices++;
}
}
String authDeviceList = authSb.length() > 0 ? authSb.substring(0, authSb.length() - 2) : "";
String unauthDeviceList = unauthSb.length() > 0 ? unauthSb.substring(0, unauthSb.length() - 2) : "";
String allDeviceList = unauthDeviceList.isEmpty() ? authDeviceList : authSb.append(unauthDeviceList).toString();
for (String itemName : authStringItems.values()) {
eventPublisher.postUpdate(itemName, StringType.valueOf(authDeviceList));
}
for (String itemName : unauthStringItems.values()) {
eventPublisher.postUpdate(itemName, StringType.valueOf(unauthDeviceList));
}
for (String itemName : allStringItems.values()) {
eventPublisher.postUpdate(itemName, StringType.valueOf(allDeviceList));
}
for (String itemName : authMeasurementItems.values()) {
eventPublisher.postUpdate(itemName, new DecimalType(noOfAuthDevices));
}
for (String itemName : unauthMeasurementItems.values()) {
eventPublisher.postUpdate(itemName, new DecimalType(noOfUnauthDevices));
}
for (String itemName : allMeasurementItems.values()) {
eventPublisher.postUpdate(itemName, new DecimalType(noOfAuthDevices + noOfUnauthDevices));
}
}
}
Aggregations