Moved raspberry pi plugin into its own plugin folder
This commit is contained in:
parent
26ed8584c8
commit
eab6c72800
21 changed files with 471 additions and 436 deletions
12
Hal.iml
12
Hal.iml
|
|
@ -9,7 +9,9 @@
|
||||||
<sourceFolder url="file://$MODULE_DIR$/plugins/zwave/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/plugins/zwave/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/plugins/tellstick/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/plugins/tellstick/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/plugins/tellstick/test" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/plugins/tellstick/test" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/plugins/raspberry/src" isTestSource="false" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/plugins/raspberry/build" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/plugins/tellstick/build" />
|
<excludeFolder url="file://$MODULE_DIR$/plugins/tellstick/build" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/plugins/zwave/build" />
|
<excludeFolder url="file://$MODULE_DIR$/plugins/zwave/build" />
|
||||||
</content>
|
</content>
|
||||||
|
|
@ -49,6 +51,16 @@
|
||||||
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
|
<jarDirectory url="file://$MODULE_DIR$/lib" recursive="false" />
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="lib Raspberry">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="file://$MODULE_DIR$/plugins/raspberry/lib" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
<jarDirectory url="file://$MODULE_DIR$/plugins/raspberry/lib" recursive="false" />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
<orderEntry type="module-library" scope="TEST">
|
<orderEntry type="module-library" scope="TEST">
|
||||||
<library name="JUnit4">
|
<library name="JUnit4">
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
|
|
|
||||||
212
build.xml
212
build.xml
|
|
@ -1,31 +1,31 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project name="Hal" >
|
<project name="Hal" >
|
||||||
|
|
||||||
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
|
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
|
||||||
|
|
||||||
<!--common properties-->
|
<!--common properties-->
|
||||||
<property name="root" value="." />
|
<property name="root" value="." />
|
||||||
<property name="srcDir" value="${root}/src" />
|
<property name="srcDir" value="${root}/src" />
|
||||||
<property name="testDir" value="${root}/test" />
|
<property name="testDir" value="${root}/test" />
|
||||||
<property name="libDir" value="${root}/lib" />
|
<property name="libDir" value="${root}/lib" />
|
||||||
|
|
||||||
<property name="buildRoot" value="${root}/build" />
|
<property name="buildRoot" value="${root}/build" />
|
||||||
<property name="compileDir" value="${buildRoot}/production" />
|
<property name="compileDir" value="${buildRoot}/production" />
|
||||||
<property name="compileTestDir" value="${buildRoot}/test" />
|
<property name="compileTestDir" value="${buildRoot}/test" />
|
||||||
<property name="releaseDir" value="${buildRoot}/release" />
|
<property name="releaseDir" value="${buildRoot}/release" />
|
||||||
<property name="reportsDir" value="${buildRoot}/reports" />
|
<property name="reportsDir" value="${buildRoot}/reports" />
|
||||||
<property name="releaseJar" value="hal.jar" />
|
<property name="releaseJar" value="hal.jar" />
|
||||||
|
|
||||||
|
|
||||||
<!-- ________________________ CLASSPATH ________________________ -->
|
<!-- ________________________ CLASSPATH ________________________ -->
|
||||||
|
|
||||||
<!--define standard arguments for javac-->
|
<!--define standard arguments for javac-->
|
||||||
<presetdef name="javac">
|
<presetdef name="javac">
|
||||||
<javac includeantruntime="false" />
|
<javac includeantruntime="false" />
|
||||||
</presetdef>
|
</presetdef>
|
||||||
|
|
||||||
<!--classpath included when building-->
|
<!--classpath included when building-->
|
||||||
<path id="classpath.build">
|
<path id="classpath.build">
|
||||||
<fileset dir="${libDir}">
|
<fileset dir="${libDir}">
|
||||||
<include name="**/*.jar"/>
|
<include name="**/*.jar"/>
|
||||||
</fileset>
|
</fileset>
|
||||||
|
|
@ -53,111 +53,111 @@
|
||||||
<target name="debug-remote" depends="package">
|
<target name="debug-remote" depends="package">
|
||||||
<java fork="true" failonerror="true" classname="se.hal.HalServer">
|
<java fork="true" failonerror="true" classname="se.hal.HalServer">
|
||||||
<jvmarg value="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" />
|
<jvmarg value="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" />
|
||||||
<classpath>
|
<classpath>
|
||||||
<pathelement path="${releaseDir}/${releaseJar}"/>
|
<pathelement path="${releaseDir}/${releaseJar}"/>
|
||||||
<pathelement path="${libDir}/*"/> <!--wildcard may not be platform independent, ok?-->
|
<pathelement path="${libDir}/*"/> <!--wildcard may not be platform independent, ok?-->
|
||||||
</classpath>
|
</classpath>
|
||||||
</java>
|
</java>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<!-- Test targets -->
|
<!-- Test targets -->
|
||||||
<target name="test-all" depends="test,test-plugins" />
|
<target name="test-all" depends="test,test-plugins" />
|
||||||
|
|
||||||
<target name="test" depends="build-test">
|
<target name="test" depends="build-test">
|
||||||
<mkdir dir="${reportsDir}" />
|
<mkdir dir="${reportsDir}" />
|
||||||
<junit printsummary="yes" haltonfailure="false" fork="true">
|
<junit printsummary="yes" haltonfailure="false" fork="true">
|
||||||
<classpath refid="classpath.test" />
|
<classpath refid="classpath.test" />
|
||||||
<formatter type="plain" usefile="false" /> <!-- to screen -->
|
<formatter type="plain" usefile="false" /> <!-- to screen -->
|
||||||
<formatter type="xml" /> <!-- to file -->
|
<formatter type="xml" /> <!-- to file -->
|
||||||
|
|
||||||
<batchtest todir="${reportsDir}" skipNonTests="true">
|
<batchtest todir="${reportsDir}" skipNonTests="true">
|
||||||
<fileset dir="${compileTestDir}" includes="**/*Test*.class" />
|
<fileset dir="${compileTestDir}" includes="**/*Test*.class" />
|
||||||
</batchtest>
|
</batchtest>
|
||||||
</junit>
|
</junit>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="test-plugins" depends="build-test">
|
<target name="test-plugins" depends="build-test">
|
||||||
<subant target="test" verbose="true">
|
<subant target="test" verbose="true">
|
||||||
<fileset dir="plugins/" includes="*/build.xml"/>
|
<fileset dir="plugins/" includes="*/build.xml"/>
|
||||||
</subant>
|
</subant>
|
||||||
</target>
|
</target>
|
||||||
<!-- ________________________ BUILD TARGETS ________________________ -->
|
<!-- ________________________ BUILD TARGETS ________________________ -->
|
||||||
|
|
||||||
<!-- clean all build paths -->
|
<!-- clean all build paths -->
|
||||||
<target name="clean">
|
<target name="clean">
|
||||||
<delete includeemptydirs="true" failonerror="false">
|
<delete includeemptydirs="true" failonerror="false">
|
||||||
<fileset dir="${buildRoot}" includes="**/*"/>
|
<fileset dir="${buildRoot}" includes="**/*"/>
|
||||||
</delete>
|
</delete>
|
||||||
<!-- clean plugins -->
|
<!-- clean plugins -->
|
||||||
<subant target="clean" verbose="true">
|
<subant target="clean" verbose="true">
|
||||||
<fileset dir="plugins/" includes="*/build.xml"/>
|
<fileset dir="plugins/" includes="*/build.xml"/>
|
||||||
</subant>
|
</subant>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
|
|
||||||
<!-- build product code -->
|
<!-- build product code -->
|
||||||
<target name="build-all" depends="build,build-plugins" />
|
<target name="build-all" depends="build,build-plugins" />
|
||||||
|
|
||||||
<target name="build" depends="build-dependencies">
|
<target name="build" depends="build-dependencies">
|
||||||
<mkdir dir="${compileDir}" />
|
<mkdir dir="${compileDir}" />
|
||||||
<javac srcdir="${srcDir}" destdir="${compileDir}" debug="yes" debugLevel="lines,vars,source" fork="yes">
|
<javac srcdir="${srcDir}" destdir="${compileDir}" debug="yes" debugLevel="lines,vars,source" fork="yes">
|
||||||
<classpath refid="classpath.build" />
|
<classpath refid="classpath.build" />
|
||||||
<include name="**/*.java" />
|
<include name="**/*.java" />
|
||||||
<exclude name="se/hal/tts/GoogleTTSClient.java" />
|
<exclude name="se/hal/tts/GoogleTTSClient.java" />
|
||||||
</javac>
|
</javac>
|
||||||
<copy todir="${compileDir}">
|
<copy todir="${compileDir}">
|
||||||
<fileset dir="${srcDir}"
|
<fileset dir="${srcDir}"
|
||||||
excludes="**/*.java" />
|
excludes="**/*.java" />
|
||||||
</copy>
|
</copy>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="build-dependencies">
|
<target name="build-dependencies">
|
||||||
<mkdir dir="${compileDir}" />
|
<mkdir dir="${compileDir}" />
|
||||||
<get src="http://ci.koc.se/jenkins/job/Zutil/147/artifact/build/release/Zutil.jar"
|
<get src="http://ci.koc.se/jenkins/job/Zutil/147/artifact/build/release/Zutil.jar"
|
||||||
dest="${libDir}" verbose="true" usetimestamp="true"/>
|
dest="${libDir}" verbose="true" usetimestamp="true"/>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="build-plugins" depends="build">
|
<target name="build-plugins" depends="build">
|
||||||
<subant target="build" verbose="true">
|
<subant target="build" verbose="true">
|
||||||
<fileset dir="plugins/" includes="*/build.xml"/>
|
<fileset dir="plugins/" includes="*/build.xml"/>
|
||||||
</subant>
|
</subant>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="build-test" depends="build">
|
<target name="build-test" depends="build">
|
||||||
<mkdir dir="${compileTestDir}" />
|
<mkdir dir="${compileTestDir}" />
|
||||||
<javac srcdir="${testDir}" destdir="${compileTestDir}" debug="yes" debugLevel="lines,vars,source" fork="yes">
|
<javac srcdir="${testDir}" destdir="${compileTestDir}" debug="yes" debugLevel="lines,vars,source" fork="yes">
|
||||||
<classpath refid="classpath.test" />
|
<classpath refid="classpath.test" />
|
||||||
<include name="**/*.java" />
|
<include name="**/*.java" />
|
||||||
</javac>
|
</javac>
|
||||||
<copy todir="${compileTestDir}">
|
<copy todir="${compileTestDir}">
|
||||||
<fileset dir="${testDir}"
|
<fileset dir="${testDir}"
|
||||||
excludes="**/*.java" />
|
excludes="**/*.java" />
|
||||||
</copy>
|
</copy>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<!-- generate release packages -->
|
<!-- generate release packages -->
|
||||||
<target name="package-all" depends="package,package-plugins" />
|
<target name="package-all" depends="package,package-plugins" />
|
||||||
|
|
||||||
<target name="package" depends="build">
|
<target name="package" depends="build">
|
||||||
<copy todir="${releaseDir}" >
|
<copy todir="${releaseDir}" >
|
||||||
<fileset file="${root}/hal.conf.example" />
|
<fileset file="${root}/hal.conf.example" />
|
||||||
<fileset file="${root}/hal-default.db" />
|
<fileset file="${root}/hal-default.db" />
|
||||||
<fileset file="${root}/logging.properties" />
|
<fileset file="${root}/logging.properties" />
|
||||||
<fileset file="${root}/run.sh" />
|
<fileset file="${root}/run.sh" />
|
||||||
</copy>
|
</copy>
|
||||||
<copy todir="${releaseDir}/lib">
|
<copy todir="${releaseDir}/lib">
|
||||||
<fileset dir="${libDir}" excludes="junit-*.jar,hamcrest-*.jar" />
|
<fileset dir="${libDir}" excludes="junit-*.jar,hamcrest-*.jar" />
|
||||||
</copy>
|
</copy>
|
||||||
<copy todir="${releaseDir}/resource/web">
|
<copy todir="${releaseDir}/resource/web">
|
||||||
<fileset dir="resource/web" />
|
<fileset dir="resource/web" />
|
||||||
</copy>
|
</copy>
|
||||||
<jar destfile="${releaseDir}/${releaseJar}" basedir="${compileDir}" />
|
<jar destfile="${releaseDir}/${releaseJar}" basedir="${compileDir}" />
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<target name="package-plugins" depends="package">
|
<target name="package-plugins" depends="package">
|
||||||
<subant target="package" verbose="true">
|
<subant target="package" verbose="true">
|
||||||
<fileset dir="plugins/" includes="*/build.xml"/>
|
<fileset dir="plugins/" includes="*/build.xml"/>
|
||||||
</subant>
|
</subant>
|
||||||
<!-- Copy plugin jars -->
|
<!-- Copy plugin jars -->
|
||||||
<copy todir="${releaseDir}/plugins" flatten="true" includeEmptyDirs="false">
|
<copy todir="${releaseDir}/plugins" flatten="true" includeEmptyDirs="false">
|
||||||
<fileset dir="plugins/">
|
<fileset dir="plugins/">
|
||||||
|
|
@ -170,19 +170,19 @@
|
||||||
<include name="*/lib/**"/>
|
<include name="*/lib/**"/>
|
||||||
</fileset>
|
</fileset>
|
||||||
</copy>
|
</copy>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
<!-- ________________________ UTILITY TARGETS ________________________ -->
|
<!-- ________________________ UTILITY TARGETS ________________________ -->
|
||||||
|
|
||||||
<target name="clear-aggr-data-from-db" depends="">
|
<target name="clear-aggr-data-from-db" depends="">
|
||||||
<exec executable="sqlite3">
|
<exec executable="sqlite3">
|
||||||
<arg line="hal.db 'DELETE FROM sensor_data_aggr'" />
|
<arg line="hal.db 'DELETE FROM sensor_data_aggr'" />
|
||||||
</exec>
|
</exec>
|
||||||
|
|
||||||
<!-- update all internal sensors aggregation version to indicate for peers that they need to re-sync all data -->
|
<!-- update all internal sensors aggregation version to indicate for peers that they need to re-sync all data -->
|
||||||
<exec executable="sqlite3">
|
<exec executable="sqlite3">
|
||||||
<arg line="hal.db 'UPDATE sensor SET aggr_version = (aggr_version+1) WHERE id = (SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)'" />
|
<arg line="hal.db 'UPDATE sensor SET aggr_version = (aggr_version+1) WHERE id = (SELECT sensor.id FROM user, sensor WHERE user.external == 0 AND sensor.user_id = user.id)'" />
|
||||||
</exec>
|
</exec>
|
||||||
</target>
|
</target>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
23
plugins/raspberry/build.xml
Executable file
23
plugins/raspberry/build.xml
Executable file
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project name="RaspberryPi Plugin" >
|
||||||
|
|
||||||
|
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
|
||||||
|
|
||||||
|
<!--common properties-->
|
||||||
|
<property name="root" value="." />
|
||||||
|
<property name="srcDir" value="${root}/src" />
|
||||||
|
<property name="testDir" value="${root}/test" />
|
||||||
|
<property name="libDir" value="${root}/lib" />
|
||||||
|
|
||||||
|
<property name="buildRoot" value="${root}/build" />
|
||||||
|
<property name="compileDir" value="${buildRoot}/production" />
|
||||||
|
<property name="compileTestDir" value="${buildRoot}/test" />
|
||||||
|
<property name="releaseDir" value="${buildRoot}/release" />
|
||||||
|
<property name="releaseJar" value="raspberry.jar" />
|
||||||
|
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
|
||||||
|
|
||||||
|
<!-- ________________________ TARGETS ________________________ -->
|
||||||
|
|
||||||
|
<import file="../../build_plugin.xml"/>
|
||||||
|
|
||||||
|
</project>
|
||||||
98
plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java
Executable file
98
plugins/raspberry/src/se/hal/plugin/raspberry/RPiController.java
Executable file
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -8,14 +8,14 @@ import zutil.ui.Configurator;
|
||||||
|
|
||||||
public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
||||||
|
|
||||||
@Configurator.Configurable("GPIO-Pin")
|
@Configurator.Configurable("GPIO-Pin")
|
||||||
private int gpioPin = -1;
|
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) {
|
public RPiPowerConsumptionSensor(int gpioPin) {
|
||||||
this.gpioPin = gpioPin;
|
this.gpioPin = gpioPin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -41,14 +41,14 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj){
|
public boolean equals(Object obj){
|
||||||
if(obj instanceof RPiPowerConsumptionSensor)
|
if(obj instanceof RPiPowerConsumptionSensor)
|
||||||
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getGpioPin() {
|
public int getGpioPin() {
|
||||||
return gpioPin;
|
return gpioPin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return "gpioPin:" + gpioPin;
|
return "gpioPin:" + gpioPin;
|
||||||
2
src/se/hal/plugin/raspberry/RPiSensor.java → plugins/raspberry/src/se/hal/plugin/raspberry/RPiSensor.java
Normal file → Executable file
2
src/se/hal/plugin/raspberry/RPiSensor.java → plugins/raspberry/src/se/hal/plugin/raspberry/RPiSensor.java
Normal file → Executable file
|
|
@ -2,6 +2,6 @@ package se.hal.plugin.raspberry;
|
||||||
|
|
||||||
public interface RPiSensor {
|
public interface RPiSensor {
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ import zutil.ui.Configurator;
|
||||||
|
|
||||||
public class RPiTemperatureSensor implements HalSensorConfig {
|
public class RPiTemperatureSensor implements HalSensorConfig {
|
||||||
|
|
||||||
@Configurator.Configurable("1-Wire Address")
|
@Configurator.Configurable("1-Wire Address")
|
||||||
private String w1Address;
|
private String w1Address;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -41,13 +41,13 @@ public class RPiTemperatureSensor implements HalSensorConfig {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj){
|
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 this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get1WAddress() {
|
public String get1WAddress() {
|
||||||
return w1Address;
|
return w1Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
75
plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java
Executable file
75
plugins/raspberry/src/se/hal/plugin/raspberry/RPiUtility.java
Executable file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
68
plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java
Executable file
68
plugins/raspberry/src/se/hal/plugin/raspberry/hardware/RPiDS18B20.java
Executable file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -17,74 +17,74 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor {
|
public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDigital, RPiSensor {
|
||||||
private static final int REPORT_TIMEOUT = 60_000; //one minute
|
private static final int REPORT_TIMEOUT = 60_000; //one minute
|
||||||
|
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
private RPiController controller;
|
private RPiController controller;
|
||||||
private ExecutorService executorPool;
|
private ExecutorService executorPool;
|
||||||
private long nanoSecondsSleep = REPORT_TIMEOUT * 1_000_000L;
|
private long nanoSecondsSleep = REPORT_TIMEOUT * 1_000_000L;
|
||||||
private volatile Integer impulseCount = 0;
|
private volatile Integer impulseCount = 0;
|
||||||
private GpioPinDigitalInput irLightSensor;
|
private GpioPinDigitalInput irLightSensor;
|
||||||
private final int gpioPin;
|
private final int gpioPin;
|
||||||
|
|
||||||
public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){
|
public RPiInteruptPulseFlankCounter(int gpioPin, RPiController controller){
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
this.gpioPin = gpioPin;
|
this.gpioPin = gpioPin;
|
||||||
|
|
||||||
// setup a thread pool for executing jobs
|
// setup a thread pool for executing jobs
|
||||||
this.executorPool = Executors.newCachedThreadPool();
|
this.executorPool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
//Enable non privileged access to the GPIO pins (no sudo required from now)
|
//Enable non privileged access to the GPIO pins (no sudo required from now)
|
||||||
GpioUtil.enableNonPrivilegedAccess();
|
GpioUtil.enableNonPrivilegedAccess();
|
||||||
|
|
||||||
// create gpio controller
|
// create gpio controller
|
||||||
GpioController gpio = null;
|
GpioController gpio = null;
|
||||||
try{
|
try{
|
||||||
gpio = GpioFactory.getInstance();
|
gpio = GpioFactory.getInstance();
|
||||||
}catch(IllegalArgumentException e){
|
}catch(IllegalArgumentException e){
|
||||||
logger.log(Level.SEVERE, "", e);
|
logger.log(Level.SEVERE, "", e);
|
||||||
throw e;
|
throw e;
|
||||||
}catch(UnsatisfiedLinkError e){
|
}catch(UnsatisfiedLinkError e){
|
||||||
logger.log(Level.SEVERE, "", e);
|
logger.log(Level.SEVERE, "", e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
// provision gpio pin as an input pin with its internal pull up resistor enabled
|
// provision gpio pin as an input pin with its internal pull up resistor enabled
|
||||||
irLightSensor = gpio.provisionDigitalInputPin(RPiUtility.getPin(gpioPin), PinPullResistance.PULL_UP);
|
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
|
// 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);
|
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 thread = new Thread(this);
|
||||||
thread.setDaemon(false);
|
thread.setDaemon(false);
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
irLightSensor.removeListener(this);
|
irLightSensor.removeListener(this);
|
||||||
executorPool.shutdown();
|
executorPool.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GpioPinListenerDigital interface
|
* GpioPinListenerDigital interface
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
|
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");
|
//System.out.println("IR LED turned ON");
|
||||||
//logger.log(Level.INFO, "IR LED turned on");
|
//logger.log(Level.INFO, "IR LED turned on");
|
||||||
synchronized(impulseCount){
|
synchronized(impulseCount){
|
||||||
impulseCount++;
|
impulseCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
long startTime = System.nanoTime();
|
long startTime = System.nanoTime();
|
||||||
synchronized(impulseCount){
|
synchronized(impulseCount){
|
||||||
impulseCount = 0; //reset the impulse count
|
impulseCount = 0; //reset the impulse count
|
||||||
}
|
}
|
||||||
while(!executorPool.isShutdown()) {
|
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
|
nanoSecondsSleep -= nanoSecondsTooMany / 3; //divide by constant to take into account varaiations im loop time
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sleep for [ns] nanoseconds
|
* Sleep for [ns] nanoseconds
|
||||||
* @param ns
|
* @param ns
|
||||||
*/
|
*/
|
||||||
private void sleepNano(long ns){
|
private void sleepNano(long ns){
|
||||||
//System.out.println("will go to sleep for " + ns + "ns");
|
//System.out.println("will go to sleep for " + ns + "ns");
|
||||||
try{
|
try{
|
||||||
Thread.sleep(ns/1000000L, (int)(ns%1000000L));
|
Thread.sleep(ns/1000000L, (int)(ns%1000000L));
|
||||||
}catch(InterruptedException e){
|
}catch(InterruptedException e){
|
||||||
//ignore
|
//ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -128,18 +128,18 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
||||||
* @param data
|
* @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.
|
//offload the timed loop by not doing the db interaction in this thread.
|
||||||
executorPool.execute(new Runnable(){
|
executorPool.execute(new Runnable(){
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
|
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
|
||||||
controller.sendDataReport(
|
controller.sendDataReport(
|
||||||
new RPiPowerConsumptionSensor(gpioPin),
|
new RPiPowerConsumptionSensor(gpioPin),
|
||||||
new PowerConsumptionSensorData(
|
new PowerConsumptionSensorData(
|
||||||
timestamp_end, data
|
timestamp_end, data
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
8
plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json
Executable file
8
plugins/raspberry/src/se/hal/plugin/raspberry/plugin.json
Executable file
|
|
@ -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"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -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<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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -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"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue