use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData in project solarthing by wildmountainfarms.
the class HeartbeatCommandChatBotHandler method displayHeartbeats.
// I really don't care at the moment. Extracting a method will make this less readable IMO
@SuppressWarnings("DuplicatedCode")
private void displayHeartbeats(MessageSender messageSender) {
Map<HeartbeatIdentifier, HeartbeatNode> map = new HashMap<>();
eventDatabaseCacheManager.access(eventDatabaseCache -> {
eventDatabaseCache.createCachedPacketsInRangeStream(TimeRange.createAfter(Instant.now().minus(Duration.ofHours(3))), false).map(// We are accessing new packets, so they should have source ID and fragment ID
PacketGroups::parseToInstancePacketGroupRequireNoDefaults).forEach(instancePacketGroup -> {
long dateMillis = instancePacketGroup.getDateMillis();
for (Packet packet : instancePacketGroup.getPackets()) {
if (packet instanceof HeartbeatPacket) {
HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), instancePacketGroup.getFragmentId());
HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
map.put(identifier, node);
}
}
});
});
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> activeHeartbeats = new ArrayList<>();
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> expectedHeartbeats = new ArrayList<>();
List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> deadHeartbeats = new ArrayList<>();
Instant now = Instant.now();
map.entrySet().stream().sorted(// iterate over oldest first
Comparator.comparingLong(entry -> entry.getValue().getDateMillis())).forEach(entry -> {
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).compareTo(now) > 0) {
activeHeartbeats.add(entry);
} else if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).plus(data.getBufferDuration()).compareTo(now) > 0) {
expectedHeartbeats.add(entry);
} else {
deadHeartbeats.add(entry);
}
});
StringBuilder builder = new StringBuilder();
if (!activeHeartbeats.isEmpty()) {
builder.append("Active:\n");
activeHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
if (!expectedHeartbeats.isEmpty()) {
builder.append("Expected:\n");
expectedHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
if (!deadHeartbeats.isEmpty()) {
builder.append("Dead:\n");
deadHeartbeats.forEach(entry -> {
int fragmentId = entry.getKey().getFragmentId();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
builder.append(" ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
});
}
String message = builder.toString();
if (message.isEmpty()) {
messageSender.sendMessage("No heartbeats");
} else {
messageSender.sendMessage(message);
}
}
use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData in project solarthing by wildmountainfarms.
the class CommandOpenPacketsTest method test.
@Test
void test() throws JsonProcessingException {
PacketTestUtil.testJson(new ImmutableRequestCommandPacket("GEN OFF"), CommandOpenPacket.class, true);
PacketTestUtil.testJson(new ImmutableRequestFlagPacket(new FlagData("disable_automations", new TimeRangeActivePeriod(TimeRange.create(Instant.parse("2021-10-05T04:53:22.877307Z"), Instant.parse("2021-10-05T04:53:44.305146Z"))))), CommandOpenPacket.class, true);
PacketTestUtil.testJson(new ImmutableScheduleCommandPacket(new ScheduledCommandData(System.currentTimeMillis(), "GEN OFF", Collections.singleton(1)), UUID.randomUUID()), CommandOpenPacket.class, true);
PacketTestUtil.testJson(new ImmutableDeleteAlterPacket("my_document_id", new RevisionUpdateToken("46-9ab7d71841380a36e1ec9e367deae36e")), CommandOpenPacket.class, true);
PacketTestUtil.testJson(new ImmutableRequestHeartbeatPacket(new HeartbeatData("Hourly Mate Ping", "heartbeat.ping.mate.hourly", Duration.ofHours(1), Duration.ofMinutes(5)), UUID.randomUUID()), CommandOpenPacket.class, true);
}
use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData in project solarthing by wildmountainfarms.
the class RequestHeartbeatReceiver method receivePacketGroup.
@Override
public void receivePacketGroup(String sender, TargetPacketGroup packetGroup) {
Instant now = Instant.now();
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof RequestHeartbeatPacket) {
RequestHeartbeatPacket requestHeartbeatPacket = (RequestHeartbeatPacket) packet;
HeartbeatData data = requestHeartbeatPacket.getData();
OpenSource openSource = new OpenSource(sender, packetGroup.getDateMillis(), requestHeartbeatPacket, requestHeartbeatPacket.getUniqueString());
ExecutionReason executionReason = new OpenSourceExecutionReason(openSource);
HeartbeatPacket heartbeatPacket = new ImmutableHeartbeatPacket(data, executionReason);
eventPacketListReceiverHandler.uploadSimple(now, Arrays.asList(heartbeatPacket));
}
}
}
use of me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData in project solarthing by wildmountainfarms.
the class NoHeartbeatEvent method checkForAlerts.
private void checkForAlerts(MessageSender messageSender) {
long now = System.currentTimeMillis();
for (Iterator<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<HeartbeatIdentifier, HeartbeatNode> entry = it.next();
HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
long latestNextHeartbeat = entry.getValue().getDateMillis() + data.getExpectedDurationToNextHeartbeat().toMillis() + data.getBufferDuration().toMillis();
if (now > latestNextHeartbeat) {
it.remove();
alertFor(messageSender, entry.getValue());
}
}
}
Aggregations