2022-07-08 06:31:47 -07:00
|
|
|
//
|
2022-09-14 07:29:31 -07:00
|
|
|
// DeviceMetricsLog.swift
|
2022-07-08 06:31:47 -07:00
|
|
|
// Meshtastic
|
|
|
|
|
//
|
|
|
|
|
// Copyright(c) Garth Vander Houwen 7/7/22.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
2023-04-07 19:47:24 -07:00
|
|
|
import Charts
|
2022-07-08 06:31:47 -07:00
|
|
|
|
2022-09-14 06:50:27 -07:00
|
|
|
struct EnvironmentMetricsLog: View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-08 06:31:47 -07:00
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-10-03 20:13:33 -07:00
|
|
|
@State private var isPresentingClearLogConfirm: Bool = false
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-15 15:01:42 -07:00
|
|
|
@State var isExporting = false
|
|
|
|
|
@State var exportString = ""
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-08 06:31:47 -07:00
|
|
|
var node: NodeInfoEntity
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-08 06:31:47 -07:00
|
|
|
var body: some View {
|
2023-03-31 12:08:42 -07:00
|
|
|
|
2023-04-07 19:47:24 -07:00
|
|
|
|
|
|
|
|
let oneWeekAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date())
|
2023-03-31 12:08:42 -07:00
|
|
|
let environmentMetrics = node.telemetries?.filtered(using: NSPredicate(format: "metricsType == 1")).reversed() as? [TelemetryEntity] ?? []
|
2023-04-07 19:47:24 -07:00
|
|
|
let chartData = environmentMetrics
|
|
|
|
|
.filter { $0.time != nil && $0.time! >= oneWeekAgo! }
|
|
|
|
|
.sorted { $0.time! < $1.time! }
|
2023-05-31 21:39:56 -07:00
|
|
|
let locale = NSLocale.current as NSLocale
|
|
|
|
|
let localeUnit = locale.object(forKey: NSLocale.Key(rawValue: "kCFLocaleTemperatureUnitKey"))
|
|
|
|
|
var format: UnitTemperature = localeUnit as? String ?? "Celsius" == "Fahrenheit" ? .fahrenheit : .celsius
|
|
|
|
|
|
2022-09-14 06:50:27 -07:00
|
|
|
NavigationStack {
|
2023-04-07 19:47:24 -07:00
|
|
|
|
|
|
|
|
if chartData.count > 0 {
|
|
|
|
|
GroupBox(label: Label("\(environmentMetrics.count) Readings Total", systemImage: "chart.xyaxis.line")) {
|
|
|
|
|
|
|
|
|
|
Chart {
|
|
|
|
|
ForEach(chartData, id: \.time) { dataPoint in
|
|
|
|
|
AreaMark(
|
|
|
|
|
x: .value("Time", dataPoint.time!),
|
|
|
|
|
y: .value("Temperature", dataPoint.temperature.localeTemperature()),
|
|
|
|
|
stacking: .unstacked
|
|
|
|
|
)
|
|
|
|
|
.interpolationMethod(.cardinal)
|
|
|
|
|
.foregroundStyle(
|
|
|
|
|
.linearGradient(
|
|
|
|
|
colors: [.blue, .yellow, .orange, .red, .red],
|
|
|
|
|
startPoint: .bottom, endPoint: .top
|
|
|
|
|
)
|
|
|
|
|
.opacity(0.6)
|
|
|
|
|
)
|
|
|
|
|
.alignsMarkStylesWithPlotArea()
|
|
|
|
|
.accessibilityHidden(true)
|
|
|
|
|
|
|
|
|
|
LineMark(
|
|
|
|
|
x: .value("Time", dataPoint.time!),
|
|
|
|
|
y: .value("Temperature", dataPoint.temperature.localeTemperature())
|
|
|
|
|
)
|
|
|
|
|
.interpolationMethod(.cardinal)
|
|
|
|
|
.foregroundStyle(
|
|
|
|
|
.linearGradient(
|
|
|
|
|
colors: [.blue, .yellow, .orange, .red, .red],
|
|
|
|
|
startPoint: .bottom, endPoint: .top
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.lineStyle(StrokeStyle(lineWidth: 4))
|
|
|
|
|
.alignsMarkStylesWithPlotArea()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.chartXAxis(content: {
|
|
|
|
|
AxisMarks(position: .top)
|
|
|
|
|
})
|
2023-06-07 17:19:36 -07:00
|
|
|
.chartYScale(domain: format == .celsius ? -20...55 : 0...125)
|
2023-04-07 19:47:24 -07:00
|
|
|
.chartForegroundStyleScale([
|
|
|
|
|
"Temperature" : .clear
|
|
|
|
|
])
|
|
|
|
|
.chartLegend(position: .automatic, alignment: .bottom)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 00:26:59 -08:00
|
|
|
let localeDateFormat = DateFormatter.dateFormat(fromTemplate: "yyMMddjmma", options: 0, locale: Locale.current)
|
2023-01-12 07:57:24 -08:00
|
|
|
let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma").replacingOccurrences(of: ",", with: "")
|
2022-11-11 07:07:48 -08:00
|
|
|
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
|
2023-03-06 10:33:18 -08:00
|
|
|
// Add a table for mac and ipad
|
2023-03-31 12:08:42 -07:00
|
|
|
Table(environmentMetrics) {
|
2022-11-11 07:07:48 -08:00
|
|
|
TableColumn("Temperature") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text(em.temperature.formattedTemperature())
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
|
|
|
|
TableColumn("Humidity") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text("\(String(format: "%.2f", em.relativeHumidity))%")
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
|
|
|
|
TableColumn("Barometric Pressure") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text("\(String(format: "%.2f", em.barometricPressure)) hPa")
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2023-01-01 14:48:50 -08:00
|
|
|
TableColumn("gas.resistance") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text("\(String(format: "%.2f", em.gasResistance)) ohms")
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2022-12-31 02:23:21 -08:00
|
|
|
TableColumn("current") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text("\(String(format: "%.2f", em.current))")
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2022-12-31 02:23:21 -08:00
|
|
|
TableColumn("voltage") { em in
|
2023-03-31 12:08:42 -07:00
|
|
|
Text("\(String(format: "%.2f", em.voltage))")
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2022-12-31 02:23:21 -08:00
|
|
|
TableColumn("timestamp") { em in
|
2023-05-05 09:27:24 -07:00
|
|
|
Text(em.time?.formattedDate(format: dateFormatString) ?? "unknown.age".localized)
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2023-04-22 18:12:48 -07:00
|
|
|
.width(min: 180)
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ScrollView {
|
2022-12-27 17:44:27 -08:00
|
|
|
let columns = [
|
2023-05-10 13:44:27 -07:00
|
|
|
GridItem(.flexible(minimum: 30, maximum: 50), spacing: 0.1),
|
2023-01-12 01:30:06 -08:00
|
|
|
GridItem(.flexible(minimum: 30, maximum: 60), spacing: 0.1),
|
|
|
|
|
GridItem(.flexible(minimum: 30, maximum: 60), spacing: 0.1),
|
2023-05-10 13:44:27 -07:00
|
|
|
GridItem(.flexible(minimum: 30, maximum: 50), spacing: 0.1),
|
2023-01-12 01:27:17 -08:00
|
|
|
GridItem(spacing: 0)
|
2022-12-27 17:44:27 -08:00
|
|
|
]
|
2023-04-07 19:47:24 -07:00
|
|
|
LazyVGrid(columns: columns, alignment: .leading, spacing: 1, pinnedViews: [.sectionHeaders]) {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2023-03-31 12:08:42 -07:00
|
|
|
GridRow {
|
2023-04-07 19:47:24 -07:00
|
|
|
Text("Temp")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
.fontWeight(.bold)
|
|
|
|
|
Text("Hum")
|
2023-03-31 12:08:42 -07:00
|
|
|
.font(.caption)
|
2023-04-07 19:47:24 -07:00
|
|
|
.fontWeight(.bold)
|
|
|
|
|
Text("Bar")
|
2023-03-31 12:08:42 -07:00
|
|
|
.font(.caption)
|
2023-04-07 19:47:24 -07:00
|
|
|
.fontWeight(.bold)
|
|
|
|
|
Text("gas")
|
2023-03-31 12:08:42 -07:00
|
|
|
.font(.caption)
|
2023-04-07 19:47:24 -07:00
|
|
|
.fontWeight(.bold)
|
|
|
|
|
Text("timestamp")
|
2023-03-31 12:08:42 -07:00
|
|
|
.font(.caption)
|
2023-04-07 19:47:24 -07:00
|
|
|
.fontWeight(.bold)
|
|
|
|
|
}
|
|
|
|
|
ForEach(environmentMetrics, id: \.self) { em in
|
|
|
|
|
|
|
|
|
|
GridRow {
|
|
|
|
|
|
|
|
|
|
Text(em.temperature.formattedTemperature())
|
|
|
|
|
.font(.caption)
|
|
|
|
|
Text("\(String(format: "%.2f", em.relativeHumidity))%")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
Text("\(String(format: "%.2f", em.barometricPressure))")
|
|
|
|
|
.font(.caption)
|
|
|
|
|
Text("\(String(format: "%.2f", em.gasResistance))")
|
|
|
|
|
.font(.caption)
|
2023-05-05 09:27:24 -07:00
|
|
|
Text(em.time?.formattedDate(format: dateFormatString) ?? "unknown.age".localized)
|
2023-05-10 13:44:27 -07:00
|
|
|
.font(.caption)
|
2023-04-07 19:47:24 -07:00
|
|
|
}
|
2022-07-08 06:31:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 19:47:24 -07:00
|
|
|
.padding(.leading, 15)
|
|
|
|
|
.padding(.trailing, 5)
|
2022-07-08 06:31:47 -07:00
|
|
|
}
|
2022-11-11 07:07:48 -08:00
|
|
|
}
|
2022-07-08 06:31:47 -07:00
|
|
|
}
|
2022-10-03 20:13:33 -07:00
|
|
|
HStack {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-10-03 20:13:33 -07:00
|
|
|
Button(role: .destructive) {
|
|
|
|
|
isPresentingClearLogConfirm = true
|
|
|
|
|
} label: {
|
2023-05-10 13:44:27 -07:00
|
|
|
Label("clear.log", systemImage: "trash.fill")
|
2022-10-03 20:13:33 -07:00
|
|
|
}
|
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
2023-05-10 13:44:27 -07:00
|
|
|
.padding(.bottom)
|
|
|
|
|
.padding(.trailing)
|
2022-10-03 20:13:33 -07:00
|
|
|
.confirmationDialog(
|
2022-12-13 08:47:14 -08:00
|
|
|
"are.you.sure",
|
2022-10-03 20:13:33 -07:00
|
|
|
isPresented: $isPresentingClearLogConfirm,
|
|
|
|
|
titleVisibility: .visible
|
|
|
|
|
) {
|
|
|
|
|
Button("Delete all environment metrics?", role: .destructive) {
|
|
|
|
|
if clearTelemetry(destNum: node.num, metricsType: 1, context: context) {
|
|
|
|
|
print("Clear Environment Metrics Log Failed")
|
2023-03-06 10:33:18 -08:00
|
|
|
}
|
2022-10-03 20:13:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Button {
|
2023-04-05 21:26:53 -07:00
|
|
|
exportString = telemetryToCsvFile(telemetry: environmentMetrics, metricsType: 1)
|
2022-10-03 20:13:33 -07:00
|
|
|
isExporting = true
|
|
|
|
|
} label: {
|
2022-12-12 20:35:38 -08:00
|
|
|
Label("save", systemImage: "square.and.arrow.down")
|
2022-10-03 20:13:33 -07:00
|
|
|
}
|
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
2023-05-10 13:44:27 -07:00
|
|
|
.padding(.bottom)
|
|
|
|
|
.padding(.leading)
|
2022-07-15 15:01:42 -07:00
|
|
|
}
|
2022-09-14 07:29:31 -07:00
|
|
|
.navigationTitle("Environment Metrics Log")
|
2022-07-08 06:31:47 -07:00
|
|
|
.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,
|
2023-08-10 12:04:16 -07:00
|
|
|
defaultFilename: String("\(node.user?.longName ?? "Node") Environment Metrics Log"),
|
2022-07-15 15:01:42 -07:00
|
|
|
onCompletion: { result in
|
|
|
|
|
if case .success = result {
|
2022-09-14 07:29:31 -07:00
|
|
|
print("Environment metrics log download succeeded.")
|
2022-07-15 15:01:42 -07:00
|
|
|
self.isExporting = false
|
|
|
|
|
} else {
|
2022-09-14 07:29:31 -07:00
|
|
|
print("Environment metrics log download failed: \(result).")
|
2022-07-15 15:01:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-07-08 06:31:47 -07:00
|
|
|
}
|
|
|
|
|
}
|