Meshtastic-Apple/Meshtastic/Views/Settings/Config/Module/TelemetryConfig.swift

184 lines
7.5 KiB
Swift
Raw Normal View History

2022-06-14 16:45:43 -07:00
//
// TelemetryConfig.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/13/22.
//
import SwiftUI
struct TelemetryConfig: View {
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -07:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2023-03-06 10:33:18 -08:00
var node: NodeInfoEntity?
2023-03-06 10:33:18 -08: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
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -07:00
var body: some View {
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -07:00
VStack {
Form {
if node != nil && node?.metadata == nil && node?.num ?? 0 != bleManager.connectedPeripheral?.num ?? 0 {
Text("There has been no response to a request for device metadata over the admin channel for this node.")
.font(.callout)
.foregroundColor(.orange)
2023-03-14 12:44:10 -07:00
} else if node != nil && node?.num ?? 0 != bleManager.connectedPeripheral?.num ?? 0 {
// Let users know what is going on if they are using remote admin and don't have the config yet
2023-03-14 12:44:10 -07:00
if node?.telemetryConfig == nil {
Text("Telemetry config data was requested over the admin channel but no response has been returned from the remote node. You can check the status of admin message requests in the admin message log.")
.font(.callout)
.foregroundColor(.orange)
} else {
Text("Remote administration for: \(node?.user?.longName ?? "Unknown")")
.font(.title3)
.onAppear {
setTelemetryValues()
}
}
2023-03-14 12:44:10 -07:00
} else if node != nil && node?.num ?? 0 == bleManager.connectedPeripheral?.num ?? 0 {
Text("Configuration for: \(node?.user?.longName ?? "Unknown")")
.font(.title3)
} else {
Text("Please connect to a radio to configure settings.")
.font(.callout)
.foregroundColor(.orange)
}
Section(header: Text("update.interval")) {
2022-06-22 09:05:56 -07:00
Picker("Device Metrics", selection: $deviceUpdateInterval ) {
ForEach(UpdateIntervals.allCases) { ui in
Text(ui.description)
}
}
.pickerStyle(DefaultPickerStyle())
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())
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")) {
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) {
2022-12-13 08:47:14 -08:00
Label("enabled", systemImage: "chart.xyaxis.line")
2022-06-22 09:05:56 -07:00
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2022-06-22 09:05:56 -07:00
Toggle(isOn: $environmentScreenEnabled) {
Label("Show on device screen", systemImage: "display")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2022-06-22 09:05:56 -07:00
Toggle(isOn: $environmentDisplayFahrenheit) {
Label("Display Fahrenheit", systemImage: "thermometer")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2022-06-22 09:05:56 -07:00
}
2022-06-14 16:45:43 -07:00
}
.disabled(self.bleManager.connectedPeripheral == nil || node?.telemetryConfig == nil)
Button {
isPresentingSaveConfirm = true
} label: {
2022-12-12 20:35:38 -08:00
Label("save", systemImage: "square.and.arrow.down")
}
.disabled(bleManager.connectedPeripheral == nil || !hasChanges || node!.telemetryConfig == nil)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding()
.confirmationDialog(
2022-12-13 08:47:14 -08:00
"are.you.sure",
isPresented: $isPresentingSaveConfirm,
titleVisibility: .visible
) {
2023-02-22 20:31:08 -08:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral?.num ?? -1, context: context)
if connectedNode != nil {
let nodeName = node?.user?.longName ?? "unknown".localized
let buttonText = String.localizedStringWithFormat("save.config %@".localized, nodeName)
Button(buttonText) {
var tc = ModuleConfig.TelemetryConfig()
tc.deviceUpdateInterval = UInt32(deviceUpdateInterval)
tc.environmentUpdateInterval = UInt32(environmentUpdateInterval)
tc.environmentMeasurementEnabled = environmentMeasurementEnabled
tc.environmentScreenEnabled = environmentScreenEnabled
tc.environmentDisplayFahrenheit = environmentDisplayFahrenheit
2023-03-06 10:33:18 -08:00
let adminMessageId = bleManager.saveTelemetryModuleConfig(config: tc, fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
if adminMessageId > 0 {
// 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
goBack()
}
}
}
}
2022-12-30 11:08:59 -08:00
message: {
Text("config.save.confirm")
}
2022-12-12 22:33:06 -08:00
.navigationTitle("telemetry.config")
2022-06-14 16:45:43 -07:00
.navigationBarItems(trailing:
ZStack {
2023-09-02 17:37:35 -07: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
setTelemetryValues()
2023-03-06 10:33:18 -08:00
// Need to request a TelemetryModuleConfig from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.telemetryConfig == nil {
print("empty telemetry module config")
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
_ = bleManager.requestTelemetryModuleConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
}
}
2022-07-02 13:43:13 -07:00
}
.onChange(of: deviceUpdateInterval) { newDeviceInterval in
2023-02-03 10:15:11 -08:00
if node != nil && node?.telemetryConfig != nil {
if newDeviceInterval != node!.telemetryConfig!.deviceUpdateInterval { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
.onChange(of: environmentUpdateInterval) { newEnvInterval in
2023-02-03 10:15:11 -08:00
if node != nil && node?.telemetryConfig != nil {
if newEnvInterval != node!.telemetryConfig!.environmentUpdateInterval { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
.onChange(of: environmentMeasurementEnabled) { newEnvEnabled in
2023-02-03 10:15:11 -08:00
if node != nil && node?.telemetryConfig != nil {
if newEnvEnabled != node!.telemetryConfig!.environmentMeasurementEnabled { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
.onChange(of: environmentScreenEnabled) { newEnvScreenEnabled in
if node!.telemetryConfig != nil {
if newEnvScreenEnabled != node!.telemetryConfig!.environmentScreenEnabled { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
.onChange(of: environmentDisplayFahrenheit) { newEnvDisplayF in
2023-02-03 10:15:11 -08:00
if node != nil && node?.telemetryConfig != nil {
if newEnvDisplayF != node!.telemetryConfig!.environmentDisplayFahrenheit { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
2022-06-14 16:45:43 -07:00
}
}
func setTelemetryValues() {
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-06-14 16:45:43 -07:00
}