Bug fix, Optimized imports, Changed Timer to ExecutorService

Former-commit-id: b23ccf25f1f121589bd8887dbe833b95db4d2b24
This commit is contained in:
Ziver Koc 2015-12-18 17:20:33 +01:00
parent d635888742
commit 405d7ebf02
25 changed files with 122 additions and 153 deletions

View file

@ -27,21 +27,15 @@
* THIS SOFTWARE.
*/
import marytts.client.MaryClient;
import marytts.util.data.audio.AudioPlayer;
import marytts.util.http.Address;
import javax.sound.sampled.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Locale;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.UnsupportedAudioFileException;
import marytts.util.data.audio.AudioPlayer;
import marytts.client.MaryClient;
import marytts.util.http.Address;
/**
* A demo class illustrating how to use the MaryClient class.

View file

@ -2,13 +2,13 @@ package se.koc.hal;
import se.koc.hal.bot.AliceBot;
import se.koc.hal.intf.HalBot;
import se.koc.hal.intf.HalSpeachToText;
import se.koc.hal.intf.HalTextToSpeach;
import se.koc.hal.plugin.tellstick.TellstickChangeListener;
import se.koc.hal.plugin.tellstick.TellstickProtocol;
import se.koc.hal.plugin.tellstick.TellstickSerialComm;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import se.koc.hal.struct.SwitchEvent;
import se.koc.hal.intf.HalSpeachToText;
import se.koc.hal.stt.ManualSTTClient;
import se.koc.hal.tts.MaryRemoteTTSClient;

View file

@ -57,6 +57,7 @@ public class HalContext {
// Read DB conf
dbConf = db.exec("SELECT * FROM conf", new PropertiesSQLResult());
// Upgrade DB needed?
DBConnection referenceDB = new DBConnection(DBConnection.DBMS.SQLite, DEFAULT_DB_FILE);
Properties defaultDBConf =
@ -64,16 +65,16 @@ public class HalContext {
// Check DB version
logger.fine("DB version: "+ dbConf.getProperty(PROPERTY_DB_VERSION));
int defaultDBVersion = Integer.parseInt(defaultDBConf.getProperty(PROPERTY_DB_VERSION));
int dbVersion = Integer.parseInt(dbConf.getProperty(PROPERTY_DB_VERSION));
if(dbConf.getProperty(PROPERTY_DB_VERSION) == null || defaultDBVersion > dbVersion ) {
int dbVersion = (dbConf.getProperty(PROPERTY_DB_VERSION) != null ?
Integer.parseInt(dbConf.getProperty(PROPERTY_DB_VERSION)) :
-1);
if(defaultDBVersion > dbVersion ) {
logger.info("Starting DB upgrade...");
File backupDB = FileUtil.getNextFile(dbFile);
logger.fine("Backing up DB to: "+ backupDB);
FileUtil.copy(dbFile, backupDB);
logger.fine(String.format("Upgrading DB (from: v%s, to: v%s)...",
dbConf.getProperty(PROPERTY_DB_VERSION),
defaultDBConf.getProperty(PROPERTY_DB_VERSION)));
logger.fine(String.format("Upgrading DB (from: v%s, to: v%s)...", dbVersion, defaultDBVersion));
DBUpgradeHandler handler = new DBUpgradeHandler(referenceDB);
handler.setTargetDB(db);
//handler.setForcedDBUpgrade(true);

View file

@ -16,7 +16,8 @@ import zutil.log.LogUtil;
import zutil.net.http.HttpServer;
import zutil.net.http.pages.HttpFilePage;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
/**
@ -47,9 +48,10 @@ public class PowerChallenge {
new DataSynchronizationClient(),
new DataCleanupDaemon()
};
Timer daemonTimer = new Timer();
// We set only one thread for easier troubleshooting
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
for(HalDaemon daemon : daemons){
daemon.initiate(daemonTimer);
daemon.initiate(executor);
}
pages = new HalHttpPage[]{

View file

@ -1,15 +1,5 @@
package se.koc.hal.deamon;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalDaemon;
import se.koc.hal.struct.HalSensor;
@ -21,11 +11,21 @@ import zutil.db.SQLResultHandler;
import zutil.db.handler.SimpleSQLResult;
import zutil.log.LogUtil;
public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataAggregatorDaemon implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
public void initiate(Timer timer){
timer.schedule(this, 0, TimeUtility.FIVE_MINUTES_IN_MS);
public void initiate(ScheduledExecutorService executor){
executor.scheduleAtFixedRate(this, 0, TimeUtility.FIVE_MINUTES_IN_MS, TimeUnit.MILLISECONDS);
}
@Override
@ -58,8 +58,8 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
/**
* Aggregate data from the raw DB table to the aggregated table
* @param sensorId The sensor for to aggregate data
* @param toPeriodSizeInMs The period length in ms to aggregate to
* @param sensor The sensor for to aggregate data
* @param toPeriodSizeInMs The period length in ms to aggregate to
*/
private void aggregateRawData(HalSensor sensor, long toPeriodSizeInMs, int expectedSampleCount){
long sensorId = sensor.getId();
@ -94,9 +94,9 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
/**
* Re-aggregate data from the aggregated DB table to itself
* @param sensorId The sensor for to aggregate data
* @param fromPeriodSizeInMs The period length in ms to aggregate from
* @param toPeriodSizeInMs The period length in ms to aggregate to
* @param sensor The sensor for to aggregate data
* @param fromPeriodSizeInMs The period length in ms to aggregate from
* @param toPeriodSizeInMs The period length in ms to aggregate to
*/
private void aggrigateAggregatedData(HalSensor sensor, long fromPeriodSizeInMs, long toPeriodSizeInMs){
long sensorId = sensor.getId();

View file

@ -1,15 +1,5 @@
package se.koc.hal.deamon;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalDaemon;
import se.koc.hal.struct.HalSensor;
@ -20,11 +10,21 @@ import zutil.db.SQLResultHandler;
import zutil.db.handler.SimpleSQLResult;
import zutil.log.LogUtil;
public class DataCleanupDaemon extends TimerTask implements HalDaemon {
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataCleanupDaemon implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
public void initiate(Timer timer){
timer.schedule(this, 5000, TimeUtility.FIVE_MINUTES_IN_MS);
public void initiate(ScheduledExecutorService executor){
executor.scheduleAtFixedRate(this, 5000, TimeUtility.FIVE_MINUTES_IN_MS, TimeUnit.MILLISECONDS);
}
@Override

View file

@ -1,18 +1,5 @@
package se.koc.hal.deamon;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
import se.koc.hal.HalContext;
import se.koc.hal.deamon.DataSynchronizationDaemon.SensorDataDTO;
import se.koc.hal.deamon.DataSynchronizationDaemon.SensorDataListDTO;
@ -22,14 +9,27 @@ import se.koc.hal.struct.User;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
public class DataSynchronizationClient extends TimerTask implements HalDaemon {
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class DataSynchronizationClient implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
private static final long SYNC_INTERVALL = 5 * 60 * 1000; // 5 min
private static final long SYNC_INTERVAL = 5 * 60 * 1000; // 5 min
@Override
public void initiate(Timer timer) {
timer.schedule(this, 10000, SYNC_INTERVALL);
public void initiate(ScheduledExecutorService executor){
executor.scheduleAtFixedRate(this, 10000, SYNC_INTERVAL, TimeUnit.MILLISECONDS);
}
@Override

View file

@ -1,5 +1,14 @@
package se.koc.hal.deamon;
import se.koc.hal.HalContext;
import se.koc.hal.deamon.DataSynchronizationClient.PeerDataReqDTO;
import se.koc.hal.intf.HalDaemon;
import zutil.db.DBConnection;
import zutil.db.SQLResultHandler;
import zutil.log.LogUtil;
import zutil.net.threaded.ThreadedTCPNetworkServer;
import zutil.net.threaded.ThreadedTCPNetworkServerThread;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@ -10,18 +19,9 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Timer;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;
import se.koc.hal.HalContext;
import se.koc.hal.deamon.DataSynchronizationClient.PeerDataReqDTO;
import se.koc.hal.intf.HalDaemon;
import zutil.db.DBConnection;
import zutil.db.SQLResultHandler;
import zutil.log.LogUtil;
import zutil.net.threaded.ThreadedTCPNetworkServer;
import zutil.net.threaded.ThreadedTCPNetworkServerThread;
public class DataSynchronizationDaemon extends ThreadedTCPNetworkServer implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
@ -31,7 +31,7 @@ public class DataSynchronizationDaemon extends ThreadedTCPNetworkServer implemen
}
@Override
public void initiate(Timer timer) {
public void initiate(ScheduledExecutorService executor){
this.start();
}

View file

@ -1,12 +1,11 @@
package se.koc.hal.intf;
import java.util.Timer;
import java.util.concurrent.ScheduledExecutorService;
/**
* Created by Ziver on 2015-12-03.
*/
public interface HalDaemon {
public void initiate(Timer timer);
public interface HalDaemon extends Runnable{
public void initiate(ScheduledExecutorService executor);
public void run();
}

View file

@ -1,7 +1,5 @@
package se.koc.hal.page;
import java.util.Map;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalHttpPage;
import se.koc.hal.struct.HalSensor;
@ -10,6 +8,8 @@ import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
import java.util.Map;
public class PCConfigureHttpPage extends HalHttpPage {
public PCConfigureHttpPage() {

View file

@ -1,11 +1,11 @@
package se.koc.hal.page;
import java.util.Map;
import se.koc.hal.intf.HalHttpPage;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
import java.util.Map;
public class PCHeatMapHttpPage extends HalHttpPage {
public PCHeatMapHttpPage() {

View file

@ -1,5 +1,13 @@
package se.koc.hal.page;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalHttpPage;
import se.koc.hal.util.TimeUtility;
import zutil.db.DBConnection;
import zutil.db.SQLResultHandler;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -7,14 +15,6 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import se.koc.hal.HalContext;
import se.koc.hal.util.TimeUtility;
import se.koc.hal.intf.HalHttpPage;
import zutil.db.DBConnection;
import zutil.db.SQLResultHandler;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
public class PCOverviewHttpPage extends HalHttpPage {
public PCOverviewHttpPage() {

View file

@ -1,24 +1,17 @@
package se.koc.hal.plugin.localsensor;
import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
public class RPiImpulseCountSensor implements Runnable {
private static final Logger logger = LogUtil.getLogger();

View file

@ -22,16 +22,16 @@
package se.koc.hal.plugin.tellstick;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.fazecast.jSerialComm.SerialPort;
import zutil.log.InputStreamLogger;
import zutil.log.LogUtil;
import zutil.log.OutputStreamLogger;
import zutil.struct.TimedHashSet;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.

View file

@ -1,9 +1,5 @@
package se.koc.hal.struct;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalSensorController;
import zutil.db.DBConnection;
@ -11,6 +7,10 @@ import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanSQLResultHandler;
import zutil.db.handler.SimpleSQLResult;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
@DBBean.DBTable("sensor")
public class HalSensor extends DBBean{
public enum AggregationMethod{

View file

@ -1,13 +1,12 @@
package se.koc.hal.struct;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanSQLResultHandler;
import zutil.ui.Configurator;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
/**
* Created by Ziver on 2015-12-03.

View file

@ -1,11 +1,5 @@
package se.koc.hal.stt;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.LineUnavailableException;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;
import com.darkprograms.speech.recognizer.FlacEncoder;
import com.darkprograms.speech.recognizer.GSpeechDuplex;
@ -14,6 +8,9 @@ import com.darkprograms.speech.recognizer.GoogleResponse;
import se.koc.hal.intf.HalSpeachToText;
import zutil.io.file.FileUtil;
import javax.sound.sampled.AudioFileFormat;
import java.io.File;
public class GoogleSTTClient implements HalSpeachToText, GSpeechResponseListener {
private MicrophoneAnalyzer mic;
private File audioFile;

View file

@ -24,7 +24,9 @@ package se.koc.hal.stt;
import se.koc.hal.intf.HalSpeachToText;
import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ManualSTTClient implements HalSpeachToText {
private BufferedReader in;

View file

@ -24,14 +24,12 @@ package se.koc.hal.tts;
import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import marytts.client.RemoteMaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioPlayer;
import se.koc.hal.intf.HalTextToSpeach;
import javax.sound.sampled.AudioInputStream;
import java.io.IOException;
import java.util.Set;
/**

View file

@ -1,12 +1,6 @@
package se.koc.hal.plugin.tellstick;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import se.koc.hal.plugin.tellstick.protocols.Oregon0x1A2D;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import java.sql.SQLException;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-11-19.

View file

@ -1,7 +1,5 @@
package se.koc.hal.plugin.tellstick;
import se.koc.hal.plugin.tellstick.TellstickSerialComm;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import se.koc.hal.plugin.tellstick.protocols.Oregon0x1A2D;
import zutil.db.DBConnection;
import zutil.log.CompactLogFormatter;

View file

@ -22,7 +22,6 @@
package se.koc.hal.plugin.tellstick.protocols;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import zutil.converters.Converter;
import static org.junit.Assert.*;

View file

@ -22,11 +22,10 @@
package se.koc.hal.test;
import java.net.URL;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.result.WordResult;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
public class LiveSpeechRecognizerTest {

View file

@ -22,20 +22,15 @@
package se.koc.hal.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
import edu.cmu.sphinx.decoder.adaptation.Stats;
import edu.cmu.sphinx.decoder.adaptation.Transform;
import edu.cmu.sphinx.result.WordResult;
import net.didion.jwnl.data.Exc;
import javax.sound.sampled.AudioFileFormat;
import java.io.File;
import java.io.FileInputStream;
/**
* A simple example that shows how to transcribe a continuous audio file that

9
test/se/koc/hal/util/TimeUtilityTest.java Normal file → Executable file
View file

@ -1,12 +1,11 @@
package se.koc.hal.util;
import static org.junit.Assert.*;
import java.util.Calendar;
import org.junit.Before;
import org.junit.Test;
import se.koc.hal.util.TimeUtility;
import java.util.Calendar;
import static org.junit.Assert.assertEquals;
public class TimeUtilityTest {
private long currentTime_UTC;