This chapter will lead you to register Infairy Cocina device object.
Fig-1-2-1:What is device
Fig-1-2-1:What is device
Infairy device object is a object which can interactive with others. Every bundle is a device.
package com.mycompany.App.myBundle;
import java.util.Dictionary;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.infairy.cocina.SDK.device.DevicePool;
import com.infairy.cocina.SDK.gene.InfairyInterface;
import com.infairy.cocina.SDK.property.Property;
import com.infairy.smarthome.tools.Tools;
public class myFirstBundle implements BundleActivator{
/*
* declare bundle context
*/
BundleContext context;
/*
* declare device sdk
*/
DevicePool device;
/*
* declare property sdk
*/
Property property;
Dictionary dict = new Hashtable();
String BundleID="";
/*
* entry point
*/
public void start(BundleContext context) throws Exception {
this.context=context;
/*
* get property service
*/
property=(Property)Tools.getService(context, Property.class.getName(), "(Property=Setting)");
/*
* get device service
*/
device=(DevicePool)Tools.getService(context, DevicePool.class.getName(), property.getDeviceService());
/**
* Register bundle
*/
Dictionary props = new Hashtable();
props.put(InfairyInterface.BundleAlias, "MyInfairyBundle");
boolean TF=device.registerBundle(context, this, props);
/*
* register device alias name which will show on all UI.
*/
dict.put(InfairyInterface.DeviceAlias,"mybundle");
/*
* Define the device's global kind for device's used.
*/
dict.put(InfairyInterface.DeviceGlobalKind, "mycompany");
BundleID=device.addDevice(this, dict);
}
/*
* exit bundle
*/
public void stop(BundleContext context) throws Exception {
device.stopInfairyBundle(context, this, "", "");
}
}
Now, the bundle was transform to a virtual device.We can also defined the device's activity, ex:
public void start(BundleContext context) throws Exception {
...
/*
* register device alias name which will show on all UI.
*/
dict.put(InfairyInterface.DeviceAlias,"mybundle");
/*
* Define the device's global kind for device's used.
*/
dict.put(InfairyInterface.DeviceGlobalKind, "mycompany");
/*
* Define the device command class
*/
dict.put(InfairyInterface.DeviceCommandClass, new String[]{device.SOFTWARE_CMDCLASS_SWITCH_BINARY});
BundleID=device.addDevice(this, dict);
}
Now, your device was defined as a Binary Switch, So we also have to defined the function be let you device alive,
public void start(BundleContext context) throws Exception {
...
Vector Operation=new Vector();
/*
* On operation mapping to DoOn function
*/
Operation.addElement(new Object[]{device.DEVICE_OPERATION_ON, "DoON"});
/*
* Off operation mapping to DoOff function
*/
Operation.addElement(new Object[]{device.DEVICE_OPERATION_OFF, "DoOff"});
/*
* Set the operation
*/
dict.put(InfairyInterface.DeviceOperation, Operation);
...
BundleID=device.addDevice(this, dict);
}
/*
* Defined the ON operation
*/
public void DoOn(){
...
}
/*
* Defined the OFF operation
*/
public void DoOFF(){
....
}