Search in sources :

Example 1 with TracerStatusPacket

use of me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket in project solarthing by wildmountainfarms.

the class TracerEventUpdaterListReceiver method useData.

private void useData(@NotNull TracerStatusPacket tracer, @Nullable TracerStatusPacket previous) {
    final Integer lastChargingEquipmentStatusValue;
    if (previous == null) {
        lastChargingEquipmentStatusValue = null;
    } else {
        lastChargingEquipmentStatusValue = previous.getChargingEquipmentStatus();
    }
    int currentChargingEquipmentStatusValue = tracer.getChargingEquipmentStatus();
    List<Packet> packets = new ArrayList<>();
    if (lastChargingEquipmentStatusValue == null || lastChargingEquipmentStatusValue != currentChargingEquipmentStatusValue) {
        packets.add(new ImmutableTracerChargingEquipmentStatusChangePacket(tracer.getIdentifier(), currentChargingEquipmentStatusValue, lastChargingEquipmentStatusValue));
    }
    if (!packets.isEmpty()) {
        eventReceiver.receive(packets);
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) ImmutableTracerChargingEquipmentStatusChangePacket(me.retrodaredevil.solarthing.solar.tracer.event.ImmutableTracerChargingEquipmentStatusChangePacket) ImmutableTracerChargingEquipmentStatusChangePacket(me.retrodaredevil.solarthing.solar.tracer.event.ImmutableTracerChargingEquipmentStatusChangePacket) ArrayList(java.util.ArrayList)

Example 2 with TracerStatusPacket

use of me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket in project solarthing by wildmountainfarms.

the class TracerEventUpdaterListReceiver method receive.

@Override
public void receive(List<Packet> packets) {
    for (Packet packet : packets) {
        if (packet instanceof TracerStatusPacket) {
            TracerStatusPacket tracer = (TracerStatusPacket) packet;
            TracerStatusPacket previous = previousPacketMap.get(tracer.getIdentifier());
            previousPacketMap.put(tracer.getIdentifier(), tracer);
            useData(tracer, previous);
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) ImmutableTracerChargingEquipmentStatusChangePacket(me.retrodaredevil.solarthing.solar.tracer.event.ImmutableTracerChargingEquipmentStatusChangePacket) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket)

Example 3 with TracerStatusPacket

use of me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket 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;
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket) ChargingStatus(me.retrodaredevil.solarthing.solar.tracer.mode.ChargingStatus) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) ChargerMode(me.retrodaredevil.solarthing.solar.outback.mx.ChargerMode) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) ChargingState(me.retrodaredevil.solarthing.solar.renogy.rover.ChargingState) Identifiable(me.retrodaredevil.solarthing.packets.identification.Identifiable) Identifier(me.retrodaredevil.solarthing.packets.identification.Identifier) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket) List(java.util.List) Map(java.util.Map)

Example 4 with TracerStatusPacket

use of me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket in project solarthing by wildmountainfarms.

the class PowerUtil method getPowerData.

public static Data getPowerData(PacketGroup packetGroup, GeneratingType generatingType) {
    requireNonNull(packetGroup);
    requireNonNull(generatingType);
    Integer generatingW = null;
    Integer usingW = null;
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof MXStatusPacket) {
            if (generatingW == null) {
                generatingW = 0;
            }
            if (generatingType == GeneratingType.PV_ONLY) {
                generatingW += ((MXStatusPacket) packet).getPVWattage();
            } else if (generatingType == GeneratingType.TOTAL_CHARGING) {
                generatingW += ((MXStatusPacket) packet).getChargingPower().intValue();
            } else
                throw new AssertionError("Unknown generatingType: " + generatingType);
        } else if (packet instanceof FXStatusPacket) {
            if (usingW == null) {
                usingW = 0;
            }
            usingW += ((FXStatusPacket) packet).getPowerUsageWattage();
        } else if (packet instanceof RoverStatusPacket) {
            if (generatingW == null) {
                generatingW = 0;
            }
            if (usingW == null) {
                usingW = 0;
            }
            if (generatingType == GeneratingType.PV_ONLY) {
                generatingW += ((RoverStatusPacket) packet).getPVWattage().intValue();
            } else if (generatingType == GeneratingType.TOTAL_CHARGING) {
                generatingW += ((RoverStatusPacket) packet).getChargingPower();
            } else
                throw new AssertionError("Unknown generatingType: " + generatingType);
            usingW += ((RoverStatusPacket) packet).getLoadPower();
        } else if (packet instanceof TracerStatusPacket) {
            if (generatingW == null) {
                generatingW = 0;
            }
            if (usingW == null) {
                usingW = 0;
            }
            if (generatingType == GeneratingType.PV_ONLY) {
                generatingW += ((TracerStatusPacket) packet).getPVWattage().intValue();
            } else if (generatingType == GeneratingType.TOTAL_CHARGING) {
                generatingW += ((TracerStatusPacket) packet).getChargingPower().intValue();
            } else
                throw new AssertionError("Unknown generatingType: " + generatingType);
            usingW += (int) ((TracerStatusPacket) packet).getLoadPower();
        }
    }
    return new Data(generatingW, usingW);
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket)

Example 5 with TracerStatusPacket

use of me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket in project solarthing by wildmountainfarms.

the class TracerPacketListUpdater method receive.

@Override
public void receive(List<Packet> packets) {
    TracerStatusPacket packet = TracerStatusPackets.createFromReadTable(number, read);
    packets.add(packet);
    if (tracerClockOptions != null) {
        // Sorry to anyone in the year 2256, that's just too bad for you
        // We make the year 21 represent 2021, because the tracer stores the year using 8 bits
        LocalDateTime currentTime = packet.getSolarThingLocalDateTime();
        final LocalDateTime desiredTime;
        ZoneOffset zoneOffset = tracerClockOptions.getZoneOffset();
        if (zoneOffset != null) {
            desiredTime = Instant.now().atOffset(zoneOffset).toLocalDateTime();
        } else {
            ZoneId zone = tracerClockOptions.getZoneId() != null ? tracerClockOptions.getZoneId() : ZoneId.systemDefault();
            desiredTime = Instant.now().atZone(zone).toLocalDateTime();
        }
        if (Duration.between(currentTime, desiredTime).abs().compareTo(tracerClockOptions.getDurationThreshold()) > 0) {
            LOGGER.info("Going to update time to " + desiredTime + " from " + currentTime);
            write.setSolarThingLocalDateTime(desiredTime);
            LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Success updating time!");
        }
    }
    if (connectionHandler != null) {
        TracerBatteryConfigBuilder builder = new TracerBatteryConfigBuilder(packet);
        connectionHandler.handleRequests(request -> {
            String[] split = StringUtil.terminalSplit(request);
            if (split.length == 1) {
                if ("flushBatteryConfig".equals(split[0])) {
                    try {
                        write.setBatteryConfig(builder);
                    } catch (FunctionCodeException ex) {
                        return ex.getMessage();
                    } catch (ModbusRuntimeException ex) {
                        LOGGER.error("Got modbus exception while flushing", ex);
                        return "Unknown error:" + ex.getMessage();
                    }
                    return "flush success";
                }
            }
            if (connectionHandlerHasFlushLogic && split.length > 1) {
                // check if we're writing something
                String fieldName = split[0];
                String value = split[1];
                // If we can successfully read from this reader, then that means that the input is a field in TracerBatteryConfigBuilder
                // 
                ObjectReader reader = MAPPER.readerForUpdating(builder);
                ObjectNode input = new ObjectNode(JsonNodeFactory.instance);
                input.put(fieldName, value);
                boolean success = true;
                try {
                    reader.readValue(input);
                } catch (IOException e) {
                    success = false;
                }
                if (success) {
                    return "queued up";
                }
            }
            return NetCatUtil.handle(write, packet, request);
        });
    }
}
Also used : FunctionCodeException(me.retrodaredevil.io.modbus.handling.FunctionCodeException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException) IOException(java.io.IOException) TracerStatusPacket(me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket) TracerBatteryConfigBuilder(me.retrodaredevil.solarthing.solar.tracer.batteryconfig.TracerBatteryConfigBuilder) ObjectReader(com.fasterxml.jackson.databind.ObjectReader)

Aggregations

TracerStatusPacket (me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket)5 Packet (me.retrodaredevil.solarthing.packets.Packet)4 MXStatusPacket (me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket)2 RoverStatusPacket (me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket)2 ImmutableTracerChargingEquipmentStatusChangePacket (me.retrodaredevil.solarthing.solar.tracer.event.ImmutableTracerChargingEquipmentStatusChangePacket)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)1 FunctionCodeException (me.retrodaredevil.io.modbus.handling.FunctionCodeException)1 Identifiable (me.retrodaredevil.solarthing.packets.identification.Identifiable)1 Identifier (me.retrodaredevil.solarthing.packets.identification.Identifier)1 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)1 ChargerMode (me.retrodaredevil.solarthing.solar.outback.mx.ChargerMode)1 ChargingState (me.retrodaredevil.solarthing.solar.renogy.rover.ChargingState)1 TracerBatteryConfigBuilder (me.retrodaredevil.solarthing.solar.tracer.batteryconfig.TracerBatteryConfigBuilder)1 ChargingStatus (me.retrodaredevil.solarthing.solar.tracer.mode.ChargingStatus)1