IFTTT is a usefull function for the users. IFTTT can be assigned by user in Infairy Cocina.
IFTTT contains two devices: sensor and device. When the sensor was trigger, then the device were action relatively. Both of these devices can be a hardware device or virtual device. Yes, you did not make mistake, the VIRTUAL device and sensor are existed in Infairy Cocina, let us take look what is VIRTUAL sensor and device.
The virtual device was called bunde device in Infairy, you can create bundle device by yourselves, see Device and Bundle Device, you can learn to create a operatable virtual device, but in this section, we will let you know how to create a virtual sensor and trigger by conditions.
Now, we will show you how the IFTTT with virtual sensor works, see as below:
IFTTT contains two devices: sensor and device. When the sensor was trigger, then the device were action relatively. Both of these devices can be a hardware device or virtual device. Yes, you did not make mistake, the VIRTUAL device and sensor are existed in Infairy Cocina, let us take look what is VIRTUAL sensor and device.
The virtual device was called bunde device in Infairy, you can create bundle device by yourselves, see Device and Bundle Device, you can learn to create a operatable virtual device, but in this section, we will let you know how to create a virtual sensor and trigger by conditions.
Now, we will show you how the IFTTT with virtual sensor works, 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 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"); /* * set the device to be a sensor binary */ dict.put(InfairyInterface.DeviceCommandClass, new String[]{device.SOFTWARE_CMDCLASS_SENSOR_BINARY}); /* * defined Virtual sensor */ Vector condition=new Vector(); IFTTTDNA ifobj=new IFTTTDNA(); ifobj.ID="0"; ifobj.TriggerAlias="Weather"; ifobj.ConditionAliases=new String[]{"Equals"}; ifobj.TriggerCondition=new String[]{device.TRIGGER_CONDITION_EQUALS}; ifobj.ConditionValues=item; condition.addElement(ifobj); IFTTTDNA ifobj1=new IFTTTDNA(); ifobj1.ID="1"; ifobj1.TriggerAlias="Temperature"; ifobj1.ConditionAliases=new String[]{"Lower","higher"}; ifobj1.TriggerCondition=new String[]{device.TRIGGER_CONDITION_LOWER, device.TRIGGER_CONDITION_HIGHER}; ifobj1.ConditionValues=new String[]{""}; condition.addElement(ifobj1); /* * set the IFTTT condition object */ dict.put(InfairyInterface.DeviceCondition, condition); BundleID=device.addDevice(this, dict); } /* * exit bundle */ public void stop(BundleContext context) throws Exception { ... } }The above code let your device have a Trigger ability, if your trigger condition was mathed as below:
We also can let the bundle device to be a virtual sensor by set the code as below:
bdev.CommandClass=new String[]{ device.SOFTWARE_CMDCLASS_SENSOR_BINARY };SOFTWARE_CMDCLASS_SENSOR_BINARY was must in Bundle Device definition. so, we set the device's command class to the sensor type was needed. we also define three virtual sensors: SunShine / Storm / Raining:
String[] BundleDeviceID=new String[3]; private void createBundleDevice(){ ... /* * Define three virtual sensor devices */ String[] Alias=new String[]{"SunShine", "Storm", "Raining"}; for(int i=0; i < Alias.length ; i++){ BundleDeviceImpl bdev=new BundleDeviceImpl(); bdev.Object=this.getClass(); bdev.Alias="Sunshin"; bdev.KIND=device.DEVICE_TYPE_SENSOR_BINARY; bdev.GlobalKind="myCompanyKind"; /* * BINARY SENSOR */ bdev.CommandClass=new String[]{device.SOFTWARE_CMDCLASS_SENSOR_BINARY}; /* * Create bundle device */ BundleDeviceID[i]=bundleDevice.setBundleDevice(BundleID, bdev); } /* * add device as bundle device */ boolean tf=device.addDevice(this, BundleDeviceID); bdev=null; }The SOFTWARE_CMDCLASS_SENSOR_BINARY was matched with IFTTT object. Then we can get the weather report string from accweather.com, to parsing the string into three situations: sunshine / storm and raining, then do the following code to trigger your own situation:
/* * get Acc Weather report */ private void getAccWetherReport(){ // do the get weather process and report key String key=AccWeather(....); //based on the report key to trigger the condition triggerBy(key); } /* * Trigger action */ public function triggerBy(String key){ String triggerID=""; /* * Based on key to get the Bundle device id */ if(key.equals("SunShine")) triggerID=BundleDeviceID[0]; else if(key.equals("Storm")) triggerID=BundleDeviceID[1]; else if(key.equals("Raining")) triggerID=BundleDeviceID[2]; /* * Broadcast the change event to others */ EventSensorDNA ev=new EventSensorDNA(); ev.ZID=triggerID; //Assign trigger object id ev.CHANNEL=0; //Set trigger channel, default is 0 ev.TYPE=device.TRIGGER_TYPE_TRIGGER; //Set the trigger type to Trigger ev.ALIAS=Key; //set the trigger alias ev.NOW_VALUE=key; device.SensorBroadcast(ev); ev=null; }Now, when the weather report is one of SunShine, Storm and Rainging, This bundle will trigger the condition. since your trigger event was not match with Infairy defined, so, you have to trigger by yourself.
The EventSensorDNA was describe in the Evnet section : Sensor