Search in sources :

Example 6 with UnDefType

use of org.openhab.core.types.UnDefType 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 influxdb 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.getAcceptedCommandTypes().contains(HSBType.class)) {
        state = item.getStateAs(HSBType.class);
        logger.trace("Tried to get item as {}, state is {}", HSBType.class, state.toString());
    } else if (item.getAcceptedDataTypes().contains(PercentType.class)) {
        state = item.getStateAs(PercentType.class);
        logger.trace("Tried to get item as {}, state is {}", PercentType.class, state.toString());
    } else {
        // All other items should return the best format by default
        state = item.getState();
        logger.trace("Tried to get item from item class {}, state is {}", item.getClass(), state.toString());
    }
    Object value = stateToObject(state);
    logger.trace("storing {} in influxdb value {}, {}", name, value, item);
    Point point = Point.measurement(name).field(VALUE_COLUMN_NAME, value).time(System.currentTimeMillis(), timeUnit).build();
    try {
        influxDB.write(dbName, retentionPolicy, point);
    } catch (RuntimeException e) {
        logger.error("storing failed with exception for item: {}", name);
        handleDatabaseException(e);
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) PercentType(org.openhab.core.library.types.PercentType) Point(org.influxdb.dto.Point) HSBType(org.openhab.core.library.types.HSBType)

Example 7 with UnDefType

use of org.openhab.core.types.UnDefType in project openhab1-addons by openhab.

the class MapDBPersistenceService method store.

@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        return;
    }
    if (alias == null) {
        alias = item.getName();
    }
    logger.debug("store called for {}", alias);
    State state = item.getState();
    if (item instanceof ColorItem) {
        state = item.getStateAs(HSBType.class);
    } else if (item instanceof DimmerItem || item instanceof RollershutterItem) {
        state = item.getStateAs(PercentType.class);
    }
    MapDBItem mItem = new MapDBItem();
    mItem.setName(alias);
    mItem.setState(state);
    mItem.setTimestamp(new Date());
    MapDBItem oldItem = map.put(alias, mItem);
    if (!commitSameState) {
        if (oldItem != null) {
            if (!oldItem.getState().toString().equals(state.toString())) {
                needsCommit = true;
            }
        }
    }
    logger.debug("Stored '{}' with state '{}' in mapdb database", alias, state.toString());
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ColorItem(org.openhab.core.library.items.ColorItem) HSBType(org.openhab.core.library.types.HSBType) Date(java.util.Date)

Example 8 with UnDefType

use of org.openhab.core.types.UnDefType in project openhab1-addons by openhab.

the class MongoDBPersistenceService 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) {
        logger.warn("MongoDB not initialized");
        return;
    }
    // Connect to mongodb server if we're not already connected
    if (!isConnected()) {
        connectToDatabase();
    }
    // If we still didn't manage to connect, then return!
    if (!isConnected()) {
        logger.warn("mongodb: No connection to database. Can not persist item '{}'! Will retry connecting to database next time.", item);
        return;
    }
    String realName = item.getName();
    String name = (alias != null) ? alias : realName;
    Object value = this.convertValue(item.getState());
    DBObject obj = new BasicDBObject();
    obj.put(FIELD_ID, new ObjectId());
    obj.put(FIELD_ITEM, name);
    obj.put(FIELD_REALNAME, realName);
    obj.put(FIELD_TIMESTAMP, new Date());
    obj.put(FIELD_VALUE, value);
    this.mongoCollection.save(obj);
    logger.debug("MongoDB save {}={}", name, value);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) UnDefType(org.openhab.core.types.UnDefType) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Date(java.util.Date)

Example 9 with UnDefType

use of org.openhab.core.types.UnDefType in project openhab1-addons by openhab.

the class BenqProjectorBinding method internalReceiveCommand.

/**
     * @{inheritDoc
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    for (BenqProjectorBindingProvider binding : super.providers) {
        if (binding.providesBindingFor(itemName)) {
            logger.debug("Process command " + command + " for " + itemName);
            BenqProjectorBindingConfig cfg = binding.getConfigForItemName(itemName);
            String resp = sendCommandToProjector(cfg, command);
            State s = cfg.mode.parseResponse(resp);
            if (!(s instanceof UnDefType)) {
                eventPublisher.postUpdate(itemName, s);
                logger.debug(itemName + " status is " + s);
            } else {
                logger.debug(itemName + " not updated as result was undefined");
            }
        }
    }
}
Also used : State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) BenqProjectorBindingProvider(org.openhab.binding.benqprojector.BenqProjectorBindingProvider)

Example 10 with UnDefType

use of org.openhab.core.types.UnDefType in project openhab1-addons by openhab.

the class BenqProjectorBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    for (BenqProjectorBindingProvider binding : super.providers) {
        for (String itemName : binding.getItemNames()) {
            logger.debug("Polling projector status for " + itemName);
            BenqProjectorBindingConfig cfg = binding.getConfigForItemName(itemName);
            State s = queryProjector(cfg);
            if (!(s instanceof UnDefType)) {
                eventPublisher.postUpdate(itemName, s);
                logger.debug(itemName + " status is " + s);
            } else {
                logger.debug(itemName + " not updated as result was undefined");
            }
        }
    }
}
Also used : State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) BenqProjectorBindingProvider(org.openhab.binding.benqprojector.BenqProjectorBindingProvider)

Aggregations

UnDefType (org.openhab.core.types.UnDefType)10 State (org.openhab.core.types.State)7 Date (java.util.Date)4 ColorItem (org.openhab.core.library.items.ColorItem)3 RollershutterItem (org.openhab.core.library.items.RollershutterItem)3 PercentType (org.openhab.core.library.types.PercentType)3 BenqProjectorBindingProvider (org.openhab.binding.benqprojector.BenqProjectorBindingProvider)2 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 HSBType (org.openhab.core.library.types.HSBType)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 EntityManager (javax.persistence.EntityManager)1 ObjectId (org.bson.types.ObjectId)1 Point (org.influxdb.dto.Point)1