2022-06-14 16:45:43 -07:00
|
|
|
//
|
|
|
|
|
// TelemetryConfig.swift
|
|
|
|
|
// Meshtastic Apple
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) Garth Vander Houwen 6/13/22.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
2022-06-22 09:05:56 -07:00
|
|
|
enum UpdateIntervals: Int, CaseIterable, Identifiable {
|
|
|
|
|
|
|
|
|
|
case fifteenSeconds = 15
|
|
|
|
|
case thirtySeconds = 30
|
|
|
|
|
case oneMinute = 60
|
|
|
|
|
case fiveMinutes = 300
|
|
|
|
|
case tenMinutes = 600
|
2022-09-10 18:27:44 -07:00
|
|
|
case fifteenMinutes = 900
|
2022-06-22 09:05:56 -07:00
|
|
|
case thirtyMinutes = 1800
|
|
|
|
|
case oneHour = 3600
|
2022-06-22 15:52:51 -07:00
|
|
|
case twoHours = 7200
|
|
|
|
|
case threeHours = 10800
|
|
|
|
|
case fourHours = 14400
|
|
|
|
|
case fiveHours = 18000
|
|
|
|
|
case sixHours = 21600
|
|
|
|
|
case twelveHours = 43200
|
|
|
|
|
case eighteenHours = 64800
|
|
|
|
|
case twentyFourHours = 86400
|
2022-06-22 09:05:56 -07:00
|
|
|
|
|
|
|
|
var id: Int { self.rawValue }
|
|
|
|
|
var description: String {
|
|
|
|
|
get {
|
|
|
|
|
switch self {
|
|
|
|
|
case .fifteenSeconds:
|
|
|
|
|
return "Fifteen Seconds"
|
|
|
|
|
case .thirtySeconds:
|
|
|
|
|
return "Thirty Seconds"
|
|
|
|
|
case .oneMinute:
|
|
|
|
|
return "One Minute"
|
|
|
|
|
case .fiveMinutes:
|
|
|
|
|
return "Five Minutes"
|
|
|
|
|
case .tenMinutes:
|
|
|
|
|
return "Ten Minutes"
|
|
|
|
|
case .fifteenMinutes:
|
|
|
|
|
return "Fifteen Minutes"
|
|
|
|
|
case .thirtyMinutes:
|
|
|
|
|
return "Thirty Minutes"
|
|
|
|
|
case .oneHour:
|
|
|
|
|
return "One Hour"
|
2022-06-22 15:52:51 -07:00
|
|
|
case .twoHours:
|
|
|
|
|
return "Two Hours"
|
|
|
|
|
case .threeHours:
|
|
|
|
|
return "Three Hours"
|
|
|
|
|
case .fourHours:
|
|
|
|
|
return "Four Hours"
|
|
|
|
|
case .fiveHours:
|
|
|
|
|
return "Five Hours"
|
|
|
|
|
case .sixHours:
|
|
|
|
|
return "Six Hours"
|
|
|
|
|
case .twelveHours:
|
|
|
|
|
return "Twelve Hours"
|
|
|
|
|
case .eighteenHours:
|
|
|
|
|
return "Eighteen Hours"
|
|
|
|
|
case .twentyFourHours:
|
|
|
|
|
return "Twenty Four Hours"
|
2022-06-22 09:05:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 16:45:43 -07:00
|
|
|
struct TelemetryConfig: View {
|
|
|
|
|
|
|
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
var node: NodeInfoEntity?
|
2022-07-01 10:57:54 -07:00
|
|
|
|
|
|
|
|
@State private var isPresentingSaveConfirm: Bool = false
|
|
|
|
|
@State var hasChanges = false
|
2022-06-22 09:05:56 -07:00
|
|
|
@State var deviceUpdateInterval = 0
|
|
|
|
|
@State var environmentUpdateInterval = 0
|
|
|
|
|
@State var environmentMeasurementEnabled = false
|
|
|
|
|
@State var environmentScreenEnabled = false
|
|
|
|
|
@State var environmentDisplayFahrenheit = false
|
2022-06-14 16:45:43 -07:00
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
|
|
|
|
|
VStack {
|
|
|
|
|
Form {
|
2022-06-22 09:05:56 -07:00
|
|
|
Section(header: Text("Update Intervals")) {
|
|
|
|
|
Picker("Device Metrics", selection: $deviceUpdateInterval ) {
|
|
|
|
|
ForEach(UpdateIntervals.allCases) { ui in
|
|
|
|
|
Text(ui.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-22 15:52:51 -07:00
|
|
|
Text("How often device metrics are sent out over the mesh. Default is 15 minutes.")
|
|
|
|
|
.font(.caption)
|
2022-06-22 09:05:56 -07:00
|
|
|
Picker("Sensor Metrics", selection: $environmentUpdateInterval ) {
|
|
|
|
|
ForEach(UpdateIntervals.allCases) { ui in
|
|
|
|
|
Text(ui.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-22 15:52:51 -07:00
|
|
|
Text("How often sensor metrics are sent out over the mesh. Default is 15 minutes.")
|
|
|
|
|
.font(.caption)
|
2022-06-22 09:05:56 -07:00
|
|
|
}
|
|
|
|
|
Section(header: Text("Sensor Options")) {
|
2022-11-11 09:07:14 -08:00
|
|
|
Text("Supported I2C Connected sensors will be detected automatically, sensors are BMP280, BME280, BME680, MCP9808, INA219, INA260, LPS22 and SHTC3.")
|
2022-08-01 07:11:03 -07:00
|
|
|
.font(.caption)
|
2022-06-22 09:05:56 -07:00
|
|
|
Toggle(isOn: $environmentMeasurementEnabled) {
|
|
|
|
|
Label("Enabled", systemImage: "chart.xyaxis.line")
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2022-06-22 09:05:56 -07:00
|
|
|
Toggle(isOn: $environmentScreenEnabled) {
|
|
|
|
|
Label("Show on device screen", systemImage: "display")
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2022-06-22 09:05:56 -07:00
|
|
|
Toggle(isOn: $environmentDisplayFahrenheit) {
|
|
|
|
|
Label("Display Fahrenheit", systemImage: "thermometer")
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
2022-06-22 09:05:56 -07:00
|
|
|
}
|
2022-06-14 16:45:43 -07:00
|
|
|
}
|
2022-07-14 07:14:43 -07:00
|
|
|
.disabled(bleManager.connectedPeripheral == nil)
|
2022-07-01 10:57:54 -07:00
|
|
|
Button {
|
|
|
|
|
isPresentingSaveConfirm = true
|
|
|
|
|
} label: {
|
|
|
|
|
Label("Save", systemImage: "square.and.arrow.down")
|
|
|
|
|
}
|
2022-07-09 06:42:30 -07:00
|
|
|
.disabled(bleManager.connectedPeripheral == nil || !hasChanges || node!.telemetryConfig == nil)
|
2022-07-01 10:57:54 -07:00
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
|
|
|
|
.padding()
|
|
|
|
|
.confirmationDialog(
|
|
|
|
|
"Are you sure?",
|
2022-10-04 18:19:02 -07:00
|
|
|
isPresented: $isPresentingSaveConfirm,
|
|
|
|
|
titleVisibility: .visible
|
2022-07-01 10:57:54 -07:00
|
|
|
) {
|
|
|
|
|
Button("Save Telemetry Module Config to \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")?") {
|
|
|
|
|
var tc = ModuleConfig.TelemetryConfig()
|
2022-07-02 13:25:34 -07:00
|
|
|
tc.deviceUpdateInterval = UInt32(deviceUpdateInterval)
|
|
|
|
|
tc.environmentUpdateInterval = UInt32(environmentUpdateInterval)
|
2022-07-01 10:57:54 -07:00
|
|
|
tc.environmentMeasurementEnabled = environmentMeasurementEnabled
|
|
|
|
|
tc.environmentScreenEnabled = environmentScreenEnabled
|
|
|
|
|
tc.environmentDisplayFahrenheit = environmentDisplayFahrenheit
|
2022-08-11 23:34:09 -07:00
|
|
|
let adminMessageId = bleManager.saveTelemetryModuleConfig(config: tc, fromUser: node!.user!, toUser: node!.user!)
|
2022-07-02 19:50:08 -07:00
|
|
|
if adminMessageId > 0 {
|
2022-07-01 10:57:54 -07:00
|
|
|
// Should show a saved successfully alert once I know that to be true
|
|
|
|
|
// for now just disable the button after a successful save
|
|
|
|
|
hasChanges = false
|
2022-07-02 19:50:08 -07:00
|
|
|
} else {
|
2022-07-01 10:57:54 -07:00
|
|
|
|
2022-07-02 19:50:08 -07:00
|
|
|
}
|
2022-07-01 10:57:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 16:45:43 -07:00
|
|
|
.navigationTitle("Telemetry Config")
|
|
|
|
|
.navigationBarItems(trailing:
|
|
|
|
|
ZStack {
|
2022-11-12 08:48:01 -08:00
|
|
|
ConnectedDevice(bluetoothOn: bleManager.isSwitchedOn, deviceConnected: bleManager.connectedPeripheral != nil, name: (bleManager.connectedPeripheral != nil) ? bleManager.connectedPeripheral.shortName : "????")
|
2022-06-14 16:45:43 -07:00
|
|
|
})
|
|
|
|
|
.onAppear {
|
2022-11-12 08:48:01 -08:00
|
|
|
self.bleManager.context = context
|
|
|
|
|
self.deviceUpdateInterval = Int(node?.telemetryConfig?.deviceUpdateInterval ?? 0)
|
|
|
|
|
self.environmentUpdateInterval = Int(node?.telemetryConfig?.environmentUpdateInterval ?? 0)
|
|
|
|
|
self.environmentMeasurementEnabled = node?.telemetryConfig?.environmentMeasurementEnabled ?? false
|
|
|
|
|
self.environmentScreenEnabled = node?.telemetryConfig?.environmentScreenEnabled ?? false
|
|
|
|
|
self.environmentDisplayFahrenheit = node?.telemetryConfig?.environmentDisplayFahrenheit ?? false
|
|
|
|
|
self.hasChanges = false
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: deviceUpdateInterval) { newDeviceInterval in
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.telemetryConfig != nil {
|
2022-07-09 06:42:30 -07:00
|
|
|
if newDeviceInterval != node!.telemetryConfig!.deviceUpdateInterval { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: environmentUpdateInterval) { newEnvInterval in
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.telemetryConfig != nil {
|
2022-07-09 06:42:30 -07:00
|
|
|
if newEnvInterval != node!.telemetryConfig!.environmentUpdateInterval { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: environmentMeasurementEnabled) { newEnvEnabled in
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.telemetryConfig != nil {
|
2022-07-09 06:42:30 -07:00
|
|
|
if newEnvEnabled != node!.telemetryConfig!.environmentMeasurementEnabled { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: environmentScreenEnabled) { newEnvScreenEnabled in
|
2022-07-09 06:42:30 -07:00
|
|
|
if node!.telemetryConfig != nil {
|
|
|
|
|
if newEnvScreenEnabled != node!.telemetryConfig!.environmentScreenEnabled { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: environmentDisplayFahrenheit) { newEnvDisplayF in
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.telemetryConfig != nil {
|
2022-07-09 06:42:30 -07:00
|
|
|
if newEnvDisplayF != node!.telemetryConfig!.environmentDisplayFahrenheit { hasChanges = true }
|
|
|
|
|
}
|
2022-07-02 13:43:13 -07:00
|
|
|
}
|
2022-06-14 16:45:43 -07:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|