use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class LightwaveRfGenericBindingProviderTest method testProcessBindingConfiguratiLionForOutOnly.
@Test
public void testProcessBindingConfiguratiLionForOutOnly() throws Exception {
LightwaveRfGenericBindingProvider bingindProvider = new LightwaveRfGenericBindingProvider();
bingindProvider.processBindingConfiguration(context, new SwitchItem("MySwitch"), ">room=3,device=4,type=SWITCH");
assertEquals(Arrays.asList("MySwitch"), bingindProvider.getBindingItemsForRoomDevice("3", "4"));
assertEquals(Arrays.asList("MySwitch"), bingindProvider.getItemNames());
assertEquals("3", bingindProvider.getRoomId("MySwitch"));
assertEquals("4", bingindProvider.getDeviceId("MySwitch"));
assertEquals(LightwaveRfType.SWITCH, bingindProvider.getTypeForItemName("MySwitch"));
assertEquals(LightwaveRfItemDirection.OUT_ONLY, bingindProvider.getDirection("MySwitch"));
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class PLCLogoBinding method execute.
@Override
protected void execute() {
if (!bindingsExist()) {
logger.debug("There is no existing plclogo binding configuration => refresh cycle aborted!");
return;
}
Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, PLCLogoConfig> thisEntry = entries.next();
String controller = thisEntry.getKey();
PLCLogoConfig logoConfig = thisEntry.getValue();
S7Client LogoS7Client = logoConfig.getS7Client();
if (LogoS7Client == null) {
logger.debug("No S7client for {} found", controller);
} else {
lock.lock();
int result = ReadLogoDBArea(LogoS7Client, logoConfig.getMemorySize());
lock.unlock();
if (result != 0) {
logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
return;
}
// Now have the LOGO! memory (note: not suitable for S7) - more efficient than multiple reads (test
// shows <14mS to read all)
// iterate through bindings to see what has changed - this approach assumes a small number (< 100)of
// bindings
// otherwise might see what has changed in memory and map to binding
}
for (PLCLogoBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
if (config.getController().equals(controller)) {
// it is for our currently selected controller
PLCLogoMemoryConfig rd = config.getRD();
int address = -1;
try {
address = rd.getAddress(logoConfig.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid address for block {} on {}", rd.getBlockName(), controller);
continue;
}
int currentValue;
if (rd.isDigital()) {
int bit = -1;
try {
bit = rd.getBit(logoConfig.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid bit for block {} on {}", rd.getBlockName(), controller);
continue;
}
currentValue = S7.GetBitAt(data, address, bit) ? 1 : 0;
} else {
/*
* After the data transfer from a LOGO! Base Module to LOGO!Soft Comfort,
* you can view only analog values within the range of -32768 to 32767 on LOGO!Soft Comfort.
* If an analog value exceeds the value range,
* then only the nearest upper limit (32767) or lower limit (-32768) can be displayed.
*/
currentValue = S7.GetShortAt(data, address);
}
if (config.isSet()) {
if (currentValue == config.getLastValue()) {
continue;
}
int delta = Math.abs(config.getLastValue() - currentValue);
if (!rd.isDigital() && (delta < config.getAnalogDelta())) {
continue;
}
}
boolean isValid = false;
Item item = provider.getItem(itemName);
switch(rd.getKind()) {
case I:
case NI:
{
isValid = item instanceof ContactItem;
break;
}
case Q:
case NQ:
{
isValid = item instanceof SwitchItem;
break;
}
case M:
case VB:
case VW:
{
isValid = item instanceof ContactItem || item instanceof SwitchItem;
break;
}
default:
{
break;
}
}
if (item instanceof NumberItem || isValid) {
eventPublisher.postUpdate(itemName, createState(item, currentValue));
config.setLastValue(currentValue);
} else {
String block = rd.getBlockName();
logger.warn("Block {} is incompatible with item {} on {}", block, item.getName(), controller);
}
}
}
}
}
}
use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.
the class XplBinding method handleXPLMessage.
@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
for (XplBindingProvider provider : providers) {
List<String> matchingItems = provider.hasMessage(theMessage);
for (String itemName : matchingItems) {
XplBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
String current = theMessage.getNamedValue(config.NamedParameter);
Item item = provider.getItem(itemName);
if (item != null) {
if (item instanceof SwitchItem) {
OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((SwitchItem) item).setState(status);
}
}
} else if (item instanceof ContactItem) {
OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((ContactItem) item).setState(status);
}
}
} else if (item instanceof NumberItem) {
DecimalType value = new DecimalType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((NumberItem) item).setState(value);
}
}
} else if (item instanceof StringItem) {
StringType value = new StringType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((StringItem) item).setState(value);
}
}
}
}
}
}
}
use of org.openhab.core.library.items.SwitchItem 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.SwitchItem in project openhab1-addons by openhab.
the class ErroringQueriesTestCase method testConnectionTimeout.
@Test
public void testConnectionTimeout() throws UnknownHostException, ConfigurationException, BindingConfigParseException, InterruptedException {
/**
* Test that connection timeout is handled properly
*
* In the test we have non-responding slave (see http://stackoverflow.com/a/904609), and we use connection
* timeout of 300ms
*
* We assert that after 100ms the binding still have not given up on the connection (no UNDEF posted to the
* event bus)
* but after 400ms from the connection, the UNDEF is there.
*/
binding = new ModbusBinding();
Dictionary<String, Object> config = newLongPollBindingConfig();
addSlave(config, ServerType.TCP, "10.255.255.1:9999:0:0:0:1:300", SLAVE_NAME, ModbusBindingProvider.TYPE_DISCRETE, null, 0, 0, 2);
putSlaveConfigParameter(config, serverType, SLAVE_NAME, "postundefinedonreaderror", "true");
binding.updated(config);
// Configure items
final ModbusGenericBindingProvider provider = new ModbusGenericBindingProvider();
provider.processBindingConfiguration("test.items", new SwitchItem("Item1"), String.format("%s:%d", SLAVE_NAME, 0));
binding.setEventPublisher(eventPublisher);
binding.addBindingProvider(provider);
final CountDownLatch lock = new CountDownLatch(1);
Thread thread = new Thread() {
@Override
public void run() {
binding.execute();
lock.countDown();
}
;
};
try {
thread.start();
lock.await(100, TimeUnit.MILLISECONDS);
// After 100ms the binding has not yet given up, i.e. no UNDEF posted to event bus
verifyNoMoreInteractions(eventPublisher);
// After 100ms+300ms the timeout of 300ms has passed and UNDEF should have been posted
lock.await(300, TimeUnit.MILLISECONDS);
verify(eventPublisher).postUpdate("Item1", UnDefType.UNDEF);
verifyNoMoreInteractions(eventPublisher);
} finally {
thread.interrupt();
}
}
Aggregations