Commit 958330af authored by Vlado Altmann's avatar Vlado Altmann
Browse files

LightBulb service is running

parent a34e719c
package com.pi.lightbulb.configuration;
import org.ws4d.java.types.URI;
public class Constants {
public final static String NAMESPACE_CONFIGURATION = "http://www.ws4d.org/Configuration";
public final static String NAMESPACE_DEMO = "http://www.demo.com/bbsr";
public final static String NAMESPACE_DEMO = "http://www.demo.com/bbsr";
public final static String NAMESPACE = "http://www.ws4d.org/Device";
public final static String DEVICE = "LightBulb";
public final static String WSDL = "local:/com/pi/lightbulb/device/Switch.wsdl";
public final static String NAMESPACE = "http://www.ws4d.org/Device";
public final static String SERVICE_ID = "LightBulbService";
public final static String DEVICE = "LightBulb";
public final static String SERVICE_TYPE = "Switch";
}
package com.pi.lightbulb.device;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.schema.ComplexType;
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;
import org.ws4d.java.types.QName;
import com.pi.lightbulb.configuration.Constants;
public class GetStateOperation extends Operation {
private String state = "off";
public GetStateOperation() {
super("GetCurrentState", new QName(LightBulbService.SERVICE_TYPE, Constants.NAMESPACE));
// create element of type TemperatureType
Element stateElement = new Element(new QName("state", Constants.NAMESPACE), SchemaUtil.TYPE_STRING);
// set the input of the operation
// setInput(null);
// set this element as output
setOutput(stateElement);
}
public ParameterValue invokeImpl(ParameterValue parameterValue, CredentialInfo credentialInfo) throws InvocationException, CommunicationException {
// create output and set value
ParameterValue result = createOutputValue();
ParameterValueManagement.setString(result, "state", "" + state);
return result;
}
public void setState(String value)
{
state = value;
}
}
package com.pi.lightbulb.device;
import java.io.IOException;
import org.ws4d.java.communication.CommunicationManagerRegistry;
import org.ws4d.java.security.CredentialInfo;
import org.ws4d.java.service.DefaultService;
import org.ws4d.java.service.InvocationException;
import org.ws4d.java.service.InvokeDelegate;
import org.ws4d.java.service.Operation;
import org.ws4d.java.service.OperationStub;
import org.ws4d.java.service.parameter.ParameterValue;
import org.ws4d.java.service.parameter.ParameterValueManagement;
import org.ws4d.java.types.URI;
/**
* Implementation of the service described in "An Introduction to WS4D and to
* JMEDS, a framework for distributed communication between devices in a domotic
* environment" by Pierre-Alexandre Gagn
*
* @author ajordan
import com.pi.lightbulb.configuration.Constants;
/**
* @author va015
*/
public class LightBulbService extends DefaultService {
public final static URI SERVICE_ID = new URI("LightBulbService");
public final static String SERVICE_TYPE = "TriggerInterface";
private boolean state = false;
/**
* Standard Constructor
*/
public LightBulbService() {
super();
super(CommunicationManagerRegistry.getPreferredCommunicationManagerID());
//Define Service ID
// setServiceId(new URI(Constants.NAMESPACE + "/" + Constants.DEVICE));
setServiceId(new URI(Constants.SERVICE_ID));
try {
define(new URI(Constants.WSDL), CredentialInfo.EMPTY_CREDENTIAL_INFO, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
configureGetStateOperation();
configureSetStateOperation();
configureToggleOperation();
}
private void configureGetStateOperation()
{
// get a reference to a certain operation given its input action URI ...
OperationStub operation = (OperationStub) getOperation(null, "GetState", null, null);
this.setServiceId(SERVICE_ID);
// ... and specify its business logic by means of an InvokeDelegate
operation.setDelegate(new InvokeDelegate() {
// add Operations to the service
GetStateOperation getState = new GetStateOperation();
addOperation(getState);
/*
* (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, "someValue");
SwitchOperation switchOp = new SwitchOperation();
addOperation(switchOp);
switchOp.setGetStateOperation(getState);
// create suitable response ...
ParameterValue result = operation.createOutputValue();
ParameterValueManagement.setString(result, null, Boolean.toString(state));
// ... and send it back
return result;
}
});
}
private void configureSetStateOperation()
{
// get a reference to a certain operation given its input action URI ...
OperationStub operation = (OperationStub) getOperation(null, "SetState", 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");
state = Boolean.parseBoolean(value);
// create suitable response ...
ParameterValue result = operation.createOutputValue();
// ... and send it back
return result;
}
});
}
private void configureToggleOperation()
{
// get a reference to a certain operation given its input action URI ...
OperationStub operation = (OperationStub) getOperation(null, "Toggle", 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");
state = !state;
// create suitable response ...
ParameterValue result = operation.createOutputValue();
// ... and send it back
return result;
}
});
}
}
/**
* This project is a prototype that shows the idea of a DPWS "Connector" of a legacy system.
*
* author:
* Vlado Altmann
* University of Rostock
* Faculty of Computer Science and Electrical Engineering
* Institute of Applied Microelectronics and Computer Engineering
*
* The implementation uses code from the JMEDS Documentation and example code from Materna GmbH
* See:
* http://ws4d.e-technik.uni-rostock.de/jmeds/
* and/or
* http://sourceforge.net/projects/ws4d-javame/
*
*/
package com.pi.lightbulb.device;
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.constants.DPWS2006.DPWSConstants2006;
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.types.QName;
import org.ws4d.java.types.QNameSet;
import org.ws4d.java.types.SearchParameter;
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.gpio.GpioListener;
import com.pi.lightbulb.configuration.ConfigurationClient;
import com.pi.lightbulb.configuration.ConfigurationService;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSInterface;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSParameterDescription;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DataType;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.Role;
import com.pi.lightbulb.configuration.Constants;
public class LightBulbServiceProvider {
/**
* @param args
*/
public static void main(String[] args) {
// Disable debug output
Log.setLogLevel(Log.DEBUG_LEVEL_INFO);
DPWSProperties properties = DPWSProperties.getInstance();
// properties.setDiscoveryButton0(1);
......@@ -39,29 +60,49 @@ public class LightBulbServiceProvider {
// First we need a device.
LightBulbDevice device = new LightBulbDevice();
LightBulbClient client = new LightBulbClient();
LightBulbService mainService = new LightBulbService();
// Then we create a service.
final LightBulbService service = new LightBulbService();
final ConfigurationService configurationService = new ConfigurationService();
ConfigurationClient client = configurationService.getConfigurationClient();
// Set supported interface types
ArrayList<DPWSInterface> list = new ArrayList<DPWSInterface>();
DPWSInterface dpwsInterface = new DPWSInterface();
// dpwsInterface1.type = new QName("SensorInterface", Constants.NAMESPACE);
dpwsInterface.localName = new String("Brightness");
dpwsInterface.namespace = new URI(Constants.NAMESPACE);
dpwsInterface.role = Role.Client;
dpwsInterface.ruleSupport = true;
list.add(dpwsInterface);
client.setSupportedInterfaces(list);
//Set rule parameter
DPWSParameterDescription description = new DPWSParameterDescription();
description.name = "Light";
description.type = DataType.Integer;
configurationService.setParameter(description);
description = new DPWSParameterDescription();
description.name = "Time";
description.type = DataType.Time;
configurationService.setParameter(description);
// In the end we add our service to the device.
device.addService(service);
device.addService(configurationService);
device.addService(mainService);
// Do not forget to start the device!
try {
device.start();
// Thread.sleep(5000);
// client.resetPairing();
// client.searchKnownDevices();
client.registerKnownHello();
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();
}
}
}
/**
* This project is a prototype that shows the idea of a DPWS "Connector" of a legacy system.
*
* author:
* Vlado Altmann
* University of Rostock
* Faculty of Computer Science and Electrical Engineering
* Institute of Applied Microelectronics and Computer Engineering
*
* The implementation uses code from the JMEDS Documentation and example code from Materna GmbH
* See:
* http://ws4d.e-technik.uni-rostock.de/jmeds/
* and/or
* http://sourceforge.net/projects/ws4d-javame/
*
*/
package com.pi.lightbulb.device;
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.configuration.ConfigurationClient;
import com.pi.lightbulb.configuration.ConfigurationService;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSInterface;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DPWSParameterDescription;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.DataType;
import com.pi.lightbulb.configuration.ConfigurationServiceInterface.Role;
import com.pi.lightbulb.configuration.Constants;
public class ServiceProviderWSDL {
/**
* @param args
*/
public static void main(String[] args) {
// Disable debug output
Log.setLogLevel(Log.DEBUG_LEVEL_INFO);
DPWSProperties properties = DPWSProperties.getInstance();
// properties.setDiscoveryButton0(1);
// properties.setDiscoveryButton1(3);
properties.addSupportedDPWSVersion(DPWSProtocolVersion.DPWS_VERSION_2006);
// properties.removeSupportedDPWSVersion(DPWSProtocolVersion.DPWS_VERSION_2009);
DispatchingProperties.getInstance().setResponseWaitTime(10000);
// mandatory: Starting the DPWS Framework.
CoreFramework.start(args);
// First we need a device.
LightBulbDevice device = new LightBulbDevice();
// Then we create a service.
final ConfigurationService configurationService = new ConfigurationService();
//final Service aircondService = new Service();
final DefaultService mainService = new DefaultService(CommunicationManagerRegistry.getPreferredCommunicationManagerID());
mainService.setServiceId(new URI(Constants.NAMESPACE + "/LightBulb"));
try {
// configurationService.define(new URI("local:/org/ws4d/airConditioner/Configuration.wsdl"), CredentialInfo.EMPTY_CREDENTIAL_INFO, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
// configurationService.setServiceId(new URI("Configuration"));
mainService.define(new URI("local:/com/pi/lightbulb/device/Switch.wsdl"), CredentialInfo.EMPTY_CREDENTIAL_INFO, CommunicationManagerRegistry.getPreferredCommunicationManagerID());
mainService.setServiceId(new URI("LightBulb"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ConfigurationClient client = configurationService.getConfigurationClient();
// Set supported interface types
ArrayList<DPWSInterface> list = new ArrayList<DPWSInterface>();
DPWSInterface dpwsInterface = new DPWSInterface();
// dpwsInterface1.type = new QName("SensorInterface", Constants.NAMESPACE);
dpwsInterface.localName = new String("TriggerInterface");
dpwsInterface.namespace = new URI(Constants.NAMESPACE_DEMO);
dpwsInterface.role = Role.Client;
dpwsInterface.ruleSupport = true;
list.add(dpwsInterface);
dpwsInterface = new DPWSInterface();
// dpwsInterface1.type = new QName("SensorInterface", Constants.NAMESPACE);
dpwsInterface.localName = new String("SmartWindowInterface");
dpwsInterface.namespace = new URI(Constants.NAMESPACE_DEMO);
dpwsInterface.role = Role.Client;
dpwsInterface.ruleSupport = true;
list.add(dpwsInterface);
dpwsInterface = new DPWSInterface();
// dpwsInterface1.type = new QName("SensorInterface", Constants.NAMESPACE);
dpwsInterface.localName = new String("TemperatureInterface");
dpwsInterface.namespace = new URI(Constants.NAMESPACE_DEMO);
dpwsInterface.role = Role.Service;
dpwsInterface.ruleSupport = true;
list.add(dpwsInterface);
client.setSupportedInterfaces(list);
//Set rule parameter
DPWSParameterDescription description = new DPWSParameterDescription();
description.name = "Temperature";
description.type = DataType.Integer;
configurationService.setParameter(description);
description = new DPWSParameterDescription();
description.name = "Time";
description.type = DataType.Time;
configurationService.setParameter(description);
// In the end we add our service to the device.
device.addService(configurationService);
device.addService(mainService);
// Do not forget to start the device!
try {
device.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.pi.lightbulb.device;
import org.ws4d.java.communication.CommunicationException;
import org.ws4d.java.schema.ComplexType;
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;
import org.ws4d.java.types.QName;
import com.pi.lightbulb.configuration.Constants;
public class SwitchOperation extends Operation {
private GetStateOperation getOperation;
public SwitchOperation() {
super("Switch", new QName(LightBulbService.SERVICE_TYPE, Constants.NAMESPACE));
// create element of type TemperatureType
Element switchElement = new Element(new QName("switch", Constants.NAMESPACE), SchemaUtil.TYPE_STRING);
// set the input of the operation
setInput(switchElement);
// create reply element
Element reply = new Element(new QName("status", Constants.NAMESPACE), SchemaUtil.TYPE_STRING);
// set this element as output
setOutput(reply);
}
public ParameterValue invokeImpl(ParameterValue parameterValue, CredentialInfo credentialInfo) throws InvocationException, CommunicationException {
// get string value from input
String value = ParameterValueManagement.getString(parameterValue, "switch");
// create output and set value
ParameterValue result = createOutputValue();
if (value.compareTo("on") == 0)
{
BulbGPIOListener.getInstance().setBulbStateOn();
getOperation.setState(value);
ParameterValueManagement.setString(result, "status", value);
}
else if (value.compareTo("off") == 0)
{
BulbGPIOListener.getInstance().setBulbStateOff();
getOperation.setState(value);
ParameterValueManagement.setString(result, "status", value);
}
else
{
ParameterValueManagement.setString(result, "status", "error");
}
return result;
}
public void setGetStateOperation(GetStateOperation op)
{
getOperation = op;
}
}
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