THE ONE OF IoT,YOUR ONLY CHOICE!
Operation - The key defined on Layout class
 

Operation


The Operation function is a import object of Infairy layout object, refer to Layout section. Developer define the operation object mapping with layout for user interaction.

package com.mycompany.App.myBundle;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Vector;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.infairy.cocina.SDK.Layout.Layout;
import com.infairy.cocina.SDK.Layout.LayoutImage;
import com.infairy.cocina.SDK.Layout.LayoutTag;
import com.infairy.cocina.SDK.device.BundleDevice;
import com.infairy.cocina.SDK.device.BundleDeviceImpl;
import com.infairy.cocina.SDK.device.DevicePool;
import com.infairy.cocina.SDK.gene.InfairyInterface;
import com.infairy.cocina.SDK.gene.OperationDNA;
import com.infairy.cocina.SDK.property.Property;
import com.infairy.smarthome.tools.Tools;


public class myFirstBundle implements BundleActivator, Runnable{
...
     
     Dictionary dict = new Hashtable();
     
     /*
      * Bundle ID
      */
     String BundleID="";
     
     /*
      * Layout ID
      */
     String LayoutID="";
     
     Thread me;

     /*
      * Layout object 
      */
     Layout layout;
     
    /*
     * entry point
     */
    public void start(BundleContext context) throws Exception {
        this.context=context;

   
   ...     
        
        /*
         * get layout service
         */
        layout=(Layout)Tools.getService(context, Layout.class.getName(), "(FUNCTION=LAYOUT)");
        
        /**
         * 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 operation for layout key mapping
         */
        Vector Operation=new Vector();
        OperationDNA oobj=new OperationDNA();
        //when user click the Button1.png or Button2.png, this operation object will trigger to run the Funtoin:Go()
        oobj.KEY=device.DEVICE_OPERATION_USER_DEFINED_0;   
        oobj.Function="Go";
        Operation.addElement(oobj);
        
        dict.put(InfairyInterface.DeviceOperation, Operation);

        ...
        
        BundleID=device.addDevice(this, dict);

        
        /*
         * Create bundle device
         */
        createBundleDevice();
            
            
        me=new Thread(this);
        me.start();
        
    }


    /*
     * exit bundle
     */
    public void stop(BundleContext context) throws Exception {
        device.stopInfairyBundle(context, this,  "", "");
    }

    



  public void run() {

    drawLayout();
  }
  
  private void drawLayout(){
    
    /*
     * define Layout Tag properties
     */
    LayoutTag ltag=new LayoutTag();
    ltag.backgroundImage="img/background.png";
    ltag.bundleID=BundleID;
    ltag.LayoutHeight=450;
    ltag.LayoutWidth=800;
    ltag.title="My App";
    /*
     * Create layout tag and get layout id for other layout object use
     */
    LayoutID=layout.createUI(this, ltag);
    
    /*
     * define Layout image properties
     */
    LayoutImage limg=new LayoutImage();
    limg.LayoutID=LayoutID;  //this image object will appear in this layout view scope
    limg.imageSrc="img/Button1.png";
    limg.left=100;
    limg.top=200;
    limg.width=120;
    limg.height=25;
    limg.key=device.DEVICE_OPERATION_USER_DEFINED_0;  //define key 
    limg.value="GET";  //define key value
    limg.index="0";  //define key index
    /*
     * Create layout image
     */
    layout.createUI(this, limg);

    limg.LayoutID=LayoutID;  //this image object will appear in this layout view scope
    limg.imageSrc="img/Button2.png";
    limg.left=300;
    limg.top=200;
    limg.width=120;
    limg.height=25;
    limg.key=device.DEVICE_OPERATION_USER_DEFINED_0;  //define key 
    limg.value="GET";  //define key value
    limg.index="1";  //define key index
    /*
     * Create layout image
     */
    layout.createUI(this, limg);

    /*
     * Update layout and refresh layout view scope
     */
    layout.update(BundleID);
    
  }
  

  /*
   * Function Go have two parameters which maping to Layout image's value and index
   */
  public void Go(String value, String index){
    ;//do the process
  }
}


This sudo code shows the relationship between operation object and layout object, developer design the layout for their own bundles to provide the GUI to let user, the relationship show as below:
Fig-1-5-1:Layout Key Index Value and Operation Key function
You can also define different parameter count for your purpose:
  public void drawLayout(){

    ......

    limg.LayoutID=LayoutID;  //this image object will appear in this layout view scope
    limg.imageSrc="img/Button1.png";
    limg.left=100;
    limg.top=200;
    limg.width=120;
    limg.height=25;
    limg.key=device.DEVICE_OPERATION_USER_DEFINED_0;  //define key 
    limg.value="DO_GET";  //define key value only

    ...

    limg.LayoutID=LayoutID;  //this image object will appear in this layout view scope
    limg.imageSrc="img/Button2.png";
    limg.left=300;
    limg.top=200;
    limg.width=120;
    limg.height=25;
    limg.key=device.DEVICE_OPERATION_USER_DEFINED_0;  //define key 
    limg.value="GET";  //define key value
    limg.index="1";  //define key index


  }

  /*
   * One parameter
   */
  public void Go(String value){
      ;
  }

  /*
   * Two parameter
   */
  public void Go(String value, String index){
      ;
  }