use of me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket in project solarthing by wildmountainfarms.
the class AlterManagerAction method doSendCommand.
private void doSendCommand(VersionedPacket<StoredAlterPacket> versionedPacket, ScheduledCommandPacket scheduledCommandPacket) {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Sending command from data: " + scheduledCommandPacket.getData());
ScheduledCommandData data = scheduledCommandPacket.getData();
RequestCommandPacket requestCommandPacket = new ImmutableRequestCommandPacket(data.getCommandName());
// Having a document ID based off of the StoredAlterPacket's _id helps make sure we don't process it twice in case we are unable to delete it.
// -- if there's an update conflict while uploading, we know we already processed it
String documentId = "scheduled-request-" + versionedPacket.getPacket().getDbId();
PacketCollectionCreator creator = commandManager.makeCreator(sourceId, zoneId, InstanceTargetPackets.create(data.getTargetFragmentIds()), requestCommandPacket, zonedDateTime -> documentId);
executorService.execute(() -> {
Instant uploadingNow = Instant.now();
PacketCollection packetCollection = creator.create(uploadingNow);
boolean shouldDeleteAlter = false;
try {
database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Successfully uploaded packet collection that schedules command from data: " + scheduledCommandPacket.getData() + " document ID: " + documentId);
shouldDeleteAlter = true;
} catch (UpdateConflictSolarThingDatabaseException e) {
LOGGER.error("Got update conflict exception while uploading document ID: " + documentId + ". Will inspect existing document and overwrite if it's a malicious actor...", e);
VersionedPacket<StoredPacketGroup> existingDocument = null;
try {
existingDocument = database.getOpenDatabase().getPacketCollection(documentId);
} catch (SolarThingDatabaseException ex) {
LOGGER.error("Could not retrieve document with document ID: " + documentId, ex);
}
if (existingDocument != null) {
if (isDocumentMadeByUs(uploadingNow, data, existingDocument.getPacket())) {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "False alarm everyone. The packet in the database was made by us and its timestamp is reasonable. document ID: " + documentId);
shouldDeleteAlter = true;
} else {
LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The packet in the database with document ID: " + documentId + " was not made by us. Could be a malicious actor. We will overwrite that packet.");
try {
database.getOpenDatabase().uploadPacketCollection(packetCollection, existingDocument.getUpdateToken());
shouldDeleteAlter = true;
} catch (SolarThingDatabaseException ex) {
LOGGER.error("Could not overwrite malicious packet. Will likely try again. document ID: " + documentId, ex);
}
}
}
} catch (SolarThingDatabaseException e) {
LOGGER.error("Failed to upload our request command packet. documentId: " + documentId, e);
}
if (shouldDeleteAlter) {
try {
database.getAlterDatabase().delete(versionedPacket);
} catch (SolarThingDatabaseException e) {
LOGGER.error("Error while deleting an alter document. document ID: " + versionedPacket.getPacket().getDbId() + " update token: " + versionedPacket.getUpdateToken(), e);
}
}
});
}
use of me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket in project solarthing by wildmountainfarms.
the class ActionNodeDataReceiver method receivePacketGroup.
@Override
public void receivePacketGroup(String sender, TargetPacketGroup packetGroup) {
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof CommandOpenPacket) {
CommandOpenPacket commandOpenPacket = (CommandOpenPacket) packet;
if (commandOpenPacket.getPacketType() == CommandOpenPacketType.REQUEST_COMMAND) {
RequestCommandPacket requestCommand = (RequestCommandPacket) commandOpenPacket;
OpenSource source = new OpenSource(sender, packetGroup.getDateMillis(), requestCommand, requestCommand.getCommandName());
receiveData(source, requestCommand.getCommandName());
}
}
}
}
Aggregations