use of org.openhab.core.library.items.StringItem in project openhab1-addons by openhab.
the class BaseIntegrationTest method initService.
@BeforeClass
public static void initService() throws InterruptedException {
items.put("dimmer", new DimmerItem("dimmer"));
items.put("number", new NumberItem("number"));
items.put("string", new StringItem("string"));
items.put("switch", new SwitchItem("switch"));
items.put("contact", new ContactItem("contact"));
items.put("color", new ColorItem("color"));
items.put("rollershutter", new RollershutterItem("rollershutter"));
items.put("datetime", new DateTimeItem("datetime"));
items.put("call", new CallItem("call"));
items.put("location", new LocationItem("location"));
service = new DynamoDBPersistenceService();
service.setItemRegistry(new ItemRegistry() {
@Override
public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
throw new NotImplementedException();
}
@Override
public boolean isValidItemName(String itemName) {
throw new NotImplementedException();
}
@Override
public Collection<Item> getItems(String pattern) {
throw new NotImplementedException();
}
@Override
public Collection<Item> getItems() {
throw new NotImplementedException();
}
@Override
public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
throw new NotImplementedException();
}
@Override
public Item getItem(String name) throws ItemNotFoundException {
Item item = items.get(name);
if (item == null) {
throw new ItemNotFoundException(name);
}
return item;
}
@Override
public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
throw new NotImplementedException();
}
});
HashMap<String, Object> config = new HashMap<>();
config.put("region", System.getProperty("DYNAMODBTEST_REGION"));
config.put("accessKey", System.getProperty("DYNAMODBTEST_ACCESS"));
config.put("secretKey", System.getProperty("DYNAMODBTEST_SECRET"));
config.put("tablePrefix", "dynamodb-integration-tests-");
for (Entry<String, Object> entry : config.entrySet()) {
if (entry.getValue() == null) {
logger.warn(String.format("Expecting %s to have value for integration tests. Integration tests will be skipped", entry.getKey()));
service = null;
return;
}
}
service.activate(null, config);
// Clear data
for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string" }) {
try {
service.getDb().getDynamoClient().deleteTable(table);
service.getDb().getDynamoDB().getTable(table).waitForDelete();
} catch (ResourceNotFoundException e) {
}
}
}
use of org.openhab.core.library.items.StringItem in project openhab1-addons by openhab.
the class KM200Comm method parseJSONData.
/**
* This function parses the receviced JSON Data and return the right state
*
*/
public State parseJSONData(String decodedData, String type, String item, KM200BindingProvider provider) {
JSONObject nodeRoot = null;
State state = null;
Class<? extends Item> itemType = provider.getItemType(item);
String service = checkParameterReplacement(provider, item);
KM200CommObject object = device.serviceMap.get(service);
logger.debug("parseJSONData service: {}, data: {}", service, decodedData);
/* Now parsing of the JSON String depending on its type and the type of binding item */
try {
if (decodedData.length() > 0) {
nodeRoot = new JSONObject(decodedData);
} else {
logger.warn("Get empty reply");
return null;
}
switch(type) {
case "stringValue":
/* Check whether the type is a single value containing a string value */
logger.debug("initDevice: type string value: {}", decodedData);
String sVal = nodeRoot.getString("value");
device.serviceMap.get(service).setValue(sVal);
/* SwitchItem Binding */
if (itemType.isAssignableFrom(SwitchItem.class)) {
if (provider.getParameter(item).containsKey("on")) {
if (sVal.equals(provider.getParameter(item).get("off"))) {
state = OnOffType.OFF;
} else if (sVal.equals(provider.getParameter(item).get("on"))) {
state = OnOffType.ON;
}
} else {
logger.warn("Switch-Item only on configured on/off string values: {}", decodedData);
return null;
}
/* NumberItem Binding */
} else if (itemType.isAssignableFrom(NumberItem.class)) {
try {
state = new DecimalType(Float.parseFloat(sVal));
} catch (NumberFormatException e) {
logger.error("Conversion of the string value to Decimal wasn't possible, data: {} error: {}", decodedData, e);
return null;
}
/* DateTimeItem Binding */
} else if (itemType.isAssignableFrom(DateTimeItem.class)) {
try {
state = new DateTimeType(sVal);
} catch (IllegalArgumentException e) {
logger.error("Conversion of the string value to DateTime wasn't possible, data: {} error: {}", decodedData, e);
return null;
}
/* StringItem Binding */
} else if (itemType.isAssignableFrom(StringItem.class)) {
state = new StringType(sVal);
} else {
logger.warn("Bindingtype not supported for string values: {}", itemType.getClass());
return null;
}
return state;
case "floatValue":
/* Check whether the type is a single value containing a float value */
logger.debug("state of type float value: {}", decodedData);
BigDecimal bdVal = nodeRoot.getBigDecimal("value");
device.serviceMap.get(service).setValue(bdVal);
/* NumberItem Binding */
if (itemType.isAssignableFrom(NumberItem.class)) {
state = new DecimalType(bdVal.floatValue());
/* StringItem Binding */
} else if (itemType.isAssignableFrom(StringItem.class)) {
state = new StringType(bdVal.toString());
} else {
logger.warn("Bindingtype not supported for float values: {}", itemType.getClass());
return null;
}
return state;
case "switchProgram":
/* Check whether the type is a switchProgram */
KM200SwitchProgramService sPService = null;
logger.debug("state of type switchProgram: {}", decodedData);
/* Get the KM200SwitchProgramService class object with all specific parameters */
if (object.getVirtual() == 0) {
sPService = ((KM200SwitchProgramService) object.getValueParameter());
} else {
sPService = ((KM200SwitchProgramService) device.serviceMap.get(object.getParent()).getValueParameter());
}
/* Update the switches insode the KM200SwitchProgramService */
sPService.updateSwitches(nodeRoot);
/* the parsing of switch program-services have to be outside, using json in strings */
if (object.getVirtual() == 1) {
return this.getVirtualState(object, itemType, service);
} else {
/* if access to the parent non virtual service the return the switchPoints jsonarray */
if (itemType.isAssignableFrom(StringItem.class)) {
state = new StringType(nodeRoot.getJSONArray("switchPoints").toString());
} else {
logger.warn("Bindingtype not supported for switchProgram, only json over strings supported: {}", itemType.getClass());
return null;
}
return state;
}
case "errorList":
/* Check whether the type is a errorList */
KM200ErrorService eService = null;
logger.debug("state of type errorList: {}", decodedData);
/* Get the KM200ErrorService class object with all specific parameters */
if (object.getVirtual() == 0) {
eService = ((KM200ErrorService) object.getValueParameter());
} else {
eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
}
/* Update the switches insode the KM200SwitchProgramService */
eService.updateErrors(nodeRoot);
/* the parsing of switch program-services have to be outside, using json in strings */
if (object.getVirtual() == 1) {
return this.getVirtualState(object, itemType, service);
} else {
/* if access to the parent non virtual service the return the switchPoints jsonarray */
if (itemType.isAssignableFrom(StringItem.class)) {
state = new StringType(nodeRoot.getJSONArray("values").toString());
} else {
logger.warn("Bindingtype not supported for error list, only json over strings is supported: {}", itemType.getClass());
return null;
}
return state;
}
case "yRecording":
/* Check whether the type is a yRecording */
logger.info("state of: type yRecording is not supported yet: {}", decodedData);
/* have to be completed */
break;
case "systeminfo":
/* Check whether the type is a systeminfo */
logger.info("state of: type systeminfo is not supported yet: {}", decodedData);
/* have to be completed */
break;
case "arrayData":
/* Check whether the type is a arrayData */
logger.info("state of: type arrayData is not supported yet: {}", decodedData);
/* have to be completed */
break;
}
} catch (JSONException e) {
logger.error("Parsingexception in JSON, data: {} error: {} ", decodedData, e.getMessage());
}
return null;
}
Aggregations