Some refactoring for better testing

Former-commit-id: 159d14c7ee81ea75ef9bade2d48c67a056ea4c42
This commit is contained in:
Ziver Koc 2016-01-05 00:42:25 +01:00
parent e9168e6136
commit 060f784611
11 changed files with 47 additions and 28 deletions

1
.gitignore vendored
View file

@ -2,4 +2,3 @@
/hal.db*
/build/
/lib/Zutil.jar
/tellstick.conf

View file

@ -1,2 +1,3 @@
http_port=8080
sync_port=6666
tellstick.com_port=COM5

BIN
lib/jSerialComm-1.3.10.jar Executable file

Binary file not shown.

Binary file not shown.

View file

@ -182,10 +182,15 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor
logger.fine("Instantiating new controller: " + c.getName());
try {
controller = c.newInstance();
if(controller instanceof HalSensorController)
if(controller instanceof HalSensorController) {
((HalSensorController) controller).setListener(this);
if(controller instanceof HalEventController)
((HalSensorController) controller).initialize();
}
if(controller instanceof HalEventController) {
((HalEventController) controller).setListener(this);
if( ! (controller instanceof HalSensorController))
((HalEventController) controller).initialize();
}
controllerMap.put(c, controller);
} catch (Exception e){

View file

@ -45,9 +45,12 @@ public class HalContext {
try {
// Read conf
fileConf = new Properties(defaultFileConf);
if (FileUtil.find(CONF_FILE).exists()) {
FileReader in = new FileReader(CONF_FILE);
fileConf.load(in);
in.close();
}
else logger.info("No hal.conf file found");
// Init DB
File dbFile = FileUtil.find(DB_FILE);
@ -66,7 +69,7 @@ public class HalContext {
Properties defaultDBConf =
referenceDB.exec("SELECT * FROM conf", new PropertiesSQLResult());
// Check DB version
logger.fine("DB version: "+ dbConf.getProperty(PROPERTY_DB_VERSION));
logger.info("DB version: "+ dbConf.getProperty(PROPERTY_DB_VERSION));
int defaultDBVersion = Integer.parseInt(defaultDBConf.getProperty(PROPERTY_DB_VERSION));
int dbVersion = (dbConf.getProperty(PROPERTY_DB_VERSION) != null ?
Integer.parseInt(dbConf.getProperty(PROPERTY_DB_VERSION)) :
@ -123,8 +126,10 @@ public class HalContext {
public static String getStringProperty(String key){
String value = fileConf.getProperty(key);
if(value == null)
String value = null;
if (fileConf != null)
value = fileConf.getProperty(key);
if (dbConf != null && value == null)
value = dbConf.getProperty(key);
return value;
}

View file

@ -4,6 +4,9 @@ package se.koc.hal.intf;
* Created by Ziver on 2015-12-15.
*/
public interface HalEventController {
void initialize() throws Exception;
/**
* Will register an event type to be handled by this controller
*/

View file

@ -5,6 +5,8 @@ package se.koc.hal.intf;
*/
public interface HalSensorController {
void initialize() throws Exception;
/**
* Will register a sensor type to be handled by this controller
*/

View file

@ -23,6 +23,7 @@
package se.koc.hal.plugin.tellstick;
import com.fazecast.jSerialComm.SerialPort;
import se.koc.hal.HalContext;
import se.koc.hal.intf.*;
import zutil.io.file.FileUtil;
import zutil.log.InputStreamLogger;
@ -58,33 +59,35 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv
public TellstickSerialComm() throws Exception {
public TellstickSerialComm() {
set = new TimedHashSet(TRANSMISSION_UNIQUENESS_TTL);
parser = new TellstickParser();
registeredObjects = 0;
// Read properties
Properties prop = new Properties();
prop.setProperty("com_port", "COM6"); // defaults
if(FileUtil.find("tellstick.conf") != null) {
Reader reader = new FileReader("tellstick.conf");
prop.load(reader);
reader.close();
}
connect(prop.getProperty("com_port"));
@Override
public void initialize() throws Exception {
// Read properties
String port = HalContext.getStringProperty("tellstick.com_port");
if (port == null)
port = "COM1"; // defaults
connect(port);
}
public void connect(String portName) throws Exception {
logger.info("Connecting to com port... ("+ portName +")");
serial = SerialPort.getCommPort(portName);
serial.setBaudRate(9600);
if(!serial.openPort())
throw new IOException("Could not open port: "+portName);
serial.setComPortTimeouts(
SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
in = new BufferedReader(new InputStreamReader(new InputStreamLogger(serial.getInputStream()), "UTF-8"));
out = new BufferedWriter(new OutputStreamWriter(new OutputStreamLogger(serial.getOutputStream()), "UTF-8"));
Executors.newSingleThreadExecutor().submit(this);
Executors.newSingleThreadExecutor().execute(this);
}
public void close() {

View file

@ -12,9 +12,8 @@ public class TelstickSerialCommNexaOnOffTest {
System.out.println("Connecting to db...");
TellstickSerialComm comm = new TellstickSerialComm();
// http://developer.telldus.com/doxygen/TellStick.html
System.out.println("Connecting to com port...");
//comm.connect("COM5");
comm.connect("/dev/ttyUSB1");
comm.connect("COM5");
//comm.connect("/dev/ttyUSB1");
Thread.sleep(1000);

View file

@ -1,5 +1,6 @@
package se.koc.hal.plugin.tellstick;
import se.koc.hal.HalContext;
import se.koc.hal.intf.HalSensor;
import se.koc.hal.intf.HalSensorController;
import se.koc.hal.intf.HalSensorReportListener;
@ -24,8 +25,9 @@ public class TelstickSerialCommTest {
LogUtil.setGlobalFormatter(new CompactLogFormatter());
LogUtil.setGlobalLevel(Level.FINEST);
logger.info("Connecting to db...");
final DBConnection db = new DBConnection(DBConnection.DBMS.SQLite, "hal.db");
logger.info("Initializing HalContext...");
HalContext.initialize();
final DBConnection db = HalContext.getDB();
logger.info("Setting up Tellstick listeners...");
TellstickSerialComm comm = new TellstickSerialComm();
@ -47,8 +49,8 @@ public class TelstickSerialCommTest {
}
}
});
logger.info("Connecting to com port...");
comm.connect("/dev/ttyUSB1");
comm.connect("COM5");
//comm.connect("/dev/ttyUSB1");
logger.info("Up and Running");
} catch (Exception e) {