diff --git a/src/main/java/de/hottis/mbusMaster/MbusMaster.java b/src/main/java/de/hottis/mbusMaster/MbusMaster.java index 99af8b4..6a531b5 100644 --- a/src/main/java/de/hottis/mbusMaster/MbusMaster.java +++ b/src/main/java/de/hottis/mbusMaster/MbusMaster.java @@ -11,7 +11,7 @@ import org.apache.logging.log4j.Logger; public class MbusMaster { static final String PROPS_FILENAME = "mbusMaster.props"; static final Logger logger = LogManager.getRootLogger(); - + static boolean stopSignal = false; public static void main(String[] args) throws Exception { logger.info("MbusMaster starting"); @@ -24,23 +24,43 @@ public class MbusMaster { logger.debug("Configuration loaded"); */ + /* + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + stopSignal = true; + })); + logger.debug("Shutdown hook added"); + */ + MbusgwChild mbusgw = new MbusgwChild(false); mbusgw.start(); + byte[] devices = { (byte)80, (byte)81, (byte)82, (byte)83, (byte)84, (byte)85, (byte)86, (byte)87 }; - try { - mbusgw.sendRequest((byte)0x5b, (byte)80); + int cnt = 0; + while (! stopSignal) { + System.out.println("--- " + cnt + " ----------------------------------------------------"); + cnt++; + for (byte device : devices) { + System.out.println("Querying device " + device); + try { + mbusgw.sendRequest((byte)0x5b, device); - byte[] frame = mbusgw.collectResponse(); - for (byte x : frame) { - System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " "); + byte[] frame = mbusgw.collectResponse(); + for (byte x : frame) { + System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " "); + } + System.out.println(); + } catch (IOException e) { + logger.error("Error " + e.toString() + ", " + e.getMessage() + " in Meterbus dialog for device " + device); + } } - System.out.println(); - } catch (IOException e) { - logger.error("Error in Meterbus dialog: " + e.toString() + ", " + e.getMessage()); + // if (cnt >= 10) { + // break; + //} + Thread.sleep(15*1000); } - System.out.println("Stopping mbusgw process"); + logger.info("Stopping mbusgw process"); mbusgw.stop(); } diff --git a/src/main/java/de/hottis/mbusMaster/MbusgwChild.java b/src/main/java/de/hottis/mbusMaster/MbusgwChild.java index 149d445..d916a77 100644 --- a/src/main/java/de/hottis/mbusMaster/MbusgwChild.java +++ b/src/main/java/de/hottis/mbusMaster/MbusgwChild.java @@ -63,9 +63,10 @@ public class MbusgwChild { logger.debug("stderrToLog thread started"); } - public void stop() throws InterruptedException { + public void stop() throws InterruptedException, IOException { logger.info("About to stop mbusgw child process"); - this.mbusgwProcess.destroy(); + this.sendRequest((byte)0, (byte)0); + // this.mbusgwProcess.destroy(); this.mbusgwProcess.waitFor(); logger.info("Process stopped"); diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 1177afa..3e6281e 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -9,7 +9,7 @@ - +