From a788753e8a8df8805d7357f873a7b0a566176c65 Mon Sep 17 00:00:00 2001 From: Stefan Persson Date: Thu, 19 May 2011 09:52:00 +0000 Subject: [PATCH] Java sensor poll and callback examples --- examples/java/sensors/SensorsJNACallback.java | 94 +++++++++++++++++++ examples/java/sensors/SensorsJNAPoll.java | 60 ++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 examples/java/sensors/SensorsJNACallback.java create mode 100644 examples/java/sensors/SensorsJNAPoll.java diff --git a/examples/java/sensors/SensorsJNACallback.java b/examples/java/sensors/SensorsJNACallback.java new file mode 100644 index 00000000..f0740292 --- /dev/null +++ b/examples/java/sensors/SensorsJNACallback.java @@ -0,0 +1,94 @@ +import java.io.*; +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.ptr.*; +import com.sun.jna.Callback; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; +import com.sun.jna.Memory; + +import java.util.Date; + +public class SensorsJNACallback +{ + public interface CLibrary extends Library { + + static int TELLSTICK_TEMPERATURE = 1; + static int TELLSTICK_HUMIDITY = 2; + + void tdInit(); + void tdClose(); + int tdRegisterSensorEvent(SensorCallback function, Pointer context); + int tdUnregisterCallback(int callbackID); + + public interface SensorCallback extends Callback{ + public void callbackfunction(Pointer protocol, Pointer model, int id, int dataType, Pointer value, int timestamp, int callbackId, Pointer context); + } + } + + int callbackID; + CLibrary lib; + CLibrary.SensorCallback callback; + + public SensorsJNACallback(){ + lib = (CLibrary)Native.loadLibrary("libtelldus-core.so.2", CLibrary.class); + } + + public void startListening(){ + + lib.tdInit(); + + //save reference to callback to avoid garbage collection + CLibrary.SensorCallback callback = new CLibrary.SensorCallback(){ + + public void callbackfunction(Pointer protocol, Pointer model, int id, int dataType, Pointer value, int timestamp, int callbackId, Pointer context){ + System.out.println("Sensor: " + protocol.getString(0) + " " + model.getString(0)); + long timestampvalue = (long)timestamp * 1000; + Date date = new Date(timestampvalue); + + if(dataType == CLibrary.TELLSTICK_TEMPERATURE){ + System.out.println("Temperature: " + value.getString(0) + "C, " + date.toString()); + } + else if(dataType == CLibrary.TELLSTICK_HUMIDITY){ + System.out.println("Humidity: " + value.getString(0) + "%, " + date.toString()); + } + System.out.println(""); + } + }; + + //register callback function for sensor events + callbackID = lib.tdRegisterSensorEvent(callback, (Pointer)null); + + while(true){ + try{ + //just wait for sensor callbacks + Thread.currentThread().sleep(1000); + } + catch(InterruptedException e){ + System.exit(0); + } + } + } + + public void stopListening(){ + System.out.println("Exiting"); + lib.tdUnregisterCallback(callbackID); + callback = null; + lib.tdClose(); + } + + public static void main(String[] args) + { + final SensorsJNACallback sensorsjnacallback = new SensorsJNACallback(); + + //add shutdown hook to allow callback unregistration + Runtime.getRuntime().addShutdownHook(new Thread(){ + public void run(){ + sensorsjnacallback.stopListening(); + Runtime.getRuntime().halt(0); + } + }); + + sensorsjnacallback.startListening(); + } +} \ No newline at end of file diff --git a/examples/java/sensors/SensorsJNAPoll.java b/examples/java/sensors/SensorsJNAPoll.java new file mode 100644 index 00000000..51512936 --- /dev/null +++ b/examples/java/sensors/SensorsJNAPoll.java @@ -0,0 +1,60 @@ +import java.io.*; +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.ptr.*; + +import java.util.Date; + +public class SensorsJNAPoll +{ + public interface CLibrary extends Library { + + static int TELLSTICK_TEMPERATURE = 1; + static int TELLSTICK_HUMIDITY = 2; + + void tdInit(); + void tdClose(); + int tdTurnOn(int deviceId); + int tdTurnOff(int deviceId); + int tdSensor(byte[] protocol, int protocolLength, byte[] model, int modelLength, IntByReference id,IntByReference dataTypes); + int tdSensorValue(byte[] protocol, byte[] model, int id, int dataType, byte[] value, int valueLength, IntByReference timestamp); + } + + public static void main(String[] args) + { + CLibrary lib = (CLibrary)Native.loadLibrary("libtelldus-core.so.2", CLibrary.class); + lib.tdInit(); + + IntByReference id = new IntByReference(); + IntByReference dataTypes = new IntByReference(); + + System.out.println("getting sensors"); + System.out.println(""); + + byte protocol[] = new byte[20]; + byte model[] = new byte[20]; + + //check every sensor + while(lib.tdSensor(protocol, 20, model, 20, id, dataTypes) == 0){ + System.out.println("Sensor: " + Native.toString(protocol) + " " + Native.toString(model)); + + byte value[] = new byte[20]; + IntByReference timestamp = new IntByReference(); + if((dataTypes.getValue() & CLibrary.TELLSTICK_TEMPERATURE) != 0){ + lib.tdSensorValue(protocol, model, id.getValue(), 1, value, 20, timestamp); + long timestampvalue = (long)timestamp.getValue() * 1000; + Date date = new Date(timestampvalue); + System.out.println("Temperature: " + Native.toString(value) + "C, " + date.toString()); + } + if((dataTypes.getValue() & CLibrary.TELLSTICK_HUMIDITY) != 0){ + lib.tdSensorValue(protocol, model, id.getValue(), 2, value, 20, timestamp); + long timestampvalue = (long)timestamp.getValue() * 1000; + Date date = new Date(timestampvalue); + System.out.println("Humidity: " + Native.toString(value) + "%, " + date.toString()); + } + System.out.println(""); + } + lib.tdClose(); + } + +} \ No newline at end of file