Sensor Listner is one of Infairy broadcast channel which can let all developer receive the evetn from all other bundle in Infairy.
Developer can listen Sensor broadcast channel to get all event by implement "EventSensorDNA" class, please follow the step as below:
First, import package as follows:
Developer can listen Sensor broadcast channel to get all event by implement "EventSensorDNA" class, please follow the step as below:
import org.osgi.service.event.Event; import org.osgi.service.event.EventHandler; import com.infairy.cocina.SDK.gene.EventSensorDNA;then, implement "EventHandler" in your bundle class:
package yourBundle; public class CommData implements BundleActivator, EventHandler{ .... }Third, implement HandleEvent() method,
public void handleEvent(Event evnt) { ... }Last, do not forget listen Sensor broadcast channel in start() method,
package yourBundle; public class CommData implements BundleActivator, EventHandler{ .... public void start(BundleContext context) throws Exception { ... device.ListenBroadcast(context, this, device.BROADCAST_CHANNEL_SENSOR); } }after done the process as above, you can get the event from all other sensor:
public void handleEvent(Event evnt) { if(evnt==null) return; //Cast the event by EventSensorDNA from the property of BROADCAST_SENSOR_TRIGGER_EVENTOBJECT EventSensorDNA ESO=(EventSensorDNA)evnt.getProperty(device.BROADCAST_SENSOR_TRIGGER_EVENTOBJECT); if(ESO!=null){ if(ESO.TYPE.equals("")) return; String TYPE=ESO.TYPE; if(TYPE.equals("")) return; String triggerZID=ESO.ZID; //Get the trigger device id if(triggerZID.equals("")) return; //Analysis by trigger type, The trigger type defined by com.infairy.cocina.SDK.device.DevicePool#TROGGET_TYPE_xxx if(TYPE.equals(device.TRIGGER_TYPE_TRIGGER)){ ... } } }Now, you learned how to get the sensor event.
Next, we will show you how to send the sensor event:
//Create EventSensorDNA object EventSensorDNA ev=new EventSensorDNA(); //Assign the device id ev.ZID=myBundleID; //Set the trigger channel ev.CHANNEL=0; //Assigned trigger type ev.TYPE=device.TRIGGER_TYPE_TRIGGER; //Alias ev.ALIAS="MyWeather"; //Value defined ev.NOW_VALUE="28"; ev.PREVIOUS_VALUE="27"; ev.NOW_UNIT=device.DEVICE_VALUE_UNIT_DEGREE_C; ev.PREVIOUS_UNIT=device.DEVICE_VALUE_UNIT_DEGREE_C; //Broadcast the event device.SensorBroadcast(ev); ev=null;The EventSensorDNA interface shows as below:
/** * Trigger object id */ public String ZID=""; /** * Trigger Kind, default is SOFTWARE {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_AC}{@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_AV}{@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_METER} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_SOFTWARE} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_SENSOR_BINARY} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_SENSOR_MULTILEVEL} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_SWITCH_BINARY} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_SWITCH_MULTILEVEL} {@link com.infairy.cocina.SDK.device.DevicePool#DEVICE_TYPE_UNKNOWNN} */ public String KIND="SOFTWARE"; /** * Trigger object alias */ public String ALIAS=""; /** * Trigger data type */ public String TYPE=""; /** * the trigger object's previous value, which can be analysis value changed by NOW_VALUE */ public String PREVIOUS_VALUE=""; /** * the trigger object's value, */ public String NOW_VALUE=""; /** * the trigger object's value unit of previous value */ public String PREVIOUS_UNIT=""; /** * the trigger object's value */ public String NOW_UNIT=""; /** * * the trigger event from channel */ public int CHANNEL=1; /** * trigger object by user defined */ public Object OBJECT=null; /** * Trigger ITEM, paired with VALUE * */ public String ITEM=""; /** * * Trigger value, paired with ITEM */ public String VALUE=""; /** * Trigger function defined */ public String FUNCTION="";