Formatting cleanup
This commit is contained in:
parent
c7e286f51e
commit
5a1be3c2e4
50 changed files with 283 additions and 282 deletions
|
|
@ -26,44 +26,44 @@ public class RPiController implements HalSensorController {
|
|||
|
||||
@Override
|
||||
public void register(HalDeviceConfig deviceConfig) {
|
||||
if(deviceConfig instanceof RPiPowerConsumptionSensor){
|
||||
if (deviceConfig instanceof RPiPowerConsumptionSensor){
|
||||
RPiPowerConsumptionSensor powerConsumptionSensor = (RPiPowerConsumptionSensor) deviceConfig;
|
||||
int gpioPin = powerConsumptionSensor.getGpioPin();
|
||||
if(!pinToSensorMap.containsKey("GPIO_"+gpioPin)){
|
||||
if (!pinToSensorMap.containsKey("GPIO_"+gpioPin)){
|
||||
RPiInteruptPulseFlankCounter impulseCounter = new RPiInteruptPulseFlankCounter(gpioPin, this);
|
||||
pinToSensorMap.put("GPIO_"+gpioPin, impulseCounter);
|
||||
}else{
|
||||
} else {
|
||||
logger.warning("Cannot create a RPiPowerConsumptionSensor on GPIO pin " + gpioPin + " since is already is in use by another sensor.");
|
||||
}
|
||||
} else if(deviceConfig instanceof RPiTemperatureSensor){
|
||||
} else if (deviceConfig instanceof RPiTemperatureSensor){
|
||||
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) deviceConfig;
|
||||
String w1Address = temperatureSensor.get1WAddress();
|
||||
if(!pinToSensorMap.containsKey("W1_"+w1Address)){
|
||||
if (!pinToSensorMap.containsKey("W1_"+w1Address)){
|
||||
RPiDS18B20 ds12b20 = new RPiDS18B20(w1Address, this);
|
||||
pinToSensorMap.put("W1_"+w1Address, ds12b20);
|
||||
}else{
|
||||
} else {
|
||||
logger.warning("Cannot create a RPi1WireTemperatureSensor on 1-Wire address " + w1Address + " since is already is in use by another sensor.");
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
logger.warning("Cannot register a non-supported sensor");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(HalDeviceConfig deviceConfig) {
|
||||
if(deviceConfig instanceof RPiPowerConsumptionSensor){
|
||||
if (deviceConfig instanceof RPiPowerConsumptionSensor){
|
||||
RPiPowerConsumptionSensor powerConsumptionSensor = (RPiPowerConsumptionSensor) deviceConfig;
|
||||
RPiSensor sensorToDeregister = pinToSensorMap.remove("GPIO_"+powerConsumptionSensor.getGpioPin());
|
||||
if(sensorToDeregister != null){
|
||||
if (sensorToDeregister != null){
|
||||
sensorToDeregister.close();
|
||||
}
|
||||
} else if(deviceConfig instanceof RPiTemperatureSensor){
|
||||
} else if (deviceConfig instanceof RPiTemperatureSensor){
|
||||
RPiTemperatureSensor temperatureSensor = (RPiTemperatureSensor) deviceConfig;
|
||||
RPiSensor sensorToDeregister = pinToSensorMap.remove("W1_"+temperatureSensor.get1WAddress());
|
||||
if(sensorToDeregister != null){
|
||||
if (sensorToDeregister != null){
|
||||
sensorToDeregister.close();
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
logger.warning("Cannot deregister a non-supported sensor");
|
||||
return;
|
||||
}
|
||||
|
|
@ -81,16 +81,16 @@ public class RPiController implements HalSensorController {
|
|||
|
||||
@Override
|
||||
public void close() {
|
||||
for(String key : this.pinToSensorMap.keySet()){
|
||||
for (String key : this.pinToSensorMap.keySet()){
|
||||
pinToSensorMap.get(key).close();
|
||||
pinToSensorMap.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendDataReport(HalSensorConfig sensorConfig, HalSensorData sensorData){
|
||||
if(sensorListener != null){
|
||||
if (sensorListener != null){
|
||||
sensorListener.reportReceived(sensorConfig, sensorData);
|
||||
}else{
|
||||
} else {
|
||||
logger.log(Level.WARNING, "Could not report data. No registered listener");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
private int gpioPin = -1;
|
||||
|
||||
|
||||
public RPiPowerConsumptionSensor(){ } //need to be empty for the framework to create an instance
|
||||
public RPiPowerConsumptionSensor() { } //need to be empty for the framework to create an instance
|
||||
public RPiPowerConsumptionSensor(int gpioPin) {
|
||||
this.gpioPin = gpioPin;
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
|
||||
|
||||
@Override
|
||||
public long getDataInterval(){
|
||||
public long getDataInterval() {
|
||||
return 60*1000; // 1 min
|
||||
}
|
||||
|
||||
|
|
@ -41,8 +41,8 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof RPiPowerConsumptionSensor)
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof RPiPowerConsumptionSensor)
|
||||
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
||||
return false;
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
return gpioPin;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
return "gpioPin:" + gpioPin;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ public class RPiTemperatureSensor implements HalSensorConfig {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof RPiTemperatureSensor && w1Address != null)
|
||||
if (obj instanceof RPiTemperatureSensor && w1Address != null)
|
||||
return this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address);
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ import com.pi4j.io.gpio.RaspiPin;
|
|||
|
||||
public class RPiUtility {
|
||||
|
||||
public static Pin getPin(int gpioPin){
|
||||
switch(gpioPin){
|
||||
public static Pin getPin(int gpioPin) {
|
||||
switch(gpioPin) {
|
||||
case 0:
|
||||
return RaspiPin.GPIO_00;
|
||||
case 1:
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class RPiDS18B20 implements RPiSensor, Runnable {
|
||||
public class RPiDS18B20 implements RPiSensor, Runnable {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private final String DEGREE_SIGN = "\u00b0";
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
private ScheduledExecutorService scheduler;
|
||||
private W1Master w1Mater;
|
||||
|
||||
public RPiDS18B20(String w1Address, RPiController controller){
|
||||
public RPiDS18B20(String w1Address, RPiController controller) {
|
||||
this.controller = controller;
|
||||
this.w1Address = w1Address;
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
w1Mater = new W1Master();
|
||||
|
||||
//print out all sensors found
|
||||
for(TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)){
|
||||
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)));
|
||||
}
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
scheduler.scheduleAtFixedRate(this, 10, 60, TimeUnit.SECONDS); //wait 10s and run every 60s
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
scheduler.shutdown();
|
||||
try {
|
||||
|
|
@ -52,8 +52,8 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
for(TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)){
|
||||
if(device.getName().equals(w1Address)){
|
||||
for (TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)) {
|
||||
if (device.getName().equals(w1Address)) {
|
||||
controller.sendDataReport(
|
||||
new RPiTemperatureSensor(w1Address),
|
||||
new TemperatureSensorData(
|
||||
|
|
@ -64,5 +64,5 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
private GpioPinDigitalInput irLightSensor;
|
||||
private final int gpioPin;
|
||||
|
||||
public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){
|
||||
public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller) {
|
||||
this.controller = controller;
|
||||
this.gpioPin = gpioPin;
|
||||
|
||||
|
|
@ -42,10 +42,10 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
GpioController gpio = null;
|
||||
try{
|
||||
gpio = GpioFactory.getInstance();
|
||||
}catch(IllegalArgumentException e){
|
||||
}catch(IllegalArgumentException e) {
|
||||
logger.log(Level.SEVERE, "", e);
|
||||
throw e;
|
||||
}catch(UnsatisfiedLinkError e){
|
||||
}catch(UnsatisfiedLinkError e) {
|
||||
logger.log(Level.SEVERE, "", e);
|
||||
throw e;
|
||||
}
|
||||
|
|
@ -55,49 +55,49 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
|
||||
// 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
|
||||
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){
|
||||
synchronized(impulseCount) {
|
||||
impulseCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long startTime = System.nanoTime();
|
||||
synchronized(impulseCount){
|
||||
synchronized(impulseCount) {
|
||||
impulseCount = 0; //reset the impulse count
|
||||
}
|
||||
while(!executorPool.isShutdown()) {
|
||||
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){
|
||||
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
|
||||
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
|
||||
|
|
@ -109,27 +109,27 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
* Sleep for [ns] nanoseconds
|
||||
* @param ns
|
||||
*/
|
||||
private void sleepNano(long 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){
|
||||
}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).
|
||||
*
|
||||
* 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){
|
||||
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(){
|
||||
executorPool.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
|
||||
|
|
@ -141,5 +141,5 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue