Meshtastic-Apple/Meshtastic/Persistence/QueryCoreData.swift

93 lines
2.4 KiB
Swift
Raw Permalink 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
let fetchNodeInfoRequest = NodeInfoEntity.fetchRequest()
2023-01-31 10:50:17 -08:00
fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(id))
fetchNodeInfoRequest.fetchLimit = 1
2023-03-06 10:33:18 -08:00
2023-01-31 10:50:17 -08:00
do {
let fetchedNode = try context.fetch(fetchNodeInfoRequest)
2023-03-06 15:30:10 -08:00
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
}
2023-10-01 12:25:38 -07:00
public func getStoreAndForwardMessageIds(seconds: Int, context: NSManagedObjectContext) -> [UInt32] {
2023-10-01 12:25:38 -07:00
let time = seconds * -1
let fetchMessagesRequest = MessageEntity.fetchRequest()
2023-10-01 12:25:38 -07:00
let timeRange = Calendar.current.date(byAdding: .minute, value: time, to: Date())
let milleseconds = Int32(timeRange?.timeIntervalSince1970 ?? 0)
2024-07-31 20:56:09 -07:00
fetchMessagesRequest.predicate = NSPredicate(format: "messageTimestamp >= %d", milleseconds)
2023-10-01 12:25:38 -07:00
do {
let fetchedMessages = try context.fetch(fetchMessagesRequest)
2023-10-01 12:25:38 -07:00
if fetchedMessages.count == 1 {
return fetchedMessages.map { UInt32($0.messageId) }
}
} catch {
return []
}
return []
}
2023-12-08 11:41:29 -08:00
public func getTraceRoute(id: Int64, context: NSManagedObjectContext) -> TraceRouteEntity? {
let fetchTraceRouteRequest = TraceRouteEntity.fetchRequest()
2023-12-08 11:41:29 -08:00
fetchTraceRouteRequest.predicate = NSPredicate(format: "id == %lld", Int64(id))
do {
let fetchedTraceRoute = try context.fetch(fetchTraceRouteRequest)
2023-12-08 11:41:29 -08:00
if fetchedTraceRoute.count == 1 {
return fetchedTraceRoute[0]
}
} catch {
return nil
}
return nil
}
2023-10-01 12:25:38 -07:00
2023-01-31 10:50:17 -08:00
public func getUser(id: Int64, context: NSManagedObjectContext) -> UserEntity {
2023-03-06 10:33:18 -08:00
let fetchUserRequest = UserEntity.fetchRequest()
2023-01-31 10:50:17 -08:00
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 {
let fetchedUser = try context.fetch(fetchUserRequest)
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
let fetchWaypointRequest = WaypointEntity.fetchRequest()
2023-01-16 17:40:28 -08:00
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 {
let fetchedWaypoint = try context.fetch(fetchWaypointRequest)
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
}