use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class MongoDBPersistenceService method query.
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
if (!initialized) {
return Collections.emptyList();
}
if (!isConnected()) {
connectToDatabase();
}
if (!isConnected()) {
return Collections.emptyList();
}
String name = filter.getItemName();
Item item = getItem(name);
List<HistoricItem> items = new ArrayList<HistoricItem>();
DBObject query = new BasicDBObject();
if (filter.getItemName() != null) {
query.put(FIELD_ITEM, filter.getItemName());
}
if (filter.getState() != null && filter.getOperator() != null) {
String op = convertOperator(filter.getOperator());
Object value = convertValue(filter.getState());
query.put(FIELD_VALUE, new BasicDBObject(op, value));
}
if (filter.getBeginDate() != null) {
query.put(FIELD_TIMESTAMP, new BasicDBObject("$gte", filter.getBeginDate()));
}
if (filter.getEndDate() != null) {
query.put(FIELD_TIMESTAMP, new BasicDBObject("$lte", filter.getEndDate()));
}
Integer sortDir = (filter.getOrdering() == Ordering.ASCENDING) ? 1 : -1;
DBCursor cursor = this.mongoCollection.find(query).sort(new BasicDBObject(FIELD_TIMESTAMP, sortDir)).skip(filter.getPageNumber() * filter.getPageSize()).limit(filter.getPageSize());
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
final State state;
if (item instanceof NumberItem) {
state = new DecimalType(obj.getDouble(FIELD_VALUE));
} else if (item instanceof DimmerItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(obj.getString(FIELD_VALUE));
} else if (item instanceof RollershutterItem) {
state = new PercentType(obj.getInt(FIELD_VALUE));
} else if (item instanceof ColorItem) {
state = new HSBType(obj.getString(FIELD_VALUE));
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance();
cal.setTime(obj.getDate(FIELD_VALUE));
state = new DateTimeType(cal);
} else {
state = new StringType(obj.getString(FIELD_VALUE));
}
items.add(new MongoDBItem(name, state, obj.getDate(FIELD_TIMESTAMP)));
}
return items;
}
use of org.openhab.core.library.items.NumberItem 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.items.NumberItem 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.items.NumberItem in project openhab1-addons by openhab.
the class BticinoDevice method handleEvent.
public void handleEvent(ProtocolRead p_protocol_read) throws Exception {
// the events on the bus are now received
// map them to events on the openhab bus
logger.debug("Gateway [" + m_gateway_id + "], Bticino WHO [" + p_protocol_read.getProperty("who") + "], WHAT [" + p_protocol_read.getProperty("what") + "], WHERE [" + p_protocol_read.getProperty("where") + "]");
// Get all the configs that are connected to this (who,where), multiple
// possible
List<BticinoBindingConfig> l_binding_configs = m_bticino_binding.getItemForBticinoBindingConfig(p_protocol_read.getProperty("who"), p_protocol_read.getProperty("where"));
// log it when an event has occured that no item is bound to
if (l_binding_configs.isEmpty()) {
logger.debug("Gateway [" + m_gateway_id + "], No Item found for bticino event, WHO [" + p_protocol_read.getProperty("who") + "], WHAT [" + p_protocol_read.getProperty("what") + "], WHERE [" + p_protocol_read.getProperty("where") + "]");
}
// every item associated with this who/where update the status
for (BticinoBindingConfig l_binding_config : l_binding_configs) {
// Get the Item out of the config
Item l_item = l_binding_config.getItem();
if (l_item instanceof SwitchItem) {
// Lights
if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("lighting")) {
logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR SwitchItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Light ON")) {
eventPublisher.postUpdate(l_item.getName(), OnOffType.ON);
} else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Light OFF")) {
eventPublisher.postUpdate(l_item.getName(), OnOffType.OFF);
}
} else // CENs
if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("CEN Basic and Evolved")) {
// Pushbutton virtual address must match
if (l_binding_config.what.equalsIgnoreCase(p_protocol_read.getProperty("what"))) {
logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR SwitchItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual pressure")) {
// only returns when finished
eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
} else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual release after short pressure")) {
// only returns when finished
eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
} else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual release after an extended pressure")) {
// only returns when finished
eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
} else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual extended pressure")) {
// only returns when finished
eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
}
}
}
} else if (l_item instanceof RollershutterItem) {
logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR RollershutterItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("automation")) {
if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Automation UP")) {
eventPublisher.postUpdate(l_item.getName(), UpDownType.UP);
} else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Automation DOWN")) {
eventPublisher.postUpdate(l_item.getName(), UpDownType.DOWN);
}
}
} else if (l_item instanceof NumberItem) {
logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR NumberItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
// THERMOREGULATION
if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("thermoregulation")) {
if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Temperature value")) {
eventPublisher.postUpdate(l_item.getName(), DecimalType.valueOf(p_protocol_read.getProperty("temperature")));
}
}
}
}
}
use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.
the class FritzahaGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
FritzahaDevice config = null;
TimeDef timedef = null;
String[] configParts = bindingConfig.trim().split(",");
if (configParts.length < 2) {
throw new BindingConfigParseException("FritzAHA items must start with <hostID>,<deviceID/AIN>");
}
if (item instanceof SwitchItem) {
if (configParts.length != 2) {
throw new BindingConfigParseException("FritzAHA switches must be of format <hostID>,<deviceID/AIN>");
}
if (configParts[1].length() > 8) {
config = new FritzahaWebserviceSwitch(configParts[0], configParts[1]);
} else {
config = new FritzahaQueryscriptSwitch(configParts[0], configParts[1]);
}
} else if (item instanceof NumberItem) {
if (configParts.length < 3 || configParts.length > 4) {
throw new BindingConfigParseException("FritzAHA meters must be of format <hostID>,<deviceID/AIN>,<valueToMeasure>[,timespec]");
} else if (configParts.length == 4 && !"energy".equalsIgnoreCase(configParts[2])) {
throw new BindingConfigParseException("FritzAHA non-energy meters must be of format <hostID>,<deviceID/AIN>,<valueToMeasure>");
} else if (configParts[1].length() > 8) {
if ("power".equalsIgnoreCase(configParts[2])) {
config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.POWER);
} else if ("energy".equalsIgnoreCase(configParts[2])) {
config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.ENERGY);
} else if ("temperature".equalsIgnoreCase(configParts[2])) {
config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.TEMPERATURE);
} else {
logger.warn("Could not configure item " + item + " - Unsupported meter type for webservice");
return;
}
} else {
if ("voltage".equalsIgnoreCase(configParts[2])) {
config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.VOLTAGE);
} else if ("current".equalsIgnoreCase(configParts[2])) {
config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.CURRENT);
} else if ("power".equalsIgnoreCase(configParts[2])) {
config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.POWER);
} else if ("energy".equalsIgnoreCase(configParts[2])) {
if (configParts.length > 3) {
if ("mins".equalsIgnoreCase(configParts[3])) {
timedef = TimeDef.MINUTES;
} else if ("day".equalsIgnoreCase(configParts[3])) {
timedef = TimeDef.DAY;
} else if ("month".equalsIgnoreCase(configParts[3])) {
timedef = TimeDef.MONTH;
} else if ("year".equalsIgnoreCase(configParts[3])) {
timedef = TimeDef.YEAR;
} else {
timedef = TimeDef.YEAR;
logger.warn("Timedef of item " + item + "is set to default YEAR. " + "Please check your syntax. Shall be year, month, day or mins.");
}
} else {
timedef = TimeDef.YEAR;
logger.debug("Timedef of item " + item + "is set to default YEAR because no timespec was given.");
}
config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.ENERGY, timedef);
} else {
logger.warn("Could not configure item " + item + " - Unsupported meter type for query script");
return;
}
}
} else {
logger.warn("Could not configure item " + item + " - Unsupported item type");
}
if (config != null) {
addBindingConfig(item, config);
} else {
logger.error("Could not configure item " + item + " - An error occurred");
}
}
Aggregations