threads
This commit is contained in:
		
							
								
								
									
										82
									
								
								src/main/java/de/hottis/mbusMaster/ConfigProperties.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								src/main/java/de/hottis/mbusMaster/ConfigProperties.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
package de.hottis.mbusMaster;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
 | 
			
		||||
import java.util.Properties;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class ConfigProperties extends Properties {
 | 
			
		||||
	static final String DEFAULT_PROPS_FILENAME = "mbusMaster.props";
 | 
			
		||||
  static final String PROPS_VERBOSE = "verbose";
 | 
			
		||||
  static final String PRPOS_MAINCONFIGFILE = "mainConfigFile";
 | 
			
		||||
 | 
			
		||||
  static final Logger logger = LogManager.getRootLogger();
 | 
			
		||||
 | 
			
		||||
  private boolean overwriteVerbose;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  public ConfigProperties() throws ConfigPropertiesException {
 | 
			
		||||
    super();
 | 
			
		||||
    String propsFilename = System.getProperty(PRPOS_MAINCONFIGFILE, DEFAULT_PROPS_FILENAME); 
 | 
			
		||||
    try {
 | 
			
		||||
      try (FileInputStream propsFileInputStream = new FileInputStream(propsFilename)) {
 | 
			
		||||
        load(propsFileInputStream);
 | 
			
		||||
      }
 | 
			
		||||
    logger.debug("Configuration loaded");
 | 
			
		||||
    } catch (FileNotFoundException e) {
 | 
			
		||||
      String msg = "Config file " + propsFilename + " not found";
 | 
			
		||||
      logger.error(msg);
 | 
			
		||||
      throw new ConfigPropertiesException(msg);
 | 
			
		||||
    } catch (IOException e) {
 | 
			
		||||
      String msg = "Error when reading config file " + propsFilename;
 | 
			
		||||
      logger.error(msg);
 | 
			
		||||
      throw new ConfigPropertiesException(msg, e);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String overwriteVerboseStr = System.getProperty(PROPS_VERBOSE, "no");
 | 
			
		||||
    this.overwriteVerbose = ("yes".equalsIgnoreCase(overwriteVerboseStr) || "true".equalsIgnoreCase(overwriteVerboseStr));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getStringProperty(String key) throws ConfigPropertiesException {
 | 
			
		||||
    String returnValue = this.getProperty(key);
 | 
			
		||||
    if (returnValue == null) {
 | 
			
		||||
      throw new ConfigPropertiesException(key + "not found");
 | 
			
		||||
    }
 | 
			
		||||
    return returnValue;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  String getStringProperty(String key, String def) {
 | 
			
		||||
    String returnValue;
 | 
			
		||||
    try {
 | 
			
		||||
      returnValue = this.getStringProperty(key);
 | 
			
		||||
    } catch (ConfigPropertiesException  e) {
 | 
			
		||||
      returnValue = def;
 | 
			
		||||
    }
 | 
			
		||||
    return returnValue;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public boolean getBooleanProperty(String key) throws ConfigPropertiesException {
 | 
			
		||||
    String v = this.getStringProperty(key);
 | 
			
		||||
    return ("yes".equalsIgnoreCase(v) || "true".equalsIgnoreCase(v));    
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public boolean getBooleanProperty(String key, boolean def) {
 | 
			
		||||
    boolean returnValue;
 | 
			
		||||
    try {
 | 
			
		||||
      returnValue = this.getBooleanProperty(key);
 | 
			
		||||
    } catch (ConfigPropertiesException e) {
 | 
			
		||||
      returnValue = def;
 | 
			
		||||
    }
 | 
			
		||||
    return returnValue;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public boolean isVerbose() {
 | 
			
		||||
    return this.getBooleanProperty("verbose", false) || this.overwriteVerbose;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,21 @@
 | 
			
		||||
package de.hottis.mbusMaster;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
public class ConfigPropertiesException extends Exception {
 | 
			
		||||
    public ConfigPropertiesException() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ConfigPropertiesException(String message, Throwable cause) {
 | 
			
		||||
        super(message, cause);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ConfigPropertiesException(String message) {
 | 
			
		||||
        super(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ConfigPropertiesException(Throwable cause) {
 | 
			
		||||
        super(cause);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main/java/de/hottis/mbusMaster/DummyDequeuer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/main/java/de/hottis/mbusMaster/DummyDequeuer.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package de.hottis.mbusMaster;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.BlockingQueue;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
public class DummyDequeuer extends Thread {
 | 
			
		||||
	static final Logger logger = LogManager.getRootLogger();
 | 
			
		||||
 | 
			
		||||
  private BlockingQueue<String> queue;
 | 
			
		||||
 | 
			
		||||
  public DummyDequeuer(BlockingQueue<String> queue) {
 | 
			
		||||
    super("DummyDequeuer");
 | 
			
		||||
 | 
			
		||||
    this.queue = queue;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void run() {
 | 
			
		||||
    while(true) {
 | 
			
		||||
      try {
 | 
			
		||||
        String o = this.queue.take();
 | 
			
		||||
        System.out.println("DummyDequeuer: " + o);
 | 
			
		||||
      } catch (InterruptedException e) {
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,11 @@
 | 
			
		||||
package de.hottis.mbusMaster;
 | 
			
		||||
 | 
			
		||||
import java.io.FileInputStream;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import java.util.Properties;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.LinkedBlockingQueue;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
@@ -13,20 +15,13 @@ import org.openmuc.jmbus.MBusMessage;
 | 
			
		||||
import org.openmuc.jmbus.VariableDataStructure;
 | 
			
		||||
 | 
			
		||||
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");
 | 
			
		||||
 | 
			
		||||
		/*
 | 
			
		||||
		final Properties config = new Properties();
 | 
			
		||||
		try (FileInputStream propsFileInputStream = new FileInputStream(PROPS_FILENAME)) {
 | 
			
		||||
			config.load(propsFileInputStream);
 | 
			
		||||
		}
 | 
			
		||||
		logger.debug("Configuration loaded");
 | 
			
		||||
		*/
 | 
			
		||||
		final ConfigProperties config = new ConfigProperties();
 | 
			
		||||
 | 
			
		||||
		/*
 | 
			
		||||
		Runtime.getRuntime().addShutdownHook(new Thread(() -> {
 | 
			
		||||
@@ -35,50 +30,19 @@ public class MbusMaster {
 | 
			
		||||
		logger.debug("Shutdown hook added");
 | 
			
		||||
		*/
 | 
			
		||||
 | 
			
		||||
		MbusgwChild mbusgw = new MbusgwChild(false);
 | 
			
		||||
		mbusgw.start();
 | 
			
		||||
		LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
 | 
			
		||||
 | 
			
		||||
		MbusDevice[] devices = {
 | 
			
		||||
			new FinderThreePhasePowerMeter("Total Electricity", (byte)80, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Dryer", (byte)81, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Laundry", (byte)82, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Dishwasher", (byte)83, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Light", (byte)84, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Computer", (byte)85, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Freezer", (byte)86, 0),
 | 
			
		||||
			new FinderOnePhasePowerMeter("Fridge", (byte)87, 0)
 | 
			
		||||
		};
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
		int cnt = 0;
 | 
			
		||||
		int errCnt = 0;
 | 
			
		||||
		int successCnt = 0;
 | 
			
		||||
		while (! stopSignal) {
 | 
			
		||||
			cnt++;
 | 
			
		||||
			for (MbusDevice device : devices) {
 | 
			
		||||
				System.out.println("Querying " + device.getName() + " meter");
 | 
			
		||||
				try {
 | 
			
		||||
					mbusgw.sendRequest((byte)0x5b, device.getAddress());
 | 
			
		||||
					byte[] frame = mbusgw.collectResponse();
 | 
			
		||||
					device.parse(frame);
 | 
			
		||||
 | 
			
		||||
					System.out.println(device);
 | 
			
		||||
		MbusScheduledQuerier querier = new MbusScheduledQuerier(config, queue);
 | 
			
		||||
		querier.start();
 | 
			
		||||
 | 
			
		||||
					successCnt++;
 | 
			
		||||
				} catch (IOException e) {
 | 
			
		||||
					errCnt++;
 | 
			
		||||
					logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device.shortString());
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			// if (cnt >= 10) {
 | 
			
		||||
			//	break;
 | 
			
		||||
			//}
 | 
			
		||||
			System.out.println("--- " + cnt + " - " + successCnt + " - " + errCnt + " ---------------------------------------------------");
 | 
			
		||||
			Thread.sleep(5*1000);
 | 
			
		||||
		}
 | 
			
		||||
		DummyDequeuer ddq = new DummyDequeuer(queue);
 | 
			
		||||
		ddq.start();
 | 
			
		||||
 | 
			
		||||
		logger.info("Stopping mbusgw process");
 | 
			
		||||
		mbusgw.stop();
 | 
			
		||||
 | 
			
		||||
		querier.join();
 | 
			
		||||
		ddq.join();
 | 
			
		||||
		logger.info("MbusMaster terminating");
 | 
			
		||||
 | 
			
		||||
	}		
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +1,86 @@
 | 
			
		||||
package de.hottis.mbusMaster;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.concurrent.BlockingQueue;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class MbusScheduledQuerier {
 | 
			
		||||
public class MbusScheduledQuerier extends Thread {
 | 
			
		||||
	static final Logger logger = LogManager.getRootLogger();
 | 
			
		||||
 | 
			
		||||
  private ArrayList<MbusDevice> devices;
 | 
			
		||||
  private boolean stopSignal = false;
 | 
			
		||||
  private ConfigProperties config;
 | 
			
		||||
  private BlockingQueue<String> queue;
 | 
			
		||||
 | 
			
		||||
  public MbusScheduledQuerier(ConfigProperties config, BlockingQueue<String> queue) {
 | 
			
		||||
    super("MbusScheduledQuerier");
 | 
			
		||||
 | 
			
		||||
    this.config = config;
 | 
			
		||||
    this.queue = queue;
 | 
			
		||||
 | 
			
		||||
  public MbusScheduledQuerier() {
 | 
			
		||||
    this.devices = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
		this.devices.add(new FinderThreePhasePowerMeter("Total Electricity", (byte)80, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Dryer", (byte)81, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Laundry", (byte)82, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Dishwasher", (byte)83, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Light", (byte)84, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Computer", (byte)85, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Freezer", (byte)86, 0));
 | 
			
		||||
		this.devices.add(new FinderOnePhasePowerMeter("Fridge", (byte)87, 0));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setStopSignal() {
 | 
			
		||||
    this.stopSignal = true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void run() {
 | 
			
		||||
    try {
 | 
			
		||||
      MbusgwChild mbusgw = new MbusgwChild(config);
 | 
			
		||||
      mbusgw.start();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
      int cnt = 0;
 | 
			
		||||
      int errCnt = 0;
 | 
			
		||||
      int successCnt = 0;
 | 
			
		||||
 | 
			
		||||
      while (! this.stopSignal) {
 | 
			
		||||
        cnt++;
 | 
			
		||||
        for (MbusDevice device : this.devices) {
 | 
			
		||||
          logger.info("Querying " + device.getName() + " meter");
 | 
			
		||||
          try {
 | 
			
		||||
            mbusgw.sendRequest((byte)0x5b, device.getAddress());
 | 
			
		||||
            byte[] frame = mbusgw.collectResponse();
 | 
			
		||||
            device.parse(frame);
 | 
			
		||||
 | 
			
		||||
            logger.info("Got: " + device.toString());
 | 
			
		||||
            this.queue.add(device.toString());
 | 
			
		||||
            
 | 
			
		||||
            successCnt++;
 | 
			
		||||
          } catch (IOException e) {
 | 
			
		||||
            errCnt++;
 | 
			
		||||
            logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device.shortString());
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        logger.info("Cnt: " + cnt + ", SuccessCnt: " + successCnt + ", ErrCnt: " + errCnt);
 | 
			
		||||
        try {
 | 
			
		||||
          Thread.sleep(5*1000);
 | 
			
		||||
        } catch (InterruptedException e) {
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      logger.info("Stopping mbusgw process");
 | 
			
		||||
      try {
 | 
			
		||||
        mbusgw.stop();
 | 
			
		||||
      } catch (InterruptedException | IOException e) {
 | 
			
		||||
        logger.error("Problems to stop Meterbus Gateway process: " + e.toString() + ", however, terminating anyway");
 | 
			
		||||
      }
 | 
			
		||||
    } catch (IOException e) {
 | 
			
		||||
      logger.error("Unable to start Meterbus Gateway process");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,6 @@ import java.io.BufferedReader;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Properties;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
@@ -16,17 +15,19 @@ import org.apache.logging.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
public class MbusgwChild {
 | 
			
		||||
	static final Logger logger = LogManager.getRootLogger();
 | 
			
		||||
	static final String PROPS_VERBOSE = "verbose";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  private ConfigProperties config;
 | 
			
		||||
  private Process mbusgwProcess;
 | 
			
		||||
  private InputStream processInput;
 | 
			
		||||
  private OutputStream processOutput;
 | 
			
		||||
  private boolean verbose;
 | 
			
		||||
  private Thread stderrToLog;
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  public MbusgwChild(boolean verbose) {
 | 
			
		||||
    this.verbose = verbose;
 | 
			
		||||
  public MbusgwChild(ConfigProperties config) {
 | 
			
		||||
    this.config = config;
 | 
			
		||||
    this.verbose = this.config.isVerbose();
 | 
			
		||||
  }
 | 
			
		||||
	
 | 
			
		||||
	public void start() throws IOException {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user