Meshtastic-Apple/Meshtastic/Views/Nodes/TelemetryLog.swift

397 lines
11 KiB
Swift
Raw Normal View History

2022-07-08 06:31:47 -07:00
//
// TelemetryLog.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 7/7/22.
//
import SwiftUI
struct TelemetryLog: View {
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
2022-07-15 15:01:42 -07:00
@State var isExporting = false
@State var exportString = ""
2022-07-08 06:31:47 -07:00
var node: NodeInfoEntity
var body: some View {
2022-07-11 08:25:16 -07:00
List {
2022-08-11 23:34:09 -07:00
ForEach(node.telemetries!.reversed() as! [TelemetryEntity], id: \.self) { (tel: TelemetryEntity) in
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
VStack (alignment: .leading) {
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
if tel.metricsType == 0 {
2022-07-11 08:25:16 -07:00
// Device Metrics
HStack {
Text("Device Metrics")
.font(.title)
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
BatteryIcon(batteryLevel: tel.batteryLevel, font: .callout, color: .accentColor)
if tel.batteryLevel == 0 {
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
Text("Plugged In")
2022-07-08 06:31:47 -07:00
.font(.callout)
.foregroundColor(.gray)
2022-07-11 08:25:16 -07:00
} else {
Text("Battery Level: \(String(tel.batteryLevel))%")
.font(.callout)
2022-07-08 06:31:47 -07:00
.foregroundColor(.gray)
2022-07-11 08:25:16 -07:00
}
if tel.batteryLevel > 0 {
Image(systemName: "bolt")
2022-07-08 06:31:47 -07:00
.font(.callout)
2022-07-11 08:25:16 -07:00
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Voltage: \(String(tel.voltage))")
2022-07-08 06:31:47 -07:00
.foregroundColor(.gray)
.font(.callout)
}
2022-07-11 08:25:16 -07:00
Text("Channel Utilization: \(String(format: "%.2f", tel.channelUtilization))%")
.foregroundColor(.gray)
.font(.callout)
2022-07-11 08:25:16 -07:00
Text("Airtime Utilization: \(String(format: "%.2f", tel.airUtilTx))%")
.foregroundColor(.gray)
.font(.callout)
2022-07-11 08:25:16 -07:00
Image(systemName: "clock.badge.checkmark.fill")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Time:")
.foregroundColor(.gray)
.font(.callout)
DateTimeText(dateTime: tel.time)
.foregroundColor(.gray)
.font(.callout)
}
} else if tel.metricsType == 1 {
// Environment Metrics
HStack {
Text("Environment Metrics")
.font(.title)
2022-07-11 08:25:16 -07:00
let tempReadingType = (!(node.telemetryConfig?.environmentDisplayFahrenheit ?? false)) ? "°C" : "°F"
2022-07-11 08:25:16 -07:00
2022-08-01 07:11:03 -07:00
if tel.temperature > 0 {
2022-07-11 08:25:16 -07:00
Image(systemName: "thermometer")
.font(.callout)
2022-07-11 08:25:16 -07:00
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Temperature: \(String(format: "%.2f", tel.temperature))\(tempReadingType)")
.foregroundColor(.gray)
.font(.callout)
}
2022-08-01 07:11:03 -07:00
if tel.relativeHumidity > 0 {
2022-07-11 08:25:16 -07:00
Image(systemName: "humidity")
.font(.callout)
2022-07-11 08:25:16 -07:00
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Relative Humidity: \(String(format: "%.2f", tel.relativeHumidity))")
.foregroundColor(.gray)
.font(.callout)
}
2022-08-01 07:11:03 -07:00
if tel.barometricPressure > 0 {
2022-07-11 08:25:16 -07:00
Image(systemName: "barometer")
.font(.callout)
2022-07-11 08:25:16 -07:00
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Barometric Pressure: \(String(format: "%.2f", tel.barometricPressure))")
.foregroundColor(.gray)
.font(.callout)
}
2022-08-01 07:11:03 -07:00
if tel.gasResistance > 0 {
2022-07-11 08:25:16 -07:00
Image(systemName: "aqi.medium")
.font(.callout)
2022-07-11 08:25:16 -07:00
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Gas Resistance: \(String(format: "%.2f", tel.gasResistance))")
.foregroundColor(.gray)
.font(.callout)
}
2022-08-01 07:11:03 -07:00
if tel.current > 0 {
2022-07-11 08:25:16 -07:00
Image(systemName: "directcurrent")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
2022-07-11 08:25:16 -07:00
Text("Current: \(String(format: "%.2f", tel.current))")
.foregroundColor(.gray)
.font(.callout)
2022-07-11 08:25:16 -07:00
Image(systemName: "bolt")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Voltage: \(String(format: "%.2f", tel.voltage))")
.foregroundColor(.gray)
.font(.callout)
}
2022-07-11 08:25:16 -07:00
Image(systemName: "clock.badge.checkmark.fill")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Time:")
.foregroundColor(.gray)
.font(.callout)
DateTimeText(dateTime: tel.time)
.foregroundColor(.gray)
.font(.callout)
2022-07-08 06:31:47 -07:00
}
2022-07-11 08:25:16 -07:00
}
2022-07-08 06:31:47 -07:00
2022-07-11 08:25:16 -07:00
} else {
if tel.metricsType == 0 {
2022-07-26 07:35:16 -07:00
// Device Metrics iPhone Template
2022-07-11 08:25:16 -07:00
VStack {
2022-07-26 07:35:16 -07:00
2022-07-12 08:12:31 -07:00
HStack {
Spacer()
2022-07-08 12:16:25 -07:00
Text("Device Metrics")
.font(.title3)
2022-07-12 08:12:31 -07:00
Spacer()
}
2022-07-11 08:25:16 -07:00
HStack {
2022-07-08 12:16:25 -07:00
BatteryIcon(batteryLevel: tel.batteryLevel, font: .callout, color: .accentColor)
if tel.batteryLevel == 0 {
Text("Plugged In")
.font(.callout)
.foregroundColor(.gray)
} else {
Text("Battery Level: \(String(tel.batteryLevel))%")
.font(.callout)
.foregroundColor(.gray)
}
2022-07-11 08:25:16 -07:00
}
HStack {
2022-07-08 12:16:25 -07:00
if tel.batteryLevel > 0 {
Image(systemName: "bolt")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Voltage: \(String(tel.voltage))")
.foregroundColor(.gray)
.font(.callout)
}
2022-07-11 08:25:16 -07:00
}
Text("Channel Utilization: \(String(format: "%.2f", tel.channelUtilization))%")
.foregroundColor(.gray)
.font(.callout)
Text("Airtime Utilization: \(String(format: "%.2f", tel.airUtilTx))%")
.foregroundColor(.gray)
.font(.callout)
HStack {
2022-07-08 12:16:25 -07:00
Image(systemName: "clock.badge.checkmark.fill")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Time:")
.foregroundColor(.gray)
.font(.callout)
DateTimeText(dateTime: tel.time)
.foregroundColor(.gray)
.font(.callout)
}
2022-07-11 08:25:16 -07:00
}
} else if tel.metricsType == 1 {
// Environment Metrics
2022-07-26 07:35:16 -07:00
let tempReadingType = (!(node.telemetryConfig?.environmentDisplayFahrenheit ?? true)) ? "°C" : "°F"
2022-07-11 08:25:16 -07:00
2022-07-26 07:35:16 -07:00
// Environment Metrics iPhone Template
2022-07-11 08:25:16 -07:00
VStack {
2022-07-26 07:35:16 -07:00
HStack {
2022-07-08 12:16:25 -07:00
2022-07-26 07:35:16 -07:00
Spacer()
Text("Environment Metrics")
.font(.title3)
Spacer()
}
HStack {
2022-08-01 07:11:03 -07:00
if tel.temperature > 0 {
2022-07-26 07:35:16 -07:00
Image(systemName: "thermometer")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Temperature: \(String(format: "%.2f", tel.temperature))\(tempReadingType)")
.foregroundColor(.gray)
2022-07-08 12:16:25 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-11 08:25:16 -07:00
}
2022-07-26 07:35:16 -07:00
HStack {
2022-07-08 12:16:25 -07:00
2022-08-01 07:11:03 -07:00
if tel.relativeHumidity > 0 {
2022-07-26 07:35:16 -07:00
Image(systemName: "humidity")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Relative Humidity: \(String(format: "%.2f", tel.relativeHumidity))")
.foregroundColor(.gray)
2022-07-08 12:16:25 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-11 08:25:16 -07:00
}
2022-08-01 07:11:03 -07:00
if tel.current > 0 {
2022-07-08 12:16:25 -07:00
2022-07-26 07:35:16 -07:00
HStack {
Image(systemName: "directcurrent")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Current: \(String(format: "%.2f", tel.current))")
.foregroundColor(.gray)
2022-07-08 12:16:25 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-08 12:16:25 -07:00
2022-07-26 07:35:16 -07:00
HStack {
Image(systemName: "bolt")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Voltage: \(String(format: "%.2f", tel.voltage))")
.foregroundColor(.gray)
2022-07-08 12:16:25 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-11 08:25:16 -07:00
}
2022-08-01 07:11:03 -07:00
if tel.barometricPressure > 0 {
2022-07-08 12:16:25 -07:00
2022-07-26 07:35:16 -07:00
HStack {
Image(systemName: "barometer")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Barometric Pressure: \(String(format: "%.2f", tel.barometricPressure))")
.foregroundColor(.gray)
2022-07-08 12:16:25 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-11 08:25:16 -07:00
}
2022-08-01 07:11:03 -07:00
if tel.gasResistance > 0 {
2022-07-11 08:25:16 -07:00
2022-07-26 07:35:16 -07:00
HStack {
Image(systemName: "aqi.medium")
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
Text("Gas Resistance: \(String(format: "%.2f", tel.gasResistance))")
.foregroundColor(.gray)
.font(.callout)
}
}
HStack {
Image(systemName: "clock.badge.checkmark.fill")
2022-07-11 08:25:16 -07:00
.font(.callout)
.foregroundColor(.accentColor)
.symbolRenderingMode(.hierarchical)
2022-07-26 07:35:16 -07:00
Text("Time:")
2022-07-08 12:16:25 -07:00
.foregroundColor(.gray)
.font(.callout)
2022-07-26 07:35:16 -07:00
DateTimeText(dateTime: tel.time)
.foregroundColor(.gray)
2022-07-11 08:25:16 -07:00
.font(.callout)
2022-07-26 07:35:16 -07:00
}
2022-07-08 06:31:47 -07:00
}
}
}
}
}
}
2022-07-15 15:01:42 -07:00
Button {
exportString = TelemetryToCsvFile(telemetry: node.telemetries!.array as! [TelemetryEntity], metricsType: 0)
isExporting = true
} label: {
Label("Save", systemImage: "square.and.arrow.down")
2022-07-15 15:01:42 -07:00
}
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
2022-07-08 06:31:47 -07:00
.navigationTitle("Telemetry Log \(node.telemetries?.count ?? 0) Readings")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(trailing:
ZStack {
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
})
.onAppear {
self.bleManager.context = context
}
2022-07-15 15:01:42 -07:00
.fileExporter(
isPresented: $isExporting,
document: CsvDocument(emptyCsv: exportString),
contentType: .commaSeparatedText,
defaultFilename: String("\(node.user!.longName ?? "Node") Telemetry Log"),
onCompletion: { result in
if case .success = result {
2022-07-15 15:09:12 -07:00
print("Telemetry log download succeeded.")
2022-07-15 15:01:42 -07:00
self.isExporting = false
} else {
2022-07-15 15:09:12 -07:00
print("Telemetry log download failed: \(result).")
2022-07-15 15:01:42 -07:00
}
}
)
2022-07-08 06:31:47 -07:00
}
}