Update detection sensor log query to update in real time

This commit is contained in:
Garth Vander Houwen 2023-11-29 23:01:51 -08:00
parent 6c2e3cd316
commit 9c57bdd4c4
2 changed files with 4 additions and 19 deletions

View file

@ -82,21 +82,3 @@ public func getWaypoint(id: Int64, context: NSManagedObjectContext) -> WaypointE
}
return WaypointEntity(context: context)
}
public func getDetectionSensorMessages(nodeNum: Int64?, context: NSManagedObjectContext) -> [MessageEntity] {
let fetchDetectionMessagesPredicate: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "MessageEntity")
fetchDetectionMessagesPredicate.predicate = NSPredicate(format: "portNum == %d", Int32(PortNum.detectionSensorApp.rawValue))
do {
let fetched = try context.fetch(fetchDetectionMessagesPredicate) as? [MessageEntity] ?? []
if nodeNum == nil {
return fetched.reversed()
}
return fetched.filter { message in
return message.fromUser?.num == nodeNum!
}.reversed()
} catch {
return []
}
}

View file

@ -15,10 +15,13 @@ struct DetectionSensorLog: View {
@State var isExporting = false
@State var exportString = ""
@ObservedObject var node: NodeInfoEntity
@FetchRequest(sortDescriptors: [NSSortDescriptor(key: "messageTimestamp", ascending: false)],
predicate: NSPredicate(format: "portNum == %d", Int32(PortNum.detectionSensorApp.rawValue)), animation: .none)
private var detections: FetchedResults<MessageEntity>
var body: some View {
let oneDayAgo = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let detections = getDetectionSensorMessages(nodeNum: node.num, context: context)
let chartData = detections
.filter { $0.timestamp >= oneDayAgo! }
.sorted { $0.timestamp < $1.timestamp }