Search in sources :

Example 1 with StateVariable

use of org.cybergarage.upnp.StateVariable in project felix by apache.

the class BuildDevice method addServices.

/**
 * Method used to create a new Service in CyberLink world without creating the XML
 *
 * @param id ServiceId
 * @param devUPnP the CyberLink device that where the new Service will be created
 * @param sr ServiceReference to OSGi Device that used as source of the information
 *              for the creation of the device
 */
private static void addServices(String id, Device devUPnP, ServiceReference sr) {
    UPnPDevice devOSGi = (UPnPDevice) Activator.bc.getService(sr);
    if (devOSGi == null) {
        // added by twa to prevent a null pointer exception
        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " is deregistered from the framework while is exported");
        return;
    }
    UPnPService[] services = devOSGi.getServices();
    if (services == null || services.length == 0)
        return;
    for (int i = 0; i < services.length; i++) {
        Service ser = new Service();
        devUPnP.addService(ser);
        ser.setServiceType(services[i].getType());
        ser.setServiceID(services[i].getId());
        ser.setSCPDURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setDescriptionURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setControlURL(id + "/service/" + i + "/ctrl");
        ser.setEventSubURL(id + "/service/" + i + "/event");
        UPnPAction[] actions = services[i].getActions();
        for (int j = 0; j < actions.length; j++) {
            boolean valid = true;
            Action act = new Action(ser.getServiceNode());
            act.setName(actions[j].getName());
            ArgumentList al = new ArgumentList();
            String[] names = actions[j].getInputArgumentNames();
            if (names != null) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Input argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                        valid = false;
                        break;
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.IN);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            names = actions[j].getOutputArgumentNames();
            if (names != null && valid) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Output argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.OUT);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            if (valid) {
                act.setArgumentList(al);
                ser.addAction(act);
            }
        }
        UPnPStateVariable[] vars = services[i].getStateVariables();
        for (int j = 0; j < vars.length; j++) {
            StateVariable var = new StateVariable();
            var.setDataType(vars[j].getUPnPDataType());
            var.setName(vars[j].getName());
            var.setSendEvents(vars[j].sendsEvents());
            String[] values = vars[j].getAllowedValues();
            if (values != null) {
                AllowedValueList avl = new AllowedValueList(values);
                var.setAllowedValueList(avl);
            } else if (vars[j].getMaximum() != null) {
                AllowedValueRange avr = new AllowedValueRange(vars[j].getMaximum(), vars[j].getMinimum(), vars[j].getStep());
                var.setAllowedValueRange(avr);
            }
            if (vars[j].getDefaultValue() != null)
                try {
                    var.setDefaultValue(Converter.toString(vars[j].getDefaultValue(), vars[j].getUPnPDataType()));
                } catch (Exception ignored) {
                }
            ser.addStateVariable(var);
        }
        Activator.bc.ungetService(sr);
    }
}
Also used : UPnPAction(org.osgi.service.upnp.UPnPAction) Action(org.cybergarage.upnp.Action) AllowedValueList(org.cybergarage.upnp.AllowedValueList) AllowedValueRange(org.cybergarage.upnp.AllowedValueRange) Argument(org.cybergarage.upnp.Argument) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) StateVariable(org.cybergarage.upnp.StateVariable) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 2 with StateVariable

use of org.cybergarage.upnp.StateVariable in project felix by apache.

the class MyCtrlPoint method searchForListener.

public void searchForListener(Device device) {
    Activator.logger.DEBUG("[Importer] searching for UPnPEventListener");
    ServiceReference[] listeners = null;
    try {
        listeners = context.getServiceReferences(UPnPEventListener.class.getName(), null);
    } catch (InvalidSyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (listeners != null) {
        String deviceID = device.getUDN();
        String serviceID;
        String deviceType = device.getDeviceType();
        String serviceType;
        Hashtable hash = new Hashtable();
        hash.put(UPnPDevice.ID, deviceID);
        hash.put(UPnPDevice.TYPE, deviceType);
        ServiceList services = device.getServiceList();
        Vector eventedSers = new Vector();
        for (int i = 0; i < services.size(); i++) {
            Service service = (Service) services.elementAt(i);
            ServiceStateTable vars = service.getServiceStateTable();
            for (int j = 0; j < vars.size(); j++) {
                StateVariable var = (StateVariable) vars.elementAt(j);
                if (var.isSendEvents()) {
                    eventedSers.add(service);
                    break;
                }
            }
        }
        for (int i = 0; i < listeners.length; i++) {
            UPnPEventListener listener = (UPnPEventListener) context.getService(listeners[i]);
            Filter filter = (Filter) listeners[i].getProperty(UPnPEventListener.UPNP_FILTER);
            if (filter == null) {
                for (int j = 0; j < eventedSers.size(); j++) {
                    Service ser = (Service) eventedSers.elementAt(j);
                    subQueue.enqueue(new FirstMessage(ser, listener));
                }
            } else {
                for (int j = 0; j < eventedSers.size(); j++) {
                    Service ser = (Service) eventedSers.elementAt(j);
                    serviceID = ser.getServiceID();
                    serviceType = ser.getServiceType();
                    hash.put(UPnPService.ID, serviceID);
                    hash.put(UPnPService.TYPE, serviceType);
                    boolean bool = filter.match(hash);
                    if (bool) {
                        subQueue.enqueue(new FirstMessage(ser, listener));
                    }
                }
            }
        }
    }
}
Also used : UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) StateVariable(org.cybergarage.upnp.StateVariable) Hashtable(java.util.Hashtable) ServiceList(org.cybergarage.upnp.ServiceList) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) FirstMessage(org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage) ServiceStateTable(org.cybergarage.upnp.ServiceStateTable) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference) Filter(org.osgi.framework.Filter) UPnPEventListener(org.osgi.service.upnp.UPnPEventListener) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Vector(java.util.Vector)

Example 3 with StateVariable

use of org.cybergarage.upnp.StateVariable in project i2p.i2p by i2p.

the class UPnP method listStateTable.

/**
 * debug only
 */
private static void listStateTable(Service serv, StringBuilder sb) {
    ServiceStateTable table;
    try {
        table = serv.getServiceStateTable();
    } catch (RuntimeException e) {
        // getSCPDNode() returns null,
        // NPE at org.cybergarage.upnp.Service.getServiceStateTable(Service.java:526)
        sb.append(" : no state");
        return;
    }
    sb.append("<ul><small>");
    for (int i = 0; i < table.size(); i++) {
        StateVariable current = table.getStateVariable(i);
        sb.append("<li>").append(DataHelper.escapeHTML(current.getName())).append(" : \"").append(DataHelper.escapeHTML(current.getValue())).append("\"</li>");
    }
    sb.append("</small></ul>");
}
Also used : StateVariable(org.cybergarage.upnp.StateVariable) ServiceStateTable(org.cybergarage.upnp.ServiceStateTable) ControlPoint(org.cybergarage.upnp.ControlPoint)

Example 4 with StateVariable

use of org.cybergarage.upnp.StateVariable in project felix by apache.

the class ExporterUPnPEventListener method notifyUPnPEvent.

/**
 * @see org.osgi.service.upnp.UPnPEventListener#notifyUPnPEvent(java.lang.String, java.lang.String, java.util.Dictionary)
 */
public void notifyUPnPEvent(String deviceId, String serviceId, Dictionary events) {
    Device dAux = null;
    if (d.getUDN().equals(deviceId)) {
        dAux = d;
    } else {
        dAux = d.getDevice(deviceId);
    }
    Service s = dAux.getService(serviceId);
    // fix 2/9/2004 francesco
    Enumeration e = events.keys();
    while (e.hasMoreElements()) {
        StateVariable sv;
        String dataType;
        String name;
        Object key = e.nextElement();
        if (key instanceof String) {
            name = (String) key;
            sv = s.getStateVariable(name);
            dataType = sv.getDataType();
        } else {
            Activator.logger.ERROR(deviceId + " notified the change in the StateVariable of " + serviceId + " but the key Java type contained in the Dictiories was " + key.getClass().getName() + " instead of " + String.class.getName() + " as specified by Javadoc");
            continue;
        }
        try {
            sv.setValue(Converter.toString(events.get(key), dataType));
        } catch (Exception ignored) {
            Activator.logger.ERROR("UPnP Base Driver Exporter: error converting datatype while sending event, exception message follows:" + ignored.getMessage());
        }
    }
}
Also used : Enumeration(java.util.Enumeration) StateVariable(org.cybergarage.upnp.StateVariable) Device(org.cybergarage.upnp.Device) Service(org.cybergarage.upnp.Service)

Aggregations

StateVariable (org.cybergarage.upnp.StateVariable)4 Service (org.cybergarage.upnp.Service)3 ControlPoint (org.cybergarage.upnp.ControlPoint)2 ServiceStateTable (org.cybergarage.upnp.ServiceStateTable)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 UPnPService (org.osgi.service.upnp.UPnPService)2 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)2 Enumeration (java.util.Enumeration)1 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1 FirstMessage (org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage)1 Action (org.cybergarage.upnp.Action)1 AllowedValueList (org.cybergarage.upnp.AllowedValueList)1 AllowedValueRange (org.cybergarage.upnp.AllowedValueRange)1 Argument (org.cybergarage.upnp.Argument)1 ArgumentList (org.cybergarage.upnp.ArgumentList)1 Device (org.cybergarage.upnp.Device)1 ServiceList (org.cybergarage.upnp.ServiceList)1 Filter (org.osgi.framework.Filter)1 ServiceReference (org.osgi.framework.ServiceReference)1