2023-09-03 22:14:59 -07:00
|
|
|
//
|
|
|
|
|
// NodeInfoEntityExtension.swift
|
|
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 9/3/23.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2024-01-16 08:55:23 -08:00
|
|
|
import CoreData
|
2023-09-03 22:14:59 -07:00
|
|
|
|
|
|
|
|
extension NodeInfoEntity {
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-07-11 08:42:27 -07:00
|
|
|
var latestPosition: PositionEntity? {
|
2024-07-12 01:28:56 -07:00
|
|
|
return self.positions?.lastObject as? PositionEntity
|
2024-07-11 08:42:27 -07:00
|
|
|
}
|
|
|
|
|
|
2024-08-17 15:31:35 -07:00
|
|
|
var latestDeviceMetrics: TelemetryEntity? {
|
|
|
|
|
return self.telemetries?.filtered(using: NSPredicate(format: "metricsType == 0")).lastObject as? TelemetryEntity
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 08:42:27 -07:00
|
|
|
var latestEnvironmentMetrics: TelemetryEntity? {
|
2024-07-12 01:28:56 -07:00
|
|
|
return self.telemetries?.filtered(using: NSPredicate(format: "metricsType == 1")).lastObject as? TelemetryEntity
|
2024-07-11 08:42:27 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-03 22:14:59 -07:00
|
|
|
var hasPositions: Bool {
|
2024-09-29 10:25:37 -07:00
|
|
|
return self.positions?.count ?? 0 > 0
|
2023-09-03 22:14:59 -07:00
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-09-03 22:14:59 -07:00
|
|
|
var hasDeviceMetrics: Bool {
|
2024-05-29 16:40:07 -05:00
|
|
|
let deviceMetrics = telemetries?.filter { ($0 as AnyObject).metricsType == 0 }
|
2023-09-03 22:14:59 -07:00
|
|
|
return deviceMetrics?.count ?? 0 > 0
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-09-03 22:14:59 -07:00
|
|
|
var hasEnvironmentMetrics: Bool {
|
2024-05-29 16:40:07 -05:00
|
|
|
let environmentMetrics = telemetries?.filter { ($0 as AnyObject).metricsType == 1 }
|
2023-09-03 22:14:59 -07:00
|
|
|
return environmentMetrics?.count ?? 0 > 0
|
|
|
|
|
}
|
2023-09-09 21:23:14 -07:00
|
|
|
var hasDetectionSensorMetrics: Bool {
|
|
|
|
|
return user?.sensorMessageList.count ?? 0 > 0
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-12-22 08:25:59 -08:00
|
|
|
var hasTraceRoutes: Bool {
|
|
|
|
|
return traceRoutes?.count ?? 0 > 0
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-02-25 21:40:25 -08:00
|
|
|
var hasPax: Bool {
|
|
|
|
|
return pax?.count ?? 0 > 0
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-02-10 17:43:01 -08:00
|
|
|
var isStoreForwardRouter: Bool {
|
|
|
|
|
return storeForwardConfig?.isRouter ?? false
|
|
|
|
|
}
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2023-09-07 23:38:53 -07:00
|
|
|
var isOnline: Bool {
|
2024-08-25 09:50:01 -07:00
|
|
|
let twoHoursAgo = Calendar.current.date(byAdding: .minute, value: -120, to: Date())
|
|
|
|
|
if lastHeard?.compare(twoHoursAgo!) == .orderedDescending {
|
2023-09-07 23:38:53 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-08-18 11:39:41 -07:00
|
|
|
|
2024-08-18 10:00:15 -07:00
|
|
|
var canRemoteAdmin: Bool {
|
2024-08-18 17:15:55 -07:00
|
|
|
if UserDefaults.enableAdministration {
|
2024-08-18 10:00:15 -07:00
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
let adminChannel = myInfo?.channels?.filter { ($0 as AnyObject).name?.lowercased() == "admin" }
|
|
|
|
|
return adminChannel?.count ?? 0 > 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-16 08:55:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func createNodeInfo(num: Int64, context: NSManagedObjectContext) -> NodeInfoEntity {
|
2024-05-29 16:40:07 -05:00
|
|
|
|
2024-01-16 08:55:23 -08:00
|
|
|
let newNode = NodeInfoEntity(context: context)
|
|
|
|
|
newNode.id = Int64(num)
|
|
|
|
|
newNode.num = Int64(num)
|
|
|
|
|
let newUser = UserEntity(context: context)
|
|
|
|
|
newUser.num = Int64(num)
|
2024-08-04 15:53:59 -07:00
|
|
|
let userId = num.toHex()
|
2024-01-16 08:55:23 -08:00
|
|
|
newUser.userId = "!\(userId)"
|
|
|
|
|
let last4 = String(userId.suffix(4))
|
|
|
|
|
newUser.longName = "Meshtastic \(last4)"
|
|
|
|
|
newUser.shortName = last4
|
|
|
|
|
newUser.hwModel = "UNSET"
|
|
|
|
|
newNode.user = newUser
|
|
|
|
|
return newNode
|
2023-09-03 22:14:59 -07:00
|
|
|
}
|