Module "upnp.devicefactory"
Device factory for generating standardized services and devices (xml's + implementation code). This module has methods for creating services and devices from standard elements (provided the required modules to support that device/service) are available.
Functions
| devicefactory.builddevice (domain, devicetype, version, customtable) | Creates a (standard) device, customizes it, generates xml's, parses them and returns the UPnP device object. |
| devicefactory.createdevice (domain, devicetype, version) | Creates the requested device table (if available). |
| devicefactory.createservice (domain, servicetype, version) | Creates the requested service table (if available). |
| devicefactory.customizedevice (device, customtable) | Customizes a device by dropping optional elements (statevariables and/or actions) and adding the implementation functions/methods. |
| devicefactory.customizeservice (service, customtable) | Customizes a service by dropping optional elements (statevariables and/or actions) and adding the implementation functions/methods. |
| devicefactory.emptydevice () | Creates an empty device table. |
| devicefactory.emptyservice () | Creates an empty service table. |
Functions
- devicefactory.builddevice (domain, devicetype, version, customtable)
-
Creates a (standard) device, customizes it, generates xml's, parses them and returns the UPnP device object. This method takes a number of steps to create a fully functional device;
- Creates a device table for a standard device (
devicefactory.createdevice()) or takes a device table - Drops optionals as set in the
customtableparameter (devicefactory.customizedevice()) and adds the implementations of device/variable/action methods from thecustomtableto the device table - Creates the XML's for the device and its services (
xmlfactory.rootxml()) - Writes the XML's to the webroot directory, so they are accessible (
xmlfactory.writetoweb()) - Parses the XML's into a root-device object structure, whilst adding the custom implementations as set in the devicetable (
upnp.classes.device:parsefromxml()) - sets the
devicexmlurlon the device and returns the device object -
domain: domainname of the type to create, alternatively, the fulldeviceTypecontents or a device table. In the latter 2 cases thedevicetypeandversionarguments can be omitted. -
devicetype: [optional] name of the type to create, or nil if the domain contains the full type identifier -
version: [optional] version number of the type to create, or nil if the domain contains the full type identifier -
customtable: [optional] table with customizations (seedevicefactory.customizedevice())
Parameters:
Return value:
device aupnp.classes.deviceobject representing the device, ornil + errormsgExample:
-- three ways to create the same device, all without customization/implementation
devicefactory.builddevice("schemas.upnp.org", "BinaryLight", "1", {} )
-- or full schema and no customtable
devicefactory.builddevice("urn:schemas-upnp-org:device:BinaryLight:1")
-- or a device table and empty customtable
local d = require("upnp.devices.urn_schemas-upnp-org_device_Basic_1")()
devicefactory.builddevice(d, {}) - Creates a device table for a standard device (
- devicefactory.createdevice (domain, devicetype, version)
-
Creates the requested device table (if available). The output can be used as input for the
xmlfactory. For the parameters check the device propertydeviceTypein the device xml of the UPnP architecture documents.Parameters:
-
domain: domainname of the type to create, alternatively, the fulldeviceTypecontents -
devicetype: name of the type to create, or nil if the domain contains the full type identifier -
version: version number of the type to create, or nil if the domain contains the full type identifier
Return value:
device table, or nil + errormsgExample:
-- two ways to create the same device
devicefactory.createdevice("schemas.upnp.org", "BinaryLight", "1")
-- or
devicefactory.createdevice("urn:schemas-upnp-org:device:BinaryLight:1") -
- devicefactory.createservice (domain, servicetype, version)
-
Creates the requested service table (if available). The output can be used as input for the
xmlfactory. Seecreatedevice()for more details.Parameters:
-
domain: domainname of the type to create, alternatively, the fullserviceTypecontents -
servicetype: name of the type to create, or nil if the domain contains the full type identifier -
version: version number of the type to create, or nil if the domain contains the full type identifier
Return value:
service table, or nil + errormsgSee also:
-
- devicefactory.customizedevice (device, customtable)
-
Customizes a device by dropping optional elements (statevariables and/or actions) and adding the implementation functions/methods. Includes any underlying services and sub-devices. On device level you can set device properties like
friendlyName, etc. A service can be dropped by including an element with itsserviceId, set tofalse. A device can be dropped by including an element with itsdeviceType, set tofalse. Thestart()andstop()methods on device level can also be provided. For implementing code for statevariables and actions, seedevicefactory.customizeservice
NOTE: the subdevices are indexed bydeviceTypein the customtable hence if a device contains 2 sub-devices of the same type, things might go berserk!Parameters:
-
device: the device table where elements need to be dropped from (typically this is the table returned fromdevicefactory.createdevice()). -
customtable: a table containing the elements to drop byserviceId(for services) ordeviceType(for devices), with valuefalse.
Return value:
device table, but it will have been modified, might also throw an errorExample:
-- example customtable for a 'urn:schemas-upnp-org:device:DimmableLight:1' device
local customtable = {
-- customize device level first
friendlyName = "This is my new UPnP device",
start = function(self) -- implement startup behaviour
self:superclass().start(self)
print("myDevice is now starting...")
end,
stop = function(self) -- implement device shutdown behaviour
print("myDevice is now stopped")
self:superclass().stop(self)
end,
-- customize services next
serviceList = {
["urn:upnp-org:serviceId:Dimming:1"] = {
serviceStateTable = {
StepDelta = false,
},
actionList = {
StepUp = false,
StepDown = {
execute = function(self, params)
print ("method being executed now!")
end,
},
}, -- actionList
}, -- dimming service
}, -- serviceList
}, -- customtable
--
-- go create a dimmable light and then customize it
local myDevTable = devicefactory.customizedevice(devicefactory.createdevice("schemas.upnp.org", "DimmableLight", "1") , customtable) -
- devicefactory.customizeservice (service, customtable)
-
Customizes a service by dropping optional elements (statevariables and/or actions) and adding the implementation functions/methods.
Parameters:
-
service: the service table to be customized (typically this is the table returned fromdevicefactory.createservice()). -
customtable: a table containing the elements to customize by name, with valuefalseto drop, or a table containing theexecute, beforeset, aftersetfunctions.
Return value:
service table, but it will have been modified, might also throw an errorExample:
-- example customtable for a 'urn:schemas-upnp-org:service:Dimming:1' service
local customtable = {
serviceStateTable = {
StepDelta = false, -- remove this variable
},
actionList = {
StepUp = false, -- remove this action
StepDown = { -- implement the action behaviour
execute = function(self, params)
print ("method being executed now!")
end,
},
},
} -
- devicefactory.emptydevice ()
-
Creates an empty device table.
Return value:
new table with two empty subtables;serviceListanddeviceList. - devicefactory.emptyservice ()
-
Creates an empty service table.
Return value:
new table with two empty subtables;actionListandserviceStateTable.