-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BMS.java
182 lines (153 loc) · 5.61 KB
/
BMS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* This software is free to use and to distribute in its unchanged form for private use.
* Commercial use is prohibited without an explicit license agreement of the copyright holder.
* Any changes to this software must be made solely in the project repository at https://github.com/ai-republic/bms-to-inverter.
* The copyright holder is not liable for any damages in whatever form that may occur by using this software.
*
* (c) Copyright 2022 and onwards - Torsten Oltmanns
*
* @author Torsten Oltmanns - bms-to-inverter''AT''gmail.com
*/
package com.airepublic.bmstoinverter.core;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.airepublic.bmstoinverter.core.bms.data.BatteryPack;
import com.airepublic.bmstoinverter.core.bms.data.EnergyStorage;
import jakarta.inject.Inject;
/**
* The abstract class to identify a BMS.
*/
public abstract class BMS {
private final static Logger LOG = LoggerFactory.getLogger(BMS.class);
private final Map<Integer, BatteryPack> batteryPacks = new LinkedHashMap<>();
private BMSConfig config;
@Inject
private transient EnergyStorage energyStorage;
/**
* Initializes the BMS with the specified {@link BMSConfig}, initializing the port parameters
* from the system properties.
*
* @param config the {@link BMSConfig}
*/
public void initialize(final BMSConfig config) {
if (!PortAllocator.hasPort(config.getPortLocator())) {
final Port port = config.getDescriptor().createPort(config);
PortAllocator.addPort(config.getPortLocator(), port);
}
this.config = config;
}
/**
* Gets the name of the {@link BMSDescriptor}.
*
* @return the name
*/
public String getName() {
return config.getDescriptor().getName();
}
/**
* Gets the id of the BMS.
*
* @return the id of the BMS
*/
public int getBmsId() {
return config.getBmsId();
}
/**
* Gets the assigned {@link Port}s locator.
*
* @return the assigned {@link Port}s locator
*/
public String getPortLocator() {
return config.getPortLocator();
}
/**
* Gets the {@link BatteryPack}s associated with this {@link BMS}.
*
* @return the {@link BatteryPack}s associated with this {@link BMS}
*/
public Collection<BatteryPack> getBatteryPacks() {
return batteryPacks.values();
}
/**
* Gets the {@link BatteryPack} at the specified index associated with this {@link BMS}. If the
* number is greater than already known, then the pack size will increase until it has the
* number specified.
*
* @param batteryId the id of the {@link BatteryPack}
* @return the {@link BatteryPack} with the specified id associated with this {@link BMS}
*/
public BatteryPack getBatteryPack(final int batteryId) {
BatteryPack pack = batteryPacks.get(batteryId);
if (pack == null) {
pack = new BatteryPack();
batteryPacks.put(batteryId, pack);
energyStorage.getBatteryPacks().add(pack);
}
return pack;
}
/**
* Gets the delay after no bytes were received in milliseconds.
*
* @return the delay after no bytes were received in milliseconds
*/
public long getDelayAfterNoBytes() {
return config.getDelayAfterNoBytes();
}
/**
* Processes the collection of data for each configured {@link BatteryPack}.
*
* @param callback the function will be called after successfully collecting data from all
* {@link BatteryPack}s
*/
public final void process(final Runnable callback) {
try {
LOG.info("---------------------------------> Thread " + Thread.currentThread().getId());
final Port port = PortAllocator.allocate(getPortLocator());
try {
port.ensureOpen();
port.clearBuffers();
collectData(port);
} catch (final NoDataAvailableException e) {
LOG.error("Received no bytes too many times - trying to close and re-open port!");
// try to close and re-open the port
port.close();
port.open();
}
// autoCalibrateSOC();
} catch (final TooManyInvalidFramesException e) {
LOG.error("Received too many invalid frames - start new reading round!");
return;
} catch (final Throwable e) {
LOG.error("Error requesting data!", e);
return;
} finally {
PortAllocator.free(getPortLocator());
}
try {
callback.run();
} catch (final Throwable e) {
LOG.error("BMS process callback threw an exception!", e);
}
}
/**
* Processes the collection of data from the specified {@link BatteryPack}.
*
* @param port the allocated {@link Port}
* @throws IOException if there is a problem with the port
* @throws TooManyInvalidFramesException when too many invalid frames were received
* @throws NoDataAvailableException when no data was received too many times
*/
protected abstract void collectData(Port port) throws IOException, TooManyInvalidFramesException, NoDataAvailableException;
/**
* Sets the {@link EnergyStorage}.
*
* @param energyStorage the {@link EnergyStorage}
*/
protected void setEnergyStorage(final EnergyStorage energyStorage) {
this.energyStorage = energyStorage;
}
}