Search in sources :

Example 1 with PLCLogoBindingProvider

use of org.openhab.binding.plclogo.PLCLogoBindingProvider in project openhab1-addons by openhab.

the class PLCLogoBinding method deactivate.

@Override
public void deactivate() {
    for (PLCLogoBindingProvider provider : providers) {
        provider.removeBindingChangeListener(this);
    }
    providers.clear();
    Iterator<Entry<String, PLCLogoConfig>> entries = controllers.entrySet().iterator();
    while (entries.hasNext()) {
        Entry<String, PLCLogoConfig> thisEntry = entries.next();
        PLCLogoConfig logoConfig = thisEntry.getValue();
        S7Client LogoS7Client = logoConfig.getS7Client();
        if (LogoS7Client != null) {
            LogoS7Client.Disconnect();
        }
    }
    controllers.clear();
}
Also used : Entry(java.util.Map.Entry) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) S7Client(Moka7.S7Client)

Example 2 with PLCLogoBindingProvider

use of org.openhab.binding.plclogo.PLCLogoBindingProvider 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);
                    }
                }
            }
        }
    }
}
Also used : ContactItem(org.openhab.core.library.items.ContactItem) 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) Entry(java.util.Map.Entry) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) PLCLogoBindingProvider(org.openhab.binding.plclogo.PLCLogoBindingProvider) PLCLogoBindingConfig(org.openhab.binding.plclogo.PLCLogoBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) S7Client(Moka7.S7Client)

Example 3 with PLCLogoBindingProvider

use of org.openhab.binding.plclogo.PLCLogoBindingProvider 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

S7Client (Moka7.S7Client)3 PLCLogoBindingProvider (org.openhab.binding.plclogo.PLCLogoBindingProvider)3 Entry (java.util.Map.Entry)2 PLCLogoBindingConfig (org.openhab.binding.plclogo.PLCLogoBindingConfig)2 Item (org.openhab.core.items.Item)2 ContactItem (org.openhab.core.library.items.ContactItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)2 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1