From eab6c72800534ee7fe51c8f6825aee8ed770c34f Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 29 Aug 2017 15:46:29 +0200 Subject: [PATCH] Moved raspberry pi plugin into its own plugin folder --- Hal.iml | 12 + build.xml | 212 +++++++++--------- plugins/raspberry/build.xml | 23 ++ .../raspberry/lib}/pi4j-core-1.1.jar | Bin .../raspberry/lib}/pi4j-device-1.1.jar | Bin .../lib}/pi4j-gpio-extension-1.1.jar | Bin .../raspberry/lib}/pi4j-service-1.1.jar | Bin .../raspberry/lib}/pi4j.LICENSE.txt | 0 .../raspberry/lib}/pi4j.NOTICE.txt | 0 .../hal/plugin/raspberry/RPiController.java | 98 ++++++++ .../raspberry/RPiPowerConsumptionSensor.java | 26 +-- .../se/hal/plugin/raspberry/RPiSensor.java | 2 +- .../raspberry/RPiTemperatureSensor.java | 16 +- .../se/hal/plugin/raspberry/RPiUtility.java | 75 +++++++ .../plugin/raspberry/hardware/RPiDS18B20.java | 68 ++++++ .../RPiInteruptPulseFlankCounter.java | 118 +++++----- .../src/se/hal/plugin/raspberry/plugin.json | 8 + .../hal/plugin/raspberry/RPiController.java | 98 -------- src/se/hal/plugin/raspberry/RPiUtility.java | 75 ------- .../plugin/raspberry/hardware/RPiDS18B20.java | 68 ------ src/se/hal/plugin/raspberry/plugin.json | 8 - 21 files changed, 471 insertions(+), 436 deletions(-) create mode 100755 plugins/raspberry/build.xml rename {lib => plugins/raspberry/lib}/pi4j-core-1.1.jar (100%) rename {lib => plugins/raspberry/lib}/pi4j-device-1.1.jar (100%) rename {lib => plugins/raspberry/lib}/pi4j-gpio-extension-1.1.jar (100%) rename {lib => plugins/raspberry/lib}/pi4j-service-1.1.jar (100%) rename {lib => plugins/raspberry/lib}/pi4j.LICENSE.txt (100%) rename {lib => plugins/raspberry/lib}/pi4j.NOTICE.txt (100%) create mode 100755 plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java rename {src => plugins/raspberry/src}/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java (68%) rename {src => plugins/raspberry/src}/se/hal/plugin/raspberry/RPiSensor.java (78%) mode change 100644 => 100755 rename {src => plugins/raspberry/src}/se/hal/plugin/raspberry/RPiTemperatureSensor.java (77%) create mode 100755 plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java create mode 100755 plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java rename {src => plugins/raspberry/src}/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java (58%) create mode 100755 plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json delete mode 100755 src/se/hal/plugin/raspberry/RPiController.java delete mode 100644 src/se/hal/plugin/raspberry/RPiUtility.java delete mode 100755 src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java delete mode 100755 src/se/hal/plugin/raspberry/plugin.json diff --git a/Hal.iml b/Hal.iml index fafa66f7..9a7d7c89 100755 --- a/Hal.iml +++ b/Hal.iml @@ -9,7 +9,9 @@ + + @@ -49,6 +51,16 @@ + + + + + + + + + + diff --git a/build.xml b/build.xml index 007bd074..9a9e8047 100755 --- a/build.xml +++ b/build.xml @@ -1,31 +1,31 @@ - + - - - - - + + + + + - - - - - + + + + + - + - - - - + + + + - - + + @@ -53,111 +53,111 @@ - - - - - - + + + + + + - - - - - - + + + + + + - - - - - + + + + + - + - - - - - + + + + + - + - + - - - - - - - - - - - + + + + + + + + + + + - - - - + + + + - - - - - + + + + + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + + @@ -170,19 +170,19 @@ - + - + - - - - + + + + - - - - - + + + + + diff --git a/plugins/raspberry/build.xml b/plugins/raspberry/build.xml new file mode 100755 index 00000000..7b72d214 --- /dev/null +++ b/plugins/raspberry/build.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/pi4j-core-1.1.jar b/plugins/raspberry/lib/pi4j-core-1.1.jar similarity index 100% rename from lib/pi4j-core-1.1.jar rename to plugins/raspberry/lib/pi4j-core-1.1.jar diff --git a/lib/pi4j-device-1.1.jar b/plugins/raspberry/lib/pi4j-device-1.1.jar similarity index 100% rename from lib/pi4j-device-1.1.jar rename to plugins/raspberry/lib/pi4j-device-1.1.jar diff --git a/lib/pi4j-gpio-extension-1.1.jar b/plugins/raspberry/lib/pi4j-gpio-extension-1.1.jar similarity index 100% rename from lib/pi4j-gpio-extension-1.1.jar rename to plugins/raspberry/lib/pi4j-gpio-extension-1.1.jar diff --git a/lib/pi4j-service-1.1.jar b/plugins/raspberry/lib/pi4j-service-1.1.jar similarity index 100% rename from lib/pi4j-service-1.1.jar rename to plugins/raspberry/lib/pi4j-service-1.1.jar diff --git a/lib/pi4j.LICENSE.txt b/plugins/raspberry/lib/pi4j.LICENSE.txt similarity index 100% rename from lib/pi4j.LICENSE.txt rename to plugins/raspberry/lib/pi4j.LICENSE.txt diff --git a/lib/pi4j.NOTICE.txt b/plugins/raspberry/lib/pi4j.NOTICE.txt similarity index 100% rename from lib/pi4j.NOTICE.txt rename to plugins/raspberry/lib/pi4j.NOTICE.txt diff --git a/plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java new file mode 100755 index 00000000..49473c30 --- /dev/null +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java @@ -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 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"); + } + } + +} diff --git a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java similarity index 68% rename from src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java rename to plugins/raspberry/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java index da0cdb50..58b7a988 100755 --- a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java @@ -7,15 +7,15 @@ import se.hal.struct.devicedata.PowerConsumptionSensorData; import zutil.ui.Configurator; public class RPiPowerConsumptionSensor implements HalSensorConfig { - - @Configurator.Configurable("GPIO-Pin") + + @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; - } + + public RPiPowerConsumptionSensor(){ } //need to be empty for the framework to create an instance + public RPiPowerConsumptionSensor(int gpioPin) { + this.gpioPin = gpioPin; + } @@ -41,14 +41,14 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig { @Override public boolean equals(Object obj){ - if(obj instanceof RPiPowerConsumptionSensor) - return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin; - return false; + if(obj instanceof RPiPowerConsumptionSensor) + return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin; + return false; } - public int getGpioPin() { - return gpioPin; - } + public int getGpioPin() { + return gpioPin; + } public String toString(){ return "gpioPin:" + gpioPin; diff --git a/src/se/hal/plugin/raspberry/RPiSensor.java b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiSensor.java old mode 100644 new mode 100755 similarity index 78% rename from src/se/hal/plugin/raspberry/RPiSensor.java rename to plugins/raspberry/src/se/hal/plugin/raspberry/RPiSensor.java index 3d6a14f8..c8351aaf --- a/src/se/hal/plugin/raspberry/RPiSensor.java +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiSensor.java @@ -2,6 +2,6 @@ package se.hal.plugin.raspberry; public interface RPiSensor { - void close(); + void close(); } diff --git a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java similarity index 77% rename from src/se/hal/plugin/raspberry/RPiTemperatureSensor.java rename to plugins/raspberry/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java index 1bcdaef1..48bfa808 100755 --- a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java @@ -7,8 +7,8 @@ import se.hal.struct.devicedata.TemperatureSensorData; import zutil.ui.Configurator; public class RPiTemperatureSensor implements HalSensorConfig { - - @Configurator.Configurable("1-Wire Address") + + @Configurator.Configurable("1-Wire Address") private String w1Address; @@ -41,13 +41,13 @@ public class RPiTemperatureSensor implements HalSensorConfig { @Override public boolean equals(Object obj){ - if(obj instanceof RPiTemperatureSensor && w1Address != null) - return this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address); - return false; + if(obj instanceof RPiTemperatureSensor && w1Address != null) + return this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address); + return false; } - public String get1WAddress() { - return w1Address; - } + public String get1WAddress() { + return w1Address; + } } diff --git a/plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java new file mode 100755 index 00000000..293344bb --- /dev/null +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java @@ -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; + } + } + +} diff --git a/plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java b/plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java new file mode 100755 index 00000000..8bed8668 --- /dev/null +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java @@ -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; + } + } + } + +} diff --git a/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java b/plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java similarity index 58% rename from src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java rename to plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java index 967ca561..94172258 100755 --- a/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiInteruptPulseFlankCounter.java @@ -17,74 +17,74 @@ 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 + 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(); + //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 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 + //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(); + irLightSensor.removeListener(this); + executorPool.shutdown(); } /** * GpioPinListenerDigital interface */ @Override - public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { - if(event.getState() == PinState.LOW){ //low = light went on + 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){ + @Override + public void run() { + long startTime = System.nanoTime(); + synchronized(impulseCount){ impulseCount = 0; //reset the impulse count } while(!executorPool.isShutdown()) { @@ -103,19 +103,19 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi 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 - } + try{ + Thread.sleep(ns/1000000L, (int)(ns%1000000L)); + }catch(InterruptedException e){ + //ignore + } } /** @@ -128,18 +128,18 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi * @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), + //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 )); - } - }); + } + }); } } diff --git a/plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json b/plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json new file mode 100755 index 00000000..c6827be1 --- /dev/null +++ b/plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json @@ -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"} + ] +} \ No newline at end of file diff --git a/src/se/hal/plugin/raspberry/RPiController.java b/src/se/hal/plugin/raspberry/RPiController.java deleted file mode 100755 index 55e589f7..00000000 --- a/src/se/hal/plugin/raspberry/RPiController.java +++ /dev/null @@ -1,98 +0,0 @@ -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 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"); - } - } - -} diff --git a/src/se/hal/plugin/raspberry/RPiUtility.java b/src/se/hal/plugin/raspberry/RPiUtility.java deleted file mode 100644 index 23edff66..00000000 --- a/src/se/hal/plugin/raspberry/RPiUtility.java +++ /dev/null @@ -1,75 +0,0 @@ -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; - } - } - -} diff --git a/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java b/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java deleted file mode 100755 index 43039649..00000000 --- a/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java +++ /dev/null @@ -1,68 +0,0 @@ -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; - } - } - } - -} diff --git a/src/se/hal/plugin/raspberry/plugin.json b/src/se/hal/plugin/raspberry/plugin.json deleted file mode 100755 index 41210dc8..00000000 --- a/src/se/hal/plugin/raspberry/plugin.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "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"} - ] -} \ No newline at end of file