Search in sources :

Example 1 with TracerBatteryConfigBuilder

use of me.retrodaredevil.solarthing.solar.tracer.batteryconfig.TracerBatteryConfigBuilder 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

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)1 FunctionCodeException (me.retrodaredevil.io.modbus.handling.FunctionCodeException)1 TracerStatusPacket (me.retrodaredevil.solarthing.solar.tracer.TracerStatusPacket)1 TracerBatteryConfigBuilder (me.retrodaredevil.solarthing.solar.tracer.batteryconfig.TracerBatteryConfigBuilder)1