Yes i'm using them in every exported device type...
Wait - i paste you some code of a whole device type implementation :-)

Note that there is also a upnpdimmabledevice which is reusing the status
things of switchabledevice
It just provides an additional level....my class hierarchy of my own
basedriver is also built like that - so i could reduce "copy paste" code




Ok every upnp device is extending my upnp general device:

public abstract class UPnPGeneralDevice {

        // device identifiers
        final protected String DEVICE_ID_PREFIX = "uuid:AT-SM-";
        protected String DEVICE_ID;
        
        // model and context
        protected BundleContext context;
        
        // device category and description
        protected String deviceCat;
        protected String description;
        protected String hubId;
        protected String identity;
        
        // localhost information
        protected String ipAddress;
        protected String portNumber;
        
        // path to presentation servlet
        protected String presServlet;
        
        // device properties
        protected Dictionary devprops;
        
        
        public void generateHostInfo() {
                
        try {
            Enumeration interfaces =
NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface =
(NetworkInterface)interfaces.nextElement();
                
                Enumeration addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress =
(InetAddress)addresses.nextElement();
                    // ignore loopback ip
                    if (!inetAddress.isLoopbackAddress() &&
!inetAddress.isLinkLocalAddress()) {
                       this.ipAddress = inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }               
                
                this.portNumber = "80";
                if(context.getProperty("org.osgi.service.http.port") !=
null) {
                        this.portNumber =
context.getProperty("org.osgi.service.http.port");
                }
        }
        
        public void setupProperties() {
                devprops = new Properties();
                // indicate for UPnP Export and set proper device category
                devprops.put(UPnPDevice.UPNP_EXPORT,"");
        
devprops.put(org.osgi.service.device.Constants.DEVICE_CATEGORY, new
String[]{UPnPDevice.DEVICE_CATEGORY});
        
                // device specific properties
                devprops.put(UPnPDevice.FRIENDLY_NAME, deviceCat);
                devprops.put(UPnPDevice.MANUFACTURER, "Daniel Felsing");
                devprops.put(UPnPDevice.MANUFACTURER_URL,
"http://www.maven.at";);
                devprops.put(UPnPDevice.MODEL_DESCRIPTION, description);
                devprops.put(UPnPDevice.MODEL_NAME, deviceCat);
                devprops.put(UPnPDevice.MODEL_NUMBER,"2.0");
                if (this.ipAddress != null && this.portNumber != null &&
this.presServlet != null) {
                        devprops.put(UPnPDevice.PRESENTATION_URL,"http://"; +
this.ipAddress + ":" + this.portNumber + presServlet + "?identity=" +
identity);
                }
                //dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");
                devprops.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:"
+ deviceCat + ":1");
                devprops.put(UPnPDevice.ID, DEVICE_ID);
                devprops.put(UPnPDevice.UPC,"SMARTHOME_" +
hubId.toUpperCase() + "_" + deviceCat.toUpperCase());           
        }       
        
        public Dictionary getDevprops() {
                return devprops;
        }


        public void setDevprops(Dictionary devprops) {
                this.devprops = devprops;
        }               
        
}


My switchabledevice (e.g. a plug or a light)

public class UPnPSwitchableDevice extends UPnPGeneralDevice implements
UPnPDevice, Destroyable {       
        
        // upnp services
        private SwitchableDevSwitchService switchdevswitchservice;
        private GeneralDevNameService switchdevnameservice;
        private UPnPService[] upnpservices;
        
        // event notifier
        private UPnPEventNotifier notify_switch;
        
        // device
        private SwitchableDevice switchDev;
        
        public UPnPSwitchableDevice(SwitchableDevice switchDev,
BundleContext context, String hubId, String deviceCat, String description,
String presServlet) {
                super();
                this.switchDev = switchDev;
                this.context = context;
                this.presServlet = presServlet;
                this.deviceCat = deviceCat;
                this.description = description;
                this.hubId = hubId;
                this.identity = switchDev.getIdentity();                
                
                if(hubId != null) {
                        this.DEVICE_ID = DEVICE_ID_PREFIX + hubId + "-" +
switchDev.getIdentity();
                } else {
                        this.DEVICE_ID = DEVICE_ID_PREFIX +
switchDev.getIdentity();        
                }
                
                // create services of the device
                this.switchdevswitchservice = new
SwitchableDevSwitchService(switchDev);
                this.switchdevnameservice = new
GeneralDevNameService(switchDev, hubId);
                this.upnpservices = new UPnPService[]{switchdevnameservice,
switchdevswitchservice};
                this.generateHostInfo();
                this.setupProperties();
                
                // setup event notifier
                notify_switch = new UPnPEventNotifier(context, this,
switchdevswitchservice, switchDev);
        }
        
        public Dictionary getDescriptions(String locale) {
                return devprops;
        }

        public UPnPIcon[] getIcons(String locale) {
                UPnPIcon icon = new DeviceIcon(switchDev);
                return new UPnPIcon[]{icon} ;
        }

        public UPnPService getService(String serviceId) {
                if  (serviceId.equals(switchdevswitchservice.getId())) 
                        return switchdevswitchservice;
                else if (serviceId.equals(switchdevnameservice.getId())) 
                        return switchdevnameservice;
                else
                        return null;
        }

        public UPnPService[] getServices() {
                return upnpservices;
        }
        
        public void destroy() {
                // destroy listeners...
                notify_switch.destroy();
        }       

}


My Switch Service:

public class SwitchableDevSwitchService implements UPnPService {
        final private String SERVICE_ID =
"urn:upnp-org:serviceId:SwitchDevice:1";
        final private String SERVICE_TYPE =
"urn:schemas-upnp-org:service:SwitchDevice:1";
        final private String VERSION ="2.0";
        
        private HashMap actions = new HashMap();
        
        private UPnPStateVariable statusstate;
        private UPnPStateVariable[] states;

        public SwitchableDevSwitchService(SwitchableDevice switchDev) {
                super();
                this.statusstate = new
SwitchableDevStatusStateVariable(switchDev);
                this.states = new UPnPStateVariable[]{statusstate};
                
                UPnPAction setUPnPStatus = new
SwitchableDevSetStatusAction(statusstate, switchDev);
                UPnPAction getUPnPStatus = new
SwitchableDevGetStatusAction(statusstate, switchDev);
                actions.put(getUPnPStatus.getName(), getUPnPStatus);
                actions.put(setUPnPStatus.getName(), setUPnPStatus);
        }

        public UPnPAction getAction(String name) {
                return (UPnPAction)actions.get(name);
        }

        public UPnPAction[] getActions() {
                return (UPnPAction[])(actions.values()).toArray(new
UPnPAction[]{});
        }

        public String getId() {
                return SERVICE_ID;
        }

        public UPnPStateVariable getStateVariable(String id) {
                if (id.equals("Status"))
                        return statusstate;
                else return null;
        }

        public UPnPStateVariable[] getStateVariables() {
                return states;
        }

        public String getType() {
                return SERVICE_TYPE;
        }

        public String getVersion() {
                return VERSION;
        }       
}


The switchdev state variable:

public class SwitchableDevStatusStateVariable implements
UPnPLocalStateVariable {

        final private String NAME = "Status";
        final private String DEFAULT_VALUE = "off";
        
        private SwitchableDevice switchDev;
        
        public SwitchableDevStatusStateVariable(SwitchableDevice switchDev){
                this.switchDev = switchDev;
        }       
        
        public String[] getAllowedValues() {
                return null;
        }

        public Object getDefaultValue() {
                return DEFAULT_VALUE;
        }

        public Class getJavaDataType() {
                return String.class;
        }

        public Number getMaximum() {
                return null;
        }

        public Number getMinimum() {
                return null;
        }

        public String getName() {
                return NAME;
        }

        public Number getStep() {
                return null;
        }

        public String getUPnPDataType() {
                return org.osgi.service.upnp.UPnPStateVariable.TYPE_STRING;
        }

        public boolean sendsEvents() {
                return true;
        }

        public Object getCurrentValue() {
                return switchDev.getStatus();
        }

}



SetStatusAction:

public class SwitchableDevSetStatusAction implements UPnPAction {

        final private String NAME = "SetStatus";
        final private String NEW_STATUS_VALUE = "NewStatusValue";
        final private String[] IN_ARG_NAMES = new
String[]{NEW_STATUS_VALUE};
        private UPnPStateVariable state;
        private SwitchableDevice switchDev;     
        
        public SwitchableDevSetStatusAction(UPnPStateVariable state,
SwitchableDevice switchDev) {
                super();
                this.state = state;
                this.switchDev = switchDev;
        }

        public String[] getInputArgumentNames() {
                return IN_ARG_NAMES;
        }

        public String getName() {
                return NAME;
        }

        public String[] getOutputArgumentNames() {
                return null;
        }

        public String getReturnArgumentName() {
                return null;
        }

        public UPnPStateVariable getStateVariable(String argumentName) {
                return state;
        }

        public Dictionary invoke(Dictionary args) throws Exception {
                String value = (String) args.get(NEW_STATUS_VALUE);
                this.switchDev.setStatus(value);
                return null;
        }

}



And finally getstatusaction:

public class SwitchableDevGetStatusAction implements UPnPAction {

        final private String NAME = "GetStatus";
        final private String RESULT_STATUS = "ResultStatus";
        final private String[] OUT_ARG_NAMES = new String[]{RESULT_STATUS};
        private UPnPStateVariable state;
        private SwitchableDevice switchDev;     
        
        public SwitchableDevGetStatusAction(UPnPStateVariable state,
SwitchableDevice switchDev) {
                super();
                this.state = state;
                this.switchDev = switchDev;
        }

        public String[] getInputArgumentNames() {
                return null;
        }

        public String getName() {
                return NAME;
        }

        public String[] getOutputArgumentNames() {
                return OUT_ARG_NAMES;
        }

        public String getReturnArgumentName() {
                return null;
        }

        public UPnPStateVariable getStateVariable(String argumentName) {
                return state;
        }

        public Dictionary invoke(Dictionary args) throws Exception {
                String status = switchDev.getStatus();
                Hashtable result = new Hashtable();
                result.put(RESULT_STATUS, status);
                return result;
        }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to