THE ONE OF IoT,YOUR ONLY CHOICE!
Infairy Bundle Device
 

Bundle Device


This chapter will lead you to create Infairy Cocina bundle device object.

Infairy device object is a object which can interactive with others. Every bundle is a device.
In Infairy Cocina IoT platform, device is not only a hardware but also a virtual device which can be create by developer.
Fig-1-3-1:Device & Bundle Device
We call it - Bundle Device, which means the bundle device was created by bundle and belongs to the bundle.
Each devices can be devided many Bundle Device by it's specific function, see as below:


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 device
      */
     BundleDevice bundleDevice;
     
     /*
      * Bundle device ids
      */
     String BundleDeviceID="";

     .....     

    /*
     * entry point
     */
    public void start(BundleContext context) throws Exception {
        
        ...

        /*
         * get bundle device service
         */
        bundleDevice=(BundleDevice)Tools.getService(context, BundleDevice.class.getName(), "(DEVICE=BUNDLEDEVICE)");

        ...


        /*
         * Create bundle device
         */
        createBundleDevice();

    }


    /*
     * exit bundle
     */
    public void stop(BundleContext context) throws Exception {
        ...
    }


    private void createBundleDevice(){
        /*
         * remove old bundle device
         */
        String deviceid[]=bundleDevice.getBundleDeviceIDList(BundleID);
        
        if(deviceid!=null){
            for(int i=0; i < deviceid.length; i++){

                BundleDeviceImpl tmp=bundleDevice.getBundleDevice(deviceid[i]);
                
                if(tmp!=null && tmp.GlobalKind.equals("myCompanyKind")){
                    bundleDevice.removeBundleDevice(deviceid[i]);
                }
            }
        }

        /*
         * start create bundle device       
         */
        BundleDeviceImpl bdev=new BundleDeviceImpl();
            
        bdev.Object=this.getClass();
        /*
         * alias of bundle device
         */
        bdev.Alias="myBundleDevice";
        /*
         * kind of bundle device, 
         * refer to Device SDK#DEVICE_TYPE_xxx
         */
        bdev.KIND=device.DEVICE_TYPE_SENSOR_BINARY;

        /*
         * the specific keyword of individual company defined
         */
        bdev.GlobalKind="myCompanyKind";

        /*
         * device's command class, which defined the type of device,
         * refer to Device SDK#SOFTWARE_CMDCLASS_xxx 
         */
        bdev.CommandClass=new String[]{device.SOFTWARE_CMDCLASS_SWITCH_BINARY};
        /*
         * Set bundle device 
         */
        BundleDeviceID=bundleDevice.setBundleDevice(BundleID, bdev);
        
        /*
         * add device as bundle device
         */
        boolean tf=device.addDevice(this, BundleDeviceID);
        bdev=null;
    }


}

This exmaple shows that we defined a Bundle device which is a Binary Sensor device, this device will appear in the condition to be a trigger devices.
You can defined any kinds of bundle device, just define in the command class array.ex:
bdev.CommandClass=new String[]{
        device.SOFTWARE_CMDCLASS_SWITCH_MULTILEVEL, 
        device.SOFTWARE_CMDCLASS_SWITCH_BINARY
      };
Then, you success create the bundle device have two types: binary switch and multilevel switch. but if the device was defined as a binary switch, you have to define ON / OFF / Set method to complete the device action, ex.
    private void createBundleDevice(){
        ...

        /*
         * start create bundle device       
         */
        BundleDeviceImpl bdev=new BundleDeviceImpl();

        ...

        /*
         *  Create On Operation 
         */
        bdev.ON=new Object[]{
          "On",    //On Action
          new String[]{HomeApplianceID, buttonKey, String.valueOf(learningCode)} //Defined the function parameter
        };

        /*
         *  Create Off Operation 
         */
        bdev.OFF=new Object[]{
          "Off",    //Off Action
          new String[]{HomeApplianceID, buttonKey, String.valueOf(learningCode)} //Defined the function parameter
        };

        ...
    }

    /*
     * Defined On operation, must be public function
     */ 
    public void On(String HomeID, String[] buttonKey, String code){

      .....

    }
    /*
     * Defined Off operation, must be public function
     */ 
    public void Off(String HomeID, String[] buttonKey, String code){

      .....

    }

Ok, maybe you will ask: Can the device can be defined as a sensor device?
bdev.CommandClass=new String[]{
        device.SOFTWARE_CMDCLASS_SENSOR_BINARY
      };
The answer is YES, but this is another story.