2022-06-13 20:43:51 -07:00
|
|
|
//
|
|
|
|
|
// DeviceConfig.swift
|
|
|
|
|
// Meshtastic Apple
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) Garth Vander Houwen 6/13/22.
|
|
|
|
|
//
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct DeviceConfig: View {
|
|
|
|
|
|
|
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
2022-12-09 18:19:00 -08:00
|
|
|
@Environment(\.dismiss) private var goBack
|
2022-06-21 02:43:37 -07:00
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
var node: NodeInfoEntity?
|
2022-06-21 02:43:37 -07:00
|
|
|
|
2022-10-03 21:19:10 -07:00
|
|
|
@State private var isPresentingNodeDBResetConfirm = false
|
2022-07-26 21:52:36 -07:00
|
|
|
@State private var isPresentingFactoryResetConfirm = false
|
|
|
|
|
@State private var isPresentingSaveConfirm = 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
|
|
|
|
|
|
|
|
|
|
var body: some View {
|
2022-06-18 00:08:01 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
VStack {
|
2022-06-13 20:43:51 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Form {
|
|
|
|
|
|
2022-12-13 08:47:14 -08:00
|
|
|
Section(header: Text("options")) {
|
2022-06-13 20:43:51 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Picker("Device Role", selection: $deviceRole ) {
|
|
|
|
|
ForEach(DeviceRoles.allCases) { dr in
|
2023-01-31 22:08:03 -08:00
|
|
|
Text(dr.name)
|
2022-06-13 20:43:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-22 15:52:51 -07:00
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-20 00:13:04 -07:00
|
|
|
.padding(.top, 10)
|
2023-01-31 22:08:03 -08:00
|
|
|
Text(DeviceRoles(rawValue: deviceRole)?.description ?? "")
|
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
|
.font(.caption)
|
2022-06-20 00:13:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Section(header: Text("Debug")) {
|
2022-06-13 20:43:51 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Toggle(isOn: $serialEnabled) {
|
2022-06-18 00:08:01 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Label("Serial Console", systemImage: "terminal")
|
|
|
|
|
}
|
|
|
|
|
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
|
|
|
|
|
|
|
|
|
|
Toggle(isOn: $debugLogEnabled) {
|
2022-06-13 20:43:51 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
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))
|
2022-06-18 00:08:01 -07:00
|
|
|
}
|
2022-10-02 09:19:03 -07:00
|
|
|
|
2022-12-05 19:47:56 -08:00
|
|
|
Section(header: Text("GPIO")) {
|
|
|
|
|
|
|
|
|
|
Picker("Button GPIO", selection: $buttonGPIO) {
|
|
|
|
|
ForEach(0..<40) {
|
|
|
|
|
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) {
|
|
|
|
|
ForEach(0..<40) {
|
|
|
|
|
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
|
|
|
}
|
2023-01-31 22:59:43 -08:00
|
|
|
.disabled(self.bleManager.connectedPeripheral == nil || node?.deviceConfig == nil)
|
2022-06-20 00:13:04 -07:00
|
|
|
|
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()
|
|
|
|
|
.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!) {
|
|
|
|
|
bleManager.disconnectPeripheral()
|
|
|
|
|
clearCoreDataDatabase(context: context)
|
|
|
|
|
} else {
|
|
|
|
|
print("NodeDB Reset Failed")
|
|
|
|
|
}
|
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()
|
|
|
|
|
.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) {
|
2022-10-07 15:57:57 -07:00
|
|
|
|
2023-02-02 22:03:27 -08:00
|
|
|
if bleManager.sendFactoryReset(fromUser: node!.user!, toUser: node!.user!) {
|
|
|
|
|
bleManager.disconnectPeripheral()
|
|
|
|
|
clearCoreDataDatabase(context: context)
|
|
|
|
|
} else {
|
|
|
|
|
print("Factory Reset Failed")
|
|
|
|
|
|
|
|
|
|
}
|
2022-10-02 09:19:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-21 02:43:37 -07:00
|
|
|
HStack {
|
2022-06-18 00:08:01 -07:00
|
|
|
|
2022-06-21 02:43:37 -07:00
|
|
|
Button {
|
|
|
|
|
isPresentingSaveConfirm = true
|
|
|
|
|
|
|
|
|
|
} label: {
|
2022-12-12 20:35:38 -08:00
|
|
|
Label("save", systemImage: "square.and.arrow.down")
|
2022-06-21 02:43:37 -07:00
|
|
|
}
|
|
|
|
|
.disabled(bleManager.connectedPeripheral == nil || !hasChanges)
|
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
|
|
|
|
.padding()
|
|
|
|
|
.confirmationDialog(
|
2022-06-13 20:43:51 -07:00
|
|
|
|
2022-12-30 11:08:59 -08:00
|
|
|
"are.you.sure",
|
2022-09-23 21:41:07 -07:00
|
|
|
isPresented: $isPresentingSaveConfirm,
|
|
|
|
|
titleVisibility: .visible
|
2022-06-21 02:43:37 -07:00
|
|
|
) {
|
2023-01-31 22:08:03 -08:00
|
|
|
let nodeName = node?.user?.longName ?? NSLocalizedString("unknown", comment: "Unknown")
|
2023-01-09 18:34:43 -08:00
|
|
|
let buttonText = String.localizedStringWithFormat(NSLocalizedString("save.config %@", comment: "Save Config for %@"), nodeName)
|
|
|
|
|
Button(buttonText) {
|
2023-01-31 22:08:03 -08:00
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
2022-06-21 02:43:37 -07:00
|
|
|
var dc = Config.DeviceConfig()
|
|
|
|
|
dc.role = DeviceRoles(rawValue: deviceRole)!.protoEnumValue()
|
2022-09-10 18:38:00 -07:00
|
|
|
dc.serialEnabled = serialEnabled
|
2022-06-21 02:43:37 -07:00
|
|
|
dc.debugLogEnabled = debugLogEnabled
|
2022-12-05 19:47:56 -08:00
|
|
|
dc.buttonGpio = UInt32(buttonGPIO)
|
|
|
|
|
dc.buzzerGpio = UInt32(buzzerGPIO)
|
2022-06-21 02:43:37 -07:00
|
|
|
|
2023-02-03 07:29:12 -08:00
|
|
|
let adminMessageId = bleManager.saveDeviceConfig(config: dc, fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: node?.myInfo?.adminIndex ?? 0)
|
2022-07-02 19:50:08 -07:00
|
|
|
if adminMessageId > 0 {
|
2022-06-21 02:43:37 -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-12-09 18:19:00 -08:00
|
|
|
goBack()
|
2022-06-21 02:43:37 -07:00
|
|
|
}
|
2022-09-23 21:41:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
message: {
|
2022-12-30 11:08:59 -08:00
|
|
|
Text("config.save.confirm")
|
2022-06-21 02:43:37 -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 {
|
2022-07-01 19:44:25 -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 {
|
2022-10-18 19:50:42 -07:00
|
|
|
self.bleManager.context = context
|
|
|
|
|
self.deviceRole = Int(node?.deviceConfig?.role ?? 0)
|
|
|
|
|
self.serialEnabled = (node?.deviceConfig?.serialEnabled ?? true)
|
|
|
|
|
self.debugLogEnabled = node?.deviceConfig?.debugLogEnabled ?? false
|
2022-12-05 19:47:56 -08:00
|
|
|
self.buttonGPIO = Int(node?.deviceConfig?.buttonGpio ?? 0)
|
|
|
|
|
self.buzzerGPIO = Int(node?.deviceConfig?.buzzerGpio ?? 0)
|
2022-10-18 19:50:42 -07:00
|
|
|
self.hasChanges = false
|
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 {
|
|
|
|
|
print("empty device config")
|
|
|
|
|
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
|
|
|
|
|
if connectedNode.id > 0 {
|
|
|
|
|
_ = bleManager.requestDeviceConfig(fromUser: connectedNode.user!, toUser: node!.user!, adminIndex: connectedNode.myInfo?.adminIndex ?? 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-21 02:43:37 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: deviceRole) { newRole in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.deviceConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newRole != node!.deviceConfig!.role { hasChanges = true }
|
|
|
|
|
}
|
2022-06-21 02:43:37 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: serialEnabled) { newSerial in
|
2022-07-26 21:52:36 -07:00
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.deviceConfig != nil {
|
2022-06-21 02:43:37 -07:00
|
|
|
|
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
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.deviceConfig != nil {
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
if node != nil && node!.deviceConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newButtonGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: buzzerGPIO) { newBuzzerGPIO in
|
|
|
|
|
|
|
|
|
|
if node != nil && node!.deviceConfig != nil {
|
|
|
|
|
|
|
|
|
|
if newBuzzerGPIO != node!.deviceConfig!.buttonGpio { hasChanges = true }
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-13 20:43:51 -07:00
|
|
|
}
|
|
|
|
|
}
|