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