use of org.openhab.core.library.items.ColorItem in project openhab1-addons by openhab.
the class DmxGenericBindingProvider method processBindingConfiguration.
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
if (dmxService == null) {
logger.error("DMX Service unavailable. Cannot process item configuration for {}", item.getName());
return;
}
super.processBindingConfiguration(context, item, bindingConfig);
String config = (bindingConfig == null) ? "" : bindingConfig.replaceAll(" ", "").toUpperCase();
logger.trace("Binding item: {} with configuration {}", item.getName(), config);
DmxItem itemBinding = null;
if (item instanceof ColorItem) {
itemBinding = new DmxColorItem(item.getName(), config, this);
} else if (item instanceof DimmerItem) {
itemBinding = new DmxDimmerItem(item.getName(), config, this);
} else if (item instanceof SwitchItem) {
itemBinding = new DmxSwitchItem(item.getName(), config, this);
} else {
throw new BindingConfigParseException("Unsupported item type " + item.getClass().getSimpleName());
}
if (itemBinding.isStatusListener()) {
final DmxStatusUpdateListener dmxStatusListener = itemBinding;
final String itemName = item.getName();
logger.trace("Registering status listener for item {} ", item.getName());
dmxService.registerStatusListener(dmxStatusListener);
// add binding change listener to clean up status listeners on item
// removal
addBindingChangeListener(new BindingChangeListener() {
@Override
public void bindingChanged(BindingProvider provider, String changedItemName) {
if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
logger.trace("Removing status listener for item {}", itemName);
dmxService.unregisterStatusListener(dmxStatusListener);
}
}
@Override
public void allBindingsChanged(BindingProvider provider) {
if (!provider.providesBindingFor(itemName)) {
logger.trace("Removing status listener for item {}", itemName);
dmxService.unregisterStatusListener(dmxStatusListener);
}
}
});
}
addBindingConfig(item, itemBinding);
}
use of org.openhab.core.library.items.ColorItem in project openhab1-addons by openhab.
the class JpaHistoricItem method fromPersistedItem.
/**
* Converts the string value of the persisted item to the state of a HistoricItem.
*
* @param pItem the persisted JpaPersistentItem
* @param item the source reference Item
* @return historic item
*/
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
State state;
if (item instanceof NumberItem) {
state = new DecimalType(Double.valueOf(pItem.getValue()));
} else if (item instanceof DimmerItem) {
state = new PercentType(Integer.valueOf(pItem.getValue()));
} else if (item instanceof SwitchItem) {
state = OnOffType.valueOf(pItem.getValue());
} else if (item instanceof ContactItem) {
state = OpenClosedType.valueOf(pItem.getValue());
} else if (item instanceof RollershutterItem) {
state = PercentType.valueOf(pItem.getValue());
} else if (item instanceof ColorItem) {
state = new HSBType(pItem.getValue());
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(Long.valueOf(pItem.getValue())));
state = new DateTimeType(cal);
} else if (item instanceof LocationItem) {
PointType pType = null;
String[] comps = pItem.getValue().split(";");
if (comps.length >= 2) {
pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
if (comps.length == 3) {
pType.setAltitude(new DecimalType(comps[2]));
}
}
state = pType;
} else if (item instanceof CallItem) {
state = new CallType(pItem.getValue());
} else {
state = new StringType(pItem.getValue());
}
return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
use of org.openhab.core.library.items.ColorItem in project openhab1-addons by openhab.
the class MysqlPersistenceService method store.
/**
* @{inheritDoc
*/
@Override
public void store(Item item, String alias) {
// Don't log undefined/uninitialised data
if (item.getState() instanceof UnDefType) {
return;
}
// If we've not initialised the bundle, then return
if (initialized == false) {
return;
}
// Connect to mySQL server if we're not already connected
if (!isConnected()) {
connectToDatabase();
}
// If we still didn't manage to connect, then return!
if (!isConnected()) {
logger.warn("mySQL: No connection to database. Can not persist item '{}'! " + "Will retry connecting to database when error count:{} equals errReconnectThreshold:{}", item, errCnt, errReconnectThreshold);
return;
}
// Get the table name for this item
String tableName = getTable(item);
if (tableName == null) {
logger.error("Unable to store item '{}'.", item.getName());
return;
}
// Do some type conversion to ensure we know the data type.
// This is necessary for items that have multiple types and may return their
// state in a format that's not preferred or compatible with the MySQL type.
// eg. DimmerItem can return OnOffType (ON, OFF), or PercentType (0-100).
// We need to make sure we cover the best type for serialisation.
String value;
if (item instanceof ColorItem) {
value = item.getStateAs(HSBType.class).toString();
} else if (item instanceof RollershutterItem) {
value = item.getStateAs(PercentType.class).toString();
} else {
/*
* !!ATTENTION!!
*
* 1.
* DimmerItem.getStateAs(PercentType.class).toString() always returns 0
* RollershutterItem.getStateAs(PercentType.class).toString() works as expected
*
* 2.
* (item instanceof ColorItem) == (item instanceof DimmerItem) = true
* Therefore for instance tests ColorItem always has to be tested before DimmerItem
*
* !!ATTENTION!!
*/
// All other items should return the best format by default
value = item.getState().toString();
}
// Get current timestamp
long timeNow = Calendar.getInstance().getTimeInMillis();
Timestamp timestamp = new Timestamp(timeNow);
String sqlCmd = null;
PreparedStatement statement = null;
try {
if (localtime) {
sqlCmd = new String("INSERT INTO " + tableName + " (TIME, VALUE) VALUES(?,?) ON DUPLICATE KEY UPDATE VALUE=?;");
statement = connection.prepareStatement(sqlCmd);
statement.setTimestamp(1, timestamp);
statement.setString(2, value);
statement.setString(3, value);
} else {
sqlCmd = new String("INSERT INTO " + tableName + " (TIME, VALUE) VALUES(NOW(),?) ON DUPLICATE KEY UPDATE VALUE=?;");
statement = connection.prepareStatement(sqlCmd);
statement.setString(1, value);
statement.setString(2, value);
}
statement.executeUpdate();
logger.debug("mySQL: Stored item '{}' as '{}'[{}] in SQL database at {}.", item.getName(), item.getState().toString(), value, timestamp.toString());
logger.debug("mySQL: query: {}", sqlCmd);
// Success
errCnt = 0;
} catch (Exception e) {
errCnt++;
logger.error("mySQL: Could not store item '{}' in database with " + "statement '{}': {}", item.getName(), sqlCmd, e.getMessage());
} finally {
if (statement != null) {
try {
statement.close();
} catch (Exception hidden) {
}
}
}
}
use of org.openhab.core.library.items.ColorItem in project openhab1-addons by openhab.
the class InfluxDBPersistenceService method store.
/**
* {@inheritDoc}
*/
@Override
public void store(Item item, String alias) {
if (item.getState() instanceof UnDefType) {
return;
}
if (!isProperlyConfigured) {
logger.warn("Configuration for influxdb08 not yet loaded or broken.");
return;
}
if (!isConnected()) {
logger.warn("InfluxDB is not yet connected");
return;
}
String realName = item.getName();
String name = (alias != null) ? alias : realName;
State state = null;
if (item instanceof DimmerItem || item instanceof RollershutterItem) {
state = item.getStateAs(PercentType.class);
} else if (item instanceof ColorItem) {
state = item.getStateAs(HSBType.class);
} else {
// All other items should return the best format by default
state = item.getState();
}
Object value = stateToObject(state);
logger.trace("storing {} in influxdb08 {}", name, value);
// For now time is calculated by influxdb08, may be this should be configurable?
Serie serie = new Serie.Builder(name).columns(VALUE_COLUMN_NAME).values(value).build();
try {
influxDB.write(dbName, TimeUnit.MILLISECONDS, serie);
} catch (RuntimeException e) {
logger.error("storing failed with exception for item: {}", name);
handleDatabaseException(e);
}
}
use of org.openhab.core.library.items.ColorItem in project openhab1-addons by openhab.
the class AbstractDynamoDBItem method asHistoricItem.
/*
* (non-Javadoc)
*
* @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
*/
@Override
public HistoricItem asHistoricItem(final Item item) {
final State[] state = new State[1];
accept(new DynamoDBItemVisitor() {
@Override
public void visit(DynamoDBStringItem dynamoStringItem) {
if (item instanceof ColorItem) {
state[0] = new HSBType(dynamoStringItem.getState());
} else if (item instanceof LocationItem) {
state[0] = new PointType(dynamoStringItem.getState());
} else if (item instanceof DateTimeItem) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
try {
cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
} catch (ParseException e) {
LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
state[0] = UnDefType.UNDEF;
}
state[0] = new DateTimeType(cal);
} else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
state[0] = UnDefType.UNDEF;
} else if (item instanceof CallItem) {
String parts = dynamoStringItem.getState();
String[] strings = parts.split("##");
String dest = strings[0];
String orig = strings[1];
state[0] = new CallType(orig, dest);
} else {
state[0] = new StringType(dynamoStringItem.getState());
}
}
@Override
public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
if (item instanceof NumberItem) {
state[0] = new DecimalType(dynamoBigDecimalItem.getState());
} else if (item instanceof DimmerItem) {
state[0] = new PercentType(dynamoBigDecimalItem.getState());
} else if (item instanceof SwitchItem) {
state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
} else if (item instanceof ContactItem) {
state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else if (item instanceof RollershutterItem) {
state[0] = new PercentType(dynamoBigDecimalItem.getState());
} else {
LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
}
}
});
return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Aggregations