2022-06-12 01:25:42 -07:00
|
|
|
//
|
|
|
|
|
// LoRaConfig.swift
|
|
|
|
|
// Meshtastic Apple
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) by Garth Vander Houwen 6/11/22.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
|
|
struct LoRaConfig: View {
|
|
|
|
|
|
|
|
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
|
@EnvironmentObject var bleManager: BLEManager
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
var node: NodeInfoEntity?
|
2022-06-20 12:51:55 -07:00
|
|
|
|
2022-07-26 09:24:35 -07:00
|
|
|
@State var isPresentingSaveConfirm = false
|
|
|
|
|
@State var initialLoad = true
|
2022-06-21 01:12:08 -07:00
|
|
|
@State var hasChanges = false
|
2022-06-20 23:48:47 -07:00
|
|
|
|
2022-06-20 12:51:55 -07:00
|
|
|
@State var region = 0
|
2022-06-20 00:13:04 -07:00
|
|
|
@State var modemPreset = 0
|
|
|
|
|
@State var hopLimit = 0
|
2022-07-26 09:24:35 -07:00
|
|
|
@State var txPower = 0
|
2022-09-18 08:56:08 -07:00
|
|
|
@State var txEnabled = true
|
|
|
|
|
@State var usePreset = true
|
2022-06-12 01:25:42 -07:00
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
|
|
|
|
|
|
VStack {
|
|
|
|
|
|
|
|
|
|
Form {
|
|
|
|
|
Section(header: Text("Region")) {
|
|
|
|
|
|
|
|
|
|
Picker("Region", selection: $region ) {
|
|
|
|
|
ForEach(RegionCodes.allCases) { r in
|
|
|
|
|
Text(r.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-20 12:51:55 -07:00
|
|
|
Text("The region where you will be using your radios.")
|
2022-06-12 01:25:42 -07:00
|
|
|
.font(.caption)
|
|
|
|
|
}
|
|
|
|
|
Section(header: Text("Modem")) {
|
2022-06-20 22:26:35 -07:00
|
|
|
Picker("Presets", selection: $modemPreset ) {
|
2022-06-12 01:25:42 -07:00
|
|
|
ForEach(ModemPresets.allCases) { m in
|
|
|
|
|
Text(m.description)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-20 12:51:55 -07:00
|
|
|
Text("Available modem presets, default is Long Fast.")
|
2022-06-12 01:25:42 -07:00
|
|
|
.font(.caption)
|
|
|
|
|
}
|
2022-06-12 22:07:58 -07:00
|
|
|
Section(header: Text("Mesh Options")) {
|
|
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Picker("Number of hops", selection: $hopLimit) {
|
2022-06-20 12:51:55 -07:00
|
|
|
ForEach(HopValues.allCases) { hop in
|
|
|
|
|
Text(hop.description)
|
2022-06-12 22:07:58 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pickerStyle(DefaultPickerStyle())
|
2022-06-21 20:15:35 -07:00
|
|
|
Text("Sets the maximum number of hops, default is 3. Increasing hops also increases air time utilization and should be used carefully.")
|
2022-06-20 12:51:55 -07:00
|
|
|
.font(.caption)
|
2022-06-12 22:07:58 -07:00
|
|
|
}
|
2022-06-12 01:25:42 -07:00
|
|
|
}
|
2022-07-26 09:24:35 -07:00
|
|
|
.disabled(self.bleManager.connectedPeripheral == nil)
|
2022-06-20 00:13:04 -07:00
|
|
|
|
|
|
|
|
Button {
|
|
|
|
|
|
2022-06-20 12:51:55 -07:00
|
|
|
isPresentingSaveConfirm = true
|
2022-06-20 00:13:04 -07:00
|
|
|
|
|
|
|
|
} label: {
|
2022-06-21 01:12:08 -07:00
|
|
|
|
2022-06-20 00:13:04 -07:00
|
|
|
Label("Save", systemImage: "square.and.arrow.down")
|
|
|
|
|
}
|
|
|
|
|
.disabled(bleManager.connectedPeripheral == nil || !hasChanges)
|
|
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
|
|
|
|
.padding()
|
2022-06-20 12:51:55 -07:00
|
|
|
.confirmationDialog(
|
2022-06-21 01:12:08 -07:00
|
|
|
|
2022-09-23 21:41:07 -07:00
|
|
|
"Are you sure you want to save?",
|
|
|
|
|
isPresented: $isPresentingSaveConfirm,
|
|
|
|
|
titleVisibility: .visible
|
2022-06-20 12:51:55 -07:00
|
|
|
) {
|
2022-09-23 21:41:07 -07:00
|
|
|
Button("Save Config for \(bleManager.connectedPeripheral != nil ? bleManager.connectedPeripheral.longName : "Unknown")") {
|
2022-06-20 12:51:55 -07:00
|
|
|
|
|
|
|
|
var lc = Config.LoRaConfig()
|
|
|
|
|
lc.hopLimit = UInt32(hopLimit)
|
|
|
|
|
lc.region = RegionCodes(rawValue: region)!.protoEnumValue()
|
|
|
|
|
lc.modemPreset = ModemPresets(rawValue: modemPreset)!.protoEnumValue()
|
2022-09-19 15:50:06 -07:00
|
|
|
lc.usePreset = true
|
|
|
|
|
lc.txEnabled = true
|
2022-06-20 12:51:55 -07:00
|
|
|
|
2022-08-12 08:58:10 -07:00
|
|
|
let adminMessageId = bleManager.saveLoRaConfig(config: lc, fromUser: node!.user!, toUser: node!.user!)
|
2022-07-02 19:50:08 -07:00
|
|
|
|
|
|
|
|
if adminMessageId > 0 {
|
2022-06-20 12:51:55 -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
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-23 21:41:07 -07:00
|
|
|
|
|
|
|
|
} message: {
|
|
|
|
|
|
|
|
|
|
Text("After LoRa config saves the node will reboot.")
|
2022-06-20 12:51:55 -07:00
|
|
|
}
|
2022-06-12 01:25:42 -07:00
|
|
|
}
|
|
|
|
|
.navigationTitle("LoRa Config")
|
|
|
|
|
.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-12 01:25:42 -07:00
|
|
|
})
|
|
|
|
|
.onAppear {
|
2022-07-26 09:24:35 -07:00
|
|
|
|
|
|
|
|
self.bleManager.context = context
|
2022-06-12 01:25:42 -07:00
|
|
|
|
2022-06-20 23:48:47 -07:00
|
|
|
if self.initialLoad{
|
|
|
|
|
|
2022-07-26 09:24:35 -07:00
|
|
|
|
2022-09-27 22:18:50 -07:00
|
|
|
self.hopLimit = Int(node?.loRaConfig?.hopLimit ?? 0)
|
|
|
|
|
self.region = Int(node?.loRaConfig?.regionCode ?? 0)
|
|
|
|
|
self.usePreset = node?.loRaConfig?.usePreset ?? true
|
|
|
|
|
self.modemPreset = Int(node?.loRaConfig?.modemPreset ?? 0)
|
|
|
|
|
self.txEnabled = node?.loRaConfig?.txEnabled ?? true
|
|
|
|
|
self.txPower = Int(node?.loRaConfig?.txPower ?? 0)
|
2022-09-18 08:56:08 -07:00
|
|
|
|
2022-06-20 22:26:35 -07:00
|
|
|
self.hasChanges = false
|
2022-06-20 23:48:47 -07:00
|
|
|
self.initialLoad = false
|
2022-06-20 22:26:35 -07:00
|
|
|
}
|
2022-06-12 01:25:42 -07:00
|
|
|
}
|
2022-06-20 22:48:38 -07:00
|
|
|
.onChange(of: region) { newRegion in
|
2022-06-20 00:13:04 -07:00
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.loRaConfig != nil {
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
if newRegion != node!.loRaConfig!.regionCode { hasChanges = true }
|
2022-06-20 23:48:47 -07:00
|
|
|
}
|
2022-06-20 00:13:04 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: modemPreset) { newModemPreset in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.loRaConfig != nil {
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
if newModemPreset != node!.loRaConfig!.modemPreset { hasChanges = true }
|
2022-06-20 23:48:47 -07:00
|
|
|
}
|
2022-06-20 00:13:04 -07:00
|
|
|
}
|
|
|
|
|
.onChange(of: hopLimit) { newHopLimit in
|
|
|
|
|
|
2022-07-11 16:18:16 -07:00
|
|
|
if node != nil && node!.loRaConfig != nil {
|
|
|
|
|
|
2022-07-07 00:29:52 -07:00
|
|
|
if newHopLimit != node!.loRaConfig!.hopLimit { hasChanges = true }
|
2022-06-20 23:48:47 -07:00
|
|
|
}
|
2022-06-20 00:13:04 -07:00
|
|
|
}
|
2022-06-12 01:25:42 -07:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
|
}
|
|
|
|
|
}
|