Commit 7d08a53f authored by aw613's avatar aw613

SmartSensor reads value from real temperature device

parent 16737d50
......@@ -36,8 +36,14 @@ public class GetTemperatureAction extends Operation
public static final String PARAM_HW_OUTPUT = "temperature";
//public static final String PARAM_HW_OUTPUT2 = "light";
public PhySensor sensor;
public GetTemperatureAction () {
super(ACT_HW_NAME, HelloWorldService.QN_PORTTYPE);
sensor = new PhySensor("28-0000045157c3"); //Sensor Thread is started in background
Element temperatureOutput = new Element(PARAM_HW_OUTPUT, HelloWorldDevice.NAMESPACE, SchemaUtil.TYPE_INT);
//Parameter lightOutput = new Parameter(PARAM_HW_OUTPUT2, HelloWorldDevice.NAMESPACE, ParameterType.PARAMETER_TYPE_INT);
......@@ -56,7 +62,7 @@ public class GetTemperatureAction extends Operation
ParameterValue temperatureOutput = createOutputValue();
ParameterValueManagement.setString(temperatureOutput, PARAM_HW_OUTPUT, "20");
ParameterValueManagement.setString(temperatureOutput, PARAM_HW_OUTPUT, Integer.toString(sensor.readSensor()));
return temperatureOutput;
......
package org.ws4d.smartsensor.device;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.concurrent.Semaphore;
public class PhySensor {
private String pathtoData;
int temperature;
Semaphore tempSem;
public PhySensor(String SensorID){ //28-0000045157c3
initSensor(SensorID);
}
private void initSensor(String SensorID){
StringBuilder sb = new StringBuilder();
sb.append("/sys/bus/w1/devices").append("/"+SensorID).append("/w1_slave");
this.pathtoData = sb.toString();
tempSem = new Semaphore(1);
Thread tempreadingThread = new Thread(new TempReadingThread(tempSem, pathtoData, this));
tempreadingThread.start();
}
public int readSensor(){
int returnTemp;
try {
tempSem.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnTemp = this.temperature;
tempSem.release();
return returnTemp;
}
public void writetemperature(int temp){
try {
tempSem.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.temperature = temp;
tempSem.release();
}
}
package org.ws4d.smartsensor.device;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.Semaphore;
public class TempReadingThread implements Runnable {
private String pathtoData;
private int temperature;
private Semaphore tempSem;
private PhySensor phySensor;
private static int REFRESHINTERVAL = 2000; //ms
public TempReadingThread(Semaphore tempSemext, String pathtoDataext, PhySensor phySensor) {
this.pathtoData= pathtoDataext;
this.tempSem = tempSemext;
this.phySensor = phySensor;
}
@Override
public void run() {
File f = new File(pathtoData);
while(true){
try {
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = reader.readLine();
if(line.substring(line.length()-3, line.length()).equals("YES")){
line = reader.readLine();
this.temperature = Integer.parseInt(line.substring(line.length()-5, line.length()));
System.out.println(temperature);
this.temperature /= 1000;
reader.close();
phySensor.writetemperature(temperature);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(REFRESHINTERVAL);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package org.ws4d.smartsensor.device;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.ws4d.java.CoreFramework;
......@@ -9,10 +13,11 @@ import org.ws4d.java.configuration.Properties;
public class mainclass {
public static void main(String[] args) {
CoreFramework.start(args);
HelloWorldDevice device = new HelloWorldDevice();
HelloWorldDevice device = new HelloWorldDevice();
// create service
HelloWorldService service = new HelloWorldService();
// set device properties
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment