Search in sources :

Example 1 with UPnPStateVariable

use of org.osgi.service.upnp.UPnPStateVariable 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 UPnPStateVariable

use of org.osgi.service.upnp.UPnPStateVariable in project felix by apache.

the class MyCtrlPoint method serviceChanged.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
	 */
public void serviceChanged(ServiceEvent event) {
    Activator.logger.DEBUG("[Importer] serviceChanged");
    Activator.logger.DEBUG("Event::" + event.toString());
    if (event.getType() == ServiceEvent.REGISTERED) {
        /* check new listener registration */
        ServiceReference serRef = event.getServiceReference();
        Object obj = serRef.getProperty(UPnPEventListener.UPNP_FILTER);
        /* obtain interested devices for the listener */
        ServiceReference[] devicesRefs = null;
        if (obj != null) {
            Filter filter = (Filter) obj;
            String filtra = filter.toString();
            /*
				 * Avoid to implement the notification for device 
				 * that are not been created by BaseDriver
				 */
            String newfilter = "(&" + filtra + IMPORT_FLTR + ")";
            // System.out.println(newfilter);
            try {
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                /*
										   * 
										   * only if there is a compatibile device
										   */
                Dictionary dic = new Hashtable();
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
                    dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            dic.put(UPnPService.ID, services[j].getId());
                            dic.put(UPnPService.TYPE, services[j].getType());
                            // TODO add method boolean serviceEvented() so we can remove the below cycle
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length && !hasEventedVars; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    if (filter.match(dic)) {
                                        UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
                                        FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
                                        subQueue.enqueue(msg);
                                    }
                                }
                            }
                        }
                    }
                    context.ungetService(devicesRefs[i]);
                }
            }
        } else {
            /* obj==null (interested in all devices) */
            try {
                String newfilter = "(!" + EXPORT_FLTR + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean bool = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                bool = stateVars[k].sendsEvents();
                                if (bool) {
                                    break;
                                }
                            }
                            if (bool) {
                                UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
                                FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
                                subQueue.enqueue(msg);
                            }
                        }
                    }
                    context.ungetService(devicesRefs[i]);
                }
            }
        }
    } else if (event.getType() == ServiceEvent.MODIFIED) {
        Vector newServices = new Vector();
        ServiceReference serRef = event.getServiceReference();
        Filter filter = (Filter) serRef.getProperty(UPnPEventListener.UPNP_FILTER);
        UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
        ServiceReference[] devicesRefs = null;
        if (filter != null) {
            try {
                String filtra = filter.toString();
                String newfilter = "(&" + filtra + "(!" + EXPORT_FLTR + ")" + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                /*
										   * 
										   * only if there is a compatibile device
										   */
                Dictionary dic = new Hashtable();
                /* 
					 * look for the service that match
					 */
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
                    dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            dic.put(UPnPService.ID, services[j].getId());
                            dic.put(UPnPService.TYPE, services[j].getType());
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    break;
                                }
                            }
                            if (!hasEventedVars) {
                                continue;
                            }
                            boolean bool = filter.match(dic);
                            if (bool) {
                                newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
                            }
                        }
                    // for services
                    }
                    // services ==null
                    context.ungetService(devicesRefs[i]);
                }
                // for devicesRefs
                ListenerModified msg = new ListenerModified(newServices, listener);
                subQueue.enqueue(msg);
            }
        // devicesrefs !=null
        } else {
            // interrested in all devices
            try {
                String newfilter = "(!(" + UPnPDevice.UPNP_EXPORT + "=*" + ")" + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    break;
                                }
                            }
                            if (hasEventedVars) {
                                newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
                            }
                        // hasEventedvars
                        }
                    // for services
                    }
                    // services !=null
                    context.ungetService(devicesRefs[i]);
                }
                // for devicesRefs
                subQueue.enqueue(new ListenerModified(newServices, listener));
            }
        // devicesRefs !=null
        }
    } else if (event.getType() == ServiceEvent.UNREGISTERING) {
        UPnPEventListener listener = (UPnPEventListener) context.getService(event.getServiceReference());
        if (listener != null) {
            ListenerUnRegistration msg = new ListenerUnRegistration(listener);
            subQueue.enqueue(msg);
        }
        context.ungetService(event.getServiceReference());
    }
}
Also used : Dictionary(java.util.Dictionary) ListenerUnRegistration(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration) Hashtable(java.util.Hashtable) FirstMessage(org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference) Filter(org.osgi.framework.Filter) UPnPServiceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl) ListenerModified(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified) UPnPEventListener(org.osgi.service.upnp.UPnPEventListener) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) Vector(java.util.Vector)

Example 3 with UPnPStateVariable

use of org.osgi.service.upnp.UPnPStateVariable in project felix by apache.

the class UPnPEventNotifier method getCurrentStatus.

private Properties getCurrentStatus() {
    Properties status = new Properties();
    UPnPStateVariable[] vars = service.getStateVariables();
    if (vars != null) {
        for (int i = 0; i < vars.length; i++) {
            if (vars[i].sendsEvents()) {
                if (vars[i] instanceof UPnPLocalStateVariable) {
                    UPnPLocalStateVariable localVar = (UPnPLocalStateVariable) vars[i];
                    String property = localVar.getName();
                    Object value = localVar.getCurrentValue();
                    status.put(property, value);
                } else {
                    System.out.println("[Warning] The StateVariable (" + vars[i].getName() + ")" + " of device (ID:" + deviceId + ") " + "should implement the interface UPnPLocalStateVariable");
                }
            }
        }
    // end for
    }
    return status;
}
Also used : UPnPLocalStateVariable(org.osgi.service.upnp.UPnPLocalStateVariable) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) Properties(java.util.Properties)

Example 4 with UPnPStateVariable

use of org.osgi.service.upnp.UPnPStateVariable in project felix by apache.

the class ClockDevice method update.

/**
 */
public void update() {
    Clock clock = Clock.getInstance();
    Calendar cal = clock.getCalendar();
    Date date = cal.getTime();
    UPnPStateVariable variable = timerService.getStateVariable("Time");
    notifier.propertyChange(new PropertyChangeEvent(variable, "Time", null, date));
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) Calendar(java.util.Calendar) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) Date(java.util.Date)

Example 5 with UPnPStateVariable

use of org.osgi.service.upnp.UPnPStateVariable in project felix by apache.

the class ControlServlet method invoke.

private static final Map<String, Object> invoke(UPnPAction action, String[] names, String[] vals) throws Exception {
    final Map<String, Object> json = new HashMap<String, Object>();
    // check input arguments
    Hashtable inputArgs = null;
    if (names != null && vals != null && names.length > 0 && names.length == vals.length) {
        inputArgs = new Hashtable(names.length);
        for (int i = 0; i < names.length; i++) {
            final UPnPStateVariable var = action.getStateVariable(names[i]);
            final String upnpType = var.getUPnPDataType();
            final Object argObj;
            if (UPnPStateVariable.TYPE_STRING.equals(upnpType)) {
                argObj = vals[i];
            } else if (UPnPStateVariable.TYPE_CHAR.equals(upnpType)) {
                argObj = new Character(vals[i].charAt(0));
            } else if (UPnPStateVariable.TYPE_BIN_BASE64.equals(upnpType)) {
                argObj = Base64.decodeBase64(vals[i]);
            } else if (UPnPStateVariable.TYPE_BIN_HEX.equals(upnpType)) {
                argObj = Hex.decode(vals[i]);
            } else {
                Class javaType = var.getJavaDataType();
                Constructor constructor = javaType.getConstructor(new Class[] { String.class });
                argObj = constructor.newInstance(new Object[] { vals[i] });
            }
            inputArgs.put(names[i], argObj);
        }
    }
    // invoke
    final Dictionary out = action.invoke(inputArgs);
    // prepare output arguments
    if (out != null && out.size() > 0) {
        final Object[] outputs = new Object[out.size()];
        int index = 0;
        for (Enumeration e = out.keys(); e.hasMoreElements(); ) {
            final String key = (String) e.nextElement();
            final UPnPStateVariable var = action.getStateVariable(key);
            Object value = out.get(key);
            if (value instanceof Date) {
                synchronized (DATA_FORMAT) {
                    value = DATA_FORMAT.format((Date) value);
                }
            } else if (value instanceof byte[]) {
                value = Hex.encode((byte[]) value);
            }
            final Map<String, Object> output = new HashMap<String, Object>();
            // //$NON-NLS-1$
            output.put("name", key);
            // //$NON-NLS-1$
            output.put("type", var.getUPnPDataType());
            // $NON-NLS-1$
            output.put("value", value);
            outputs[index] = output;
            index++;
        }
        // //$NON-NLS-1$
        json.put("output", outputs);
    }
    return json;
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) Constructor(java.lang.reflect.Constructor) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) Date(java.util.Date)

Aggregations

UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)10 Hashtable (java.util.Hashtable)3 UPnPDevice (org.osgi.service.upnp.UPnPDevice)3 UPnPService (org.osgi.service.upnp.UPnPService)3 Date (java.util.Date)2 Dictionary (java.util.Dictionary)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 TableModelEvent (javax.swing.event.TableModelEvent)2 Argument (org.cybergarage.upnp.Argument)2 ArgumentList (org.cybergarage.upnp.ArgumentList)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 UPnPAction (org.osgi.service.upnp.UPnPAction)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 Constructor (java.lang.reflect.Constructor)1 Calendar (java.util.Calendar)1 Enumeration (java.util.Enumeration)1 Vector (java.util.Vector)1 DefaultCellEditor (javax.swing.DefaultCellEditor)1 JComboBox (javax.swing.JComboBox)1