Search in sources :

Example 36 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class MqttItemConfigTest method canParseInboundConfig.

@Test
public void canParseInboundConfig() throws BindingConfigParseException {
    MqttItemConfig c = new MqttItemConfig(new NumberItem("myItem"), "<[publicweatherservice:/london-city/temperature:state:default]");
    assertEquals(0, c.getMessagePublishers().size());
    assertEquals(1, c.getMessageSubscribers().size());
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Test(org.junit.Test)

Example 37 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class Zone method updateItem.

@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
    int current = ((properties.getStatus() >> 0) & 0x03);
    int latched = ((properties.getStatus() >> 2) & 0x03);
    int arming = ((properties.getStatus() >> 4) & 0x03);
    String latchedTxt = (latched < LATCHED_TEXT.length ? LATCHED_TEXT[latched] : "?");
    String armingTxt = (arming < ARMING_TEXT.length ? ARMING_TEXT[arming] : "?");
    String currentTxt = (current < CURRENT_TEXT.length ? CURRENT_TEXT[current] : "?");
    int setting = 0;
    String str = "";
    switch(config.getObjectType()) {
        case ZONE_STATUS_ARMING:
            setting = arming;
            str = armingTxt;
            break;
        case ZONE_STATUS_CURRENT:
            setting = current;
            str = currentTxt;
            break;
        case ZONE_STATUS_LATCHED:
            setting = latched;
            str = latchedTxt;
            break;
        case ZONE_STATUS_ALL:
            str = currentTxt + " | " + latchedTxt + " | " + armingTxt;
            break;
        default:
            break;
    }
    if (item instanceof NumberItem) {
        publisher.postUpdate(item.getName(), new DecimalType(setting));
    } else if (item instanceof StringItem) {
        publisher.postUpdate(item.getName(), new StringType(str));
    } else if (item instanceof ContactItem) {
        publisher.postUpdate(item.getName(), current == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) StringItem(org.openhab.core.library.items.StringItem)

Example 38 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class PilightGenericBindingProviderTest method init.

@Before
public void init() {
    provider = new PilightGenericBindingProvider();
    binding = new PilightBinding();
    testItem = new NumberItem("NumberItem");
    ;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Before(org.junit.Before)

Example 39 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class PLCLogoBinding method createState.

private State createState(Item item, Object value) {
    DecimalType number = null;
    if (value instanceof Number) {
        number = new DecimalType(value.toString());
    }
    State state = null;
    if (item instanceof StringType) {
        state = new StringType((String) value);
    } else if (item instanceof NumberItem) {
        if (number != null) {
            return number;
        } else if (value instanceof String) {
            state = new DecimalType(((String) value).replaceAll("[^\\d|.]", ""));
        }
    } else if (item instanceof SwitchItem && (number != null)) {
        state = (number.intValue() > 0) ? OnOffType.ON : OnOffType.OFF;
    } else if (item instanceof ContactItem && (number != null)) {
        state = (number.intValue() > 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
    }
    return state;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 40 with NumberItem

use of org.openhab.core.library.items.NumberItem in project openhab1-addons by openhab.

the class PLCLogoBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    // the code being executed when a command was sent on the openHAB
    // event bus goes here. This method is only called if one of the
    // BindingProviders provide a binding for the given 'itemName'.
    // Note itemname is the item name not the controller name/instance!
    //
    super.internalReceiveCommand(itemName, command);
    logger.debug("internalReceiveCommand() is called!");
    for (PLCLogoBindingProvider provider : providers) {
        if (!provider.providesBindingFor(itemName)) {
            continue;
        }
        PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
        if (!controllers.containsKey(config.getController())) {
            logger.warn("Invalid write requested for controller {}", config.getController());
            continue;
        }
        PLCLogoConfig controller = controllers.get(config.getController());
        PLCLogoMemoryConfig wr = config.getWR();
        int address = -1;
        try {
            address = wr.getAddress(controller.getModel());
        } catch (BindingConfigParseException exception) {
            logger.error("Invalid address for block {} on {}", wr.getBlockName(), controller);
            continue;
        }
        int bit = -1;
        try {
            bit = wr.getBit(controller.getModel());
        } catch (BindingConfigParseException exception) {
            logger.error("Invalid bit for block {} on {}", wr.getBlockName(), controller);
            continue;
        }
        if (!wr.isInRange(controller.getModel())) {
            logger.warn("Invalid write request for block {} at address {}", wr.getBlockName(), address);
            continue;
        }
        // Send command to the LOGO! controller memory
        S7Client LogoS7Client = controller.getS7Client();
        if (LogoS7Client == null) {
            logger.debug("No S7client for controller {} found", config.getController());
            continue;
        }
        final byte[] buffer = new byte[2];
        int size = wr.isDigital() ? 1 : 2;
        lock.lock();
        int result = LogoS7Client.ReadArea(S7.S7AreaDB, 1, address, size, buffer);
        logger.debug("Read word from logo memory: at {} {} bytes, result = {}", address, size, result);
        if (result == 0) {
            Item item = config.getItem();
            if (item instanceof NumberItem && !wr.isDigital()) {
                if (command instanceof DecimalType) {
                    int oldValue = S7.GetShortAt(buffer, 0);
                    int newValue = ((DecimalType) command).intValue();
                    S7.SetWordAt(buffer, 0, newValue);
                    logger.debug("Changed word at {} from {} to {}", address, oldValue, newValue);
                    result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
                    logger.debug("Wrote to memory at {} two bytes: [{}, {}]", address, buffer[0], buffer[1]);
                }
            } else if (item instanceof SwitchItem && wr.isDigital()) {
                if (command instanceof OnOffType) {
                    boolean oldValue = S7.GetBitAt(buffer, 0, bit) ? true : false;
                    boolean newValue = command == OnOffType.ON ? true : false;
                    S7.SetBitAt(buffer, 0, bit, newValue);
                    logger.debug("Changed bit {}.{} from {} to {}", address, bit, oldValue, newValue);
                    result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
                    logger.debug("Wrote to memory at {} one byte: [{}]", address, buffer[0]);
                }
            }
            // If nothing was written and read was ok, nothing will happen here
            if (result != 0) {
                logger.warn("Failed to write memory: {}. Reconnecting...", S7Client.ErrorText(result));
                ReconnectLogo(LogoS7Client);
            }
        } else {
            logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
            ReconnectLogo(LogoS7Client);
        }
        lock.unlock();
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) OnOffType(org.openhab.core.library.types.OnOffType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DecimalType(org.openhab.core.library.types.DecimalType) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) PLCLogoBindingConfig(org.openhab.binding.plclogo.PLCLogoBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) S7Client(Moka7.S7Client)

Aggregations

NumberItem (org.openhab.core.library.items.NumberItem)55 DecimalType (org.openhab.core.library.types.DecimalType)27 SwitchItem (org.openhab.core.library.items.SwitchItem)24 ContactItem (org.openhab.core.library.items.ContactItem)17 StringType (org.openhab.core.library.types.StringType)17 Test (org.junit.Test)16 DimmerItem (org.openhab.core.library.items.DimmerItem)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 StringItem (org.openhab.core.library.items.StringItem)15 Item (org.openhab.core.items.Item)11 PercentType (org.openhab.core.library.types.PercentType)11 DateTimeType (org.openhab.core.library.types.DateTimeType)10 Calendar (java.util.Calendar)9 DateTimeItem (org.openhab.core.library.items.DateTimeItem)9 ColorItem (org.openhab.core.library.items.ColorItem)8 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 BigDecimal (java.math.BigDecimal)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5