put process handling in separate class
This commit is contained in:
parent
39199f0d95
commit
1911edc49e
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
target/
|
||||
*~
|
||||
.*~
|
||||
|
@ -1,10 +1,5 @@
|
||||
package de.hottis.mbusMaster;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@ -27,40 +22,20 @@ public class MbusMaster {
|
||||
logger.debug("Configuration loaded");
|
||||
*/
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder("/usr/local/bin/mbusgw", "-l", "-v");
|
||||
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
|
||||
Process p = pb.start();
|
||||
System.out.println("Process started");
|
||||
MbusgwChild mbusgw = new MbusgwChild(true);
|
||||
mbusgw.start();
|
||||
|
||||
|
||||
mbusgw.sendRequest((byte)0x5b, (byte)80);
|
||||
|
||||
InputStream i = p.getInputStream();
|
||||
OutputStream o = p.getOutputStream();
|
||||
System.out.println("Streams collected");
|
||||
|
||||
byte[] b = { 0x5b, 80 };
|
||||
|
||||
o.write(b);
|
||||
System.out.println("Data written");
|
||||
|
||||
o.flush();
|
||||
System.out.println("Data flushed");
|
||||
|
||||
|
||||
byte[] header = new byte[2];
|
||||
int n = i.read(header, 0, 2);
|
||||
int responseCode = Byte.toUnsignedInt(header[0]);
|
||||
int responseLen = Byte.toUnsignedInt(header[1]);
|
||||
System.out.println("n: " + n + ", h: " + responseCode + ", l: " + responseLen);
|
||||
|
||||
byte[] frame = new byte[responseLen];
|
||||
n = i.read(frame, 0, responseLen);
|
||||
byte[] frame = mbusgw.collectResponse();
|
||||
for (byte x : frame) {
|
||||
System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " ");
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
|
||||
System.out.println("Stopping mbusgw process");
|
||||
mbusgw.stop();
|
||||
|
||||
}
|
||||
}
|
||||
|
100
src/main/java/de/hottis/mbusMaster/MbusgwChild.java
Normal file
100
src/main/java/de/hottis/mbusMaster/MbusgwChild.java
Normal file
@ -0,0 +1,100 @@
|
||||
package de.hottis.mbusMaster;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
|
||||
public class MbusgwChild {
|
||||
static final Logger logger = LogManager.getRootLogger();
|
||||
|
||||
|
||||
private Process mbusgwProcess;
|
||||
private InputStream processInput;
|
||||
private OutputStream processOutput;
|
||||
private boolean verbose;
|
||||
private Thread stderrToLog;
|
||||
|
||||
|
||||
public MbusgwChild(boolean verbose) {
|
||||
this.verbose = verbose;
|
||||
}
|
||||
|
||||
public void start() throws IOException {
|
||||
logger.info("InterfaceChild starting");
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder("/usr/local/bin/mbusgw", "-l", "-v");
|
||||
this.mbusgwProcess = pb.start();
|
||||
logger.debug("Process started");
|
||||
|
||||
this.processInput = this.mbusgwProcess.getInputStream();
|
||||
InputStream processError = this.mbusgwProcess.getErrorStream();
|
||||
this.processOutput = this.mbusgwProcess.getOutputStream();
|
||||
logger.debug("Streams collected");
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(processError));
|
||||
|
||||
this.stderrToLog = new Thread(() -> {
|
||||
String line;
|
||||
try {
|
||||
while ((line = br.readLine()) != null) {
|
||||
logger.debug(line);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.debug("exception caught: " + ex.getMessage());
|
||||
}
|
||||
}, "stderrToLog");
|
||||
stderrToLog.start();
|
||||
logger.debug("stderrToLog thread started");
|
||||
}
|
||||
|
||||
public void stop() throws InterruptedException {
|
||||
logger.info("About to stop mbusgw child process");
|
||||
this.mbusgwProcess.destroy();
|
||||
this.mbusgwProcess.waitFor();
|
||||
logger.info("Process stopped");
|
||||
|
||||
logger.info("About to stop stderrToLog thread");
|
||||
this.stderrToLog.interrupt();
|
||||
this.stderrToLog.join();
|
||||
logger.info("Thread joined");
|
||||
}
|
||||
|
||||
public InputStream getProcessInputStream() {
|
||||
return this.processInput;
|
||||
}
|
||||
|
||||
public OutputStream getProcessOutputStream() {
|
||||
return this.processOutput;
|
||||
}
|
||||
|
||||
public void sendRequest(byte cmd, byte addr) throws IOException {
|
||||
byte[] b = { cmd, addr };
|
||||
|
||||
this.processOutput.write(b);
|
||||
logger.debug("Request written");
|
||||
|
||||
this.processOutput.flush();
|
||||
logger.debug("Request flushed");
|
||||
}
|
||||
|
||||
public byte[] collectResponse() throws IOException {
|
||||
byte[] header = new byte[2];
|
||||
int n = this.processInput.read(header, 0, 2);
|
||||
int responseCode = Byte.toUnsignedInt(header[0]);
|
||||
int responseLen = Byte.toUnsignedInt(header[1]);
|
||||
logger.debug("n: " + n + ", h: " + responseCode + ", l: " + responseLen);
|
||||
byte[] frame = new byte[responseLen];
|
||||
n = this.processInput.read(frame, 0, responseLen);
|
||||
logger.debug("frame completely read");
|
||||
return frame;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration debug="false">
|
||||
<Configuration debug="true">
|
||||
<Appenders>
|
||||
<Console name="ConsoleAppender" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d [%t, %F, %L] %-5level - %msg%n%throwable"/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user