Skip to content

Commit

Permalink
added better threading for reading data from bms and sending to inverter
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Feb 24, 2024
1 parent 0d7af2b commit 77e8a73
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down
57 changes: 15 additions & 42 deletions core-api/src/main/java/com/airepublic/bmstoinverter/core/BMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BatteryPack> batteryPacks = new ArrayList<>();
private int pollInterval;
private long delayAfterNoBytes;
private BMSConfig config;
@Inject
private transient EnergyStorage energyStorage;

Expand All @@ -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();
}


Expand All @@ -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();
}


Expand Down Expand Up @@ -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();
}


Expand All @@ -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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}


Expand All @@ -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();
}


Expand All @@ -48,7 +55,7 @@ public int getSendInterval() {
* @return the assigned {@link Port}s locator
*/
public String getPortLocator() {
return portLocator;
return config.getPortLocator();
}


Expand Down

0 comments on commit 77e8a73

Please sign in to comment.