Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect localstats update #941

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Meshtastic/Enums/TelemetryEnums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,30 @@ enum Iaq: Int, CaseIterable, Identifiable {
return iaq
}
}


// Default of 0 is Client
enum MetricsTypes: Int, CaseIterable, Identifiable {

case device = 0
case environment = 1
case power = 2
case airQuality = 3
case stats = 4

var id: Int { self.rawValue }
var name: String {
switch self {
case .device:
return "Device Metrics"
case .environment:
return "Environment Metrics"
case .power:
return "Power Metrics"
case .airQuality:
return "Air Quality Metrics"
case .stats:
return "Stats"
}
}
}
13 changes: 5 additions & 8 deletions Meshtastic/Helpers/MeshPackets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func routingPacket (packet: MeshPacket, connectedNodeNum: Int64, context: NSMana

func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManagedObjectContext) {

if let telemetryMessage = try? Telemetry(serializedData: packet.decoded.payload) {
if let telemetryMessage = try? Telemetry(serializedBytes: packet.decoded.payload) {

let logString = String.localizedStringWithFormat("mesh.log.telemetry.received %@".localized, String(packet.from))
MeshLogger.log("📈 \(logString)")
Expand Down Expand Up @@ -729,7 +729,7 @@ func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManage
telemetry.numPacketsRxBad = Int32(truncatingIfNeeded: telemetryMessage.localStats.numPacketsRxBad)
telemetry.numOnlineNodes = Int32(truncatingIfNeeded: telemetryMessage.localStats.numOnlineNodes)
telemetry.numTotalNodes = Int32(truncatingIfNeeded: telemetryMessage.localStats.numTotalNodes)
telemetry.metricsType = 6
telemetry.metricsType = 4
Logger.statistics.info("📈 [Mesh Statistics] Channel Utilization: \(telemetryMessage.localStats.channelUtilization, privacy: .public) Airtime: \(telemetryMessage.localStats.airUtilTx, privacy: .public) Packets Sent: \(telemetryMessage.localStats.numPacketsTx, privacy: .public) Packets Received: \(telemetryMessage.localStats.numPacketsRx, privacy: .public) Bad Packets Received: \(telemetryMessage.localStats.numPacketsRxBad, privacy: .public) Nodes Online: \(telemetryMessage.localStats.numOnlineNodes, privacy: .public) of \(telemetryMessage.localStats.numTotalNodes, privacy: .public) nodes for Node: \(packet.from.toHex(), privacy: .public)")
}
telemetry.snr = packet.rxSnr
Expand All @@ -748,7 +748,7 @@ func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManage
}
try context.save()

Logger.data.info("💾 [TelemetryEntity] Saved for Node: \(packet.from.toHex())")
Logger.data.info("💾 [TelemetryEntity] of type \(MetricsTypes(rawValue: Int(telemetry.metricsType))?.name ?? "Unknown Metrics Type") Saved for Node: \(packet.from.toHex())")
if telemetry.metricsType == 0 {
// Connected Device Metrics
// ------------------------
Expand All @@ -769,9 +769,9 @@ func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManage
manager.schedule()
}
}
} else if telemetry.metricsType == 6 {
} else if telemetry.metricsType == 4 {
// Update our live activity if there is one running, not available on mac iOS >= 16.2
#if !targetEnvironment(macCatalyst)
#if canImport(ActivityKit)

let fifteenMinutesLater = Calendar.current.date(byAdding: .minute, value: (Int(15) ), to: Date())!
let date = Date.now...fifteenMinutesLater
Expand Down Expand Up @@ -843,13 +843,10 @@ func textMessageAppPacket(

if messageText?.count ?? 0 > 0 {
MeshLogger.log("💬 \("mesh.log.textmessage.received".localized)")

let messageUsers = UserEntity.fetchRequest()
messageUsers.predicate = NSPredicate(format: "num IN %@", [packet.to, packet.from])

do {
let fetchedUsers = try context.fetch(messageUsers)

let newMessage = MessageEntity(context: context)
newMessage.messageId = Int64(packet.id)
if packet.rxTime > 0 {
Expand Down
8 changes: 5 additions & 3 deletions Meshtastic/Views/Bluetooth/Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct Connect: View {
@State var invalidFirmwareVersion = false
@State var liveActivityStarted = false
@State var selectedPeripherialId = ""
@State var localStats: TelemetryEntity?

init () {
let notificationCenter = UNUserNotificationCenter.current()
Expand Down Expand Up @@ -91,8 +92,6 @@ struct Connect: View {
}
}
VStack {
let localStats = node?.telemetries?.filtered(using: NSPredicate(format: "metricsType == 6")).lastObject as? TelemetryEntity

if localStats != nil {
Divider()
if localStats?.numTotalNodes ?? 0 >= 100 {
Expand Down Expand Up @@ -351,6 +350,9 @@ struct Connect: View {
.onChange(of: (self.bleManager.invalidVersion)) { _ in
invalidFirmwareVersion = self.bleManager.invalidVersion
}
.onChange(of: [node?.telemetries]) { _ in
localStats = node?.telemetries?.filtered(using: NSPredicate(format: "metricsType == 4")).lastObject as? TelemetryEntity
}
.onChange(of: (self.bleManager.isSubscribed)) { sub in

if UserDefaults.preferredPeripheralId.count > 0 && sub {
Expand All @@ -376,7 +378,7 @@ struct Connect: View {
liveActivityStarted = true
// 15 Minutes Local Stats Interval
let timerSeconds = 900
let localStats = node?.telemetries?.filtered(using: NSPredicate(format: "metricsType == 6"))
let localStats = node?.telemetries?.filtered(using: NSPredicate(format: "metricsType == 4"))
let mostRecent = localStats?.lastObject as? TelemetryEntity

let activityAttributes = MeshActivityAttributes(nodeNum: Int(node?.num ?? 0), name: node?.user?.longName ?? "unknown")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct WeatherConditionsCompactWidget: View {
.font(.caption)
}
Text(temperature)
.font(temperature.length < 4 ? .system(size: 76) : .system(size: 60) )
.font(temperature.length < 4 ? .system(size: 72) : .system(size: 54) )
}
.frame(minWidth: 100, idealWidth: 125, maxWidth: 150, minHeight: 120, idealHeight: 130, maxHeight: 140)
.padding()
Expand Down