Commit 04e6b34a authored by aw613's avatar aw613

emulated light sensor

parent 24f267a9
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SmartSensor</name>
<name>LuminositySensor</name>
<comment></comment>
<projects>
</projects>
......
<?xml version="1.0" encoding="UTF-8"?>
<tomcatProjectProperties>
<rootDir>/</rootDir>
<exportSource>false</exportSource>
<reloadable>true</reloadable>
<redirectLogger>false</redirectLogger>
<updateXml>true</updateXml>
<warLocation></warLocation>
<extraInfo></extraInfo>
<webPath></webPath>
</tomcatProjectProperties>
package org.ws4d.coap.smartsensor.device;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ws4d.coap.core.connection.BasicCoapChannelManager;
import org.ws4d.coap.core.connection.api.CoapChannelManager;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapResourceServer;
public class CoAPSensor {
private static CoAPSensor sampleServer;
private CoapResourceServer resourceServer;
private static String environment = System.getenv("SUDO_USER");
public static void main(String[] args) {
System.out.println("Start Sample Resource Server");
// if(environment!=null && environment.equals("pi")){
// }
sampleServer = new CoAPSensor();
sampleServer.run();
}
private void run() {
if (resourceServer != null)
resourceServer.stop();
CoapChannelManager channelManager = BasicCoapChannelManager.getInstance();
resourceServer = new CoapResourceServer();
/* Show detailed logging of Resource Server*/
Logger resourceLogger = Logger.getLogger(CoapResourceServer.class.getName());
resourceLogger.setLevel(Level.ALL);
/* add resources */
TemperaturRessource temperature = new TemperaturRessource("/temperature", "Content", CoapMediaType.text_plain);
LuminosityRessource luminosity = new LuminosityRessource("/luminosity", "Content", CoapMediaType.text_plain);
temperature.setResourceType("temperature");
temperature.setPostable(false);
temperature.setObservable(false);
temperature.setDeletable(false);
temperature.setPutable(false);
temperature.setReadable(true);
luminosity.setResourceType("luminosity");
luminosity.setPostable(false);
luminosity.setObservable(false);
luminosity.setDeletable(false);
luminosity.setPutable(false);
luminosity.setReadable(true);
resourceServer.createResource(temperature);
resourceServer.createResource(luminosity);
temperature.registerServerListener(resourceServer);
luminosity.registerServerListener(resourceServer);
try {
resourceServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package org.ws4d.coap.smartsensor.device;
import java.io.IOException;
import java.util.List;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapData;
import org.ws4d.smartsensor.device.Tsl2561;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
public class LuminosityRessource extends BasicCoapResource {
public LuminosityRessource (String path, String value, CoapMediaType mediaType) {
super(path, value, mediaType);
// TODO Auto-generated constructor stub
}
@Override
public synchronized CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted) {
I2CBus bus;
try {
bus = I2CFactory.getInstance(I2CBus.BUS_1);
Tsl2561 sensor = new Tsl2561(bus.getDevice(0x39));
System.out.println("Luminosity read: " + sensor.getLux() + "Lux");
return new CoapData(Integer.toString((int)sensor.getLux()).getBytes(), CoapMediaType.text_plain);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
package org.ws4d.coap.smartsensor.device;
import java.util.List;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapData;
import org.ws4d.smartsensor.device.PhySensor;
public class TemperaturRessource extends BasicCoapResource {
private PhySensor sensor;
public TemperaturRessource (String path, String value, CoapMediaType mediaType) {
super(path, value, mediaType);
sensor = new PhySensor("28-0000045157c3");
// TODO Auto-generated constructor stub
}
@Override
public synchronized CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted) {
System.out.println("Temperature read: " + sensor.readSensor() + "C");
return new CoapData(Integer.toString((int)sensor.readSensor()).getBytes(), CoapMediaType.text_plain);
}
}
......@@ -11,8 +11,7 @@
package org.ws4d.smartsensor.device;
import java.io.IOException;
import java.util.Random;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.schema.Element;
......@@ -23,9 +22,6 @@ import org.ws4d.java.service.Operation;
import org.ws4d.java.service.parameter.ParameterValue;
import org.ws4d.java.service.parameter.ParameterValueManagement;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
/**
* The Temperature Action returns the current Temperature.
*
......@@ -34,44 +30,28 @@ import com.pi4j.io.i2c.I2CFactory;
public class GetLightOperation extends Operation
{
public static final String ACT_HW_NAME = "LightOperation";
//public static final String PARAM_HW_OUTPUT = "temperature";
public static final String PARAM_HW_OUTPUT = "light";
public static Tsl2561 sensor;
public GetLightOperation () {
super(ACT_HW_NAME, SmartSensorService.QN_PORTTYPE_BRIGHT);
I2CBus bus;
try {
bus = I2CFactory.getInstance(I2CBus.BUS_1);
sensor = new Tsl2561(bus.getDevice(0x39));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element lightOutput = new Element(PARAM_HW_OUTPUT, SensorDevice.NAMESPACE, SchemaUtil.TYPE_INT);
setOutput(lightOutput);
}
@Override
protected ParameterValue invokeImpl(ParameterValue parameterValue,
CredentialInfo credentialInfo) throws InvocationException,
CommunicationException {
protected ParameterValue invokeImpl(ParameterValue parameterValue, CredentialInfo credentialInfo) throws InvocationException, CommunicationException {
Random rand = new Random();
int MIN = 0;
int MAX = 200;
ParameterValue luminescenceOutput = createOutputValue();
try {
ParameterValueManagement.setString(luminescenceOutput, PARAM_HW_OUTPUT, Integer.toString((int)sensor.getLux()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ParameterValueManagement.setString(luminescenceOutput, PARAM_HW_OUTPUT, Integer.toString(rand.nextInt((MAX - MIN) + 1) + MIN));
return luminescenceOutput;
}
......
package org.ws4d.smartsensor.device;
/**********************************************************************************
* Copyright (c) 2007 MATERNA Information & Communications and
* TU Dortmund, Dpt. of Computer Science, Chair 4, Distributed Systems
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
**********************************************************************************/
import java.io.IOException;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.schema.Element;
import org.ws4d.java.schema.SchemaUtil;
import org.ws4d.java.security.CredentialInfo;
import org.ws4d.java.service.InvocationException;
import org.ws4d.java.service.Operation;
import org.ws4d.java.service.parameter.ParameterValue;
import org.ws4d.java.service.parameter.ParameterValueManagement;
/**
* The Temperature Action returns the current Temperature.
*
*
*/
public class GetTemperatureOperation extends Operation
{
// private ITemperatureInput tempSensor = (ITemperatureInput) Resources.lookup(ITemperatureInput.class);
//private ILightSensor lightSensor = (ILightSensor) Resources.lookup(ILightSensor.class);
public static final String ACT_HW_NAME = "TemperatureOperation";
public static final String PARAM_HW_OUTPUT = "temperature";
//public static final String PARAM_HW_OUTPUT2 = "light";
public PhySensor sensor;
public GetTemperatureOperation () {
super(ACT_HW_NAME, SmartSensorService.QN_PORTTYPE_TEMP);
sensor = new PhySensor("28-0000045157c3"); //Sensor Thread is started in background
Element temperatureOutput = new Element(PARAM_HW_OUTPUT, SensorDevice.NAMESPACE, SchemaUtil.TYPE_INT);
//Parameter lightOutput = new Parameter(PARAM_HW_OUTPUT2, HelloWorldDevice.NAMESPACE, ParameterType.PARAMETER_TYPE_INT);
setOutput(temperatureOutput);
// addOutputParameterDefinition(temperatureOutput);
//addOutputParameterDefinition(lightOutput);
}
@Override
protected ParameterValue invokeImpl(ParameterValue parameterValue,
CredentialInfo credentialInfo) throws InvocationException,
CommunicationException {
ParameterValue temperatureOutput = createOutputValue();
ParameterValueManagement.setString(temperatureOutput, PARAM_HW_OUTPUT, Integer.toString(sensor.readSensor()));
return temperatureOutput;
}
// protected ParameterValue invokeImpl() {
// try {
// ParameterValue temperatureOutput = createOutputValue(); //getOutputParameter(PARAM_HW_OUTPUT);
// //Parameter lightOutput = getOutputParameter(PARAM_HW_OUTPUT2);
//
// ParameterValueManagement.setString(temperatureOutput, PARAM_HW_OUTPUT, "20");
// // The DPWS stack handles all simple type parameters as strings. The developer is
// // responsible to check any constraints and to perform any necessary type conversions.
//
// // business logic
//// double temperature = tempSensor.getCelsius();
//// temperatureOutput.setValue(""+temperature);
//
// return temperatureOutput;
// //double light = lightSensor.getAverageValue();
// //lightOutput.setValue(""+light);
// }
// catch (IOException ex) {}
//}
}
package org.ws4d.smartsensor.device;
import org.ws4d.java.communication.DPWSCommunicationManager;
import org.ws4d.java.service.DefaultDevice;
import org.ws4d.java.service.DefaultService;
import org.ws4d.java.types.QName;
import org.ws4d.java.types.URI;
/**
* HelloWorldService describes the service that is hosted by the SPOT.
* In particular the service has one method HelloWorldAction.
*
*/
public class LightService extends DefaultService {
// global settings
public static final String NAMESPACE = "http://www.demo.com/bbsr";//HelloWorldDevice.NAMESPACE;
// public static final String PORTTYPE = "TemperatureInterface";
public static final String PORTTYPE = "Brightness";
public static final String ENDPOINT_PATH = "/SunSPOTSensor";
public static final QName QN_PORTTYPE = new QName(PORTTYPE, NAMESPACE);
public final static URI DOCU_EXAMPLE_SERVICE_ID = new URI(NAMESPACE + ENDPOINT_PATH);
public LightService() {
super(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);
// Set the property
// Properties.getInstance().setServiceProperty(this, Properties.PROP_SERVICE_SECURED, false);
this.setServiceId(DOCU_EXAMPLE_SERVICE_ID);
// if endpoint not set, port type is use
// setEndpointPath(ENDPOINT_PATH);
// -- hello world action --
//HelloWorldAction helloWorldAct = new HelloWorldAction ();
//addAction(helloWorldAct);
// -- getTemperatureAction --
// GetTemperatureOperation getTemperatureAct = new GetTemperatureOperation();
// addOperation(getTemperatureAct);
// -- getValuesAction --
GetLightOperation getLightAct = new GetLightOperation();
addOperation(getLightAct);
// -- getValuesAction --
//GetValuesAction getValuesAct = new GetValuesAction();
//addAction(getValuesAct);
}
}
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"); //file that contains temperature value
this.pathtoData = sb.toString(); //TempReadingThread will poll value
tempSem = new Semaphore(1);
Thread tempreadingThread = new Thread(new TempReadingThread(tempSem, pathtoData, this));
tempreadingThread.start();
}
public int readSensor(){
int returnTemp;
try {
tempSem.acquire();
returnTemp = this.temperature; //temperature value that was red by the TempReadingThread and written to PhySensor
tempSem.release();
return returnTemp;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public void writetemperature(int temp){
try {
tempSem.acquire();
this.temperature = temp;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tempSem.release();
}
}
......@@ -7,12 +7,7 @@ import org.ws4d.java.types.LocalizedString;
import org.ws4d.java.types.QName;
import org.ws4d.java.types.QNameSet;
/**
* HelloWorldDevice describes the SPOT as a DPWS hosting device.
*
* modified by Christin Groba
*
*/
public class SensorDevice extends DefaultDevice{
public static final String NAMESPACE = "http://www.demo.com/bbsr";
......@@ -24,16 +19,13 @@ public class SensorDevice extends DefaultDevice{
super(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);
// set PortType
this.setPortTypes(new QNameSet ( new QName (PORTTYPE, NAMESPACE )));
// add device name ( name is language specific )
this.addFriendlyName ("en -US", "SensorDevice");
this.addFriendlyName ( LocalizedString.LANGUAGE_DE , "Sensor");
// add device manufacturer ( manufacturer is language specific )
this.addManufacturer ( LocalizedString.LANGUAGE_EN , "Test Inc.");
this.addManufacturer ("de -DE", " Test GmbH ");
......
......@@ -8,25 +8,17 @@ import org.ws4d.java.types.URI;
public class SmartSensorService extends DefaultService {
// global settings
public static final String NAMESPACE = "http://www.demo.com/bbsr";//HelloWorldDevice.NAMESPACE;
// public static final String PORTTYPE = "TemperatureInterface";
public static final String PORTTYPE_BRIGHT = "BrightnessInterface";
public static final String PORTTYPE_TEMP = "TemperatureInterface";
public static final String ENDPOINT_PATH = "SmartSensorService";
public static final String NAMESPACE = "http://www.demo.com/bbsr";
public static final String PORTTYPE_BRIGHT = "Brightness";
public static final String ENDPOINT_PATH = "SmartSensorService";
public static final QName QN_PORTTYPE_BRIGHT = new QName(PORTTYPE_BRIGHT, NAMESPACE);
public static final QName QN_PORTTYPE_TEMP = new QName(PORTTYPE_TEMP, NAMESPACE);
public final static URI DOCU_EXAMPLE_SERVICE_ID = new URI(ENDPOINT_PATH);
public SmartSensorService() {
super(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);
// Set the property
// Properties.getInstance().setServiceProperty(this, Properties.PROP_SERVICE_SECURED, false);
this.setServiceId(DOCU_EXAMPLE_SERVICE_ID);
// -- getTemperatureAction --
GetTemperatureOperation getTemperatureAct = new GetTemperatureOperation();
addOperation(getTemperatureAct);
this.setServiceId(DOCU_EXAMPLE_SERVICE_ID);
// -- getValuesAction --
GetLightOperation getLightAct = new GetLightOperation();
......
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;
import org.ws4d.java.configuration.FrameworkProperties;
import org.ws4d.java.configuration.Properties;
public class mainclass {
public class StartSensor {
public static void main(String[] args) {
CoreFramework.start(args);
// create a device
SensorDevice device = new SensorDevice();
// create service
// create a service
SmartSensorService service = new SmartSensorService();
// LightService service = new LightService();
//
// TempService tempservice = new TempService();
// set device properties
// Properties.getInstance().setDeviceProperty(device, Properties.PROP_DEVICE_UUID, "urn:uuid:df0794e0-40d5-11dc-9999-40f84sunspot" );
// add service to device
// attach service to device
device.addService(service);
// device.addService(tempservice);
// start device
try {
device.start();
......
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)); //temperature value is stored in text document
String line = reader.readLine();
if(line.substring(line.length()-3, line.length()).equals("YES")){ //YES indicates the availability of a value
line = reader.readLine();
this.temperature = Integer.parseInt(line.substring(line.length()-5, line.length()));
this.temperature /= 1000;
this.temperature -= 6; //offset correction
reader.close();
phySensor.writetemperature(temperature); //write temperature value to sensor object
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block