Search in sources :

Example 41 with Item

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

the class ItemStateRequestProcessor method getItemStates.

public List<ItemStateData> getItemStates() throws ServiceException {
    List<ItemStateData> itemStates = new ArrayList<ItemStateData>();
    ItemRegistry itemRegistry = getItemRegistry();
    for (Item item : itemRegistry.getItems()) {
        logger.debug("Item: " + item.getName() + " " + item.getState());
        StateTransformable state = getState(item);
        ItemStateData itemState = new ItemStateData(System.currentTimeMillis(), item.getName(), state);
        itemStates.add(itemState);
    }
    return itemStates;
}
Also used : Item(org.openhab.core.items.Item) StateTransformable(org.creek.mailcontrol.model.data.StateTransformable) ArrayList(java.util.ArrayList) ItemStateData(org.creek.mailcontrol.model.data.ItemStateData) ItemRegistry(org.openhab.core.items.ItemRegistry)

Example 42 with Item

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

the class LgTvMessageReader method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String remoteaddr = req.getRemoteAddr();
    int start = remoteaddr.indexOf(":");
    String t;
    if (start > -1) {
        t = remoteaddr.substring(0, start);
    } else {
        t = remoteaddr;
    }
    remoteaddr = t;
    start = remoteaddr.indexOf("/");
    if (start > -1) {
        t = remoteaddr.substring(start + 1, remoteaddr.length());
    } else {
        t = remoteaddr;
    }
    remoteaddr = t;
    PrintWriter out = res.getWriter();
    res.setStatus(200);
    String devicename = req.getParameter("devicename");
    String value = req.getParameter("command");
    if (value != null && !value.equals("")) {
        res.setContentType("text/plain");
        if (value.equals("geturl")) {
            if (bindingprovider != null && itemregistry != null) {
                for (String itemName : bindingprovider.getItemNames()) {
                    HashMap<String, String> values = bindingprovider.getDeviceCommands(itemName);
                    for (String cmd : values.keySet()) {
                        String[] commandParts = values.get(cmd).split(":");
                        String deviceCmd = commandParts[1];
                        String deviceId = commandParts[0];
                        if (deviceId.equals(devicename) && deviceCmd.equals("BROWSER_URL")) {
                            try {
                                Item i = itemregistry.getItem(itemName);
                                State state = i.getState();
                                String va = state.toString();
                                out.print(va);
                            } catch (ItemNotFoundException e) {
                                logger.error("item not found");
                            }
                        }
                    }
                }
            } else {
                logger.error("itemregistry=null or bindingprovider=null");
            }
        } else {
            out.println("command: " + value);
        }
    } else {
        String url = req.getRequestURL().toString();
        // String devicename=req.getParameter("devicename");
        res.setContentType("text/html");
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>LgTv Binding</TITLE>");
        out.println("<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>");
        out.println("<script type=\"text/javascript\">");
        out.println("var serviceaddress;");
        out.println("var oldpage;");
        out.println("function CallRegular(){");
        out.println("$.ajax({ type:\'Get\', url: serviceaddress, success:function(data) ");
        out.println(" { ");
        out.println("   if (data!=\"Uninitialized\"&&data!=oldpage) {document.getElementById(\'content1\').src=data;oldpage=data;} ");
        out.println(" } })");
        out.println("}");
        out.println("function LoadPage(){");
        out.println(" serviceaddress=\'" + url + "?command=geturl&devicename=" + devicename + "\'");
        out.println(" CallRegular(); ");
        out.println(" setInterval(CallRegular,10000);");
        out.println("}");
        out.println(" </script>");
        out.println("</HEAD>");
        out.println("<frameset rows=\"100%,*\" onload=\"LoadPage();\">");
        out.println("<frame src=\"#\" id=\"content1\">");
        out.println("</frameset>");
        out.println("<BODY>");
        out.println("<BIG>Hello World</BIG>");
        out.println("</BODY></HTML>");
    }
    out.close();
}
Also used : Item(org.openhab.core.items.Item) State(org.openhab.core.types.State) PrintWriter(java.io.PrintWriter) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 43 with Item

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

the class OneWireBinding method updateItemFromOneWire.

/**
     * Update an item with value from 1-wire device property
     *
     * @param pvItemName
     */
public void updateItemFromOneWire(String pvItemName) {
    if (OneWireConnection.getConnection() != null) {
        AbstractOneWireDevicePropertyBindingConfig pvBindingConfig = (AbstractOneWireDevicePropertyBindingConfig) getBindingConfig(pvItemName);
        if (pvBindingConfig == null) {
            logger.error("no bindingConfig found for itemName={} cannot update! It will be removed from scheduler", pvItemName);
            ivOneWireReaderScheduler.removeItem(pvItemName);
            return;
        }
        String lvReadValue = OneWireConnection.readFromOneWire(pvBindingConfig);
        Item lvItem = getItem(pvItemName);
        if (lvReadValue != null) {
            Type lvNewType = pvBindingConfig.convertReadValueToType(lvReadValue);
            if (lvItem != null) {
                postUpdate(lvItem, lvNewType);
            } else {
                logger.error("There is no Item for ItemName={}", pvItemName);
            }
        } else {
            String lvLogText = "Set Item for itemName=" + pvItemName + " to Undefined, because the readvalue is null";
            if (pvBindingConfig.isIgnoreReadErrors()) {
                logger.debug(lvLogText);
            } else {
                logger.error(lvLogText);
            }
            postUpdate(lvItem, UnDefType.UNDEF);
        }
    }
}
Also used : Item(org.openhab.core.items.Item) Type(org.openhab.core.types.Type) UnDefType(org.openhab.core.types.UnDefType) AbstractOneWireDevicePropertyBindingConfig(org.openhab.binding.onewire.internal.deviceproperties.AbstractOneWireDevicePropertyBindingConfig)

Example 44 with Item

use of org.openhab.core.items.Item 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)

Example 45 with Item

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

the class SappBinding method updateState.

/**
     * updates item repository for a single item
     */
private void updateState(String pnmasId, SappAddressType sappAddressType, int addressToUpdate, int newState, SappBindingProvider provider) {
    logger.debug("Updating {} {} with new value {}", sappAddressType, addressToUpdate, newState);
    for (String itemName : provider.getItemNames()) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
                SappBindingConfigSwitchItem sappBindingConfigSwitchItem = (SappBindingConfigSwitchItem) provider.getBindingConfig(itemName);
                if (!sappBindingConfigSwitchItem.isPollerSuspender()) {
                    SappAddressOnOffStatus statusAddress = sappBindingConfigSwitchItem.getStatus();
                    if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                        logger.debug("found binding to update {}", sappBindingConfigSwitchItem);
                        int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                        State stateToSet = result == statusAddress.getOnValue() ? OnOffType.ON : OnOffType.OFF;
                        if (!stateToSet.equals(item.getState())) {
                            eventPublisher.postUpdate(itemName, stateToSet);
                        }
                    }
                }
            } else if (item instanceof ContactItem) {
                SappBindingConfigContactItem sappBindingConfigContactItem = (SappBindingConfigContactItem) provider.getBindingConfig(itemName);
                SappAddressOpenClosedStatus statusAddress = sappBindingConfigContactItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigContactItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                    State stateToSet = result == statusAddress.getOpenValue() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof NumberItem) {
                SappBindingConfigNumberItem sappBindingConfigNumberItem = (SappBindingConfigNumberItem) provider.getBindingConfig(itemName);
                SappAddressDecimal address = sappBindingConfigNumberItem.getStatus();
                if (address.getAddressType() == sappAddressType && address.getPnmasId().equals(pnmasId) && addressToUpdate == address.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigNumberItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(address.getSubAddress(), newState);
                    State stateToSet = new DecimalType(address.scaledValue(result, address.getSubAddress()));
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof RollershutterItem) {
                SappBindingConfigRollershutterItem sappBindingConfigRollershutterItem = (SappBindingConfigRollershutterItem) provider.getBindingConfig(itemName);
                SappAddressRollershutterStatus statusAddress = sappBindingConfigRollershutterItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigRollershutterItem);
                    int result = SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState);
                    State stateToSet = result == statusAddress.getOpenValue() ? PercentType.HUNDRED : (result == statusAddress.getClosedValue() ? PercentType.ZERO : PercentType.valueOf("50"));
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else if (item instanceof DimmerItem) {
                SappBindingConfigDimmerItem sappBindingConfigDimmerItem = (SappBindingConfigDimmerItem) provider.getBindingConfig(itemName);
                SappAddressDimmer statusAddress = sappBindingConfigDimmerItem.getStatus();
                if (statusAddress.getAddressType() == sappAddressType && statusAddress.getPnmasId().equals(pnmasId) && addressToUpdate == statusAddress.getAddress()) {
                    logger.debug("found binding to update {}", sappBindingConfigDimmerItem);
                    int result = statusAddress.scaledValue(SappBindingConfigUtils.maskWithSubAddress(statusAddress.getSubAddress(), newState), statusAddress.getSubAddress()).round(new MathContext(0, RoundingMode.HALF_EVEN)).intValue();
                    State stateToSet;
                    if (result <= PercentType.ZERO.intValue()) {
                        stateToSet = PercentType.ZERO;
                    } else if (result >= PercentType.HUNDRED.intValue()) {
                        stateToSet = PercentType.HUNDRED;
                    } else {
                        stateToSet = PercentType.valueOf(String.valueOf(result));
                    }
                    if (!stateToSet.equals(item.getState())) {
                        eventPublisher.postUpdate(itemName, stateToSet);
                    }
                }
            } else {
                logger.error("unimplemented item type: {}", item.getClass().getSimpleName());
            }
        } catch (ItemNotFoundException e) {
            logger.error("Item {} not found", itemName);
        }
    }
}
Also used : SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) MathContext(java.math.MathContext) SappAddressOpenClosedStatus(org.openhab.binding.sapp.internal.model.SappAddressOpenClosedStatus) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) SappBindingConfigContactItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem) NumberItem(org.openhab.core.library.items.NumberItem) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SappAddressOnOffStatus(org.openhab.binding.sapp.internal.model.SappAddressOnOffStatus) SappAddressDimmer(org.openhab.binding.sapp.internal.model.SappAddressDimmer) SappAddressDecimal(org.openhab.binding.sapp.internal.model.SappAddressDecimal) State(org.openhab.core.types.State) SappAddressRollershutterStatus(org.openhab.binding.sapp.internal.model.SappAddressRollershutterStatus) SappBindingConfigDimmerItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SappBindingConfigNumberItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SappBindingConfigRollershutterItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) SwitchItem(org.openhab.core.library.items.SwitchItem) SappBindingConfigSwitchItem(org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

Item (org.openhab.core.items.Item)59 SwitchItem (org.openhab.core.library.items.SwitchItem)23 NumberItem (org.openhab.core.library.items.NumberItem)20 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)16 State (org.openhab.core.types.State)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14 DecimalType (org.openhab.core.library.types.DecimalType)9 HistoricItem (org.openhab.core.persistence.HistoricItem)9 ArrayList (java.util.ArrayList)7 ColorItem (org.openhab.core.library.items.ColorItem)7 StringItem (org.openhab.core.library.items.StringItem)7 HomematicBindingConfig (org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)6 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6 Test (org.junit.Test)5 GenericItem (org.openhab.core.items.GenericItem)5 GroupItem (org.openhab.core.items.GroupItem)5 ItemRegistry (org.openhab.core.items.ItemRegistry)5 Calendar (java.util.Calendar)3