Skip to content

Commit

Permalink
renamed bmsNo to bmsId for further ToDos
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Feb 24, 2024
1 parent e9b2642 commit b7c728f
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class DalyBmsCANProcessor extends AbstractDalyBmsProcessor {
private final ByteBuffer sendFrame = ByteBuffer.allocateDirect(16).order(ByteOrder.LITTLE_ENDIAN);

@Override
protected List<ByteBuffer> sendMessage(final Port port, final int bmsNo, final DalyCommand cmd, final byte[] data) throws IOException, NoDataAvailableException {
final ByteBuffer sendFrame = prepareSendFrame(bmsNo + 1, cmd, data);
protected List<ByteBuffer> sendMessage(final Port port, final int bmsId, final DalyCommand cmd, final byte[] data) throws IOException, NoDataAvailableException {
final ByteBuffer sendFrame = prepareSendFrame(bmsId - 1, cmd, data);
int framesToBeReceived = getResponseFrameCount(cmd);
final int frameCount = framesToBeReceived;
int skip = 20;
Expand Down Expand Up @@ -87,17 +87,17 @@ protected List<ByteBuffer> sendMessage(final Port port, final int bmsNo, final D
}
} while (framesToBeReceived > 0 & skip > 0);

LOG.debug("Command 0x{} to BMS {} successfully sent and received!", HexFormat.of().toHexDigits(cmd.id), bmsNo + 1);
LOG.debug("Command 0x{} to BMS {} successfully sent and received!", HexFormat.of().toHexDigits(cmd.id), bmsId);
return readBuffers;
}


@Override
protected ByteBuffer prepareSendFrame(final int address, final DalyCommand cmd, final byte[] data) {
protected ByteBuffer prepareSendFrame(final int bmsId, final DalyCommand cmd, final byte[] data) {
sendFrame.rewind();

sendFrame.put((byte) 0x40);
sendFrame.put((byte) address);
sendFrame.put((byte) bmsId);
sendFrame.put((byte) cmd.id);
sendFrame.put((byte) 0x18);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ public DalyMessageHandler getMessageHandler() {

@Override
protected void collectData(final Port port) throws IOException, TooManyInvalidFramesException, NoDataAvailableException {
final int bmsNo = getBmsNo();
final int bmsId = getBmsId();

if (initialRound) {
try {
sendMessage(port, bmsNo, DalyCommand.READ_RATED_CAPACITY_CELL_VOLTAGE, requestData); // 0x50
sendMessage(port, bmsNo, DalyCommand.READ_BATTERY_TYPE_INFO, requestData); // 0x53
sendMessage(port, bmsNo, DalyCommand.READ_MIN_MAX_PACK_VOLTAGE, requestData); // 0x5A
sendMessage(port, bmsNo, DalyCommand.READ_MAX_PACK_DISCHARGE_CHARGE_CURRENT, requestData); // 0x5B
sendMessage(port, bmsId, DalyCommand.READ_RATED_CAPACITY_CELL_VOLTAGE, requestData); // 0x50
sendMessage(port, bmsId, DalyCommand.READ_BATTERY_TYPE_INFO, requestData); // 0x53
sendMessage(port, bmsId, DalyCommand.READ_MIN_MAX_PACK_VOLTAGE, requestData); // 0x5A
sendMessage(port, bmsId, DalyCommand.READ_MAX_PACK_DISCHARGE_CHARGE_CURRENT, requestData); // 0x5B

initialRound = false;
} catch (final Throwable t) {
LOG.error("Failed to initialize BMS!", t);
}
}

sendMessage(port, bmsNo, DalyCommand.READ_VOUT_IOUT_SOC, requestData); // 0x90
sendMessage(port, bmsNo, DalyCommand.READ_MIN_MAX_CELL_VOLTAGE, requestData); // 0x91
sendMessage(port, bmsNo, DalyCommand.READ_MIN_MAX_TEMPERATURE, requestData); // 0x92
sendMessage(port, bmsNo, DalyCommand.READ_DISCHARGE_CHARGE_MOS_STATUS, requestData); // 0x93
sendMessage(port, bmsNo, DalyCommand.READ_STATUS_INFO, requestData); // 0x94
sendMessage(port, bmsNo, DalyCommand.READ_CELL_VOLTAGES, requestData); // 0x95
sendMessage(port, bmsNo, DalyCommand.READ_CELL_TEMPERATURE, requestData); // 0x96
sendMessage(port, bmsNo, DalyCommand.READ_CELL_BALANCE_STATE, requestData); // 0x97
sendMessage(port, bmsNo, DalyCommand.READ_FAILURE_CODES, requestData); // 0x98
sendMessage(port, bmsId, DalyCommand.READ_VOUT_IOUT_SOC, requestData); // 0x90
sendMessage(port, bmsId, DalyCommand.READ_MIN_MAX_CELL_VOLTAGE, requestData); // 0x91
sendMessage(port, bmsId, DalyCommand.READ_MIN_MAX_TEMPERATURE, requestData); // 0x92
sendMessage(port, bmsId, DalyCommand.READ_DISCHARGE_CHARGE_MOS_STATUS, requestData); // 0x93
sendMessage(port, bmsId, DalyCommand.READ_STATUS_INFO, requestData); // 0x94
sendMessage(port, bmsId, DalyCommand.READ_CELL_VOLTAGES, requestData); // 0x95
sendMessage(port, bmsId, DalyCommand.READ_CELL_TEMPERATURE, requestData); // 0x96
sendMessage(port, bmsId, DalyCommand.READ_CELL_BALANCE_STATE, requestData); // 0x97
sendMessage(port, bmsId, DalyCommand.READ_FAILURE_CODES, requestData); // 0x98
}


Expand All @@ -80,8 +80,8 @@ protected void collectData(final Port port) throws IOException, TooManyInvalidFr
* @param port the {@link Port} of the {@link BMS}
*/
protected void autoCalibrateSOC(final Port port) {
final int bmsNo = 0;
final BatteryPack battery = getBatteryPack(bmsNo);
final int bmsId = 0;
final BatteryPack battery = getBatteryPack(bmsId);
final int calculatedSOC = (int) (((float) battery.packVoltage - battery.minPackVoltageLimit) * 100 / (battery.maxPackVoltageLimit - battery.minPackVoltageLimit) * 10);
final byte[] data = new byte[8];
final LocalDateTime date = LocalDateTime.now();
Expand All @@ -103,7 +103,7 @@ protected void autoCalibrateSOC(final Port port) {
final Future<List<ByteBuffer>> future = executor.submit(() -> {

LOG.info("calibrate request (SOC " + calculatedSOC + "): " + HexFormat.of().withUpperCase().withDelimiter(", 0x").formatHex(data));
final List<ByteBuffer> result = sendMessage(port, getBmsNo(), DalyCommand.WRITE_RTC_AND_SOC, data);
final List<ByteBuffer> result = sendMessage(port, getBmsId(), DalyCommand.WRITE_RTC_AND_SOC, data);
LOG.info("calibrate result: " + Port.printBuffer(result.get(0)));
return result;
});
Expand All @@ -123,13 +123,13 @@ protected void autoCalibrateSOC(final Port port) {
* Sends the specified {@link DalyCommand} and frame data to the specified BMS.
*
* @param port the {@link Port} to use
* @param bmsNo the bms to send to
* @param bmsId the bms to send to
* @param cmd the {@link DalyCommand}
* @param data the frame data
* @return
* @throws IOException
*/
protected abstract List<ByteBuffer> sendMessage(Port port, final int bmsNo, final DalyCommand cmd, final byte[] data) throws IOException, NoDataAvailableException, TooManyInvalidFramesException;
protected abstract List<ByteBuffer> sendMessage(Port port, final int bmsId, final DalyCommand cmd, final byte[] data) throws IOException, NoDataAvailableException, TooManyInvalidFramesException;


/**
Expand All @@ -139,13 +139,13 @@ protected void autoCalibrateSOC(final Port port) {
* @return the expected number of response frames
*/
protected int getResponseFrameCount(final DalyCommand cmd) {
final int bmsNo = 0;
final int bmsId = 0;

switch (cmd.id) {
case 0x21:
return 2;
case 0x95:
return Math.round(getBatteryPack(bmsNo).numberOfCells / 3f + 0.5f);
return Math.round(getBatteryPack(bmsId).numberOfCells / 3f + 0.5f);
}

return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public DalyMessageHandler() {
public void handleMessage(final BMS bms, final DalyMessage msg) {
final int batteryNo = msg.address - 1;

if (batteryNo != bms.getBmsNo()) {
if (batteryNo != bms.getBmsId()) {
LOG.error("Found invalid battery identifier: #{}", msg.address);
return;
}
Expand Down Expand Up @@ -178,7 +178,7 @@ private void getMinMaxCellVoltage(final DalyMessage msg, final BMS bms) throws I
+ "\tMax Voltage: Cell {}({}mV)\n"
+ "\tMin Voltage: Cell {}({}mV)\n"
+ "\tDifference: {}mV",
bms.getBmsNo() + 1, battery.maxCellVNum, battery.maxCellmV, battery.minCellVNum, battery.minCellmV, battery.cellDiffmV);
bms.getBmsId() + 1, battery.maxCellVNum, battery.maxCellmV, battery.minCellVNum, battery.minCellmV, battery.cellDiffmV);
}
}

Expand All @@ -199,7 +199,7 @@ private void getPackTemp(final DalyMessage msg, final BMS bms) throws IOExceptio
+ "\tMax: {}C\n"
+ "\tMin: {}C\n"
+ "\tAvg: {}C",
bms.getBmsNo() + 1, battery.tempMax, battery.tempMin, battery.tempAverage);
bms.getBmsId() + 1, battery.tempMax, battery.tempMin, battery.tempAverage);
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ private void getDischargeChargeMosStatus(final DalyMessage msg, final BMS bms) t
+ "\tChargeMOS-State: {}\n"
+ "\tDisChargeMOS-State: {}\n"
+ "\tBMSHeartBeat: {}",
bms.getBmsNo() + 1, battery.chargeDischargeStatus, battery.chargeMOSState, battery.disChargeMOSState, battery.bmsHeartBeat);
bms.getBmsId() + 1, battery.chargeDischargeStatus, battery.chargeMOSState, battery.disChargeMOSState, battery.bmsHeartBeat);
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ private void getCellVoltages(final DalyMessage msg, final BMS bms) throws IOExce
LOG.debug("BMS #{}, Frame No.: {}, Cell No: {}. {}mV", msg.address, frameNo, cellNo + 1, volt);

if (cellNo + 1 < MIN_NUMBER_CELLS || cellNo + 1 > MAX_NUMBER_CELLS) {
LOG.debug("Invalid cell number " + (cellNo + 1) + " for battery pack #" + (bms.getBmsNo() + 1) + "(" + battery.numberOfCells + "cells)");
LOG.debug("Invalid cell number " + (cellNo + 1) + " for battery pack #" + (bms.getBmsId() + 1) + "(" + battery.numberOfCells + "cells)");
break;
}

Expand All @@ -293,7 +293,7 @@ private void getCellVoltages(final DalyMessage msg, final BMS bms) throws IOExce
}

if (LOG.isDebugEnabled() && frameNo * 3 + cellNo >= battery.numberOfCells) {
final StringBuilder buf = new StringBuilder("Battery #" + (bms.getBmsNo() + 1) + " voltages:\n");
final StringBuilder buf = new StringBuilder("Battery #" + (bms.getBmsId() + 1) + " voltages:\n");

for (int i = 0; i < battery.numberOfCells; i++) {
buf.append("\t#" + (i + 1) + ": " + battery.cellVmV[i] / 1000f + "V\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void start() {
do {
for (final BMS bms : bmsList) {
try {
LOG.info("Reading BMS #" + bms.getBmsNo() + bms.getName() + " on " + bms.getPortLocator() + "...");
LOG.info("Reading BMS #" + bms.getBmsId() + " " + bms.getName() + " on " + bms.getPortLocator() + "...");
bms.process(() -> receivedData());
// TODO set poll intverall for all bms together not each
Thread.sleep(bms.getPollInterval() * 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public BMSDialog(final JFrame owner) {
if (config == null) {
config = new BMSConfig(0, portLocator, pollInterval, delayAfterNoBytes, descriptor);
} else {
config.update(config.getBmsNo(), portLocator, pollInterval, delayAfterNoBytes, descriptor);
config.update(config.getBmsId(), portLocator, pollInterval, delayAfterNoBytes, descriptor);
}
dispose();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public BMSPanel(final JFrame frame) {
final BMSConfig config = dlg.getBMSConfig();

if (config != null) {
config.setBmsNo(bmsListModel.getSize());
config.setBmsId(bmsListModel.getSize());
bmsListModel.addElement(new MenuItem<>(createBMSDisplayName(config), config));
}
});
Expand Down Expand Up @@ -131,14 +131,14 @@ public BMSPanel(final JFrame frame) {


private String createBMSDisplayName(final BMSConfig config) {
return "#" + (config.getBmsNo() + 1) + ": " + config.getDescriptor().getName() + " on " + config.getPortLocator();
return "#" + (config.getBmsId() + 1) + ": " + config.getDescriptor().getName() + " on " + config.getPortLocator();
}


private void reindexBMSList() {
for (int i = 0; i < bmsListModel.getSize(); i++) {
final MenuItem<BMSConfig> item = bmsListModel.get(i);
item.getValue().setBmsNo(i);
item.getValue().setBmsId(i);
item.setDisplayName(createBMSDisplayName(item.getValue()));
}

Expand Down Expand Up @@ -179,10 +179,10 @@ protected void generateConfiguration(final StringBuffer config) {
+ "# bms.x.pollIntervall - is the interval to request BMS data (in seconds)\n"
+ "# bms.x.delayAfterNoBytes - is the delay after receiving no data (in ms)\n");
for (final BMSConfig bmsConfig : getBMSConfigList()) {
config.append("bms." + bmsConfig.getBmsNo() + ".type=" + bmsConfig.getDescriptor().getName() + "\n");
config.append("bms." + bmsConfig.getBmsNo() + ".portLocator=" + bmsConfig.getPortLocator() + "\n");
config.append("bms." + bmsConfig.getBmsNo() + ".pollInterval=" + bmsConfig.getPollInterval() + "\n");
config.append("bms." + bmsConfig.getBmsNo() + ".delayAfterNoBytes=" + bmsConfig.getDelayAfterNoBytes() + "\n");
config.append("bms." + bmsConfig.getBmsId() + ".type=" + bmsConfig.getDescriptor().getName() + "\n");
config.append("bms." + bmsConfig.getBmsId() + ".portLocator=" + bmsConfig.getPortLocator() + "\n");
config.append("bms." + bmsConfig.getBmsId() + ".pollInterval=" + bmsConfig.getPollInterval() + "\n");
config.append("bms." + bmsConfig.getBmsId() + ".delayAfterNoBytes=" + bmsConfig.getDelayAfterNoBytes() + "\n");
config.append("\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public String getName() {


/**
* Gets the assigned BMS number.
* Gets the id of the BMS.
*
* @return the assigned BMS number
* @return the id of the BMS
*/
public int getBmsNo() {
return config.getBmsNo();
public int getBmsId() {
return config.getBmsId();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Configuration read from the confg.properties for each {@link BMS}.
*/
public class BMSConfig {
private int bmsNo;
private int bmsId;
private String portLocator;
private int pollInterval;
private long delayAfterNoBytes;
Expand All @@ -13,15 +13,15 @@ public class BMSConfig {
/**
* Constructor.
*
* @param bmsNo the number of the BMS
* @param bmsId the id of the BMS
* @param portLocator the port locator
* @param pollInterval the polling interval in seconds
* @param delayAfterNoBytes the delay after no bytes were received in milliseconds
* @param descriptor the {@link BMSDescriptor} of the {@link BMS} to use
*/
public BMSConfig(final int bmsNo, final String portLocator, final int pollInterval, final long delayAfterNoBytes, final BMSDescriptor descriptor) {
public BMSConfig(final int bmsId, final String portLocator, final int pollInterval, final long delayAfterNoBytes, final BMSDescriptor descriptor) {
super();
this.bmsNo = bmsNo;
this.bmsId = bmsId;
this.portLocator = portLocator;
this.pollInterval = pollInterval;
this.delayAfterNoBytes = delayAfterNoBytes;
Expand All @@ -32,14 +32,14 @@ public BMSConfig(final int bmsNo, final String portLocator, final int pollInterv
/**
* Updates this configuration.
*
* @param bmsNo the number of the BMS
* @param bmsId the id of the BMS
* @param portLocator the port locator
* @param pollInterval the polling interval in seconds
* @param delayAfterNoBytes the delay after no bytes were received in milliseconds
* @param bmsDescriptor the {@link BMSDescriptor} of the {@link BMS} to use
*/
public void update(final int bmsNo, final String portLocator, final int pollInterval, final long delayAfterNoBytes, final BMSDescriptor bmsDescriptor) {
this.bmsNo = bmsNo;
public void update(final int bmsId, final String portLocator, final int pollInterval, final long delayAfterNoBytes, final BMSDescriptor bmsDescriptor) {
this.bmsId = bmsId;
this.portLocator = portLocator;
this.pollInterval = pollInterval;
this.delayAfterNoBytes = delayAfterNoBytes;
Expand All @@ -52,8 +52,8 @@ public void update(final int bmsNo, final String portLocator, final int pollInte
*
* @return the bmsNo the number of the BMS
*/
public int getBmsNo() {
return bmsNo;
public int getBmsId() {
return bmsId;
}


Expand All @@ -62,8 +62,8 @@ public int getBmsNo() {
*
* @param bmsNo the bmsNo the number of the BMS
*/
public void setBmsNo(final int bmsNo) {
this.bmsNo = bmsNo;
public void setBmsId(final int bmsNo) {
bmsId = bmsNo;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private BMS createBMS(final int bmsNo, final String name) {
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);
LOG.info("Intialized BMS #" + config.getBmsId() + "[" + config.getDescriptor().getName() + "] on port " + portLocator);

return bms;
}
Expand Down

0 comments on commit b7c728f

Please sign in to comment.