use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeLocalWithStringItem.
@Test
public void testDateTimeTypeLocalWithStringItem() throws IOException {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+03:00"));
// GMT: Sun, 17 Jul 2016 16:38:07.050 GMT
calendar.setTimeInMillis(1468773487050L);
DynamoDBItem<?> dbitem = testStateGeneric(new DateTimeType(calendar), "2016-07-17T16:38:07.050Z");
testAsHistoricGeneric(dbitem, new StringItem("foo"), new StringType("2016-07-17T16:38:07.050Z"));
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class AbstractDynamoDBItemSerializationTest method testStringTypeWithStringItem.
@Test
public void testStringTypeWithStringItem() throws IOException {
DynamoDBItem<?> dbitem = testStateGeneric(new StringType("foo bar"), "foo bar");
testAsHistoricGeneric(dbitem, new StringItem("foo"), new StringType("foo bar"));
}
use of org.openhab.core.library.types.StringType 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.types.StringType 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.StringType in project openhab1-addons by openhab.
the class GaradgetBinding method commandGaradget.
/**
* Call a function in the Particle REST API using the property in the binding config. The argument to the Particle
* device function is the String version of the command, with a special case for calling the setState function so
* sending ON, OFF, UP, DOWN and STOP commands are translated as "open", "close" or "stopped". Upon successfully
* calling the Particle device function, the state of the item is updated to the return value from the function
* call.
*
* @param itemName
* the item that is receiving the command
* @param publisher
* the binding config (deviceId,funcName) to send the command to
* @param command
* the command to send to the API
*/
private void commandGaradget(final String itemName, final GaradgetPublisher publisher, Command command) {
if (connection == null) {
logger.warn("Command '{}' not sent for item '{}'; no connection.", command, itemName);
return;
}
try {
final GaradgetDevice device = devices.get(publisher.getDeviceId());
if (device == null) {
logger.warn("No device found with ID: {}", publisher.getDeviceId());
return;
}
if ("setState".equals(publisher.getFuncName())) {
if (command.equals(OnOffType.ON) || command.equals(UpDownType.UP) || command.equals(PercentType.ZERO)) {
command = new StringType("open");
} else if (command.equals(OnOffType.OFF) || command.equals(UpDownType.DOWN) || command.equals(PercentType.HUNDRED)) {
command = new StringType("close");
} else if (command.equals(StopMoveType.STOP) || command instanceof PercentType) {
command = new StringType("stopped");
}
}
// TODO: make JSON properly so special characters are escaped as needed
String json = String.format("{ \"arg\": \"%s\" }\r\n", command.toString());
connection.sendCommand(device, publisher.getFuncName(), json, new HttpResponseHandler() {
@Override
public void handleResponse(int statusCode, String responseBody) {
if (statusCode == HttpStatus.SC_OK) {
logger.debug("Calling function '{}' returned '{}'", publisher.getFuncName(), responseBody);
// A function was called successfully; poll soon
schedulePoll(quickPollInterval);
} else {
logger.warn("Failed to call function '{}', status code={}", publisher.getFuncName(), statusCode);
}
}
});
} catch (Exception ex) {
logger.warn("Exception in commandGaradget:", ex);
}
}
Aggregations