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* /hal.db*
/build/ /build/
/lib/Zutil.jar /lib/Zutil.jar
/tellstick.conf

View file

@ -1,2 +1,3 @@
http_port=8080 http_port=8080
sync_port=6666 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()); logger.fine("Instantiating new controller: " + c.getName());
try { try {
controller = c.newInstance(); controller = c.newInstance();
if(controller instanceof HalSensorController) if(controller instanceof HalSensorController) {
((HalSensorController) controller).setListener(this); ((HalSensorController) controller).setListener(this);
if(controller instanceof HalEventController) ((HalSensorController) controller).initialize();
}
if(controller instanceof HalEventController) {
((HalEventController) controller).setListener(this); ((HalEventController) controller).setListener(this);
if( ! (controller instanceof HalSensorController))
((HalEventController) controller).initialize();
}
controllerMap.put(c, controller); controllerMap.put(c, controller);
} catch (Exception e){ } catch (Exception e){

View file

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

View file

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

View file

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

View file

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

View file

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