From 1b5b146334820bbd2b3d6f60a111f6edc9a8fda2 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Thu, 10 Nov 2022 23:27:48 -0800 Subject: [PATCH] Add table to device metrics, add snr to position and message --- Meshtastic/Export/WriteCsvFile.swift | 4 +- Meshtastic/Helpers/MeshPackets.swift | 4 +- .../MeshtasticDataModel.xcdatamodel/contents | 2 + Meshtastic/Views/Nodes/DeviceMetricsLog.swift | 135 ++++++++++-------- Meshtastic/Views/Nodes/PositionLog.swift | 3 + 5 files changed, 89 insertions(+), 59 deletions(-) diff --git a/Meshtastic/Export/WriteCsvFile.swift b/Meshtastic/Export/WriteCsvFile.swift index b03411dd..6cf3da5e 100644 --- a/Meshtastic/Export/WriteCsvFile.swift +++ b/Meshtastic/Export/WriteCsvFile.swift @@ -69,7 +69,7 @@ func PositionToCsvFile(positions: [PositionEntity]) -> String { var csvString: String = "" // Create Position Header - csvString = "SeqNo, Latitude, Longitude, Alt, Sats, Speed, Heading, Timestamp" + csvString = "SeqNo, Latitude, Longitude, Alt, Sats, Speed, Heading, SNR, Timestamp" for pos in positions { @@ -88,6 +88,8 @@ func PositionToCsvFile(positions: [PositionEntity]) -> String { csvString += ", " csvString += String(pos.heading) csvString += ", " + csvString += String(pos.snr) + csvString += ", " csvString += pos.time?.formattedDate(format: "yyyy-MM-dd HH:mm:ss") ?? "Unknown Age" } diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index c37e3eea..cd1a4aea 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -1069,6 +1069,7 @@ func positionPacket (packet: MeshPacket, context: NSManagedObjectContext) { let position = PositionEntity(context: context) + position.snr = packet.rxSnr position.seqNo = Int32(positionMessage.seqNumber) position.latitudeI = positionMessage.latitudeI position.longitudeI = positionMessage.longitudeI @@ -1266,8 +1267,6 @@ func telemetryPacket(packet: MeshPacket, context: NSManagedObjectContext) { } func textMessageAppPacket(packet: MeshPacket, connectedNode: Int64, context: NSManagedObjectContext) { - - let broadcastNodeNum: UInt32 = 4294967295 if let messageText = String(bytes: packet.decoded.payload, encoding: .utf8) { @@ -1282,6 +1281,7 @@ func textMessageAppPacket(packet: MeshPacket, connectedNode: Int64, context: NSM newMessage.messageId = Int64(packet.id) newMessage.messageTimestamp = Int32(bitPattern: packet.rxTime) newMessage.receivedACK = false + newMessage.snr = packet.rxSnr newMessage.isEmoji = packet.decoded.emoji == 1 newMessage.channel = Int32(packet.channel) diff --git a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents index 31ed3498..9ae78693 100644 --- a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents +++ b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel.xcdatamodel/contents @@ -87,6 +87,7 @@ + @@ -182,6 +183,7 @@ + diff --git a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift index 29b7a504..9d21d95d 100644 --- a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift +++ b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift @@ -28,19 +28,11 @@ struct DeviceMetricsLog: View { } var body: some View { - NavigationStack { - let oneDayAgo = Calendar.current.date(byAdding: .day, value: -3, to: Date()) - let data = node.telemetries!.filtered(using: NSPredicate(format: "metricsType == 0 && time !=nil && time >= %@", oneDayAgo! as CVarArg)) - if data.count > 0 { - - #if canImport(Charts) - GroupBox(label: Label("Battery Level Trend", systemImage: "battery.100")) { - Chart(data.array as! [TelemetryEntity], id: \.self) { LineMark( x: .value("Hour", $0.time!.formattedDate(format: "ha")), @@ -53,64 +45,95 @@ struct DeviceMetricsLog: View { } .frame(height: 150) } - - #endif } - - ScrollView { - - Grid(alignment: .topLeading, horizontalSpacing: 2) { + if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { + //Add a table for mac and ipad + Table(node.telemetries!.reversed() as! [TelemetryEntity]) { - GridRow { - - Text("Batt") - .font(.callout) - .fontWeight(.bold) - Text("Voltage") - .font(.callout) - .fontWeight(.bold) - Text("ChUtil") - .font(.callout) - .fontWeight(.bold) - Text("AirTm") - .font(.callout) - .fontWeight(.bold) - Text("Timestamp") - .font(.callout) - .fontWeight(.bold) - } - Divider() - ForEach(node.telemetries!.reversed() as! [TelemetryEntity], id: \.self) { (dm: TelemetryEntity) in + TableColumn("Battery Level") { dm in if dm.metricsType == 0 { - - GridRow { + if dm.batteryLevel == 0 { - if dm.batteryLevel == 0 { - - Text("USB") - .font(.callout) - - } else { - - Text("\(String(dm.batteryLevel))%") - .font(.callout) - } + Text("Powered") - Text(String(dm.voltage)) - .font(.callout) - Text("\(String(format: "%.2f", dm.channelUtilization))%") - .font(.callout) - Text("\(String(format: "%.2f", dm.airUtilTx))%") - .font(.callout) - Text(dm.time?.formattedDate(format: "MM/dd/yy hh:mm") ?? "Unknown time") - .font(.callout) + } else { + + Text("\(String(dm.batteryLevel))%") } } } + TableColumn("Voltage") { dm in + Text(String(format: "%.6f", dm.voltage ?? 0)) + } + TableColumn("Channel Utilization") { dm in + Text(String(format: "%.2f", dm.channelUtilization)) + } + TableColumn("Airtime") { dm in + Text("\(String(format: "%.2f", dm.airUtilTx))%") + } + TableColumn("Time Stamp") { dm in + Text(dm.time?.formattedDate(format: "MM/dd/yy hh:mm") ?? "Unknown time") + } + } + + } else { + + ScrollView { + + Grid(alignment: .topLeading, horizontalSpacing: 2) { + + GridRow { + + Text("Batt") + .font(.callout) + .fontWeight(.bold) + Text("Voltage") + .font(.callout) + .fontWeight(.bold) + Text("ChUtil") + .font(.callout) + .fontWeight(.bold) + Text("AirTm") + .font(.callout) + .fontWeight(.bold) + Text("Timestamp") + .font(.callout) + .fontWeight(.bold) + } + Divider() + ForEach(node.telemetries!.reversed() as! [TelemetryEntity], id: \.self) { (dm: TelemetryEntity) in + + if dm.metricsType == 0 { + + GridRow { + + if dm.batteryLevel == 0 { + + Text("USB") + .font(.callout) + + } else { + + Text("\(String(dm.batteryLevel))%") + .font(.callout) + } + + Text(String(dm.voltage)) + .font(.callout) + Text("\(String(format: "%.2f", dm.channelUtilization))%") + .font(.callout) + Text("\(String(format: "%.2f", dm.airUtilTx))%") + .font(.callout) + Text(dm.time?.formattedDate(format: "MM/dd/yy hh:mm") ?? "Unknown time") + .font(.callout) + } + } + } + } + .padding(.leading, 15) + .padding(.trailing, 5) } - .padding(.leading, 15) - .padding(.trailing, 5) } } HStack { diff --git a/Meshtastic/Views/Nodes/PositionLog.swift b/Meshtastic/Views/Nodes/PositionLog.swift index 984d22bb..78774925 100644 --- a/Meshtastic/Views/Nodes/PositionLog.swift +++ b/Meshtastic/Views/Nodes/PositionLog.swift @@ -46,6 +46,9 @@ struct PositionLog: View { TableColumn("Heading") { position in Text(String(position.heading)) } + TableColumn("SNR") { position in + Text(String(position.snr)) + } TableColumn("Time Stamp") { position in Text(position.time?.formattedDate(format: "MM/dd/yy hh:mm") ?? "Unknown time") }