After you retrieve the DeviceAdmin service you can create and register a Device with a set of functionalities – Device Items. The Device Items are registered after the Device itself. The creation process finishes when the Device and its Device Items are created and registered in the system. You can learn more about the Device and Device Items characteristics on the Device and Device Item Developer Guides respectively.
The following examples show how you can create devices. These examples use a Device Access Protocol Adapter Demo. For this reason you should install and start it.
By Invoking the DeviceAdmin.OPERATION_CREATE Operation
Map can be following properties:Device.TAGS with value of type Set of String.Device.ADAPTER_NAME with value of type String.Collection of DeviceItemDescriptor. You can learn more about the DeviceItemDescriptor on "Creating a DeviceItemDescriptor instance" section below.Map. The keys of these attributes are String. The allowed attributes values are only String - this is the JSON String representation of the attributes values.DeviceAdmin with the following parameters:com.prosyst.mbs.services.da.Device.Map that contains the functional item creation properties.The following simple example shows how to create a Device.
private Device create() {
HashMap<String, Object> creationProperties = new HashMap<String, Object>(8, 1.0f);
creationProperties.put(Device.UID, "test:device:UID");
creationProperties.put(Device.NAME, "test:device:name");
creationProperties.put(Device.TAGS, Collections.singleton("test:device:tag"));
creationProperties.put(Device.ADAPTER_NAME, "DA Demo");
Set<DeviceItemDescriptor> deviceItems = Collections.singleton(
new DeviceItemDescriptor(StepperMotor.class.getName(), "test:device:item:UID", "test:device:item:name", Collections.singleton("test:device:item:tag"), null));
creationProperties.put(DeviceAdmin.ITEMS, deviceItems);
creationProperties.put(DeviceAdmin.ATTRIBUTES, Collections.singletonMap("test:device:attribute:key", "test:device:attribute:value"));
return deviceAdmin.create(Device.class.getName(), creationProperties);
}
By Invoking the DeviceAdmin.OPERATION_CREATE_DEVICE Operation
DeviceDescriptor instance. You can view the details on "Creating a DeviceDescriptor instance" section below. The DeviceDescriptor is a FunctionalItem bean. This bean holds the device properties which are used for creation of Device. These properties are:uid – the device UID to be set.name – the device name to be set.tags – the tags values to be set.adapterName – the adapter name to be used for the creation.items – the list with device items to be created with. You can learn more about the DeviceItemDescriptor on "Creating DeviceItemDescriptor instance" section below.attributes – the device attributes, that are adapter supported properties or additional device registration attributes.createDevice operation with parameter the constructed DeviceDescriptor.DeviceDescriptor InstanceThere are two ways of creating a DeviceDescriptor instance. The following examples describe both ways and what is the different between them. As a result a Device is created via DeviceAdmin.
private Device createDevice() {
DeviceDescriptor deviceDescriptor = createDeviceDescriptorByConsructor(); // or createDeviceDescriptorByCreateMethod()
return deviceAdmin.createDevice(deviceDescriptor);
}
DeviceDescriptorprivate DeviceDescriptor createDeviceDescriptorByConsructor() {
String deviceUID = "test:device:descriptor:UID";
String deviceName = "test:device:descriptor:name";
String adapterName = "DA Demo";
Set<String> deviceTags = Collections.singleton("test:device:descriptor:tag");
String deviceItemObjectClass = MotionDetector.class.getName();
String deviceItemUID = "test:device:item:descriptor:UID";
String deviceItemName = "test:device:item:descriptor:name";
Set<String> deviceItemTags = Collections.singleton("test:device:item:descriptor:tag");
Collection<DeviceItemDescriptor> items = Collections.singleton(
new DeviceItemDescriptor(deviceItemObjectClass, deviceItemUID, deviceItemName, deviceItemTags, null));
Map<String, String> deviceAttributes = Collections.singletonMap("test:device:descriptor:attribtue:key", "1");
return new DeviceDescriptor(deviceUID, deviceName, deviceTags, adapterName, items, deviceAttributes);
}
This example shows usage the constructor of the DeviceDescriptor - DeviceDescriptor(String uid, String name, Set<String> tags, String adapterName, Collection<DeviceItemDescriptor> items, Map<String, String> attributes). As a last parameter of the constructor is device attributes. The keys of these attributes are String. The allowed attributes values are only String - this is the JSON String representation of the attributes values.
DeviceDescriptor instanceprivate DeviceDescriptor createDeviceDescriptorByCreateMethod() {
String deviceUID = "test:device:descriptor:UID";
String deviceName = "test:device:descriptor:name";
String adapterName = "DA Demo";
Set<String> deviceTags = Collections.singleton("test:device:descriptor:tag");
String deviceItemObjectClass = MotionDetector.class.getName();
String deviceItemUID = "test:device:item:descriptor:UID";
String deviceItemName = "test:device:item:descriptor:name";
Set<String> deviceItemTags = Collections.singleton("test:device:item:descriptor:tag");
Collection<DeviceItemDescriptor> items = Collections.singleton(
new DeviceItemDescriptor(deviceItemObjectClass, deviceItemUID, deviceItemName, deviceItemTags, null));
Map<String, Integer> deviceAttributes = Collections.singletonMap("test:device:descriptor:attribtue:key", 1);
return DeviceDescriptor.create(deviceUID, deviceName, deviceTags, adapterName, items, deviceAttributes);
}
This example shows usage the so-called local static method DeviceDescriptor.create(String uid, String name, Set<String> tags, String adapterName, Collection<DeviceItemDescriptor> items, Map<String, ?> attributes). As a last parameter of this method is device attributes. The keys of these attributes are String. The allowed attributes values are FIM supported primary types:
StringDeviceItemDescriptor InstanceThe Device can be created with some functionalities – DeviceItems. The DeviceItemDescriptor is a FunctionalItem bean. This bean holds the device item properties which are used for creation of Device Item. There are two ways to creating a DeviceItemDescriptor instance. The following sections describes the both ways and what is different between them.
The DeviceItemDescriptor properties are:
objectClass – device item fully qualified class name.uid – the device item UID.name – the device item name.tags – the device item tags.attributes – the device item attributes and adapter properties.DeviceItemDescriptorprivate static DeviceItemDescriptor createDeviceItemDescriptoByConstructor() {
String deviceItemObjectClass = MotionDetector.class.getName();
String deviceItemUID = "test:device:item:descriptor:UID";
String deviceItemName = "test:device:item:descriptor:name";
Set<String> deviceItemTags = Collections.singleton("test:device:item:descriptor:tag");
Map<String, String> deviceItemAttributes = Collections.singletonMap("test:device:item:descriptor:attribute:key", "{\"test:value:key\":\"test:value:value\"}");
return new DeviceItemDescriptor(deviceItemObjectClass, deviceItemUID, deviceItemName, deviceItemTags, deviceItemAttributes);
}
This example shows usage the constructor of the DeviceItemDescriptor - DeviceItemDescriptor(String objectClass, String uid, String name, Set<String> tags, Map<String, String> attributes). As a last parameter of the constructor is device item attributes. The keys of these attributes are String. The allowed attributes values are only String – this is the JSON String representation of the attributes values.
DeviceItemDescriptor instanceprivate DeviceItemDescriptor createDeviceItemDescriptoByCreateMethod() {
String deviceItemObjectClass = MotionDetector.class.getName();
String deviceItemUID = "test:device:item:descriptor:UID";
String deviceItemName = "test:device:item:descriptor:name";
Set<String> deviceItemTags = Collections.singleton("test:device:item:descriptor:tag");
Map<String, String> attributeValues = Collections.singletonMap("test:value:key", "test:value:value");
Map<String, Map<String, String>> deviceItemAttributes = Collections.singletonMap("test:device:item:descriptor:attribute:key", attributeValues);
return DeviceItemDescriptor.create(deviceItemObjectClass, deviceItemUID, deviceItemName, deviceItemTags, deviceItemAttributes);
}
This example shows the usage of the so- called local static method DeviceItemDescriptor.create(String objectClass, String uid, String name, Set<String> tags, Map<String, ?> attributes). As a last parameter of this method it takes device item attributes. The keys of these attributes are String. The allowed attributes values are FIM supported primary types: