still maven switch

This commit is contained in:
Wolfgang Hottgenroth 2017-11-22 17:49:07 +01:00
commit 117f3c01e5
65 changed files with 136 additions and 577 deletions

View File

@ -1,14 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/jmbus-3.0.1.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/org.eclipse.paho.client.mqttv3-1.2.0.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/quick-json-1.0.2.3.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/log4j-core-2.9.1.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/log4j-api-2.9.1.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/mysql-connector-java-5.1.44-bin.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/activemq-client-5.15.2.jar"/>
<classpathentry kind="lib" path="/home/wn/workspace-java/MeasurementCollector/libraries/geronimo-jms_1.1_spec-1.1.1.jar"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
.gitignore vendored
View File

@ -1 +1,3 @@
MeasurementCollectorMqttReceiver-*
target

View File

@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -1,11 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}

View File

@ -1,9 +0,0 @@
db.period = 3600
db.url = jdbc:mysql://localhost/smarthome
db.driver = com.mysql.jdbc.Driver
db.user = smarthome
db.password = smarthome123
jms.broker = tcp://localhost:61616
jms.clientid = mdb
jms.parseddata.topic = IoT/Measurement

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

59
pom.xml Normal file
View File

@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hottis.measurementCollector</groupId>
<artifactId>MeasurementCollector</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MeasurementCollector</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.hottis.common</groupId>
<artifactId>HottisLibJava</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.hottis.smarthomelib</groupId>
<artifactId>SmarthomeLib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.openmuc</groupId>
<artifactId>jmbus</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>de.hottis.measurementCollector.MeasurementCollector</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,53 +0,0 @@
package de.hottis.measurementCollector;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public abstract class ADataObject implements Serializable {
private static final long serialVersionUID = 1L;
final protected Logger logger = LogManager.getRootLogger();
private LocalDateTime timestamp;
private String name;
private Map<String, Object> values;
public ADataObject(LocalDateTime timestamp, String name) {
this.timestamp = timestamp;
this.name = name;
}
public void setValues(Map<String, Object> values) {
this.values = values;
}
abstract public String getTableName();
public LocalDateTime getTimestamp() {
return timestamp;
}
public Map<String, Object> getValues() {
return values;
}
public String getName() {
return name;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("<");
sb.append(name);
sb.append(", ");
sb.append(timestamp);
sb.append(", ");
sb.append(values.toString());
sb.append(">");
return sb.toString();
}
}

View File

@ -1,190 +0,0 @@
package de.hottis.measurementCollector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Properties;
import java.util.Timer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class DatabaseEngine extends Thread implements ITriggerable {
static final String DATABASE_ENGINE_PERIOD_PROP = "db.period";
static final String DATABASE_URL_PROP = "db.url";
static final String DATABASE_USER_PROP = "db.user";
static final String DATABASE_PASSWORD_PROP = "db.password";
static final String DATABASE_DRIVER_PROP = "db.driver";
final protected Logger logger = LogManager.getRootLogger();
private Properties config;
private MyQueue<ADataObject> queue;
private boolean stop;
private boolean triggerFlag;
private Timer timer;
private int period;
private String dbUrl;
private String dbUsername;
private String dbPassword;
public DatabaseEngine(Properties config, MyQueue<ADataObject> queue) {
super("MeasurementCollector.DatabaseEngine");
this.config = config;
this.queue = queue;
this.stop = false;
this.triggerFlag = false;
this.period = Integer.parseInt(this.config.getProperty(DATABASE_ENGINE_PERIOD_PROP));
this.dbUrl = this.config.getProperty(DATABASE_URL_PROP);
this.dbUsername = this.config.getProperty(DATABASE_USER_PROP);
this.dbPassword = this.config.getProperty(DATABASE_PASSWORD_PROP);
}
public void requestShutdown() {
logger.info("Shutdown of database engine requested");
this.stop = true;
try {
this.join();
logger.info("Database engine is down");
} catch (InterruptedException e) {
logger.error("Waiting for shutdown of database engine interrupted");
}
}
public synchronized void trigger() {
logger.debug("DatabaseEngine triggered");
triggerFlag = true;
notify();
}
public void init() throws MeasurementCollectorException {
timer = new Timer("DatabaseEngineTrigger");
timer.schedule(new TriggerTimer(this), 0, period * 1000);
try {
Class.forName(config.getProperty(DATABASE_DRIVER_PROP));
} catch (ClassNotFoundException e) {
logger.error("Database driver class not found", e);
throw new MeasurementCollectorException("Database driver class not found", e);
}
}
private String createStatementFromDataObject(ADataObject ado) {
StringBuffer sb = new StringBuffer();
sb.append("INSERT INTO " + ado.getTableName());
sb.append("(name,ts,");
sb.append(String.join(",", ado.getValues().keySet()));
sb.append(") ");
sb.append("VALUES(?,?,");
String[] marks = (String[]) ado.getValues().values().stream()
.map(c -> "?")
.toArray(String[]::new);
sb.append(String.join(",", marks));
sb.append(")");
return sb.toString();
}
private void bindValue(PreparedStatement pstmt, int pos, Object o) throws SQLException {
if (o instanceof Integer) {
pstmt.setInt(pos, (Integer)o);
} else if (o instanceof Byte) {
pstmt.setByte(pos, (Byte)o);
} else if (o instanceof Double) {
pstmt.setDouble(pos, (Double)o);
} else if (o instanceof String) {
pstmt.setString(pos, (String)o);
} else if (o instanceof LocalDateTime) {
pstmt.setTimestamp(pos, Timestamp.valueOf(((LocalDateTime)o)));
} else {
throw new SQLException("illegal parameter type: " + o.getClass().getName());
}
}
@Override
public synchronized void run() {
while (! stop) {
logger.debug("DatabaseEngine is about to wait for (regularly) " + period + "s");
while (! triggerFlag) {
try {
wait();
} catch (InterruptedException e) {
}
}
triggerFlag = false;
logger.debug("DatabaseEngine has received trigger");
Connection dbCon = null;
HashMap<String, PreparedStatement> preparedStatements = new HashMap<String, PreparedStatement>();
try {
int itemCnt = 0;
dbCon = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
logger.debug("New connection opened: " + dbCon);
while (true) {
try {
ADataObject ado = queue.dequeue();
if (ado == null) {
logger.warn("DatabaseEngine found no data");
break;
}
String key = ado.getClass().getName();
PreparedStatement pstmt;
if (! preparedStatements.containsKey(key)) {
pstmt = dbCon.prepareStatement(createStatementFromDataObject(ado));
preparedStatements.put(key, pstmt);
logger.debug("PreparedStatement created for " + key + ": " + pstmt);
} else {
pstmt = preparedStatements.get(key);
logger.debug("PreparedStatement reused for " + key + ": " + pstmt);
}
bindValue(pstmt, 1, ado.getName());
bindValue(pstmt, 2, ado.getTimestamp());
int pos = 3;
for (Object o : ado.getValues().values()) {
bindValue(pstmt, pos, o);
pos++;
}
itemCnt++;
logger.info("DatabaseEngine received (" + itemCnt + ") " + ado);
logger.info("Statement is " + pstmt.toString());
pstmt.execute();
logger.info("Database insert executed");
pstmt.clearParameters();
} catch (SQLException e) {
logger.error("SQLException in inner database engine loop", e);
}
}
} catch (Exception e) {
logger.error("Exception in outer database engine loop", e);
} finally {
for (PreparedStatement p : preparedStatements.values()) {
try {
logger.debug("PreparedStatement " + p + " about to close");
p.close();
} catch (SQLException e) {
logger.warn("SQLException when closing prepared statement, nothing will be done");
}
}
preparedStatements.clear();
if (dbCon != null) {
try {
logger.debug("Connection " + dbCon + " about to close");
dbCon.close();
} catch (SQLException e) {
logger.warn("SQLException when closing connection, nothing will be done");
} finally {
dbCon = null;
}
}
}
}
logger.info("Database engine is terminating");
}
}

View File

@ -1,24 +0,0 @@
package de.hottis.measurementCollector;
import java.time.LocalDateTime;
import java.util.HashMap;
public class ElectricEnergyDataObject extends ADataObject {
private static final long serialVersionUID = 1L;
static final String ENERGY_KEY = "energy";
static final String POWER_KEY = "power";
static final String TABLE_NAME = "ElectricEnergy";
public ElectricEnergyDataObject(LocalDateTime timestamp, String name, double energy, double power) {
super(timestamp, name);
HashMap<String, Object> values = new HashMap<String, Object>();
values.put(ENERGY_KEY, energy);
values.put(POWER_KEY, power);
setValues(values);
}
@Override
public String getTableName() {
return TABLE_NAME;
}
}

View File

@ -1,5 +0,0 @@
package de.hottis.measurementCollector;
public interface ITriggerable {
public void trigger();
}

View File

@ -1,124 +0,0 @@
package de.hottis.measurementCollector;
import java.io.Serializable;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class JmsTopic<T extends Serializable> implements MyQueue<T> {
static final String JMS_PARSED_DATA_TOPIC = "jms.parseddata.topic";
static final String JMS_CLIENTID_PROP = "jms.clientid";
static final String JMS_BROKER ="jms.broker";
static enum Mode { CONSUMER, PRODUCER, CONSUMER_PRODUCER };
final protected Logger logger = LogManager.getRootLogger();
private String clientId;
private Properties config;
private Connection connection;
private Session session;
private Topic topic;
private MessageProducer producer;
private MessageConsumer consumer;
private String topicTxt;
private String broker;
private Mode mode;
public JmsTopic(Properties config, Mode mode) {
this.config = config;
clientId = this.config.getProperty(JMS_CLIENTID_PROP);
topicTxt = this.config.getProperty(JMS_PARSED_DATA_TOPIC);
broker = this.config.getProperty(JMS_BROKER);
this.mode = mode;
}
public void init() throws MeasurementCollectorException {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(broker);
connectionFactory.setTrustAllPackages(true);
logger.debug("connectionFactory: " + connectionFactory);
connection = connectionFactory.createConnection();
connection.setClientID(clientId);
connection.start();
logger.debug("connection: " + connection);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
logger.debug("session: " + session);
topic = session.createTopic(topicTxt);
logger.debug("topic: " + topic);
if ((mode == Mode.PRODUCER) || (mode == Mode.CONSUMER_PRODUCER)) {
producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
logger.debug("producer: " + producer);
} else {
producer = null;
}
if ((mode == Mode.CONSUMER) || (mode == Mode.CONSUMER_PRODUCER)) {
consumer = session.createDurableSubscriber(topic, "DatabaseEngine");
logger.debug("consumer: " + consumer);
} else {
consumer = null;
}
} catch (JMSException e) {
logger.error("JMSException in AJmsQueue.init", e);
throw new MeasurementCollectorException("JMSException in AJmsTopic.init", e);
}
}
public T dequeue() throws MeasurementCollectorException {
if (consumer == null) {
throw new MeasurementCollectorException("This is no consumer");
}
try {
ObjectMessage message = (ObjectMessage) consumer.receiveNoWait();
T item;
if (message != null) {
@SuppressWarnings("unchecked")
T t = (T) message.getObject();
item = t;
logger.debug("message dequeued");
} else {
item = null;
}
return item;
} catch (JMSException e) {
logger.error("JMSException in JmsTopic.dequeue", e);
logger.error("Calling init");
init();
throw new MeasurementCollectorException("JMSException in JmsTopic.dequeue", e);
}
}
public void enqueue(T item) throws MeasurementCollectorException {
if (producer == null) {
throw new MeasurementCollectorException("This is no producer");
}
try {
ObjectMessage message = session.createObjectMessage();
message.setObject(item);
producer.send(message);
logger.debug("message enqueued");
} catch (JMSException e) {
logger.error("JMSException in JmsTopic.enqueue", e);
logger.error("Calling init");
init();
throw new MeasurementCollectorException("JMSException in JmsTopic.enqueue", e);
}
}
}

View File

@ -1,13 +0,0 @@
package de.hottis.measurementCollector;
@SuppressWarnings("serial")
public class MeasurementCollectorException extends Exception {
public MeasurementCollectorException(String msg, Throwable cause) {
super(msg, cause);
}
public MeasurementCollectorException(String msg) {
super(msg);
}
}

View File

@ -1,30 +0,0 @@
package de.hottis.measurementCollector;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MeasurementDatabaseEngine {
static final String PROPS_FILENAME = "measurementDataEngine.props";
static final Logger logger = LogManager.getRootLogger();
public static void main(String[] args) throws Exception {
logger.info("MeasurementDatabaseEngine starting");
final Properties config = new Properties();
config.load(MeasurementDatabaseEngine.class.getClassLoader().getResourceAsStream(PROPS_FILENAME));
logger.debug("Configuration loaded");
JmsTopic<ADataObject> queue = new JmsTopic<ADataObject>(config, JmsTopic.Mode.CONSUMER);
queue.init();
DatabaseEngine databaseEngine = new DatabaseEngine(config, queue);
databaseEngine.init();
databaseEngine.start();
logger.debug("DatabaseEngine started");
}
}

View File

@ -1,8 +0,0 @@
package de.hottis.measurementCollector;
import java.io.Serializable;
public interface MyQueue<T extends Serializable> {
public T dequeue() throws MeasurementCollectorException;
public void enqueue(T item) throws MeasurementCollectorException;
}

View File

@ -1,21 +0,0 @@
package de.hottis.measurementCollector;
import java.time.LocalDateTime;
import java.util.HashMap;
public class TemperatureDataObject extends ADataObject {
private static final long serialVersionUID = 1L;
static final String TEMPERATURE_KEY = "temperature";
static final String TABLE_NAME = "Temperature";
public TemperatureDataObject(LocalDateTime timestamp, String name, double temperature) {
super(timestamp, name);
HashMap<String, Object> values = new HashMap<String, Object>();
values.put(TEMPERATURE_KEY, temperature);
setValues(values);
}
public String getTableName() {
return TABLE_NAME;
}
}

View File

@ -1,15 +0,0 @@
package de.hottis.measurementCollector;
import java.util.TimerTask;
public class TriggerTimer extends TimerTask {
private ITriggerable triggerable;
public TriggerTimer(ITriggerable triggerable) {
this.triggerable = triggerable;
}
public void run() {
triggerable.trigger();
}
}

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration debug="false">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t, %F, %L] %-5level - %msg%n%throwable"/>
</Console>
<File name="FileAppender" fileName="/tmp/MeasurementCollector.log">
<PatternLayout pattern="%d [%t, %F, %L] %-5level - %msg%n%throwable"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>

View File

@ -1,17 +0,0 @@
mqtt.broker = tcp://172.16.2.15:1883
;mqtt.broker = tcp://eupenstrasse20.dynamic.hottis.de:2883
;mqtt.username = tron
;mqtt.password = geheim123
mbus.dataparser.1 = light,Light,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.2 = computer,Computer,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.3 = laundry,Laundry,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.4 = dryer,Dryer,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.5 = dishwasher,Dishwasher,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.6 = freezer,Freezer,de.hottis.measurementCollector.FinderOnePhasePowerMeter
mbus.dataparser.7 = electricity,Total,de.hottis.measurementCollector.FinderThreePhasePowerMeter
mbus.dataparser.8 = thermom.,null,de.hottis.measurementCollector.HottisFourChannelThermometer
jms.broker = tcp://localhost:61616
jms.clientid = mcol
jms.parseddata.topic = IoT/Measurement

View File

@ -1,9 +0,0 @@
db.period = 3600
db.url = jdbc:mysql://localhost/smarthome
db.driver = com.mysql.jdbc.Driver
db.user = smarthome
db.password = smarthome123
jms.broker = tcp://localhost:61616
jms.clientid = mdb
jms.parseddata.topic = IoT/Measurement

View File

@ -0,0 +1,38 @@
package de.hottis.measurementCollector;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -1,12 +0,0 @@
#!/bin/bash
PWD=`pwd`
CLASSPATH=$CLASSPATH:$PWD/bin
for I in `find libraries -type f`; do
CLASSPATH=$CLASSPATH:$PWD/$I;
done
export CLASSPATH
export JAVA_HOME=/opt/jdk1.8.0_131
$JAVA_HOME/bin/java de.hottis.measurementCollector.MeasurementCollector

View File

@ -1,12 +0,0 @@
#!/bin/bash
PWD=`pwd`
CLASSPATH=$CLASSPATH:$PWD/bin
for I in `find libraries -type f`; do
CLASSPATH=$CLASSPATH:$PWD/$I;
done
export CLASSPATH
export JAVA_HOME=/opt/jdk1.8.0_131
$JAVA_HOME/bin/java de.hottis.measurementCollector.MeasurementDatabaseEngine