use of me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket in project solarthing by wildmountainfarms.
the class AlterManagerAction method receivePacketWithIntegrity.
private void receivePacketWithIntegrity(String sender, TargetPacketGroup packetGroup) {
LOGGER.debug("Sender: " + sender + " is authorized and sent a packet targeting no fragments, so we will see if we want to handle anything from it.");
long now = System.currentTimeMillis();
List<StoredAlterPacket> storedAlterPacketsToUpload = new ArrayList<>();
List<DeleteAlterPacket> deleteAlterPackets = new ArrayList<>();
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof ScheduleCommandPacket) {
ScheduleCommandPacket scheduleCommandPacket = (ScheduleCommandPacket) packet;
ScheduledCommandData data = scheduleCommandPacket.getData();
ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), scheduleCommandPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
scheduleCommandPacket.getUniqueString()));
ScheduledCommandPacket scheduledCommandPacket = new ScheduledCommandPacket(data, executionReason);
// This databaseId is basically an arbitrary way to generate a unique ID. It contains some stuff such as the command name to debug more easily
String databaseId = "alter-scheduled-command-" + data.getCommandName() + "-" + Long.toHexString(data.getScheduledTimeMillis()) + "-" + sender + "-" + Math.random();
StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, scheduledCommandPacket, this.sourceId);
storedAlterPacketsToUpload.add(storedAlterPacket);
} else if (packet instanceof DeleteAlterPacket) {
DeleteAlterPacket deleteAlterPacket = (DeleteAlterPacket) packet;
try {
database.validateUpdateToken(deleteAlterPacket.getUpdateToken());
deleteAlterPackets.add(deleteAlterPacket);
} catch (IncompatibleUpdateTokenException ex) {
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "For some reason we have an incompatible update token!", ex);
}
} else if (packet instanceof RequestFlagPacket) {
RequestFlagPacket requestFlagPacket = (RequestFlagPacket) packet;
// Originally I was going to not upload a FlagPacket if an existing FlagPacket's
// time range fully encapsulated the newly requested one, but that adds some complexity
// that is not needed at the moment
FlagData flagData = requestFlagPacket.getFlagData();
ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(sender, packetGroup.getDateMillis(), requestFlagPacket, // this is legacy data and shouldn't be used anywhere, so it doesn't matter what we put here
requestFlagPacket.getUniqueString()));
FlagPacket flagPacket = new FlagPacket(flagData, executionReason);
String databaseId = "alter-flag-" + flagData.getFlagName() + "-" + sender + "-" + Math.random();
StoredAlterPacket storedAlterPacket = new ImmutableStoredAlterPacket(databaseId, now, flagPacket, sourceId);
storedAlterPacketsToUpload.add(storedAlterPacket);
}
}
if (storedAlterPacketsToUpload.isEmpty() && deleteAlterPackets.isEmpty()) {
// Nothing for us to do, so no need to schedule a runnable
return;
}
executorService.execute(() -> {
int uploadCount = 0;
try {
for (StoredAlterPacket storedAlterPacket : storedAlterPacketsToUpload) {
this.database.getAlterDatabase().upload(storedAlterPacket);
uploadCount++;
}
} catch (SolarThingDatabaseException e) {
// TODO in future we should try multiple times to upload
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not upload a stored alter packet! uploaded: " + uploadCount + " / " + storedAlterPacketsToUpload.size(), e);
}
int deleteCount = 0;
try {
for (DeleteAlterPacket deleteAlterPacket : deleteAlterPackets) {
this.database.getAlterDatabase().delete(deleteAlterPacket.getDocumentIdToDelete(), deleteAlterPacket.getUpdateToken());
deleteCount++;
}
} catch (SolarThingDatabaseException e) {
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Could not delete alter packets! deleted: " + deleteCount + " / " + deleteAlterPackets.size(), e);
}
});
}
Aggregations