Skip to content

Commit

Permalink
reduced bean scanning and fixed injected energy storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Feb 19, 2024
1 parent 985ce39 commit aea0c56
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void handleMessage(final ByteBuffer receiveFrame) {
final byte[] dataBytes = new byte[receiveFrame.get(4)];
receiveFrame.get(8, dataBytes);

final ByteBuffer data = ByteBuffer.wrap(dataBytes);
final ByteBuffer data = ByteBuffer.wrap(dataBytes).order(ByteOrder.LITTLE_ENDIAN);

switch (frameId) {
case 0x4210:
Expand Down Expand Up @@ -159,9 +159,9 @@ private void readBatteryStatus(final BatteryPack pack, final ByteBuffer data) {
// second level temperature (0.1 Celcius) offset -100
pack.tempAverage = data.getShort() - 1000;
// Battery SOC (1%)
pack.packSOC = data.get();
pack.packSOC = data.get() * 10;
// Battery SOH (1%)
pack.packSOH = data.get();
pack.packSOH = data.get() * 10;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private void analyseBMSFaults() {
final StringBuffer alarmContent = new StringBuffer();
Email email = null;

for (int i = 0; i < energyStorage.getBatteryPacks().length; i++) {
for (int i = 0; i < energyStorage.getBatteryPacks().size(); i++) {
for (final String key : getAlarmState(energyStorage.getBatteryPack(i))) {
currentAlarms.add("BMS #" + (i + 1) + ":" + key);
alarmContent.append("\tBMS #" + (i + 1) + ":\t" + key + "\r\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.airepublic.bmstoinverter.core.bms.data.EnergyStorage;
import com.airepublic.bmstoinverter.core.util.Util;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.CDI;

@ApplicationScoped
public class BMSListProducer {
private final static Logger LOG = LoggerFactory.getLogger(BMSListProducer.class);
private static EnergyStorage energyStorage = new EnergyStorage();
private static List<BMS> bmsList = null;
private final Map<String, BMSDescriptor> bmsDescriptors = new HashMap<>();

Expand All @@ -29,13 +33,13 @@ public BMSListProducer() {


/**
* Gets the {@link BMSDescriptor} for the specified name.
* Provides the application wide {@link EnergyStorage} object.
*
* @param name the name for the {@link BMSDescriptor}
* @return the {@link BMSDescriptor}
* @return the application wide {@link EnergyStorage} object
*/
public BMSDescriptor getBMSDescriptor(final String name) {
return bmsDescriptors.get(name);
@Produces
public EnergyStorage getEnergyStorage() {
return energyStorage;
}


Expand Down Expand Up @@ -73,13 +77,26 @@ public synchronized List<BMS> produceBMSList() {
private BMS createBMS(final int bmsNo, final String name) {
final BMSDescriptor bmsDescriptor = getBMSDescriptor(name);
final BMS bms = CDI.current().select(bmsDescriptor.getBMSClass()).get();
energyStorage.getBatteryPacks().add(bms.getBatteryPack());
final String portLocator = System.getProperty("bms." + bmsNo + ".portLocator");
final int pollInverval = Integer.valueOf(System.getProperty("bms." + bmsNo + ".pollInterval"));
final int delayAfterNoBytes = Integer.valueOf(System.getProperty("bms." + bmsNo + ".delayAfterNoBytes"));
final BMSConfig config = new BMSConfig(bmsNo, portLocator, pollInverval, delayAfterNoBytes, bmsDescriptor);
bms.initialize(config);

LOG.info("Intialized BMS #" + config.getBmsNo() + "[" + config.getDescriptor().getName() + "] on port " + portLocator);

return bms;
}


/**
* Gets the {@link BMSDescriptor} for the specified name.
*
* @param name the name for the {@link BMSDescriptor}
* @return the {@link BMSDescriptor}
*/
public BMSDescriptor getBMSDescriptor(final String name) {
return bmsDescriptors.get(name);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import com.airepublic.bmstoinverter.core.util.Util;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.CDI;

@ApplicationScoped
public class InverterProducer {
private final static Logger LOG = LoggerFactory.getLogger(InverterProducer.class);
private static Inverter inverter = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.airepublic.bmstoinverter.core.bms.data;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

/**
* This class holds the data of the all battery storage modules ({@link BatteryPack} of the system.
*/
public class EnergyStorage {
private transient final static Gson gson = new Gson();
private BatteryPack[] batteryPacks;
private List<BatteryPack> batteryPacks = new ArrayList<>();

/**
* Constructor.
*
* @param numBatteryPacks the number of {@link BatteryPack}s in the system.
*/
public EnergyStorage(final BatteryPack[] batteryPacks) {
this.batteryPacks = batteryPacks;
public EnergyStorage() {
}


Expand All @@ -24,7 +24,7 @@ public EnergyStorage(final BatteryPack[] batteryPacks) {
*
* @return the battery packs
*/
public BatteryPack[] getBatteryPacks() {
public List<BatteryPack> getBatteryPacks() {
return batteryPacks;
}

Expand All @@ -34,7 +34,7 @@ public BatteryPack[] getBatteryPacks() {
*
* @param batteryPacks the battery packs to set
*/
public void setBatteryPacks(final BatteryPack[] batteryPacks) {
public void setBatteryPacks(final List<BatteryPack> batteryPacks) {
this.batteryPacks = batteryPacks;
}

Expand All @@ -46,7 +46,7 @@ public void setBatteryPacks(final BatteryPack[] batteryPacks) {
* @return the {@link BatteryPack}
*/
public BatteryPack getBatteryPack(final int bmsNo) {
return batteryPacks[bmsNo];
return batteryPacks.get(bmsNo);
}


Expand All @@ -56,7 +56,7 @@ public BatteryPack getBatteryPack(final int bmsNo) {
* @return the number of {@link BatteryPack}s in the system
*/
public int getBatteryPackCount() {
return batteryPacks.length;
return batteryPacks.size();
}


Expand Down
2 changes: 1 addition & 1 deletion core-api/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all" version="4.0">
bean-discovery-mode="annotated" version="4.0">

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.stream.Stream;

import com.airepublic.bmstoinverter.core.Inverter;
import com.airepublic.bmstoinverter.core.bms.data.EnergyStorage;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

/**
* The class to handle CAN messages for Deye {@link Inverter}.
*/
@ApplicationScoped
public class DeyeInverterCANProcessor extends Inverter {
@Inject
private EnergyStorage energyStorage;
Expand Down Expand Up @@ -57,8 +58,8 @@ private ByteBuffer createChargeDischargeInfo() {

// 0x355
private ByteBuffer createSOC() {
final int aggregatedSOC = (int) Stream.of(energyStorage.getBatteryPacks()).mapToInt(pack -> pack.packSOC).average().orElse(50);
final int aggregatedSOH = (int) Stream.of(energyStorage.getBatteryPacks()).mapToInt(pack -> pack.packSOH).average().orElse(50);
final int aggregatedSOC = (int) energyStorage.getBatteryPacks().stream().mapToInt(pack -> pack.packSOC).average().orElse(50);
final int aggregatedSOH = (int) energyStorage.getBatteryPacks().stream().mapToInt(pack -> pack.packSOH).average().orElse(50);

final ByteBuffer frame = ByteBuffer.allocateDirect(16);
frame.putInt(0x0355)
Expand All @@ -77,9 +78,9 @@ private ByteBuffer createSOC() {

// 0x356
private ByteBuffer createBatteryVoltage() {
final int aggregatedPackVoltage = (int) Stream.of(energyStorage.getBatteryPacks()).mapToInt(pack -> pack.packVoltage).average().orElse(500) * 10;
final int aggregatedPackCurrent = Stream.of(energyStorage.getBatteryPacks()).mapToInt(pack -> pack.packCurrent).sum();
final int aggregatedPackTemperature = (int) Stream.of(energyStorage.getBatteryPacks()).mapToInt(pack -> pack.tempMax).average().orElse(35) * 10;
final int aggregatedPackVoltage = (int) energyStorage.getBatteryPacks().stream().mapToInt(pack -> pack.packVoltage).average().orElse(500) * 10;
final int aggregatedPackCurrent = energyStorage.getBatteryPacks().stream().mapToInt(pack -> pack.packCurrent).sum();
final int aggregatedPackTemperature = (int) energyStorage.getBatteryPacks().stream().mapToInt(pack -> pack.tempMax).average().orElse(35) * 10;

final ByteBuffer frame = ByteBuffer.allocateDirect(16);
frame.putInt(0x0356)
Expand Down Expand Up @@ -116,21 +117,21 @@ private ByteBuffer createManufacturer() {

// 0x359
private ByteBuffer createAlarms() {
final boolean aggregatedLevelTwoCellVoltageTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoCellVoltageTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoCellVoltageTooLow = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoCellVoltageTooLow).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeTempTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoDischargeTempTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeTempTooLow = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoDischargeTempTooLow).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeCurrentTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoDischargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoChargeCurrentTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoChargeCurrentTooHigh).anyMatch(b -> true);

final boolean aggregatedLevelOneCellVoltageTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneCellVoltageTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneCellVoltageTooLow = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneCellVoltageTooLow).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeTempTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneChargeTempTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeTempTooLow = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneChargeTempTooLow).anyMatch(b -> true);
final boolean aggregatedLevelOneDischargeCurrentTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneDischargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeCurrentTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelOneChargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedFailureOfIntranetCommunicationModule = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.failureOfIntranetCommunicationModule).anyMatch(b -> true);
final boolean aggregatedLevelTwoCellVoltageDifferenceTooHigh = Stream.of(energyStorage.getBatteryPacks()).map(pack -> pack.alarms.levelTwoCellVoltageDifferenceTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoCellVoltageTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoCellVoltageTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoCellVoltageTooLow = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoCellVoltageTooLow).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeTempTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoDischargeTempTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeTempTooLow = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoDischargeTempTooLow).anyMatch(b -> true);
final boolean aggregatedLevelTwoDischargeCurrentTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoDischargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelTwoChargeCurrentTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoChargeCurrentTooHigh).anyMatch(b -> true);

final boolean aggregatedLevelOneCellVoltageTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneCellVoltageTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneCellVoltageTooLow = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneCellVoltageTooLow).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeTempTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneChargeTempTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeTempTooLow = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneChargeTempTooLow).anyMatch(b -> true);
final boolean aggregatedLevelOneDischargeCurrentTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneDischargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedLevelOneChargeCurrentTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelOneChargeCurrentTooHigh).anyMatch(b -> true);
final boolean aggregatedFailureOfIntranetCommunicationModule = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.failureOfIntranetCommunicationModule).anyMatch(b -> true);
final boolean aggregatedLevelTwoCellVoltageDifferenceTooHigh = energyStorage.getBatteryPacks().stream().map(pack -> pack.alarms.levelTwoCellVoltageDifferenceTooHigh).anyMatch(b -> true);

final BitSet bits = new BitSet(32);
final ByteBuffer frame = ByteBuffer.allocateDirect(16);
Expand Down
2 changes: 1 addition & 1 deletion inverter-deye-can/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all" version="4.0">
bean-discovery-mode="annotated" version="4.0">

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

import com.airepublic.bmstoinverter.core.Inverter;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Default;

/**
* The class to handle CAN messages for a SMA {@link Inverter}.
*/
@ApplicationScoped
@Default
public class DummyInverterProcessor extends Inverter {

@Override
Expand Down
2 changes: 1 addition & 1 deletion inverter-dummy/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all" version="4.0">
bean-discovery-mode="annotated" version="4.0">

</beans>
Loading

0 comments on commit aea0c56

Please sign in to comment.