Fixed merge issues
This commit is contained in:
parent
81a85320a2
commit
09ae1b2667
37 changed files with 22 additions and 24 deletions
|
|
@ -0,0 +1,98 @@
|
|||
package se.hal.plugin.raspberry;
|
||||
|
||||
import se.hal.intf.*;
|
||||
import se.hal.plugin.raspberry.hardware.RPiDS18B20;
|
||||
import se.hal.plugin.raspberry.hardware.RPiInteruptPulseFlankCounter;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RPiController implements HalSensorController {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private HashMap<String, RPiSensor> pinToSensorMap = new HashMap<>();
|
||||
private HalSensorReportListener sensorListener;
|
||||
|
||||
public RPiController(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(HalSensorConfig sensor) {
|
||||
if(sensor instanceof RPiPowerConsumptionSensor){
|
||||
RPiPowerConsumptionSensor powerConsumptionSensor = (RPiPowerConsumptionSensor) sensor;
|
||||
int gpioPin = powerConsumptionSensor.getGpioPin();
|
||||
if(!pinToSensorMap.containsKey("GPIO_"+gpioPin)){
|
||||
RPiInteruptPulseFlankCounter impulseCounter = new RPiInteruptPulseFlankCounter(gpioPin, this);
|
||||
pinToSensorMap.put("GPIO_"+gpioPin, impulseCounter);
|
||||
}else{
|
||||
logger.warning("Cannot create a RPiPowerConsumptionSensor on GPIO pin " + gpioPin + " since is already is in use by another sensor.");
|
||||
}
|
||||
} else if(sensor instanceof RPiTemperatureSensor){
|
||||
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor;
|
||||
String w1Address = temperatureSensor.get1WAddress();
|
||||
if(!pinToSensorMap.containsKey("W1_"+w1Address)){
|
||||
RPiDS18B20 ds12b20 = new RPiDS18B20(w1Address, this);
|
||||
pinToSensorMap.put("W1_"+w1Address, ds12b20);
|
||||
}else{
|
||||
logger.warning("Cannot create a RPi1WireTemperatureSensor on 1-Wire address " + w1Address + " since is already is in use by another sensor.");
|
||||
}
|
||||
}else{
|
||||
logger.warning("Cannot register a non-supported sensor");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(HalSensorConfig sensor) {
|
||||
if(sensor instanceof RPiPowerConsumptionSensor){
|
||||
RPiPowerConsumptionSensor powerConsumprtionSensor = (RPiPowerConsumptionSensor) sensor;
|
||||
RPiSensor sensorToDeregister = pinToSensorMap.remove("GPIO_"+powerConsumprtionSensor.getGpioPin());
|
||||
if(sensorToDeregister != null){
|
||||
sensorToDeregister.close();
|
||||
}
|
||||
} else if(sensor instanceof RPiTemperatureSensor){
|
||||
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) sensor;
|
||||
RPiSensor sensorToDeregister = pinToSensorMap.remove("W1_"+temperatureSensor.get1WAddress());
|
||||
if(sensorToDeregister != null){
|
||||
sensorToDeregister.close();
|
||||
}
|
||||
}else{
|
||||
logger.warning("Cannot deregister a non-supported sensor");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return pinToSensorMap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setListener(HalSensorReportListener listener) {
|
||||
sensorListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
for(String key : this.pinToSensorMap.keySet()){
|
||||
pinToSensorMap.get(key).close();
|
||||
pinToSensorMap.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendDataReport(HalSensorConfig sensorConfig, HalSensorData sensorData){
|
||||
if(sensorListener != null){
|
||||
sensorListener.reportReceived(sensorConfig, sensorData);
|
||||
}else{
|
||||
logger.log(Level.WARNING, "Could not report data. No registered listener");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package se.hal.plugin.raspberry;
|
||||
|
||||
import se.hal.intf.HalSensorConfig;
|
||||
import se.hal.intf.HalSensorController;
|
||||
import se.hal.intf.HalSensorData;
|
||||
import se.hal.struct.devicedata.PowerConsumptionSensorData;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
||||
|
||||
@Configurator.Configurable("GPIO-Pin")
|
||||
private int gpioPin = -1;
|
||||
|
||||
|
||||
public RPiPowerConsumptionSensor(){ } //need to be empty for the framework to create an instance
|
||||
public RPiPowerConsumptionSensor(int gpioPin) {
|
||||
this.gpioPin = gpioPin;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public long getDataInterval(){
|
||||
return 60*1000; // 1 min
|
||||
}
|
||||
|
||||
@Override
|
||||
public AggregationMethod getAggregationMethod() {
|
||||
return AggregationMethod.SUM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalSensorController> getSensorControllerClass() {
|
||||
return RPiController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalSensorData> getSensorDataClass() {
|
||||
return PowerConsumptionSensorData.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof RPiPowerConsumptionSensor)
|
||||
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getGpioPin() {
|
||||
return gpioPin;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "gpioPin:" + gpioPin;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package se.hal.plugin.raspberry;
|
||||
|
||||
public interface RPiSensor {
|
||||
|
||||
void close();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package se.hal.plugin.raspberry;
|
||||
|
||||
import se.hal.intf.HalSensorConfig;
|
||||
import se.hal.intf.HalSensorController;
|
||||
import se.hal.intf.HalSensorData;
|
||||
import se.hal.struct.devicedata.TemperatureSensorData;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
public class RPiTemperatureSensor implements HalSensorConfig {
|
||||
|
||||
@Configurator.Configurable("1-Wire Address")
|
||||
private String w1Address;
|
||||
|
||||
|
||||
|
||||
public RPiTemperatureSensor() { }
|
||||
public RPiTemperatureSensor(String w1Address) {
|
||||
this.w1Address = w1Address;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getDataInterval() {
|
||||
return 10*60*1000; // 10 min
|
||||
}
|
||||
|
||||
@Override
|
||||
public AggregationMethod getAggregationMethod() {
|
||||
return AggregationMethod.AVERAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalSensorController> getSensorControllerClass() {
|
||||
return RPiController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalSensorData> getSensorDataClass() {
|
||||
return TemperatureSensorData.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof RPiTemperatureSensor && w1Address != null)
|
||||
return this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address);
|
||||
return false;
|
||||
}
|
||||
|
||||
public String get1WAddress() {
|
||||
return w1Address;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
package se.hal.plugin.raspberry;
|
||||
|
||||
import com.pi4j.io.gpio.Pin;
|
||||
import com.pi4j.io.gpio.RaspiPin;
|
||||
|
||||
public class RPiUtility {
|
||||
|
||||
public static Pin getPin(int gpioPin){
|
||||
switch(gpioPin){
|
||||
case 0:
|
||||
return RaspiPin.GPIO_00;
|
||||
case 1:
|
||||
return RaspiPin.GPIO_01;
|
||||
case 2:
|
||||
return RaspiPin.GPIO_02;
|
||||
case 3:
|
||||
return RaspiPin.GPIO_03;
|
||||
case 4:
|
||||
return RaspiPin.GPIO_04;
|
||||
case 5:
|
||||
return RaspiPin.GPIO_05;
|
||||
case 6:
|
||||
return RaspiPin.GPIO_06;
|
||||
case 7:
|
||||
//used by 1-wire divices
|
||||
case 8:
|
||||
//used by I2C devices
|
||||
case 9:
|
||||
//used by I2C devices
|
||||
case 10:
|
||||
//used by SPI devices
|
||||
case 11:
|
||||
//used by SPI devices
|
||||
case 12:
|
||||
//used by SPI devices
|
||||
case 13:
|
||||
//used by SPI devices
|
||||
case 14:
|
||||
//used by SPI devices
|
||||
case 15:
|
||||
//used by Serial devices
|
||||
case 16:
|
||||
//used by Serial devices
|
||||
case 17:
|
||||
//reserved for future use
|
||||
case 18:
|
||||
//reserved for future use
|
||||
case 19:
|
||||
//reserved for future use
|
||||
case 20:
|
||||
//reserved for future use
|
||||
case 21:
|
||||
//reserved for future use
|
||||
case 22:
|
||||
//reserved for future use
|
||||
case 23:
|
||||
//reserved for future use
|
||||
case 24:
|
||||
//reserved for future use
|
||||
case 25:
|
||||
//reserved for future use
|
||||
case 26:
|
||||
//reserved for future use
|
||||
case 27:
|
||||
//reserved for future use
|
||||
case 28:
|
||||
//reserved for future use
|
||||
case 29:
|
||||
//reserved for future use
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package se.hal.plugin.raspberry.hardware;
|
||||
|
||||
import com.pi4j.component.temperature.TemperatureSensor;
|
||||
import com.pi4j.io.w1.W1Master;
|
||||
import com.pi4j.temperature.TemperatureScale;
|
||||
import se.hal.plugin.raspberry.RPiController;
|
||||
import se.hal.plugin.raspberry.RPiSensor;
|
||||
import se.hal.plugin.raspberry.RPiTemperatureSensor;
|
||||
import se.hal.struct.devicedata.TemperatureSensorData;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RPiDS18B20 implements RPiSensor, Runnable {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private final String DEGREE_SIGN = "\u00b0";
|
||||
|
||||
private RPiController controller;
|
||||
private String w1Address;
|
||||
private ScheduledExecutorService scheduler;
|
||||
private W1Master w1Mater;
|
||||
|
||||
public RPiDS18B20(String w1Address, RPiController controller){
|
||||
this.controller = controller;
|
||||
this.w1Address = w1Address;
|
||||
|
||||
scheduler = Executors.newScheduledThreadPool(1);
|
||||
|
||||
w1Mater = new W1Master();
|
||||
|
||||
//print out all sensors found
|
||||
for(TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)){
|
||||
logger.info(String.format("1-Wire temperature sensor divice found: %-20s: %3.1f"+DEGREE_SIGN+"C\n", device.getName(), device.getTemperature(TemperatureScale.CELSIUS)));
|
||||
}
|
||||
|
||||
//schedule job
|
||||
scheduler.scheduleAtFixedRate(this, 10, 60, TimeUnit.SECONDS); //wait 10s and run every 60s
|
||||
|
||||
}
|
||||
|
||||
public void close() {
|
||||
scheduler.shutdown();
|
||||
try {
|
||||
scheduler.awaitTermination(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
//noop
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for(TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)){
|
||||
if(device.getName().equals(w1Address)){
|
||||
controller.sendDataReport(
|
||||
new RPiTemperatureSensor(w1Address),
|
||||
new TemperatureSensorData(
|
||||
device.getTemperature(TemperatureScale.CELSIUS),
|
||||
System.currentTimeMillis()
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
package se.hal.plugin.raspberry.hardware;
|
||||
|
||||
import com.pi4j.io.gpio.*;
|
||||
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
|
||||
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
||||
import com.pi4j.wiringpi.GpioUtil;
|
||||
import se.hal.plugin.raspberry.RPiController;
|
||||
import se.hal.plugin.raspberry.RPiPowerConsumptionSensor;
|
||||
import se.hal.plugin.raspberry.RPiSensor;
|
||||
import se.hal.plugin.raspberry.RPiUtility;
|
||||
import se.hal.struct.devicedata.PowerConsumptionSensorData;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor {
|
||||
private static final int REPORT_TIMEOUT = 60_000; //one minute
|
||||
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
private RPiController controller;
|
||||
private ExecutorService executorPool;
|
||||
private long nanoSecondsSleep = REPORT_TIMEOUT * 1_000_000L;
|
||||
private volatile Integer impulseCount = 0;
|
||||
private GpioPinDigitalInput irLightSensor;
|
||||
private final int gpioPin;
|
||||
|
||||
public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){
|
||||
this.controller = controller;
|
||||
this.gpioPin = gpioPin;
|
||||
|
||||
// setup a thread pool for executing jobs
|
||||
this.executorPool = Executors.newCachedThreadPool();
|
||||
|
||||
//Enable non privileged access to the GPIO pins (no sudo required from now)
|
||||
GpioUtil.enableNonPrivilegedAccess();
|
||||
|
||||
// create gpio controller
|
||||
GpioController gpio = null;
|
||||
try{
|
||||
gpio = GpioFactory.getInstance();
|
||||
}catch(IllegalArgumentException e){
|
||||
logger.log(Level.SEVERE, "", e);
|
||||
throw e;
|
||||
}catch(UnsatisfiedLinkError e){
|
||||
logger.log(Level.SEVERE, "", e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// provision gpio pin as an input pin with its internal pull up resistor enabled
|
||||
irLightSensor = gpio.provisionDigitalInputPin(RPiUtility.getPin(gpioPin), PinPullResistance.PULL_UP);
|
||||
|
||||
// create and register gpio pin listener. May require the program to be run as sudo if the GPIO pin has not been exported
|
||||
irLightSensor.addListener(this);
|
||||
|
||||
//start a daemon thread to save the impulse count every minute
|
||||
Thread thread = new Thread(this);
|
||||
thread.setDaemon(false);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
irLightSensor.removeListener(this);
|
||||
executorPool.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* GpioPinListenerDigital interface
|
||||
*/
|
||||
@Override
|
||||
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
|
||||
if(event.getState() == PinState.LOW){ //low = light went on
|
||||
//System.out.println("IR LED turned ON");
|
||||
//logger.log(Level.INFO, "IR LED turned on");
|
||||
synchronized(impulseCount){
|
||||
impulseCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long startTime = System.nanoTime();
|
||||
synchronized(impulseCount){
|
||||
impulseCount = 0; //reset the impulse count
|
||||
}
|
||||
while(!executorPool.isShutdown()) {
|
||||
sleepNano(nanoSecondsSleep); //sleep for some time. This variable will be modified every loop to compensate for the loop time spent.
|
||||
int count = -1;
|
||||
synchronized(impulseCount){
|
||||
count = impulseCount;
|
||||
impulseCount = 0;
|
||||
}
|
||||
save(System.currentTimeMillis(), count); //save the impulse count
|
||||
long estimatedNanoTimeSpent = System.nanoTime() - startTime; //this is where the loop ends
|
||||
startTime = System.nanoTime(); //this is where the loop starts from now on
|
||||
if(estimatedNanoTimeSpent > 0){ //if no overflow
|
||||
long nanoSecondsTooMany = estimatedNanoTimeSpent - (REPORT_TIMEOUT*1000000L);
|
||||
//System.out.println("the look took ~" + estimatedNanoTimeSpent + "ns. That is " + nanoSecondsTooMany/1000000L + "ms off");
|
||||
nanoSecondsSleep -= nanoSecondsTooMany / 3; //divide by constant to take into account varaiations im loop time
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep for [ns] nanoseconds
|
||||
* @param ns
|
||||
*/
|
||||
private void sleepNano(long ns){
|
||||
//System.out.println("will go to sleep for " + ns + "ns");
|
||||
try{
|
||||
Thread.sleep(ns/1000000L, (int)(ns%1000000L));
|
||||
}catch(InterruptedException e){
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the data to the database.
|
||||
* This method should block the caller as short time as possible.
|
||||
* This method should try block the same amount of time every time it is called.
|
||||
* Try to make the time spent in the method the same for every call (low variation).
|
||||
*
|
||||
* @param timestamp_end
|
||||
* @param data
|
||||
*/
|
||||
private void save(final long timestamp_end, final int data){
|
||||
//offload the timed loop by not doing the db interaction in this thread.
|
||||
executorPool.execute(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
|
||||
controller.sendDataReport(
|
||||
new RPiPowerConsumptionSensor(gpioPin),
|
||||
new PowerConsumptionSensorData(
|
||||
timestamp_end, data
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"version": 1.1,
|
||||
"name": "Raspberry Pi Sensors",
|
||||
"interfaces": [
|
||||
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.raspberry.RPiPowerConsumptionSensor"},
|
||||
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.raspberry.RPiTemperatureSensor"}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue