Meshtastic-Apple/Meshtastic/Views/Settings/Config/DeviceConfig.swift

326 lines
12 KiB
Swift
Raw Normal View History

2022-06-13 20:43:51 -07:00
//
// DeviceConfig.swift
// Meshtastic Apple
//
// Copyright (c) Garth Vander Houwen 6/13/22.
//
import SwiftUI
2024-06-03 02:17:55 -07:00
import OSLog
2022-06-13 20:43:51 -07:00
struct DeviceConfig: View {
2023-08-26 23:17:30 -07:00
2022-06-13 20:43:51 -07:00
@Environment(\.managedObjectContext) var context
@EnvironmentObject var bleManager: BLEManager
@Environment(\.dismiss) private var goBack
2023-08-26 23:17:30 -07:00
var node: NodeInfoEntity?
2023-08-26 23:17:30 -07:00
@State private var isPresentingNodeDBResetConfirm = false
2022-07-26 21:52:36 -07:00
@State private var isPresentingFactoryResetConfirm = false
2022-06-21 02:43:37 -07:00
@State var hasChanges = false
2022-06-13 20:43:51 -07:00
@State var deviceRole = 0
2022-12-05 19:47:56 -08:00
@State var buzzerGPIO = 0
@State var buttonGPIO = 0
2022-06-13 20:43:51 -07:00
@State var serialEnabled = true
@State var debugLogEnabled = false
@State var rebroadcastMode = 0
2024-03-15 14:29:50 -07:00
@State var nodeInfoBroadcastSecs = 10800
2023-04-09 23:04:11 -07:00
@State var doubleTapAsButtonPress = false
2024-04-26 18:06:23 -07:00
@State var ledHeartbeatEnabled = true
2023-05-13 20:50:20 -07:00
@State var isManaged = false
2024-04-08 11:41:54 -07:00
@State var tzdef = ""
2023-08-26 23:17:30 -07:00
2022-06-13 20:43:51 -07:00
var body: some View {
2022-06-20 00:13:04 -07:00
VStack {
Form {
ConfigHeader(title: "Device", config: \.deviceConfig, node: node, onAppear: setDeviceValues)
2022-12-13 08:47:14 -08:00
Section(header: Text("options")) {
VStack(alignment: .leading) {
Picker("Device Role", selection: $deviceRole ) {
ForEach(DeviceRoles.allCases) { dr in
Text(dr.name)
}
2022-06-13 20:43:51 -07:00
}
Text(DeviceRoles(rawValue: deviceRole)?.description ?? "")
.foregroundColor(.gray)
.font(.callout)
2022-06-13 20:43:51 -07:00
}
.pickerStyle(DefaultPickerStyle())
VStack(alignment: .leading) {
Picker("Rebroadcast Mode", selection: $rebroadcastMode ) {
ForEach(RebroadcastModes.allCases) { rm in
Text(rm.name)
}
}
Text(RebroadcastModes(rawValue: rebroadcastMode)?.description ?? "")
.foregroundColor(.gray)
.font(.callout)
}
.pickerStyle(DefaultPickerStyle())
2024-04-26 18:06:23 -07:00
Toggle(isOn: $isManaged) {
Label("Managed Device", systemImage: "gearshape.arrow.triangle.2.circlepath")
Text("Enabling Managed mode will restrict access to all radio configurations, such as short/long names, regions, channels, modules, etc. and will only be accessible through the Admin channel. To avoid being locked out, make sure the Admin channel is working properly before enabling it.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2024-02-12 22:09:22 -08:00
Picker("Node Info Broadcast Interval", selection: $nodeInfoBroadcastSecs ) {
ForEach(UpdateIntervals.allCases) { ui in
if ui.rawValue >= 3600 {
Text(ui.description)
}
}
}
.pickerStyle(DefaultPickerStyle())
2024-04-26 18:06:23 -07:00
}
Section(header: Text("Hardware")) {
Toggle(isOn: $doubleTapAsButtonPress) {
Label("Double Tap as Button", systemImage: "hand.tap")
Text("Treat double tap on supported accelerometers as a user button press.")
2023-04-09 23:04:11 -07:00
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2024-04-26 18:06:23 -07:00
Toggle(isOn: $ledHeartbeatEnabled) {
Label("LED Heartbeat", systemImage: "waveform.path.ecg")
Text("Controls the blinking LED on the device. For most devices this will control one of the up to 4 LEDS, the charger and GPS LEDs are not controllable.")
2023-05-13 20:50:20 -07:00
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2022-06-20 00:13:04 -07:00
}
Section(header: Text("Debug")) {
Toggle(isOn: $serialEnabled) {
Label("Serial Console", systemImage: "terminal")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
Toggle(isOn: $debugLogEnabled) {
Label("Debug Log", systemImage: "ant.fill")
2022-06-13 20:43:51 -07:00
}
2022-06-20 00:13:04 -07:00
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
2024-04-08 11:41:54 -07:00
VStack(alignment: .leading) {
HStack {
Label("Time Zone", systemImage: "clock.badge.exclamationmark")
TextField("Time Zone", text: $tzdef, axis: .vertical)
2024-04-08 11:41:54 -07:00
.foregroundColor(.gray)
.onChange(of: tzdef, perform: { _ in
let totalBytes = tzdef.utf8.count
// Only mess with the value if it is too big
if totalBytes > 63 {
tzdef = String(tzdef.dropLast())
}
})
.foregroundColor(.gray)
2024-04-08 11:41:54 -07:00
}
.keyboardType(.default)
.disableAutocorrection(true)
Text("Time zone for dates on the device screen and log.")
.foregroundColor(.gray)
.font(.callout)
}
}
2022-12-05 19:47:56 -08:00
Section(header: Text("GPIO")) {
Picker("Button GPIO", selection: $buttonGPIO) {
2024-01-22 13:21:17 -08:00
ForEach(0..<49) {
2022-12-05 19:47:56 -08:00
if $0 == 0 {
2022-12-30 17:44:39 -08:00
Text("unset")
2022-12-05 19:47:56 -08:00
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
Picker("Buzzer GPIO", selection: $buzzerGPIO) {
2024-01-22 13:21:17 -08:00
ForEach(0..<49) {
2022-12-05 19:47:56 -08:00
if $0 == 0 {
2022-12-30 17:44:39 -08:00
Text("unset")
2022-12-05 19:47:56 -08:00
} else {
Text("Pin \($0)")
}
}
}
.pickerStyle(DefaultPickerStyle())
}
2022-06-20 00:13:04 -07:00
}
.disabled(self.bleManager.connectedPeripheral == nil || node?.deviceConfig == nil)
2023-02-02 22:03:27 -08:00
// Only show these buttons for the BLE connected node
if bleManager.connectedPeripheral != nil && node?.num ?? -1 == bleManager.connectedPeripheral.num {
HStack {
Button("Reset NodeDB", role: .destructive) {
isPresentingNodeDBResetConfirm = true
}
.disabled(node?.user == nil)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding(.leading)
2023-02-02 22:03:27 -08:00
.confirmationDialog(
"are.you.sure",
isPresented: $isPresentingNodeDBResetConfirm,
titleVisibility: .visible
) {
Button("Erase all device and app data?", role: .destructive) {
if bleManager.sendNodeDBReset(fromUser: node!.user!, toUser: node!.user!) {
2023-11-20 16:08:46 -08:00
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
bleManager.disconnectPeripheral()
clearCoreDataDatabase(context: context, includeRoutes: false)
2023-11-20 16:08:46 -08:00
}
2023-02-02 22:03:27 -08:00
} else {
2024-06-03 02:17:55 -07:00
Logger.mesh.error("NodeDB Reset Failed")
2023-02-02 22:03:27 -08:00
}
2022-10-02 09:19:03 -07:00
}
}
2023-02-02 22:03:27 -08:00
Button("Factory Reset", role: .destructive) {
isPresentingFactoryResetConfirm = true
}
.disabled(node?.user == nil)
.buttonStyle(.bordered)
.buttonBorderShape(.capsule)
.controlSize(.large)
.padding(.trailing)
2023-02-02 22:03:27 -08:00
.confirmationDialog(
"All device and app data will be deleted. You will also need to forget your devices under Settings > Bluetooth.",
isPresented: $isPresentingFactoryResetConfirm,
titleVisibility: .visible
) {
Button("Factory reset your device and app? ", role: .destructive) {
if bleManager.sendFactoryReset(fromUser: node!.user!, toUser: node!.user!) {
2023-11-20 19:20:54 -08:00
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
bleManager.disconnectPeripheral()
clearCoreDataDatabase(context: context, includeRoutes: false)
2023-11-20 19:20:54 -08:00
}
2023-02-02 22:03:27 -08:00
} else {
2024-06-03 02:17:55 -07:00
Logger.mesh.error("Factory Reset Failed")
2023-02-02 22:03:27 -08:00
}
2022-10-02 09:19:03 -07:00
}
}
}
}
2022-06-21 02:43:37 -07:00
HStack {
SaveConfigButton(node: node, hasChanges: $hasChanges) {
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
if connectedNode != nil {
var dc = Config.DeviceConfig()
dc.role = DeviceRoles(rawValue: deviceRole)!.protoEnumValue()
dc.serialEnabled = serialEnabled
dc.debugLogEnabled = debugLogEnabled
dc.buttonGpio = UInt32(buttonGPIO)
dc.buzzerGpio = UInt32(buzzerGPIO)
dc.rebroadcastMode = RebroadcastModes(rawValue: rebroadcastMode)?.protoEnumValue() ?? RebroadcastModes.all.protoEnumValue()
dc.nodeInfoBroadcastSecs = UInt32(nodeInfoBroadcastSecs)
dc.doubleTapAsButtonPress = doubleTapAsButtonPress
dc.isManaged = isManaged
2024-04-08 11:41:54 -07:00
dc.tzdef = tzdef
2024-04-26 18:06:23 -07:00
dc.ledHeartbeatDisabled = !ledHeartbeatEnabled
if isManaged {
serialEnabled = false
debugLogEnabled = false
}
let adminMessageId = bleManager.saveDeviceConfig(config: dc, 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-06-21 02:43:37 -07:00
}
}
2022-09-23 21:41:07 -07:00
}
2022-06-13 20:43:51 -07:00
}
2022-06-20 00:13:04 -07:00
Spacer()
}
2022-12-13 07:49:46 -08:00
.navigationTitle("device.config")
2022-06-20 00:13:04 -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-20 00:13:04 -07:00
})
.onAppear {
2024-01-14 11:25:00 -08:00
if self.bleManager.context == nil {
self.bleManager.context = context
}
setDeviceValues()
2023-01-31 22:08:03 -08:00
// Need to request a LoRaConfig from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.deviceConfig == nil {
2024-06-03 02:17:55 -07:00
Logger.mesh.info("empty device config")
2023-02-22 20:31:08 -08:00
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral?.num ?? -1, context: context)
2023-05-13 15:46:18 -07:00
if node != nil && connectedNode != nil && connectedNode?.user != nil {
_ = bleManager.requestDeviceConfig(fromUser: connectedNode!.user!, toUser: node!.user!, adminIndex: connectedNode?.myInfo?.adminIndex ?? 0)
2023-01-31 22:08:03 -08:00
}
}
2022-06-21 02:43:37 -07:00
}
.onChange(of: deviceRole) { newRole in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2022-07-11 16:18:16 -07:00
if newRole != node!.deviceConfig!.role { hasChanges = true }
}
2022-06-21 02:43:37 -07:00
}
.onChange(of: serialEnabled) { newSerial in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2022-07-11 16:18:16 -07:00
if newSerial != node!.deviceConfig!.serialEnabled { hasChanges = true }
}
2022-06-21 02:43:37 -07:00
}
.onChange(of: debugLogEnabled) { newDebugLog in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2022-07-11 16:18:16 -07:00
if newDebugLog != node!.deviceConfig!.debugLogEnabled { hasChanges = true }
}
2022-06-13 20:43:51 -07:00
}
2022-12-05 19:47:56 -08:00
.onChange(of: buttonGPIO) { newButtonGPIO in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2022-12-05 19:47:56 -08:00
if newButtonGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
}
}
.onChange(of: buzzerGPIO) { newBuzzerGPIO in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2022-12-05 19:47:56 -08:00
if newBuzzerGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
}
}
2023-04-01 15:01:11 -07:00
.onChange(of: rebroadcastMode) { newRebroadcastMode in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2023-04-01 15:01:11 -07:00
if newRebroadcastMode != node!.deviceConfig!.rebroadcastMode { hasChanges = true }
}
}
2024-02-12 22:09:22 -08:00
.onChange(of: nodeInfoBroadcastSecs) { newNodeInfoBroadcastSecs in
if node != nil && node?.deviceConfig != nil {
if newNodeInfoBroadcastSecs != node!.deviceConfig!.nodeInfoBroadcastSecs { hasChanges = true }
}
}
2023-04-09 23:04:11 -07:00
.onChange(of: doubleTapAsButtonPress) { newDoubleTapAsButtonPress in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2023-04-09 23:04:11 -07:00
if newDoubleTapAsButtonPress != node!.deviceConfig!.doubleTapAsButtonPress { hasChanges = true }
}
}
2023-05-13 20:50:20 -07:00
.onChange(of: isManaged) { newIsManaged in
2023-05-16 05:54:12 -07:00
if node != nil && node?.deviceConfig != nil {
2023-05-13 20:50:20 -07:00
if newIsManaged != node!.deviceConfig!.isManaged { hasChanges = true }
}
}
2024-04-08 11:41:54 -07:00
.onChange(of: tzdef) { newTzdef in
if node != nil && node?.deviceConfig != nil {
if newTzdef != node!.deviceConfig!.tzdef { hasChanges = true }
}
}
2022-06-13 20:43:51 -07:00
}
func setDeviceValues() {
self.deviceRole = Int(node?.deviceConfig?.role ?? 0)
self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true)
self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false
self.buttonGPIO = Int(node?.deviceConfig?.buttonGpio ?? 0)
self.buzzerGPIO = Int(node?.deviceConfig?.buzzerGpio ?? 0)
2023-04-01 15:01:11 -07:00
self.rebroadcastMode = Int(node?.deviceConfig?.rebroadcastMode ?? 0)
2024-02-12 22:09:22 -08:00
self.nodeInfoBroadcastSecs = Int(node?.deviceConfig?.nodeInfoBroadcastSecs ?? 900)
if nodeInfoBroadcastSecs < 3600 {
nodeInfoBroadcastSecs = 3600
}
2023-05-01 10:39:49 -07:00
self.doubleTapAsButtonPress = node?.deviceConfig?.doubleTapAsButtonPress ?? false
2024-04-26 18:06:23 -07:00
self.ledHeartbeatEnabled = node?.deviceConfig?.ledHeartbeatEnabled ?? true
2023-05-13 20:50:20 -07:00
self.isManaged = node?.deviceConfig?.isManaged ?? false
self.tzdef = node?.deviceConfig?.tzdef ?? ""
2024-04-08 11:41:54 -07:00
if self.tzdef.isEmpty {
self.tzdef = TimeZone.current.posixDescription
self.hasChanges = true
} else {
self.hasChanges = false
}
}
2022-06-13 20:43:51 -07:00
}