Java Magazine, September/October 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 37 internet of things virtualDevice setOnChange COOLING_ FAN_ SPEED_ ATTR new VirtualDevice ChangeCallback VirtualDevice public void onChange VirtualDevice ChangeEvent VirtualDevice e onFanSpeedChange e For cleaner code I use a Java 8 lambda expression as shown in Listing 6 The callback method not shown simply switches on the attribute name to react to the value change accordingly Listing 6 virtualDevice setOnChange VirtualDevice ChangeEvent VirtualDevice event onAttributeChange event To listen for IoTCS action requests such as the power action the device needs to implement a separate callback see Listing 7 The callback method needs to define the parameters to match those defined in the IoTCS device model Listing 7 virtualDevice setCallable power new VirtualDevice Callable Boolean public void call VirtualDevice virtualDevice Boolean on belt power on Of course devices can update their own attributes To enable them to do so use the IoTCS Client Software Library to ensure that changes are sent to the cloud eficiently You do this through the concept of a virtual device which is the clouds representation of the device itself The code in Listing 8 starts the process with a call to VirtualDevice update Next each attribute value is assigned with a call to VirtualDevice set Finally a call to VirtualDevice finish indicates the changes can be sent to IoTCS Under the covers the client library uses reliable REST based messaging to send changes to IoTCS Listing 8 virtualDevice update set BELT_ SPEED_ ATTR beltSpeed set TEMP_ ATTR temp set FAN_ SPEED_ ATTR fanSpeed set OIL_ PRESSURE_ ATTR oilPSI finish To receive device attribute updates the monitoring application registers just as the device does The code in Listing 9 shows this as well as how to call actions and change an attribute value on a device all from the monitoring application Listing 9 EnterpriseClient ec EnterpriseClient newClient APP_ NAME VirtualDevice virtualDevice ec createVirtualDevice 0 LENQ deviceModel virtualDevice setOnChange virtualDevice power true virtualDevice set BELT_ SPEED_ ATTR 1
You must have JavaScript enabled to view digital editions.