use of me.retrodaredevil.solarthing.packets.identification.Identifier in project solarthing by wildmountainfarms.
the class DefaultIdentificationCacheCreator method convertPackets.
@SuppressWarnings("unchecked")
private static <T extends Identifiable> Map<String, Map<IdentifierFragment, List<TimestampedPacket<T>>>> convertPackets(List<? extends FragmentedPacketGroup> packetGroups, Class<T> acceptedType) {
Map<String, Map<IdentifierFragment, List<TimestampedPacket<T>>>> r = new HashMap<>();
for (FragmentedPacketGroup packetGroup : packetGroups) {
for (Packet packet : packetGroup.getPackets()) {
if (!acceptedType.isInstance(packet)) {
continue;
}
T identifiablePacket = (T) packet;
String sourceId = packetGroup.getSourceId(packet);
int fragmentId = packetGroup.getFragmentId(packet);
Identifier identifier = identifiablePacket.getIdentifier();
IdentifierFragment identifierFragment = IdentifierFragment.create(fragmentId, identifier);
List<TimestampedPacket<T>> list = r.computeIfAbsent(sourceId, k -> new HashMap<>()).computeIfAbsent(identifierFragment, k -> new ArrayList<>());
long dateMillis = packetGroup.getDateMillisOrKnown(packet);
list.add(new TimestampedPacket<>(identifiablePacket, dateMillis));
}
}
return r;
}
use of me.retrodaredevil.solarthing.packets.identification.Identifier in project solarthing by wildmountainfarms.
the class RequireFullOutputActionNode method isFullOutput.
private boolean isFullOutput(FragmentedPacketGroup latestPacketGroup) {
for (Map.Entry<Integer, List<String>> entry : requiredIdentifierMap.entrySet()) {
int desiredFragmentId = entry.getKey();
if (!latestPacketGroup.hasFragmentId(desiredFragmentId)) {
continue;
}
for (String desiredIdentifierRepresentation : entry.getValue()) {
for (Packet packet : latestPacketGroup.getPackets()) {
int fragmentId = latestPacketGroup.getFragmentId(packet);
if (fragmentId != desiredFragmentId) {
continue;
}
if (packet instanceof Identifiable) {
Identifier identifier = ((Identifiable) packet).getIdentifier();
if (desiredIdentifierRepresentation.equals(identifier.getRepresentation())) {
if (packet instanceof MXStatusPacket) {
MXStatusPacket mx = (MXStatusPacket) packet;
ChargerMode mode = mx.getChargingMode();
// Some old MX firmwares consistently report being in float mode after a full absorb cycle even after the daily reset.
// This configuration option allows us to treat float as full output for a given MX
boolean canIgnoreFloatMode = ignoreReportedMXFloatMap.getOrDefault(fragmentId, Collections.emptyList()).contains(desiredIdentifierRepresentation);
if (mode != ChargerMode.SILENT && mode != ChargerMode.BULK && (!canIgnoreFloatMode || mode != ChargerMode.FLOAT)) {
if (log) {
LOGGER.info(identifier.getRepresentation() + " on fragment " + fragmentId + " is in mode " + mode.getModeName());
}
return false;
}
} else if (packet instanceof RoverStatusPacket) {
RoverStatusPacket rover = (RoverStatusPacket) packet;
ChargingState state = rover.getChargingMode();
if (state != ChargingState.ACTIVATED && state != ChargingState.DEACTIVATED && state != ChargingState.MPPT) {
if (log) {
LOGGER.info(identifier.getRepresentation() + " on fragment " + fragmentId + " is in mode " + state.getModeName());
}
return false;
}
} else if (packet instanceof TracerStatusPacket) {
TracerStatusPacket tracer = (TracerStatusPacket) packet;
ChargingStatus status = tracer.getChargingMode();
// we currently cannot tell if while in BOOST or EQUALIZE if the controller is actually in Bulk, so float is the only mode we know that isn't at full output
if (status == ChargingStatus.FLOAT) {
if (log) {
LOGGER.info(identifier.getRepresentation() + " on fragment " + fragmentId + " is in mode " + status.getModeName());
}
return false;
}
}
}
}
}
}
}
return true;
}
use of me.retrodaredevil.solarthing.packets.identification.Identifier in project solarthing by wildmountainfarms.
the class FXStatusListUpdater method receive.
@Override
public void receive(List<Packet> packets) {
long now = System.currentTimeMillis();
long timeId = timeIdentifier.getTimeId(now);
final Long lastTimeId = this.lastTimeId;
this.lastTimeId = timeId;
if (lastTimeId != null && lastTimeId != timeId) {
fxMap.clear();
}
for (Packet packet : new ArrayList<>(packets)) {
if (packet instanceof DocumentedPacket) {
DocumentedPacketType packetType = ((DocumentedPacket) packet).getPacketType();
if (packetType == SolarStatusPacketType.FX_STATUS) {
FXStatusPacket fx = (FXStatusPacket) packet;
Identifier identifier = fx.getIdentifier();
FXListUpdater updater = fxMap.get(identifier);
if (updater == null) {
updater = new FXListUpdater();
fxMap.put(identifier, updater);
}
updater.update(now, packets, fx);
}
}
}
}
use of me.retrodaredevil.solarthing.packets.identification.Identifier in project solarthing by wildmountainfarms.
the class DataCacheTest method test.
@Test
void test() throws JsonProcessingException {
ObjectMapper mapper = JacksonUtil.defaultMapper();
Identifier identifier = new OutbackIdentifier(3);
ChargeControllerAccumulationDataCache v1 = new ChargeControllerAccumulationDataCache(identifier, 5.5f, 3100L, 7900L, 8.5f, 1000L);
ChargeControllerAccumulationDataCache v2 = new ChargeControllerAccumulationDataCache(identifier, 6.5f, // even thought this period starts at 8000, we have the last packet's dateMillis from the previous period
7900L, // even thought this period starts at 8000, we have the last packet's dateMillis from the previous period
13100L, 0.0f, null);
ChargeControllerAccumulationDataCache actualResult = v1.combine(v2);
String resultJson = mapper.writeValueAsString(actualResult);
// System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(actualResult));
ChargeControllerAccumulationDataCache parsedResult = mapper.readValue(resultJson, ChargeControllerAccumulationDataCache.class);
for (ChargeControllerAccumulationDataCache result : new ChargeControllerAccumulationDataCache[] { actualResult, parsedResult }) {
assertEquals(12.0f, result.getGenerationKWH());
assertEquals(3100L, result.getFirstDateMillis());
assertEquals(13100L, result.getLastDateMillis());
assertEquals(8.5f, result.getUnknownGenerationKWH());
assertEquals(1000L, result.getUnknownStartDateMillis());
}
}
use of me.retrodaredevil.solarthing.packets.identification.Identifier in project solarthing by wildmountainfarms.
the class FXAccumulationDataCache method createFromIdentifier.
public static FXAccumulationDataCache createFromIdentifier(Identifier supplementaryIdentifier, Data mainData, Long firstDateMillis, Long lastDateMillis, Data unknownData, Long unknownStartDateMillis) {
if (!(supplementaryIdentifier instanceof SupplementaryIdentifier)) {
throw new IllegalArgumentException("The passed identifier is not a SupplementaryIdentifier! It is: " + supplementaryIdentifier.getClass() + " and its value is: " + supplementaryIdentifier.getRepresentation());
}
Identifier identifier = ((SupplementaryIdentifier) supplementaryIdentifier).getSupplementaryTo();
if (!(identifier instanceof OutbackIdentifier)) {
throw new IllegalArgumentException("identifier should be an OutbackIdentifier! It is: " + identifier.getClass() + " and its value is: " + identifier.getRepresentation());
}
OutbackIdentifier outbackIdentifier = (OutbackIdentifier) identifier;
return new FXAccumulationDataCache(outbackIdentifier.getAddress(), mainData, firstDateMillis, lastDateMillis, unknownData, unknownStartDateMillis);
}
Aggregations