Example usage for javax.xml.soap Detail getElementsByTagName

List of usage examples for javax.xml.soap Detail getElementsByTagName

Introduction

In this page you can find the example usage for javax.xml.soap Detail getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:org.openhab.binding.fritzboxtr064.internal.Tr064Comm.java

/**
 * Populates local static mapping table.
 * Sets the parser based on the itemcommand -> soap value parser "svp"
 * anonymous method for each mapping.//from  www  .j a  v  a 2 s .  c  o m
 */
// TODO: refactor to read from config file later?
private void generateItemMappings() {
    // services available from fbox. Needed for e.g. wifi select 5GHz/Guest Wifi
    if (_allServices.isEmpty()) { // no services are known yet?
        readAllServices();
    }

    // Mac Online Checker
    SingleItemMap imMacOnline = SingleItemMap.builder().itemCommand("maconline")
            .serviceId("urn:LanDeviceHosts-com:serviceId:Hosts1").itemArgumentName("NewActive")
            .configArgumentNames("NewMACAddress").readServiceCommand("GetSpecificHostEntry")
            .soapValueParser(new SoapValueParser() {

                @Override
                protected String parseValueFromSoapFault(ItemConfiguration itemConfiguration,
                        SOAPFault soapFault, ItemMap mapping) {
                    String value = null;
                    Detail detail = soapFault.getDetail();
                    if (detail != null) {
                        NodeList nlErrorCode = detail.getElementsByTagName("errorCode");
                        Node nErrorCode = nlErrorCode.item(0);
                        String errorCode = nErrorCode.getTextContent();
                        if (errorCode.equals("714")) {
                            value = "MAC not known to FritzBox!";
                            logger.debug(value);
                        }
                    }

                    if (value == null) {
                        value = super.parseValueFromSoapFault(itemConfiguration, soapFault, mapping);
                    }

                    return value;
                }
            }).build();
    addItemMap(imMacOnline);

    addItemMap(new MultiItemMap(
            Arrays.asList("modelName", "manufacturerName", "softwareVersion", "serialNumber"), "GetInfo",
            "urn:DeviceInfo-com:serviceId:DeviceInfo1", name -> "New" + WordUtils.capitalize(name)));
    addItemMap(SingleItemMap.builder().itemCommand("wanip")
            .serviceId("urn:WANPPPConnection-com:serviceId:WANPPPConnection1")
            .itemArgumentName("NewExternalIPAddress").readServiceCommand("GetExternalIPAddress").build());
    addItemMap(SingleItemMap.builder().itemCommand("externalWanip")
            .serviceId("urn:WANIPConnection-com:serviceId:WANIPConnection1")
            .itemArgumentName("NewExternalIPAddress").readServiceCommand("GetExternalIPAddress").build());

    // WAN Status
    addItemMap(new MultiItemMap(
            Arrays.asList("wanWANAccessType", "wanLayer1UpstreamMaxBitRate", "wanLayer1DownstreamMaxBitRate",
                    "wanPhysicalLinkStatus"),
            "GetCommonLinkProperties", "urn:WANCIfConfig-com:serviceId:WANCommonInterfaceConfig1",
            name -> name.replace("wan", "New")));
    addItemMap(SingleItemMap.builder().itemCommand("wanTotalBytesSent")
            .serviceId("urn:WANCIfConfig-com:serviceId:WANCommonInterfaceConfig1")
            .itemArgumentName("NewTotalBytesSent").readServiceCommand("GetTotalBytesSent").build());
    addItemMap(SingleItemMap.builder().itemCommand("wanTotalBytesReceived")
            .serviceId("urn:WANCIfConfig-com:serviceId:WANCommonInterfaceConfig1")
            .itemArgumentName("NewTotalBytesReceived").readServiceCommand("GetTotalBytesReceived").build());

    // DSL Status
    addItemMap(new MultiItemMap(
            Arrays.asList("dslEnable", "dslStatus", "dslUpstreamCurrRate", "dslDownstreamCurrRate",
                    "dslUpstreamMaxRate", "dslDownstreamMaxRate", "dslUpstreamNoiseMargin",
                    "dslDownstreamNoiseMargin", "dslUpstreamAttenuation", "dslDownstreamAttenuation"),
            "GetInfo", "urn:WANDSLIfConfig-com:serviceId:WANDSLInterfaceConfig1",
            name -> name.replace("dsl", "New")));
    addItemMap(new MultiItemMap(Arrays.asList("dslFECErrors", "dslHECErrors", "dslCRCErrors"),
            "GetStatisticsTotal", "urn:WANDSLIfConfig-com:serviceId:WANDSLInterfaceConfig1",
            name -> name.replace("dsl", "New")));

    // Wifi 2,4GHz
    SingleItemMap imWifi24Switch = SingleItemMap.builder().itemCommand("wifi24Switch")
            .serviceId("urn:WLANConfiguration-com:serviceId:WLANConfiguration1").itemArgumentName("NewEnable")
            .readServiceCommand("GetInfo").writeServiceCommand("SetEnable").build();
    addItemMap(imWifi24Switch);

    // wifi 5GHz
    SingleItemMap imWifi50Switch = SingleItemMap.builder().itemCommand("wifi50Switch")
            .serviceId("urn:WLANConfiguration-com:serviceId:WLANConfiguration2").itemArgumentName("NewEnable")
            .readServiceCommand("GetInfo").writeServiceCommand("SetEnable").build();

    // guest wifi
    SingleItemMap imWifiGuestSwitch = SingleItemMap.builder().itemCommand("wifiGuestSwitch")
            .serviceId("urn:WLANConfiguration-com:serviceId:WLANConfiguration3").itemArgumentName("NewEnable")
            .readServiceCommand("GetInfo").writeServiceCommand("SetEnable").build();

    // check if 5GHz wifi and/or guest wifi is available.
    Tr064Service svc5GHzWifi = determineServiceByItemMapping(imWifi50Switch);
    Tr064Service svcGuestWifi = determineServiceByItemMapping(imWifiGuestSwitch);

    if (svc5GHzWifi != null && svcGuestWifi != null) { // WLANConfiguration3+2 present -> guest wifi + 5Ghz present
        // prepared properly, only needs to be added
        addItemMap(imWifi50Switch);
        addItemMap(imWifiGuestSwitch);
        logger.debug("Found 2,4 Ghz, 5Ghz and Guest Wifi");
    }

    if (svc5GHzWifi != null && svcGuestWifi == null) { // WLANConfiguration3 not present but 2 -> no 5Ghz Wifi
                                                       // available but Guest Wifi
                                                       // remap itemMap for Guest Wifi from 3 to 2
        imWifiGuestSwitch.setServiceId("urn:WLANConfiguration-com:serviceId:WLANConfiguration2");
        addItemMap(imWifiGuestSwitch);// only add guest wifi, no 5Ghz
        logger.debug("Found 2,4 Ghz and Guest Wifi");
    }
    if (svc5GHzWifi == null && svcGuestWifi == null) { // WLANConfiguration3+2 not present > no 5Ghz Wifi or Guest
                                                       // Wifi
        logger.debug("Found 2,4 Ghz Wifi");
    }

    // Phonebook Download
    // itemcommand is dummy: not a real item
    ItemMap imPhonebook = SingleItemMap.builder().itemCommand("phonebook")
            .serviceId("urn:X_AVM-DE_OnTel-com:serviceId:X_AVM-DE_OnTel1").configArgumentNames("NewPhonebookID")
            .itemArgumentName("NewPhonebookURL").readServiceCommand("GetPhonebook").build();
    addItemMap(imPhonebook);

    // TAM (telephone answering machine) Switch
    SingleItemMap imTamSwitch = SingleItemMap.builder().itemCommand("tamSwitch")
            .serviceId("urn:X_AVM-DE_TAM-com:serviceId:X_AVM-DE_TAM1").configArgumentNames("NewIndex")
            .itemArgumentName("NewEnable").readServiceCommand("GetInfo").writeServiceCommand("SetEnable")
            .build();
    addItemMap(imTamSwitch);

    // New Messages per TAM ID
    // two requests needed: First gets URL to download tam info from, 2nd contains
    // info of messages
    SingleItemMap imTamNewMessages = SingleItemMap.builder().itemCommand("tamNewMessages")
            .serviceId("urn:X_AVM-DE_TAM-com:serviceId:X_AVM-DE_TAM1").configArgumentNames("NewIndex")
            .itemArgumentName("NewURL").readServiceCommand("GetMessageList")
            .soapValueParser(new SoapValueParser() {

                @Override
                protected String parseValueFromSoapBody(ItemConfiguration itemConfiguration, SOAPBody soapBody,
                        ItemMap mapping) {
                    String value = null;

                    // extract URL from soap response
                    String url = super.parseValueFromSoapBody(itemConfiguration, soapBody, mapping);

                    if (url != null) {
                        Document xmlTamInfo = getFboxXmlResponse(url);
                        if (xmlTamInfo != null) {
                            logger.debug("Parsing xml message TAM info {}",
                                    Helper.documentToString(xmlTamInfo));
                            NodeList nlNews = xmlTamInfo.getElementsByTagName("New"); // get all Nodes containing
                                                                                      // "new", indicating message
                                                                                      // was not listened to

                            // When <new> contains 1 -> message is new, when 0, message not new -> Counting
                            // 1s
                            int newMessages = 0;
                            for (int i = 0; i < nlNews.getLength(); i++) {
                                if (nlNews.item(i).getTextContent().equals("1")) {
                                    newMessages++;
                                }
                            }
                            value = Integer.toString(newMessages);
                            logger.debug("Parsed new messages as: {}", value);
                        } else {
                            logger.warn("Failed to read TAM info from URL {}", url);
                            // cause was already logged earlier
                        }
                    }

                    return value;
                }
            }).build();
    addItemMap(imTamNewMessages);

    // Missed calls
    // two requests: 1st fetches URL to download call list, 2nd fetches xml call
    // list
    SingleItemMap imMissedCalls = SingleItemMap.builder().itemCommand("missedCallsInDays")
            .serviceId("urn:X_AVM-DE_OnTel-com:serviceId:X_AVM-DE_OnTel1").itemArgumentName("NewCallListURL")
            .readServiceCommand("GetCallList").configArgumentNames("NewDays")
            .soapValueParser(new SoapValueParser() {

                @Override
                protected String parseValueFromSoapBody(ItemConfiguration itemConfiguration, SOAPBody soapBody,
                        ItemMap mapping) {
                    String value = null;

                    // extract URL from soap response
                    String url = super.parseValueFromSoapBody(itemConfiguration, soapBody, mapping);

                    // extract how many days of call list should be examined for missed calls
                    String days = "3"; // default
                    if (!itemConfiguration.getArgumentValues().isEmpty()) {
                        days = itemConfiguration.getArgumentValues().get(0); // set the days as defined in item
                                                                             // config.
                                                                             // Otherwise default value of 3 is used
                    }

                    if (url != null) {
                        // only get missed calls of the last x days
                        url = url + "&days=" + days;
                        logger.debug("Downloading call list using url {}", url);
                        Document callListInfo = getFboxXmlResponse(url); // download call list
                        if (callListInfo != null) {
                            logger.debug("Parsing xml message call list info {}",
                                    Helper.documentToString(callListInfo));
                            NodeList nlTypes = callListInfo.getElementsByTagName("Type"); // get all Nodes
                                                                                          // containing "Type". Type
                                                                                          // 2 => missed

                            // When <type> contains 2 -> call was missed -> Counting only 2 entries
                            int missedCalls = 0;
                            for (int i = 0; i < nlTypes.getLength(); i++) {
                                if (nlTypes.item(i).getTextContent().equals("2")) {
                                    missedCalls++;
                                }
                            }
                            value = Integer.toString(missedCalls);
                            logger.debug("Parsed new messages as: {}", value);
                        } else {
                            logger.warn("Failed to read call list info from URL {}", url);
                            // cause was already logged earlier
                        }
                    }

                    return value;
                }
            }).build();
    addItemMap(imMissedCalls);

    // call deflection
    SingleItemMap callDeflection = SingleItemMap.builder().itemCommand("callDeflectionSwitch")
            .serviceId("urn:X_AVM-DE_OnTel-com:serviceId:X_AVM-DE_OnTel1")
            .configArgumentNames("NewDeflectionId").itemArgumentName("NewEnable")
            .readServiceCommand("GetDeflection").writeServiceCommand("SetDeflectionEnable").build();
    addItemMap(callDeflection);
}