Search in sources :

Example 21 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class SolarMain method getSourceAndFragmentUpdater.

public static PacketListReceiver getSourceAndFragmentUpdater(PacketHandlingOption options) {
    String source = options.getSourceId();
    int fragment = options.getFragmentId();
    requireNonNull(source);
    Packet sourcePacket = InstanceSourcePackets.create(source);
    Packet fragmentPacket = InstanceFragmentIndicatorPackets.create(fragment);
    return (list) -> {
        list.add(sourcePacket);
        list.add(fragmentPacket);
    };
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) LoggerFactory(org.slf4j.LoggerFactory) CouchDbCodeException(me.retrodaredevil.couchdbjava.exception.CouchDbCodeException) Random(java.util.Random) HelpRequestedException(com.lexicalscope.jewel.cli.HelpRequestedException) me.retrodaredevil.solarthing.config.options(me.retrodaredevil.solarthing.config.options) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) Objects.requireNonNull(java.util.Objects.requireNonNull) CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ArgumentValidationException(com.lexicalscope.jewel.cli.ArgumentValidationException) ConfigException(me.retrodaredevil.solarthing.exceptions.ConfigException) Logger(org.slf4j.Logger) ErrorResponse(me.retrodaredevil.couchdbjava.response.ErrorResponse) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) HourIntervalPacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.HourIntervalPacketCollectionIdGenerator) Cli(com.lexicalscope.jewel.cli.Cli) Instant(java.time.Instant) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) InstanceFragmentIndicatorPackets(me.retrodaredevil.solarthing.packets.instance.InstanceFragmentIndicatorPackets) InstanceSourcePackets(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets) TextPacketCreator(me.retrodaredevil.solarthing.packets.creation.TextPacketCreator) List(java.util.List) SolarThingConstants(me.retrodaredevil.solarthing.SolarThingConstants) RawPacketReceiver(me.retrodaredevil.solarthing.packets.handling.RawPacketReceiver) java.io(java.io) CliFactory(com.lexicalscope.jewel.cli.CliFactory) CheckMain(me.retrodaredevil.solarthing.program.check.CheckMain) UtilityClass(me.retrodaredevil.solarthing.annotations.UtilityClass) PacketListReceiver(me.retrodaredevil.solarthing.packets.handling.PacketListReceiver) PVOutputUploadMain(me.retrodaredevil.solarthing.program.pvoutput.PVOutputUploadMain) LogManager(org.apache.logging.log4j.LogManager) Packet(me.retrodaredevil.solarthing.packets.Packet)

Example 22 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class GeneratorUnusedAlertEvent method createDesiredTrigger.

@Override
@Nullable
protected Runnable createDesiredTrigger(MessageSender sender, FragmentedPacketGroup previous, FragmentedPacketGroup current) {
    // on one FX to be greater than another, so we want to check to see if this applied to *any* FXs
    for (Packet packet : current.getPackets()) {
        if (packet instanceof FXStatusPacket) {
            FXStatusPacket fx = (FXStatusPacket) packet;
            boolean is230 = fx.is230V();
            int lowThreshold = is230 && lowRaw ? 2 * lowThresholdVoltage : lowThresholdVoltage;
            int inputVoltage = fx.getInputVoltage();
            if (fx.getACMode() == ACMode.AC_DROP) {
                return () -> sender.sendMessage("Generator dropping power! (on and not using for " + getPrettyDurationString() + ")");
            }
            if (inputVoltage >= lowThreshold && fx.getACMode() != ACMode.AC_USE) {
                return () -> sender.sendMessage("Low Generator Voltage! " + inputVoltage + "V (on and not using for " + getPrettyDurationString() + ")");
            }
        }
    }
    return null;
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 23 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class MXFloatModeStuckEvent method run.

@Override
public void run(MessageSender sender, FragmentedPacketGroup previous, FragmentedPacketGroup current) {
    for (Packet previousPacket : previous.getPackets()) {
        if (previousPacket instanceof MXStatusPacket) {
            MXStatusPacket previousMX = (MXStatusPacket) previousPacket;
            KnownIdentifierFragment<OutbackIdentifier> identifierFragment = IdentifierFragment.create(previous.getFragmentId(previousPacket), previousMX.getIdentifier());
            MXStatusPacket currentMX = null;
            for (Packet currentPacket : current.getPackets()) {
                if (currentPacket instanceof Identifiable) {
                    IdentifierFragment currentIdentifierFragment = IdentifierFragment.create(current.getFragmentId(currentPacket), ((Identifiable) currentPacket).getIdentifier());
                    if (identifierFragment.equals(currentIdentifierFragment)) {
                        currentMX = (MXStatusPacket) currentPacket;
                    }
                }
            }
            if (currentMX == null) {
                continue;
            }
            if (currentMX.isNewDay(previousMX)) {
                enabledMap.put(identifierFragment, true);
            }
            if (enabledMap.getOrDefault(identifierFragment, false)) {
                ChargerMode mode = currentMX.getChargingMode();
                if (mode == ChargerMode.FLOAT) {
                    doAlert(sender, identifierFragment);
                } else if (mode != ChargerMode.SILENT) {
                    enabledMap.put(identifierFragment, false);
                    LOGGER.debug("Disabling " + identifierFragment + " because mode is " + mode);
                }
            }
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) KnownIdentifierFragment(me.retrodaredevil.solarthing.packets.identification.KnownIdentifierFragment) IdentifierFragment(me.retrodaredevil.solarthing.packets.identification.IdentifierFragment) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) ChargerMode(me.retrodaredevil.solarthing.solar.outback.mx.ChargerMode) OutbackIdentifier(me.retrodaredevil.solarthing.solar.outback.OutbackIdentifier) Identifiable(me.retrodaredevil.solarthing.packets.identification.Identifiable)

Example 24 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class NoHeartbeatEvent method runForEvent.

@Override
public void runForEvent(MessageSender sender, InstancePacketGroup packetGroup) {
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof HeartbeatPacket) {
            HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
            long dateMillis = packetGroup.getDateMillis();
            int fragmentId = packetGroup.getFragmentId();
            HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), fragmentId);
            HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
            map.put(identifier, node);
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HeartbeatNode(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode) HeartbeatIdentifier(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)

Example 25 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class MqttPacketSaver method handle.

private void handle(InstancePacketGroup instancePacketGroup, PacketCollection entirePacketCollection) throws MqttException {
    DefaultInstanceOptions.requireNoDefaults(instancePacketGroup);
    long dateMillis = instancePacketGroup.getDateMillis();
    final String partiallyFormattedTopic = topicFormat.replace("%source", instancePacketGroup.getSourceId()).replace("%fragment", "" + instancePacketGroup.getFragmentId());
    client.connect(options);
    {
        // block for entire packet collection
        final String collectionJson;
        try {
            collectionJson = OBJECT_MAPPER.writeValueAsString(entirePacketCollection);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("We should be able to serial this to JSON!", e);
        }
        client.publish(partiallyFormattedTopic.replace("%identifier", "packetCollection"), collectionJson.getBytes(CHARSET), 2, retain);
    }
    client.publish(statusTopic, "online".getBytes(CHARSET), 1, retain);
    for (Packet packet : instancePacketGroup.getPackets()) {
        if (packet instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) packet;
            String topic = partiallyFormattedTopic.replace("%identifier", identifiable.getIdentifier().getRepresentation()) + "/";
            final String rawJson;
            try {
                rawJson = OBJECT_MAPPER.writeValueAsString(packet);
            } catch (JsonProcessingException e) {
                throw new RuntimeException("We should be able to serialize this to JSON!", e);
            }
            client.publish(topic + "dateMillis", ("" + dateMillis).getBytes(CHARSET), 2, retain);
            client.publish(topic + "json", rawJson.getBytes(CHARSET), 2, retain);
            ObjectNode json = OBJECT_MAPPER.valueToTree(packet);
            for (Map.Entry<String, ValueNode> entry : PointUtil.flattenJsonObject(json, "/")) {
                String key = entry.getKey();
                ValueNode prim = entry.getValue();
                // Make individual fields use QoS of 1 because we want them all to go through, but no one should be using them as "event"-like packets
                client.publish(topic + "packet/" + key, prim.asText().getBytes(CHARSET), 1, retain);
            }
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Map(java.util.Map) Identifiable(me.retrodaredevil.solarthing.packets.identification.Identifiable)

Aggregations

Packet (me.retrodaredevil.solarthing.packets.Packet)45 ArrayList (java.util.ArrayList)17 Instant (java.time.Instant)7 List (java.util.List)7 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)6 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)6 Identifiable (me.retrodaredevil.solarthing.packets.identification.Identifiable)5 IdentifierFragment (me.retrodaredevil.solarthing.packets.identification.IdentifierFragment)5 RoverStatusPacket (me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket)5 OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)5 Map (java.util.Map)4 CommandOpenPacket (me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)4 InstancePacketGroup (me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup)4 InstanceSourcePacket (me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket)4 ExecutionReason (me.retrodaredevil.solarthing.reason.ExecutionReason)4 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)3 RequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket)3 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)3