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);
};
}
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;
}
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;
}
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;
}
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;
}
Aggregations