From 77e8a73366f98f113035bc2fcae0b5c5128574e5 Mon Sep 17 00:00:00 2001 From: Torsten Oltmanns Date: Sat, 24 Feb 2024 14:30:23 +0800 Subject: [PATCH] added better threading for reading data from bms and sending to inverter --- .../bmstoinverter/BmsToInverter.java | 40 ++++++++++--- .../airepublic/bmstoinverter/core/BMS.java | 57 +++++-------------- .../bmstoinverter/core/Inverter.java | 19 +++++-- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/bms-to-inverter-main/src/main/java/com/airepublic/bmstoinverter/BmsToInverter.java b/bms-to-inverter-main/src/main/java/com/airepublic/bmstoinverter/BmsToInverter.java index a96a31e1..e28f02d4 100644 --- a/bms-to-inverter-main/src/main/java/com/airepublic/bmstoinverter/BmsToInverter.java +++ b/bms-to-inverter-main/src/main/java/com/airepublic/bmstoinverter/BmsToInverter.java @@ -8,7 +8,6 @@ import java.util.StringTokenizer; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -156,10 +155,24 @@ public void start() { try { LOG.info("Starting BMS receiver..."); - for (final BMS bms : bmsList) { - // receive BMS data - executorService.scheduleWithFixedDelay(() -> bms.process(() -> receivedData()), 1, bms.getPollInterval(), TimeUnit.SECONDS); - } + + new Thread(() -> { + do { + for (final BMS bms : bmsList) { + try { + LOG.info("Reading BMS #" + bms.getBmsNo() + bms.getName() + " on " + bms.getPortLocator() + "..."); + bms.process(() -> receivedData()); + // TODO set poll intverall for all bms together not each + Thread.sleep(bms.getPollInterval() * 1000); + } catch (final Throwable e) { + } + } + + } while (true); + }).start(); + + // executorService.scheduleWithFixedDelay(() -> bms.process(() -> receivedData()), + // 1, bms.getPollInterval(), TimeUnit.SECONDS); // wait for the first data to be received synchronized (this) { @@ -170,9 +183,22 @@ public void start() { if (inverter != null) { LOG.info("Starting inverter sender..."); - executorService.scheduleWithFixedDelay(() -> inverter.process(() -> sentData()), 1, inverter.getSendInterval(), TimeUnit.SECONDS); + new Thread(() -> { + do { + try { + LOG.info("Sending to inverter " + inverter.getName() + " on " + inverter.getPortLocator() + "..."); + inverter.process(() -> sentData()); + Thread.sleep(inverter.getSendInterval() * 1000); + } catch (final Throwable e) { + } + } while (true); + }).start(); + // executorService.scheduleWithFixedDelay(() -> inverter.process(() -> sentData()), + // 1, inverter.getSendInterval(), TimeUnit.SECONDS); } - } catch (final Throwable e) { + } catch ( + + final Throwable e) { LOG.error("Error occured during processing!", e); } } diff --git a/core-api/src/main/java/com/airepublic/bmstoinverter/core/BMS.java b/core-api/src/main/java/com/airepublic/bmstoinverter/core/BMS.java index 08d2c9bb..ec5df745 100644 --- a/core-api/src/main/java/com/airepublic/bmstoinverter/core/BMS.java +++ b/core-api/src/main/java/com/airepublic/bmstoinverter/core/BMS.java @@ -17,11 +17,8 @@ */ public abstract class BMS { private final static Logger LOG = LoggerFactory.getLogger(BMS.class); - private int bmsNo; - private String portLocator; private final List batteryPacks = new ArrayList<>(); - private int pollInterval; - private long delayAfterNoBytes; + private BMSConfig config; @Inject private transient EnergyStorage energyStorage; @@ -36,31 +33,27 @@ public void initialize(final BMSConfig config) { final Port port = config.getDescriptor().createPort(config); PortAllocator.addPort(config.getPortLocator(), port); } - - bmsNo = config.getBmsNo(); - portLocator = config.getPortLocator(); - setPollInterval(config.getPollInterval()); - setDelayAfterNoBytes(config.getDelayAfterNoBytes()); + this.config = config; } /** - * Gets the assigned BMS number. - * - * @return the assigned BMS number + * Gets the name of the {@link BMSDescriptor}. + * + * @return the name */ - public int getBmsNo() { - return bmsNo; + public String getName() { + return config.getDescriptor().getName(); } /** - * Sets the assigned BMS number. - * - * @param bmsNo the assigned BMS number + * Gets the assigned BMS number. + * + * @return the assigned BMS number */ - public void setBmsNo(final int bmsNo) { - this.bmsNo = bmsNo; + public int getBmsNo() { + return config.getBmsNo(); } @@ -70,7 +63,7 @@ public void setBmsNo(final int bmsNo) { * @return the assigned {@link Port}s locator */ public String getPortLocator() { - return portLocator; + return config.getPortLocator(); } @@ -109,17 +102,7 @@ public BatteryPack getBatteryPack(final int bmsNo) { * @return the polling interval in seconds */ public int getPollInterval() { - return pollInterval; - } - - - /** - * Sets the polling interval in seconds - * - * @param pollInterval the polling interval in seconds - */ - public void setPollInterval(final int pollInterval) { - this.pollInterval = pollInterval; + return config.getPollInterval(); } @@ -129,17 +112,7 @@ public void setPollInterval(final int pollInterval) { * @return the delay after no bytes were received in milliseconds */ public long getDelayAfterNoBytes() { - return delayAfterNoBytes; - } - - - /** - * Sets the delay after no bytes were received in milliseconds. - * - * @param delayAfterNoBytes the delay after no bytes were received in milliseconds - */ - public void setDelayAfterNoBytes(final long delayAfterNoBytes) { - this.delayAfterNoBytes = delayAfterNoBytes; + return config.getDelayAfterNoBytes(); } diff --git a/core-api/src/main/java/com/airepublic/bmstoinverter/core/Inverter.java b/core-api/src/main/java/com/airepublic/bmstoinverter/core/Inverter.java index 4bd95749..2904490f 100644 --- a/core-api/src/main/java/com/airepublic/bmstoinverter/core/Inverter.java +++ b/core-api/src/main/java/com/airepublic/bmstoinverter/core/Inverter.java @@ -15,8 +15,7 @@ */ public abstract class Inverter { private final static Logger LOG = LoggerFactory.getLogger(Inverter.class); - private String portLocator; - private int sendInterval; + private InverterConfig config; /** * Initializes the {@link Inverter} with the specified {@link InverterConfig}, initializing the @@ -26,9 +25,17 @@ public void initialize(final InverterConfig config) { if (!PortAllocator.hasPort(config.getPortLocator())) { PortAllocator.addPort(config.getPortLocator(), config.getDescriptor().createPort(config)); } + this.config = config; + } + - portLocator = config.getPortLocator(); - sendInterval = config.getSendInterval(); + /** + * Gets the name of the {@link InverterDescriptor}. + * + * @return the name + */ + public String getName() { + return config.getDescriptor().getName(); } @@ -38,7 +45,7 @@ public void initialize(final InverterConfig config) { * @return the interval the data is sent to the inverter */ public int getSendInterval() { - return sendInterval; + return config.getSendInterval(); } @@ -48,7 +55,7 @@ public int getSendInterval() { * @return the assigned {@link Port}s locator */ public String getPortLocator() { - return portLocator; + return config.getPortLocator(); }