Commit 25e5a33c authored by aw613's avatar aw613
Browse files

This is an emulated lightbulb. It has no GPIO dependencies of the

raspberry pi anymore.
parent fed74dd5
package com.pi.lightbulb.client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import org.ws4d.java.authorization.AuthorizationException;
import org.ws4d.java.client.DefaultClient;
import org.ws4d.java.client.SearchManager;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.communication.CommunicationManagerRegistry;
import org.ws4d.java.communication.DPWSProtocolVersion;
import org.ws4d.java.configuration.DPWSProperties;
import org.ws4d.java.configuration.DispatchingProperties;
import org.ws4d.java.constants.DPWS2006.DPWSConstants2006;
import org.ws4d.java.dispatch.DefaultServiceReference;
import org.ws4d.java.security.CredentialInfo;
import org.ws4d.java.security.SecurityKey;
import org.ws4d.java.service.DefaultService;
import org.ws4d.java.service.Device;
import org.ws4d.java.service.InvocationException;
import org.ws4d.java.service.Operation;
import org.ws4d.java.service.Service;
import org.ws4d.java.service.parameter.ParameterValue;
import org.ws4d.java.service.reference.DeviceReference;
import org.ws4d.java.types.HelloData;
import org.ws4d.java.types.QName;
import org.ws4d.java.types.QNameSet;
import org.ws4d.java.types.SearchParameter;
import org.ws4d.java.util.Log;
import com.pi.lightbulb.device.BulbGPIOListener;
import com.pi.lightbulb.gpio.DefaultClientGPIO;
import com.pi.lightbulb.gpio.GpioListener;
public class LightBulbClient extends DefaultClientGPIO {
public final static String NAMESPACE = "http://www.demo.com/bbsr";
public final static String DEVICE = "Sensor";
public final static String SERVICE = "SunSPOTSensor";
boolean bound = false;
private Service service;
private Timer timer;
private class Task extends TimerTask
{
private LightBulbClient client;
public Task(LightBulbClient airCondClient)
{
client = airCondClient;
}
@Override
public void run()
{
System.out.println("Refresh");
client.updateTemperature();
}
}
@Override
public synchronized void deviceFound(DeviceReference devRef, SearchParameter search, String comManId) {
QNameSet types = devRef.getDiscoveryData().getTypes();
org.ws4d.java.structures.Iterator i = types.iterator();
// System.out.println("Found Device: " + devRef.toString());
while (i.hasNext() && service == null)
{
QName qname = (QName) i.next();
if (qname.getLocalPart().equals(DEVICE))
{
System.out.println("Device: " + qname.getLocalPart());
try {
Device device = devRef.getDevice();
QNameSet serviceTypes = new QNameSet();
serviceTypes.add(new QName(SERVICE, NAMESPACE));
org.ws4d.java.structures.Iterator serviceIterator = device.getServiceReferences(serviceTypes, SecurityKey.EMPTY_KEY);
DefaultServiceReference serviceRef = (DefaultServiceReference) serviceIterator.next();
service = serviceRef.getService();
// System.out.println("ServiceId: " + service.toString());
bound = true;
BulbGPIOListener.getInstance().pulse(500);
Task task = new Task(this);
timer = new Timer();
timer.schedule(task, 5000, 5000);
updateTemperature();
} catch (CommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthorizationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void helloReceived(HelloData helloData)
{
Log.info("Hello received");
BulbGPIOListener.getInstance().pulse(500);
if (GpioListener.getInstance().areButtonsHighDelayed(DispatchingProperties.getInstance().getResponseWaitTime() / 1000))
{
Log.info("Hello accepted");
DeviceReference devRef = getDeviceReference(helloData);
deviceFound(devRef, null, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
}
else Log.info("Hello rejected");
}
@Override
public void searchKnownDevices()
{
Log.info("Searching for devices...");
SearchParameter search = new SearchParameter();
QNameSet qnameSet = new QNameSet();
qnameSet.add(new QName(DEVICE, NAMESPACE));
search.setDeviceTypes(qnameSet, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
SearchManager.searchDevice(search, this, null);
}
public void registerKnownHello()
{
SearchParameter search = new SearchParameter();
QNameSet qnameSet = new QNameSet();
qnameSet.add(new QName(DEVICE, NAMESPACE));
search.setDeviceTypes(qnameSet, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
this.registerHelloListening(search);
}
@Override
public void resetPairing()
{
bound = false;
if (timer != null)
{
timer.cancel();
}
service = null;
BulbGPIOListener.getInstance().pulse(1000);
}
public void updateTemperature()
{
// we are looking for an Operation by its name
Operation op = service.getOperation(null, "LightOperation", null, null);
ParameterValue input = op.createInputValue();
ParameterValue result;
try {
result = op.invoke(input, CredentialInfo.EMPTY_CREDENTIAL_INFO);
System.out.println("Response from the LightAction: " + result.toString());
double light = Double.parseDouble(result.toString());
if (light < 80)
{
BulbGPIOListener.getInstance().setBulbStateOn();
}
else
{
BulbGPIOListener.getInstance().setBulbStateOff();
}
} catch (AuthorizationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CommunicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public static void main(String[] args)
// {
// Log.setLogLevel(Log.DEBUG_LEVEL_INFO);
// DPWSProperties properties = DPWSProperties.getInstance();
// properties.addSupportedDPWSVersion(new DPWSProtocolVersion(DPWSConstants2006.DPWS_VERSION2006));
//
// JMEDSFramework.start(args);
//
// AirCondClient client = new AirCondClient();
// client.searchKnownDevices();
//
// }
}
......@@ -43,6 +43,7 @@ public class ConfigurationClient extends DefaultClient implements ConfigurationS
private ArrayList<DPWSInterface> dpwsInterfaces;
DPWSRule rule;
private Thread ruleExecution;
static boolean stopThread = false;
/**
* air conditioner device object
......
......@@ -11,7 +11,6 @@ import org.ws4d.java.service.parameter.ParameterValueManagement;
import org.ws4d.java.types.QName;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSRule;
import com.pi.lightbulb.device.BulbGPIOListener;
public class ruleExe implements Runnable {
......@@ -30,19 +29,54 @@ public class ruleExe implements Runnable {
DPWSRule rule = cl.rule;
try {
Operation op = serv.getService().getOperation(new QName("Brightness", "http://www.demo.com/bbsr"), "LightOperation", null, null);
//name of operation is known from wsdl
while(true){
ParameterValue result = op.invoke(null, null);
ParameterValue result = op.invoke(null, null); //read luminosity value from sensor
//
int light = Integer.parseInt(ParameterValueManagement.getString(result, "light"));
int light = Integer.parseInt(ParameterValueManagement.getString(result, "light")); //value of the sensor
int i = Integer.parseInt(rule.conditions.get(0).value); //value to compare with
rule.conditions.get(0);
int i = Integer.parseInt(rule.conditions.get(0).value);
if(light < i)
BulbGPIOListener.getInstance().setBulbStateOn();
else
BulbGPIOListener.getInstance().setBulbStateOff();
switch(rule.conditions.get(0).operator) //comparison operator
{
case Equal:
if(light == i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
case Greater:
if(light > i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
case GreaterOrEqual:
if(light >= i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
case Less:
if(light < i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
case LessOrQqual:
if(light <= i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
case NotEqual:
if(light != i)
System.out.println("Light ON");
else
System.out.println("Light OFF");
break;
}
Thread.sleep(1000);
}
......
package com.pi.lightbulb.device;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
public class BulbGPIOListener {
// create gpio controller instance
final GpioController gpio = GpioFactory.getInstance();
// provision gpio pins #04 as an output pin and make sure is is set to LOW at startup
GpioPinDigitalOutput bulb = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00, // PIN NUMBER
"Bulb", // PIN FRIENDLY NAME (optional)
PinState.LOW); // PIN STARTUP STATE (optional)
static BulbGPIOListener instance;
// Suppress default constructor for noninstantiability
private BulbGPIOListener() {}
public static synchronized BulbGPIOListener getInstance() {
if (BulbGPIOListener.instance == null) {
BulbGPIOListener.instance = new BulbGPIOListener();
}
return BulbGPIOListener.instance;
}
public void setBulbStateOn()
{
bulb.high();
System.out.println("Turning On");
}
public void setBulbStateOff()
{
bulb.low();
System.out.println("Turning Off");
}
public void pulse(int millis)
{
bulb.pulse(millis);
}
}
package com.pi.lightbulb.device;
import org.ws4d.java.communication.DPWSCommunicationManager;
import org.ws4d.java.service.DefaultDevice;
import org.ws4d.java.types.LocalizedString;
import org.ws4d.java.types.QName;
import org.ws4d.java.types.QNameSet;
import com.pi.lightbulb.configuration.Constants;
import com.pi.lightbulb.gpio.DefaultDeviceGPIO;
public class LightBulbDevice extends DefaultDeviceGPIO {
public class LightBulbDevice extends DefaultDevice {
/**
* Constructor of our device.
*/
public LightBulbDevice() {
super();
super(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);
/*
* The following lines add metadata information to the device to
......
......@@ -40,6 +40,8 @@ public class LightBulbService extends DefaultService {
configureGetStateOperation();
configureSetStateOperation();
configureToggleOperation();
configureswitchOnOperation();
configureswitchOffOperation();
}
......@@ -94,11 +96,11 @@ public class LightBulbService extends DefaultService {
if (state == true)
{
BulbGPIOListener.getInstance().setBulbStateOn();
System.out.println("Light OFF");
}
else
{
BulbGPIOListener.getInstance().setBulbStateOff();
System.out.println("Light OFF");
}
// create suitable response ...
......@@ -133,11 +135,11 @@ public class LightBulbService extends DefaultService {
if (state == true)
{
BulbGPIOListener.getInstance().setBulbStateOn();
System.out.println("Light ON");
}
else
{
BulbGPIOListener.getInstance().setBulbStateOff();
System.out.println("Light OFF");
}
// create suitable response ...
......@@ -149,5 +151,65 @@ public class LightBulbService extends DefaultService {
});
}
private void configureswitchOnOperation()
{
// get a reference to a certain operation given its input action URI ...
OperationStub operation = (OperationStub) getOperation(null, "On", null, null);
// ... and specify its business logic by means of an InvokeDelegate
operation.setDelegate(new InvokeDelegate() {
/*
* (non-Javadoc)
* @see
* org.ws4d.java.service.InvokeDelegate#invoke(org.ws4d.java.service
* .Operation, org.ws4d.java.service.parameter.ParameterValue)
*/
public ParameterValue invokeImpl(Operation operation, ParameterValue request, CredentialInfo credentialInfo) throws InvocationException {
// extract the expected input args ...
// String value = ParameterValueManagement.getString(request, "State");
System.out.println("Light ON");
// create suitable response ...
ParameterValue result = operation.createOutputValue();
// ... and send it back
return result;
}
});
}
private void configureswitchOffOperation()
{
// get a reference to a certain operation given its input action URI ...
OperationStub operation = (OperationStub) getOperation(null, "Off", null, null);
// ... and specify its business logic by means of an InvokeDelegate
operation.setDelegate(new InvokeDelegate() {
/*
* (non-Javadoc)
* @see
* org.ws4d.java.service.InvokeDelegate#invoke(org.ws4d.java.service
* .Operation, org.ws4d.java.service.parameter.ParameterValue)
*/
public ParameterValue invokeImpl(Operation operation, ParameterValue request, CredentialInfo credentialInfo) throws InvocationException {
// extract the expected input args ...
// String value = ParameterValueManagement.getString(request, "State");
System.out.println("Light OFF");
// create suitable response ...
ParameterValue result = operation.createOutputValue();
// ... and send it back
return result;
}
});
}
}
......@@ -21,16 +21,12 @@ import java.io.IOException;
import java.util.ArrayList;
import org.ws4d.java.CoreFramework;
import org.ws4d.java.communication.CommunicationManagerRegistry;
import org.ws4d.java.communication.DPWSProtocolVersion;
import org.ws4d.java.configuration.DPWSProperties;
import org.ws4d.java.configuration.DispatchingProperties;
import org.ws4d.java.security.CredentialInfo;
import org.ws4d.java.service.DefaultService;
import org.ws4d.java.types.URI;
import org.ws4d.java.util.Log;
import com.pi.lightbulb.client.LightBulbClient;
import com.pi.lightbulb.configuration.ConfigurationClient;
import com.pi.lightbulb.configuration.ConfigurationService;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSInterface;
......@@ -38,7 +34,6 @@ import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSParamete
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DataType;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.Role;
import com.pi.lightbulb.configuration.Constants;
import com.pi.lightbulb.gpio.GpioListener;
public class LightBulbServiceProvider {
......@@ -63,7 +58,7 @@ public class LightBulbServiceProvider {
// First we need a device.
LightBulbDevice device = new LightBulbDevice();
LightBulbService mainService = new LightBulbService();
LightBulbClient client = new LightBulbClient();
// Then we create a service.
final ConfigurationService configurationService = new ConfigurationService();
......@@ -96,36 +91,10 @@ public class LightBulbServiceProvider {
// In the end we add our service to the device.
device.addService(configurationService);
device.addService(mainService);
// Should be run as root on Pi
String env = "SUDO_USER";
String environment = System.getenv(env);
if (environment != null) {
Log.info(env + "=" + environment);
} else {
Log.info(env + " is not assigned");
}
// Do not forget to start the device!
try {
device.start();
// Thread.sleep(5000);
// client.resetPairing();
// client.searchKnownDevices();
client.registerKnownHello();
// Select control buttons if on Pi
if (environment != null && environment.equals("pi")) {
Log.info("Starting GPIO Controller");
GpioListener.getInstance().bindButton0(1);
//GpioListener.getInstance().bindButton0(3);
GpioListener.getInstance().registerDevice(device);
GpioListener.getInstance().registerClient(client);
GpioListener.getInstance().activateHello(true);
GpioListener.getInstance().activateProbe(true);
BulbGPIOListener.getInstance().pulse(500);
}
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -37,6 +37,13 @@
<wsdl:input message="tns:ToggleRequest" wsam:Action="http://www.demo.com/bbsr/LightBulb/Toggle"/>
</wsdl:operation>
<wsdl:operation name="On">
<wsdl:input message="tns:OnRequest" wsam:Action="http://www.demo.com/bbsr/LightBulb/On"/>
</wsdl:operation>
<wsdl:operation name="Off">
<wsdl:input message="tns:OffRequest" wsam:Action="http://www.demo.com/bbsr/LightBulb/Off"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LightBulbInterface" type="tns:LightBulb">
......@@ -67,6 +74,20 @@
<soap:body use="literal" />
</wsdl:input>
</wsdl:operation>
<wsdl:operation name="On">
<soap:operation soapAction="http://www.demo.com/bbsr/LightBulb/On" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
</wsdl:operation>
<wsdl:operation name="Off">
<soap:operation soapAction="http://www.demo.com/bbsr/LightBulb/Off" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
......
package com.pi.lightbulb.gpio;
// Button Interface
public interface ButtonListener {
public void buttonPressed(int pinNumber);
}
This diff is collapsed.
This diff is collapsed.
package com.pi.lightbulb.gpio;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;
import org.ws4d.java.client.DefaultClient;
import org.ws4d.java.client.SearchManager;
import org.ws4d.java.service.DefaultDevice;
import org.ws4d.java.types.QName;
import org.ws4d.java.types.QNameSet;
import org.ws4d.java.types.SearchParameter;
import org.ws4d.java.util.Log;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListener;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
// Die Klasse zur Steuerung von GPIOs auf Raspberry Pi
public class GpioListener {
// create gpio controller
private GpioController gpio = null;
// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
private GpioPinDigitalInput myButton0 = null;