Search in sources :

Example 6 with Nullable

use of me.retrodaredevil.solarthing.annotations.Nullable in project solarthing by wildmountainfarms.

the class CommandManager method makeCreator.

/**
 * @param instanceTargetPacket The {@link InstanceTargetPacket} to indicate which fragments to target or null. If null, it is not added to the packet collection
 * @param commandOpenPacket The command packet
 * @return A creator to make a packet collection. When supplied with an {@link Instant} representing now, a packet collection is created.
 */
public PacketCollectionCreator makeCreator(String sourceId, ZoneId zoneId, @Nullable InstanceTargetPacket instanceTargetPacket, CommandOpenPacket commandOpenPacket, PacketCollectionIdGenerator packetCollectionIdGenerator) {
    // instanceTargetPacket may be null
    KeyPair keyPair = getKeyPair();
    InstanceSourcePacket instanceSourcePacket = InstanceSourcePackets.create(sourceId);
    // ----
    return now -> {
        PacketCollection packetCollectionToNestAndEncrypt = PacketCollections.create(now, instanceTargetPacket == null ? Arrays.asList(commandOpenPacket, instanceSourcePacket) : Arrays.asList(commandOpenPacket, instanceSourcePacket, instanceTargetPacket), "unused document ID that does not get serialized");
        // Note, on packetCollectionToNestAndEncrypt, _id is not serialized, so the generator and zoneId used above do NOT affect anything
        final String payload;
        try {
            payload = MAPPER.writeValueAsString(packetCollectionToNestAndEncrypt);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        String hashString = Long.toHexString(now.toEpochMilli()) + "," + HashUtil.encodedHash(payload);
        final String encrypted;
        try {
            synchronized (CIPHER) {
                // It's possible we could be in a multi-threaded environment, and you cannot have multiple threads using a single cipher at once
                encrypted = Encrypt.encrypt(CIPHER, keyPair.getPrivate(), hashString);
            }
        } catch (InvalidKeyException | EncryptException e) {
            throw new RuntimeException(e);
        }
        List<Packet> packets = new ArrayList<>(Arrays.asList(new ImmutableLargeIntegrityPacket(sender, encrypted, payload), instanceSourcePacket));
        if (instanceTargetPacket != null) {
            packets.add(instanceTargetPacket);
        }
        return PacketCollections.createFromPackets(now, packets, packetCollectionIdGenerator, zoneId);
    };
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) KeyPair(java.security.KeyPair) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) me.retrodaredevil.solarthing.packets.security.crypto(me.retrodaredevil.solarthing.packets.security.crypto) LoggerFactory(org.slf4j.LoggerFactory) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) Cipher(javax.crypto.Cipher) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) Objects.requireNonNull(java.util.Objects.requireNonNull) JacksonUtil(me.retrodaredevil.solarthing.util.JacksonUtil) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StandardOpenOption(java.nio.file.StandardOpenOption) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) PublicKey(java.security.PublicKey) Instant(java.time.Instant) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) File(java.io.File) ZoneId(java.time.ZoneId) PacketCollections(me.retrodaredevil.solarthing.packets.collection.PacketCollections) InstanceSourcePackets(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets) List(java.util.List) PrivateKey(java.security.PrivateKey) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceTargetPacket(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPacket) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) KeyPair(java.security.KeyPair) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) InstanceSourcePacket(me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableLargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.ImmutableLargeIntegrityPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with Nullable

use of me.retrodaredevil.solarthing.annotations.Nullable in project solarthing by wildmountainfarms.

the class BatteryUtil method getBatteryVoltageAverage.

@Nullable
public static Float getBatteryVoltageAverage(PacketGroup packetGroup) {
    float sum = 0;
    int count = 0;
    for (Packet packet : packetGroup.getPackets()) {
        if (packet instanceof BatteryVoltage) {
            sum += ((BatteryVoltage) packet).getBatteryVoltage();
            count++;
        }
    }
    if (count == 0) {
        return null;
    }
    return sum / count;
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) BatteryVoltage(me.retrodaredevil.solarthing.solar.common.BatteryVoltage) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 8 with Nullable

use of me.retrodaredevil.solarthing.annotations.Nullable in project solarthing by wildmountainfarms.

the class LowACInputEvent method createDesiredTrigger.

@Override
@Nullable
protected Runnable createDesiredTrigger(MessageSender sender, FragmentedPacketGroup previous, FragmentedPacketGroup current) {
    FXStatusPacket fx = OutbackUtil.getMasterFX(current);
    if (fx != null) {
        boolean is230 = fx.is230V();
        int lowThreshold = is230 && lowRaw ? 2 * lowThresholdVoltage : lowThresholdVoltage;
        int highThreshold = is230 && highRaw ? 2 * highThresholdVoltage : highThresholdVoltage;
        int inputVoltage = fx.getInputVoltage();
        if (inputVoltage >= lowThreshold && inputVoltage <= highThreshold) {
            return () -> sender.sendMessage("Low AC Input Voltage! " + inputVoltage + "V (" + getPrettyDurationString() + ")");
        }
    }
    return null;
}
Also used : FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 9 with Nullable

use of me.retrodaredevil.solarthing.annotations.Nullable in project solarthing by wildmountainfarms.

the class JarUtil method getLastModified.

@Nullable
private static Long getLastModified(URI uri) {
    final File file;
    try {
        file = new File(getJarFileUri());
    } catch (IllegalArgumentException ex) {
        return null;
    }
    long value = file.lastModified();
    if (value == 0) {
        return null;
    }
    return value;
}
Also used : File(java.io.File) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Example 10 with Nullable

use of me.retrodaredevil.solarthing.annotations.Nullable in project solarthing by wildmountainfarms.

the class SolarThingGraphQLMetaService method getFragmentDeviceInfo.

@GraphQLQuery(name = "fragmentDeviceInfo")
@Nullable
public DeviceInfoPacket getFragmentDeviceInfo(@GraphQLContext SimplePacketNode packetNode) {
    int fragmentId = packetNode.getFragmentId();
    MetaDatabase metaDatabase = simpleQueryHandler.queryMeta();
    for (TargetedMetaPacket targetedMetaPacket : metaDatabase.getMeta(packetNode.getDateMillis(), fragmentId)) {
        if (targetedMetaPacket instanceof DeviceInfoPacket) {
            return (DeviceInfoPacket) targetedMetaPacket;
        }
    }
    return null;
}
Also used : MetaDatabase(me.retrodaredevil.solarthing.type.closed.meta.MetaDatabase) TargetedMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket) DeviceInfoPacket(me.retrodaredevil.solarthing.type.closed.meta.DeviceInfoPacket) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) GraphQLQuery(io.leangen.graphql.annotations.GraphQLQuery)

Aggregations

Nullable (me.retrodaredevil.solarthing.annotations.Nullable)11 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Packet (me.retrodaredevil.solarthing.packets.Packet)3 File (java.io.File)2 IOException (java.io.IOException)2 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)2 DocumentData (me.retrodaredevil.couchdbjava.response.DocumentData)2 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)2 MetaDatabase (me.retrodaredevil.solarthing.type.closed.meta.MetaDatabase)2 TargetedMetaPacket (me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 GraphQLQuery (io.leangen.graphql.annotations.GraphQLQuery)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 StandardOpenOption (java.nio.file.StandardOpenOption)1