Java sensor poll and callback examples
This commit is contained in:
parent
97e88a175f
commit
a788753e8a
2 changed files with 154 additions and 0 deletions
94
examples/java/sensors/SensorsJNACallback.java
Normal file
94
examples/java/sensors/SensorsJNACallback.java
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
60
examples/java/sensors/SensorsJNAPoll.java
Normal file
60
examples/java/sensors/SensorsJNAPoll.java
Normal file
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue