use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MqttItemConfigTest method canParseMultipleOutboundConfigs.
@Test
public void canParseMultipleOutboundConfigs() throws BindingConfigParseException {
MqttItemConfig c = new MqttItemConfig(new SwitchItem("myItem"), ">[mybroker:/mytopic:command:ON:1],>[mybroker:/mytopic:command:OFF:0]");
assertEquals(2, c.getMessagePublishers().size());
assertEquals(0, c.getMessageSubscribers().size());
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MqttMessageSubscriberTest method canParseCommand.
@Test
public void canParseCommand() throws Exception {
ColorItem colorItem = new ColorItem("ColorItem");
DimmerItem dimmerItem = new DimmerItem("DimmerItem");
LocationItem locationItem = new LocationItem("LocationItem");
NumberItem numberItem = new NumberItem("NumberItem");
RollershutterItem rollershutterItem = new RollershutterItem("SetpointItem");
StringItem stringItem = new StringItem("StringItem");
SwitchItem switchItem = new SwitchItem("SwitchItem");
MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:command:default");
assertEquals(StringType.valueOf("test"), subscriber.getCommand("test", stringItem.getAcceptedCommandTypes()));
assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getCommand("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedCommandTypes()));
assertEquals(StringType.valueOf(""), subscriber.getCommand("", stringItem.getAcceptedCommandTypes()));
assertEquals(OnOffType.ON, subscriber.getCommand("ON", switchItem.getAcceptedCommandTypes()));
assertEquals(HSBType.valueOf("5,6,5"), subscriber.getCommand("5,6,5", colorItem.getAcceptedCommandTypes()));
assertEquals(DecimalType.ZERO, subscriber.getCommand(DecimalType.ZERO.toString(), numberItem.getAcceptedCommandTypes()));
assertEquals(PercentType.HUNDRED, subscriber.getCommand(PercentType.HUNDRED.toString(), dimmerItem.getAcceptedCommandTypes()));
assertEquals(PercentType.valueOf("80"), subscriber.getCommand(PercentType.valueOf("80").toString(), rollershutterItem.getAcceptedCommandTypes()));
assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getCommand(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedCommandTypes()));
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MqttMessageSubscriberTest method canParseState.
@Test
public void canParseState() throws Exception {
LocationItem locationItem = new LocationItem("LocationItem");
StringItem stringItem = new StringItem("StringItem");
SwitchItem switchItem = new SwitchItem("SwitchItem");
MqttMessageSubscriber subscriber = new MqttMessageSubscriber("mybroker:/mytopic:state:default");
assertEquals(OnOffType.ON, subscriber.getState("ON", switchItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf(""), subscriber.getState("", stringItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf("test"), subscriber.getState("test", stringItem.getAcceptedDataTypes()));
assertEquals(StringType.valueOf("{\"person\"{\"name\":\"me\"}}"), subscriber.getState("{\"person\"{\"name\":\"me\"}}", stringItem.getAcceptedDataTypes()));
assertEquals(PointType.valueOf("53.3239919,-6.5258807"), subscriber.getState(PointType.valueOf("53.3239919,-6.5258807").toString(), locationItem.getAcceptedDataTypes()));
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class MqttItemConfigTest method canParseMultipleInboundConfigs.
@Test
public void canParseMultipleInboundConfigs() throws BindingConfigParseException {
MqttItemConfig c = new MqttItemConfig(new SwitchItem("myItem"), "<[mybroker:/myHome/doorbell:state:XSLT(doorbell.xslt)], <[mybroker:/myHome/doorbell:command:ON], <[mybroker:/myHome/doorbell:state:XSLT(doorbell.xslt)]");
assertEquals(0, c.getMessagePublishers().size());
assertEquals(3, c.getMessageSubscribers().size());
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class KM200Comm method sendProvidersState.
/**
* This function sets the state of a service on the device
*
*/
public byte[] sendProvidersState(KM200BindingProvider provider, String item, Command command) {
synchronized (device) {
String type = null;
String dataToSend = null;
KM200CommObject object = null;
Class<? extends Item> itemType = provider.getItemType(item);
String service = checkParameterReplacement(provider, item);
logger.debug("Prepare item for send: {} type: {} item: {}", service, type, itemType.getName());
if (device.blacklistMap.contains(service)) {
logger.debug("Service on blacklist: {}", service);
return null;
}
if (device.serviceMap.containsKey(service)) {
if (device.serviceMap.get(service).getWriteable() == 0) {
logger.error("Service is listed as read-only: {}", service);
return null;
}
object = device.serviceMap.get(service);
type = object.getServiceType();
} else {
logger.error("Service is not in the determined device service list: {}", service);
return null;
}
/* The service is availible, set now the values depeding on the item and binding type */
logger.debug("state of: {} type: {}", command, type);
/* Binding is a NumberItem */
if (itemType.isAssignableFrom(NumberItem.class)) {
BigDecimal bdVal = ((DecimalType) command).toBigDecimal();
/* Check the capabilities of this service */
if (object.getValueParameter() != null) {
@SuppressWarnings("unchecked") List<BigDecimal> valParas = (List<BigDecimal>) object.getValueParameter();
BigDecimal minVal = valParas.get(0);
BigDecimal maxVal = valParas.get(1);
if (bdVal.compareTo(minVal) < 0) {
bdVal = minVal;
}
if (bdVal.compareTo(maxVal) > 0) {
bdVal = maxVal;
}
}
if (type.equals("floatValue")) {
dataToSend = new JSONObject().put("value", bdVal).toString();
} else if (type.equals("stringValue")) {
dataToSend = new JSONObject().put("value", bdVal.toString()).toString();
} else if (type.equals("switchProgram") && object.getVirtual() == 1) {
/* A switchProgram as NumberItem is always virtual */
dataToSend = sendVirtualState(object, itemType, service, command);
} else if (type.equals("errorList") && object.getVirtual() == 1) {
/* A errorList as NumberItem is always virtual */
dataToSend = sendVirtualState(object, itemType, service, command);
} else {
logger.warn("Not supported type for numberItem: {}", type);
}
/* Binding is a StringItem */
} else if (itemType.isAssignableFrom(StringItem.class)) {
String val = ((StringType) command).toString();
/* Check the capabilities of this service */
if (object.getValueParameter() != null) {
@SuppressWarnings("unchecked") List<String> valParas = (List<String>) object.getValueParameter();
if (!valParas.contains(val)) {
logger.warn("Parameter is not in the service parameterlist: {}", val);
return null;
}
}
if (type.equals("stringValue")) {
dataToSend = new JSONObject().put("value", val).toString();
} else if (type.equals("floatValue")) {
dataToSend = new JSONObject().put("value", Float.parseFloat(val)).toString();
} else if (type.equals("switchProgram")) {
if (object.getVirtual() == 1) {
dataToSend = sendVirtualState(object, itemType, service, command);
} else {
/* The JSONArray of switch items can be sended directly */
try {
/* Check whether ths input string is a valid JSONArray */
JSONArray userArray = new JSONArray(val);
dataToSend = userArray.toString();
} catch (Exception e) {
logger.warn("The input for the switchProgram is not a valid JSONArray : {}", e.getMessage());
return null;
}
}
} else {
logger.warn("Not supported type for stringItem: {}", type);
}
/* Binding is a DateTimeItem */
} else if (itemType.isAssignableFrom(DateTimeItem.class)) {
String val = ((DateTimeType) command).toString();
if (type.equals("stringValue")) {
dataToSend = new JSONObject().put("value", val).toString();
} else if (type.equals("switchProgram")) {
dataToSend = sendVirtualState(object, itemType, service, command);
} else {
logger.warn("Not supported type for dateTimeItem: {}", type);
}
/* Binding is a SwitchItem */
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
String val = null;
if (provider.getParameter(item).containsKey("on")) {
if (command == OnOffType.OFF) {
val = provider.getParameter(item).get("off");
} else if (command == OnOffType.ON) {
val = provider.getParameter(item).get("on");
}
} else {
logger.warn("Switch-Item only on configured on/off string values {}", command);
return null;
}
if (type.equals("stringValue")) {
dataToSend = new JSONObject().put("value", val).toString();
} else {
logger.warn("Not supported type for SwitchItem:{}", type);
}
} else {
logger.warn("Bindingtype not supported: {}", itemType.getClass());
return null;
}
/* If some data is availible then we have to send it to device */
if (dataToSend != null) {
/* base64 + encoding */
logger.debug("Encoding: {}", dataToSend);
byte[] encData = encodeMessage(dataToSend);
if (encData == null) {
logger.error("Couldn't encrypt data");
return null;
}
return encData;
} else {
return null;
}
}
}
Aggregations