Skip to content

Commit

Permalink
new actuaortclient: dummy (for experiments if you do not have any sen…
Browse files Browse the repository at this point in the history
…sors/actuators

updated enocean
  • Loading branch information
duchon committed Oct 21, 2015
1 parent 7a89a2f commit 572cf49
Show file tree
Hide file tree
Showing 69 changed files with 1,016 additions and 1,519 deletions.
85 changes: 85 additions & 0 deletions actuatorclient.dummy.impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.fortiss.smartmicrogrid</groupId>
<artifactId>smartmicrogrid</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>actuatorclient.dummy.impl</artifactId>

<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>actuatormaster.api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ambulance.api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>containermanager.api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>smg.schemas</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>config.lib</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>remoteframework.lib</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Activator>org.fortiss.smg.actuatorclient.dummy.impl.ActuatorClientActivator</Bundle-Activator>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
<Export-Package>
org.fortiss.smg.actuatorclient.dummy.impl
</Export-Package>
<Import-Package>
*;resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.fortiss.smg.actuatorclient.dummy.impl;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.concurrent.TimeoutException;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceRegistration;import org.fortiss.smg.actuatormaster.api.AbstractClient;import org.fortiss.smg.actuatormaster.api.ActuatorMasterQueueNames;import org.fortiss.smg.actuatormaster.api.IActuatorMaster;import org.fortiss.smg.actuatormaster.api.AbstractConnector.IOnConnectListener;import org.fortiss.smg.config.lib.WrapperConfig;import org.fortiss.smg.remoteframework.lib.DefaultProxy;import org.fortiss.smg.remoteframework.lib.DefaultServer;import org.slf4j.LoggerFactory;public class ActuatorClientActivator extends AbstractClient implements BundleActivator { // Logger from sl4j private static org.slf4j.Logger logger = LoggerFactory .getLogger(ActuatorClientActivator.class); private ActuatorClientImpl implClient; private List<ActuatorClientImpl> clients = new ArrayList<ActuatorClientImpl>(); IActuatorMaster master4config = null; @Override public void start(BundleContext context) throws Exception { // register here your services etc. // DO NOT start heavy operations here - use threads /* * Try to connect to Master to get the wrapper's config file */ ArrayList<WrapperConfig> configList = new ArrayList<WrapperConfig>(); DefaultProxy<IActuatorMaster> proxyMaster = new DefaultProxy<IActuatorMaster>( IActuatorMaster.class, ActuatorMasterQueueNames.getActuatorMasterInterfaceQueue(), 5000); try { master4config = proxyMaster.init(); } catch (TimeoutException e) { logger.error("ActuatorClient: Unable to connect to master (Timeout)."); } /* * If we have connection try to get the wrapper's config file */ if (master4config != null) { try { configList = master4config.getWrapperConfig("dummy"); } catch (TimeoutException e) { logger.error("ActuatorClient: Unable to connect to master (Timeout)."); } finally { try { proxyMaster.destroy(); } catch (IOException e) { logger.info("Unable to close con. for queue:" + this.clientId); } } /* * For each received wrapper config instance (possibly the same * wrapper is used for multiple (physical) devices */ if (configList.size() > 0) { for (WrapperConfig config : configList) { final String clientKey = config.getKey(); final String clientIDextension = config.getHost(); implClient = new ActuatorClientImpl(config.getProtocol()+"://"+config.getHost(), config.getPort(), config.getWrapperID(), config.getPollingfrequency(), config.getUsername(), config.getPassword()); // Register at Actuator Master (self, human readable name // for // device) registerAsClientAtServer(implClient, config.getWrapperName(), new IOnConnectListener() { @Override public void onSuccessFullConnection() { implClient.setMaster(master); implClient.setClientId(clientId); implClient.activate(); logger.info("ActuatorClient[" + clientKey + "-" + clientIDextension + "] is alive"); clients.add(implClient); } }); } logger.info("Solarlog Wrapper started"); } else { logger.info("No Configuration available"); } } else { proxyMaster.destroy(); logger.debug("Solarlog Wrapper could not read config from Master"); this.stop(context); } } @Override public void stop(BundleContext context) throws Exception { // REMEMBER to destroy all resources, threads and do cleanup for (ActuatorClientImpl client : clients) { client.deactivate(); client = null; } logger.info("ActuatorClient[Solar] is dead"); this.destroy(); }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
package org.fortiss.smg.actuatorclient.dummy.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.fortiss.smg.actuatormaster.api.IActuatorClient;
import org.fortiss.smg.actuatormaster.api.IActuatorMaster;
import org.fortiss.smg.actuatormaster.api.events.DeviceEvent;
import org.fortiss.smg.containermanager.api.ContainerManagerInterface;
import org.fortiss.smg.containermanager.api.ContainerManagerQueueNames;
import org.fortiss.smg.containermanager.api.devices.DeviceContainer;
import org.fortiss.smg.containermanager.api.devices.DeviceId;
import org.fortiss.smg.containermanager.api.devices.SIDeviceType;
import org.fortiss.smg.remoteframework.lib.DefaultProxy;
import org.fortiss.smg.smgschemas.commands.DoubleCommand;
import org.slf4j.LoggerFactory;

public class ActuatorClientImpl implements IActuatorClient {

private static org.slf4j.Logger logger = LoggerFactory
.getLogger(ActuatorClientImpl.class);
private IActuatorMaster master;
private String clientId;
private ScheduledExecutorService executor;
private int pollFrequency;
private String host;
private String wrapperTag;
private ContainerManagerInterface broker;

/*
* Values provided by this component
*/
private Double counsumption = 0.0;
private Double temperature = 20.0;
private Double brightness = 10.0;
private Double battery = 100.0;
private Double switchdevice = 0.0;




ArrayList<DeviceContainer> devices = new ArrayList<DeviceContainer>();

public ActuatorClientImpl(String host, String port, String wrapperTag,
int pollFreq, String username, String password) {

this.host = host;
this.wrapperTag = wrapperTag;
loadStaticDevs(wrapperTag);
this.pollFrequency = pollFreq;


}


public synchronized void activate() {
sendNewDeviceEvents();

executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new DummyLooper(this), 0,
getPollFrequency(), TimeUnit.SECONDS);
}




public synchronized void deactivate() {
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public String doSomething(String s) {
return "Hello smg";
}

@Override
public boolean isComponentAlive() {
/* implement self test */
return true;
}

@Override
public void onDoubleCommand(DoubleCommand command, DeviceId dev) {
logger.info("Command " + command.getValue() + " was sent to "
+ dev.toString());

}


private void sendNewDeviceEvents() {
for (DeviceContainer dev : devices) {
try {
//master.sendDeviceEvent(dev, this.clientId);
logger.info("Try to send " + dev.getDeviceId()
+ " to master");
master.sendDeviceEvent(new DeviceEvent(dev), this.clientId);
logger.info("Sent " + dev.getDeviceId()
+ " to master");


} catch (TimeoutException e) {
logger.debug("Failed to send " + dev.getDeviceId()
+ " to master");
}
}
}

public String getClientId() {
return this.clientId;
}

public List<DeviceContainer> getDeviceSpecs() {
return this.devices;
}

public IActuatorMaster getMaster() {
return this.master;
}

public int getPollFrequency() {
return this.pollFrequency;
}

public String getWrapperTag() {
return this.wrapperTag;
}

public String getHost() {
return this.host;
}

public void setMaster(IActuatorMaster master) {
this.master = master;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public void setPollFrequency(int pollFrequency) {
this.pollFrequency = pollFrequency;
}

public void setWrapperTag(String wrapperTag) {
this.wrapperTag = wrapperTag;
}


private void loadStaticDevs(String wraperTag) {
DefaultProxy<ContainerManagerInterface> clientInfo = new DefaultProxy<ContainerManagerInterface>(
ContainerManagerInterface.class,
ContainerManagerQueueNames.getContainerManagerInterfaceQueryQueue(), 10000);

logger.info("try to init CM interface");
ContainerManagerInterface containerManagerClient = null;
try {
containerManagerClient = clientInfo.init();

/*
* Device Spec from the Solar Log Generator Devices
*/
DeviceContainer dummyConsumptionMeter = new DeviceContainer(
new org.fortiss.smg.containermanager.api.devices.DeviceId(
"dummy_consumption_watt", wrapperTag), wrapperTag
+ ".solar");

dummyConsumptionMeter.setRange(0,Integer.MAX_VALUE, 1);
dummyConsumptionMeter.setHrName("Dummy Consumption [W]");
dummyConsumptionMeter.setDeviceType(SIDeviceType.ConsumptionPowermeter);

DeviceContainer dummyTemp = new DeviceContainer(
new org.fortiss.smg.containermanager.api.devices.DeviceId(
"dummy_temperature", wrapperTag), wrapperTag
, containerManagerClient.getDeviceSpecData(13)
);


DeviceContainer dummyBrightness = new DeviceContainer(
new org.fortiss.smg.containermanager.api.devices.DeviceId(
"dummy_brightness", wrapperTag),
wrapperTag, containerManagerClient.getDeviceSpecData(5)
);

DeviceContainer dummyBattery = new DeviceContainer(
new org.fortiss.smg.containermanager.api.devices.DeviceId(
"dummy_battery", wrapperTag),
wrapperTag, containerManagerClient.getDeviceSpecData(140)
);

DeviceContainer dummySwitch = new DeviceContainer(
new org.fortiss.smg.containermanager.api.devices.DeviceId(
"dummy_switch", wrapperTag),
wrapperTag, containerManagerClient.getDeviceSpecData(145)
);


devices.add(dummyConsumptionMeter);
devices.add(dummyTemp);
devices.add(dummyBrightness);
devices.add(dummyBattery);
devices.add(dummySwitch);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


try {
clientInfo.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public Double getConsumption() {
return counsumption;
}


public void setConsumption(Double counsumption) {
this.counsumption = counsumption;
}



public Double getTemperature() {
return temperature;
}


public void setTemperature(Double temperature) {
this.temperature = temperature;
}


public Double getBrightness() {
return brightness;
}


public void setBrightness(Double brightness) {
this.brightness = brightness;
}


public Double getBattery() {
return battery;
}


public void setBattery(Double battery) {
this.battery = battery;
}


public Double getSwitchdevice() {
return switchdevice;
}


public void setSwitchdevice(Double switchdevice) {
this.switchdevice = switchdevice;
}



}
Loading

0 comments on commit 572cf49

Please sign in to comment.