Meshtastic-Apple/Meshtastic/Persistence/QueryCoreData.swift

81 lines
2.4 KiB
Swift
Raw Normal View History

2023-01-16 17:40:28 -08:00
//
// QueryCoreData.swift
// Meshtastic
//
// Created(c) Garth Vander Houwen 1/16/23.
//
import CoreData
public func getNodeInfo(id: Int64, context: NSManagedObjectContext) -> NodeInfoEntity? {
2023-03-06 10:33:18 -08:00
2023-01-31 10:50:17 -08:00
let fetchNodeInfoRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "NodeInfoEntity")
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
2023-03-06 10:33:18 -08:00
2023-01-31 10:50:17 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context.fetch(fetchNodeInfoRequest) as? [NodeInfoEntity] else {
return nil
}
if fetchedNode.count == 1 {
return fetchedNode[0]
2023-01-31 10:50:17 -08:00
}
} catch {
return nil
2023-01-31 10:50:17 -08:00
}
return nil
2023-01-31 10:50:17 -08:00
}
public func getUser(id: Int64, context: NSManagedObjectContext) -> UserEntity {
2023-03-06 10:33:18 -08:00
2023-01-31 10:50:17 -08:00
let fetchUserRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "UserEntity")
fetchUserRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
2023-03-06 10:33:18 -08:00
2023-01-31 10:50:17 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedUser = try context.fetch(fetchUserRequest) as? [UserEntity] else {
return UserEntity(context: context)
}
2023-01-31 10:50:17 -08:00
if fetchedUser.count == 1 {
return fetchedUser[0]
}
} catch {
return UserEntity(context: context)
}
return UserEntity(context: context)
}
2023-01-16 17:40:28 -08:00
public func getWaypoint(id: Int64, context: NSManagedObjectContext) -> WaypointEntity {
2023-03-06 10:33:18 -08:00
2023-01-16 17:40:28 -08:00
let fetchWaypointRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "WaypointEntity")
fetchWaypointRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
2023-03-06 10:33:18 -08:00
2023-01-16 17:40:28 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedWaypoint = try context.fetch(fetchWaypointRequest) as? [WaypointEntity] else {
return WaypointEntity(context: context)
}
2023-01-16 17:40:28 -08:00
if fetchedWaypoint.count == 1 {
return fetchedWaypoint[0]
}
} catch {
2023-01-16 23:16:57 -08:00
return WaypointEntity(context: context)
2023-01-16 17:40:28 -08:00
}
2023-01-16 23:16:57 -08:00
return WaypointEntity(context: context)
2023-01-16 17:40:28 -08:00
}
2023-08-23 07:03:05 -05:00
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()
2023-08-26 23:17:30 -07:00
} catch {
2023-08-23 07:03:05 -05:00
return []
}
}