use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class MysqlPersistenceService method query.
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
if (!initialized) {
logger.debug("Query aborted on item {} - mySQL not initialised!", filter.getItemName());
return Collections.emptyList();
}
if (!isConnected()) {
connectToDatabase();
}
if (!isConnected()) {
logger.debug("Query aborted on item {} - mySQL not connected!", filter.getItemName());
return Collections.emptyList();
}
SimpleDateFormat mysqlDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Get the item name from the filter
// Also get the Item object so we can determine the type
Item item = null;
String itemName = filter.getItemName();
logger.debug("mySQL query: item is {}", itemName);
try {
if (itemRegistry != null) {
item = itemRegistry.getItem(itemName);
}
} catch (ItemNotFoundException e1) {
logger.error("Unable to get item type for {}", itemName);
// Set type to null - data will be returned as StringType
item = null;
}
if (item instanceof GroupItem) {
// For Group Items is BaseItem needed to get correct Type of Value.
item = GroupItem.class.cast(item).getBaseItem();
}
String table = sqlTables.get(itemName);
if (table == null) {
logger.error("mySQL: Unable to find table for query '{}'.", itemName);
return Collections.emptyList();
}
String filterString = new String();
if (filter.getBeginDate() != null) {
if (filterString.isEmpty()) {
filterString += " WHERE";
} else {
filterString += " AND";
}
filterString += " TIME>'" + mysqlDateFormat.format(filter.getBeginDate()) + "'";
}
if (filter.getEndDate() != null) {
if (filterString.isEmpty()) {
filterString += " WHERE";
} else {
filterString += " AND";
}
filterString += " TIME<'" + mysqlDateFormat.format(filter.getEndDate().getTime()) + "'";
}
if (filter.getOrdering() == Ordering.ASCENDING) {
filterString += " ORDER BY Time ASC";
} else {
filterString += " ORDER BY Time DESC";
}
if (filter.getPageSize() != 0x7fffffff) {
filterString += " LIMIT " + filter.getPageNumber() * filter.getPageSize() + "," + filter.getPageSize();
}
try {
long timerStart = System.currentTimeMillis();
// Retrieve the table array
Statement st = connection.createStatement();
String queryString = new String();
queryString = "SELECT Time, Value FROM " + table;
if (!filterString.isEmpty()) {
queryString += filterString;
}
logger.debug("mySQL: query:" + queryString);
// Turn use of the cursor on.
st.setFetchSize(50);
ResultSet rs = st.executeQuery(queryString);
long count = 0;
List<HistoricItem> items = new ArrayList<HistoricItem>();
State state;
while (rs.next()) {
count++;
if (item instanceof NumberItem) {
state = new DecimalType(rs.getDouble(2));
} else if (item instanceof ColorItem) {
state = new HSBType(rs.getString(2));
} else if (item instanceof DimmerItem) {
state = new PercentType(rs.getInt(2));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(rs.getString(2));
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(rs.getString(2));
} else if (item instanceof RollershutterItem) {
state = new PercentType(rs.getInt(2));
} else if (item instanceof DateTimeItem) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(rs.getTimestamp(2).getTime());
state = new DateTimeType(calendar);
} else {
state = new StringType(rs.getString(2));
}
MysqlItem mysqlItem = new MysqlItem(itemName, state, rs.getTimestamp(1));
items.add(mysqlItem);
}
rs.close();
st.close();
long timerStop = System.currentTimeMillis();
logger.debug("mySQL: query returned {} rows in {}ms", count, timerStop - timerStart);
// Success
errCnt = 0;
return items;
} catch (SQLException e) {
errCnt++;
logger.error("mySQL: Error running querying : ", e.getMessage());
}
return null;
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class DigitalSTROMBinding method deviceCall.
private void deviceCall(String itemName, Command cm) {
Device device = deviceMap.get(itemName);
if (device != null) {
if (cm instanceof org.openhab.core.library.types.OnOffType) {
if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.ON)) {
boolean transmitted = digitalSTROM.turnDeviceOn(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(device.getMaxOutPutValue());
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MAXIMUM.getSceneNumber());
}
} else if (((org.openhab.core.library.types.OnOffType) cm).equals(OnOffType.OFF)) {
boolean transmitted = digitalSTROM.turnDeviceOff(getSessionToken(), device.getDSID(), null);
if (transmitted) {
device.setOutputValue(0);
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.MINIMUM.getSceneNumber());
}
}
} else if (cm instanceof org.openhab.core.library.types.IncreaseDecreaseType) {
if (!device.isDimmable()) {
logger.warn("device is not in dimm mode: " + itemName + " outputMode: " + device.getOutputMode().getMode());
return;
}
if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.INCREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.INCREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.INCREMENT.getSceneNumber());
if (device.getOutputValue() == 0) {
initDeviceOutputValue(device, DeviceConstants.DEVICE_SENSOR_OUTPUT);
} else {
device.increase();
}
} else {
logger.error("transmitting increase command FAILED " + itemName);
}
} else if (((org.openhab.core.library.types.IncreaseDecreaseType) cm).equals(IncreaseDecreaseType.DECREASE)) {
boolean transmitted = digitalSTROM.callDeviceScene(getSessionToken(), device.getDSID(), null, ZoneSceneEnum.DECREMENT, false);
if (transmitted) {
addEcho(device.getDSID().getValue(), (short) ZoneSceneEnum.DECREMENT.getSceneNumber());
device.decrease();
} else {
logger.error("transmitting decrease command FAILED " + itemName);
}
}
} else if (cm instanceof org.openhab.core.library.types.PercentType) {
int percent = -1;
try {
percent = (int) Float.parseFloat(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException on a PercentType with command: " + cm.toString());
}
if (percent != -1) {
if (percent > -1 && percent < 101) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int old = device.getSlatPosition();
device.setSlatPosition(fromPercentToValue(percent, device.getMaxSlatPosition()));
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, fromPercentToValue(percent, device.getMaxSlatPosition()));
if (!transmitted) {
device.setSlatPosition(old);
logger.error("could NOT successfully set new value for slats ..." + cm.toString());
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
} else {
int old = device.getOutputValue();
device.setOutputValue(fromPercentToValue(percent, device.getMaxOutPutValue()));
boolean transmitted = digitalSTROM.setDeviceValue(getSessionToken(), device.getDSID(), null, fromPercentToValue(percent, device.getMaxOutPutValue()));
if (!transmitted) {
device.setOutputValue(old);
logger.error("could NOT successfully set new value ..." + cm.toString());
}
}
}
}
} else if (cm instanceof org.openhab.core.library.types.StopMoveType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
logger.warn("stop and move command NOT possible for slats, use PercentType command or up and down please");
} else {
handleStopMoveForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleStopMoveForRollershutter(device, cm);
}
} else if (cm instanceof org.openhab.core.library.types.UpDownType) {
if (device.getOutputMode().equals(OutputModeEnum.SLAT)) {
// 255 is max open, 0 is closed
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.UP)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition + DeviceConstants.MOVE_STEP_SLAT;
if (newPosition > device.getMaxSlatPosition()) {
newPosition = device.getMaxSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
} else if (((org.openhab.core.library.types.UpDownType) cm).equals(UpDownType.DOWN)) {
int slatPosition = device.getSlatPosition();
int newPosition = slatPosition - DeviceConstants.MOVE_STEP_SLAT;
if (newPosition < device.getMinSlatPosition()) {
newPosition = device.getMinSlatPosition();
}
boolean transmitted = digitalSTROM.setDeviceOutputValue(getSessionToken(), device.getDSID(), null, DeviceConstants.DEVICE_SENSOR_SLAT_OUTPUT, newPosition);
if (transmitted) {
device.setSlatPosition(newPosition);
}
}
} else {
handleUpDownForRollershutter(device, cm);
}
}
} else if (device.getOutputMode().equals(OutputModeEnum.UP_DOWN)) {
handleUpDownForRollershutter(device, cm);
} else {
logger.warn("Wrong item configuration ... this hardware is not a rollershutter: " + itemName);
}
}
} else {
if (cm instanceof DecimalType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(((DecimalType) cm).intValue()), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(((DecimalType) cm).intValue()), false);
}
}
} else if (cm instanceof StringType) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(itemName);
if (confItem != null && confItem.context != null) {
int scene = -1;
try {
scene = Integer.parseInt(cm.toString());
} catch (java.lang.NumberFormatException e) {
logger.error("NumberFormatException by parsing " + cm.toString() + " for " + confItem.itemName);
}
if (scene != -1) {
if (confItem.context.equals(ContextConfig.apartment)) {
digitalSTROM.callApartmentScene(getSessionToken(), confItem.groupID, null, ApartmentSceneEnum.getApartmentScene(scene), false);
} else if (confItem.context.equals(ContextConfig.zone)) {
digitalSTROM.callZoneScene(getSessionToken(), confItem.zoneID, null, confItem.groupID, null, ZoneSceneEnum.getZoneScene(scene), false);
}
}
}
} else {
logger.warn("couldn't find digitalstrom device for " + itemName);
}
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class DigitalSTROMBinding method updateItemState.
private void updateItemState(Item item) {
if (item != null) {
Device device = deviceMap.get(item.getName());
if (device != null) {
State state = null;
if (item instanceof DimmerItem) {
state = new PercentType(getPercent(device.getMaxOutPutValue(), device.getOutputValue()));
} else if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
state = device.getOutputValue() > 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof NumberItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
} else if (item instanceof RollershutterItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
int output = getPercent(device.getMaxSlatPosition(), device.getSlatPosition());
state = new PercentType(100 - output);
} else if (confItem.context != null && confItem.context.equals(ContextConfig.awning)) {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(output);
} else {
int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
state = new PercentType(100 - output);
}
}
} else if (item instanceof StringItem) {
DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
if (confItem != null) {
if (confItem.sensor != null) {
int value = -1;
switch(confItem.sensor) {
case TEMPERATURE_INDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_INDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
case TEMPERATURE_OUTDOORS:
value = device.getTemperatureSensorValue();
if (value != -1) {
// Celsius
// TODO use resolution
state = new DecimalType((double) (value) / 40 - 43.15);
// from sensor
}
break;
case RELATIVE_HUMIDITY_OUTDOORS:
value = device.getHumiditySensorValue();
if (value != -1) {
// Percent
// TODO use resolution from
state = new DecimalType((double) (value) / 40);
// sensor
}
break;
default:
break;
}
} else if (confItem.consumption != null) {
int value = -1;
switch(confItem.consumption) {
case ACTIVE_POWER:
value = device.getPowerConsumption();
if (value != -1) {
state = new DecimalType(value);
}
break;
case OUTPUT_CURRENT:
value = device.getEnergyMeterValue();
if (value != -1) {
state = new DecimalType(value);
}
break;
default:
break;
}
}
}
}
eventPublisher.postUpdate(item.getName(), state);
} else {
logger.error("couldn't update item state, because device is null: " + item.getName());
}
} else {
logger.error("couldn't update item state, because item is null");
}
}
use of org.openhab.core.library.types.PercentType 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.PercentType 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()));
}
}
}
Aggregations