2022-06-13 20:43:51 -07:00
//
// D e v i c e C o n f i g . s w i f t
// M e s h t a s t i c A p p l e
//
// C o p y r i g h t ( c ) G a r t h V a n d e r H o u w e n 6 / 1 3 / 2 2 .
//
import SwiftUI
struct DeviceConfig : View {
2023-03-19 18:37:23 -07:00
2022-06-13 20:43:51 -07:00
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
2022-12-09 18:19:00 -08:00
@ Environment ( \ . dismiss ) private var goBack
2023-03-19 18:37:23 -07:00
2022-07-07 00:29:52 -07:00
var node : NodeInfoEntity ?
2023-03-19 18:37:23 -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
2023-03-19 18:37:23 -07:00
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
2023-03-05 23:01:09 -08:00
@ State var rebroadcastMode = 0
2023-03-19 18:37:23 -07:00
2022-06-13 20:43:51 -07:00
var body : some View {
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
VStack {
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
Form {
2023-03-13 19:17:43 -07:00
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-19 18:37:23 -07:00
2023-03-13 19:17:43 -07:00
} else if node != nil && node ? . num ? ? 0 != bleManager . connectedPeripheral ? . num ? ? 0 {
// L e t u s e r s k n o w w h a t i s g o i n g o n i f t h e y a r e u s i n g r e m o t e a d m i n a n d d o n ' t h a v e t h e c o n f i g y e t
2023-03-14 12:44:10 -07:00
if node ? . deviceConfig = = nil {
2023-03-13 19:17:43 -07:00
Text ( " Device 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 )
2023-03-19 18:37:23 -07:00
. onAppear {
setDeviceValues ( )
}
2023-03-13 19:17:43 -07:00
}
2023-03-14 12:44:10 -07:00
} else if node != nil && node ? . num ? ? 0 = = bleManager . connectedPeripheral ? . num ? ? 0 {
2023-03-13 19:17:43 -07:00
Text ( " Configuration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
} else {
Text ( " Please connect to a radio to configure settings. " )
. font ( . callout )
. foregroundColor ( . orange )
}
2022-12-13 08:47:14 -08:00
Section ( header : Text ( " options " ) ) {
2023-03-19 18:37:23 -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 )
2023-03-19 18:37:23 -07:00
2023-03-05 23:01:09 -08:00
Picker ( " Rebroadcast Mode " , selection : $ rebroadcastMode ) {
ForEach ( RebroadcastModes . allCases ) { rm in
Text ( rm . name )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
. padding ( . top , 10 )
Text ( RebroadcastModes ( rawValue : rebroadcastMode ) ? . description ? ? " " )
. foregroundColor ( . gray )
. font ( . caption )
2022-06-20 00:13:04 -07:00
}
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
Section ( header : Text ( " Debug " ) ) {
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
Toggle ( isOn : $ serialEnabled ) {
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
Label ( " Serial Console " , systemImage : " terminal " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
Toggle ( isOn : $ debugLogEnabled ) {
2023-03-19 18:37:23 -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
}
2023-03-19 18:37:23 -07:00
2022-12-05 19:47:56 -08:00
Section ( header : Text ( " GPIO " ) ) {
2023-03-19 18:37:23 -07:00
2022-12-05 19:47:56 -08:00
Picker ( " Button GPIO " , selection : $ buttonGPIO ) {
2023-04-03 17:48:58 -07:00
ForEach ( 0. . < 46 ) {
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 ) {
2023-04-03 17:48:58 -07:00
ForEach ( 0. . < 46 ) {
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 ( ) )
}
2023-03-19 18:37:23 -07:00
2022-06-20 00:13:04 -07:00
}
2023-01-31 22:59:43 -08:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . deviceConfig = = nil )
2023-03-19 18:37:23 -07:00
2023-02-02 22:03:27 -08:00
// O n l y s h o w t h e s e b u t t o n s f o r t h e B L E c o n n e c t e d n o d e
if bleManager . connectedPeripheral != nil && node ? . num ? ? - 1 = = bleManager . connectedPeripheral . num {
HStack {
2023-03-19 18:37:23 -07:00
2023-02-02 22:03:27 -08:00
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 ) {
2023-03-19 18:37:23 -07:00
2023-02-02 22:03:27 -08:00
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 ) {
2023-03-19 18:37:23 -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 " )
2023-03-19 18:37:23 -07:00
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 {
2023-03-19 18:37:23 -07:00
2022-06-21 02:43:37 -07:00
Button {
isPresentingSaveConfirm = true
2023-03-19 18:37:23 -07:00
2022-06-21 02:43:37 -07:00
} 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 (
2023-03-19 18:37:23 -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 )
2023-02-06 18:45:03 -08:00
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 )
2023-04-01 15:01:11 -07:00
dc . rebroadcastMode = RebroadcastModes ( rawValue : rebroadcastMode ) ? . protoEnumValue ( ) ? ? RebroadcastModes . all . protoEnumValue ( )
2023-03-19 18:37:23 -07:00
2023-03-15 09:27:33 -07:00
let adminMessageId = bleManager . saveDeviceConfig ( config : dc , fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-02-06 18:45:03 -08:00
if adminMessageId > 0 {
// S h o u l d s h o w a s a v e d s u c c e s s f u l l y a l e r t o n c e I k n o w t h a t t o b e t r u e
// f o r n o w j u s t d i s a b l e t h e b u t t o n a f t e r a s u c c e s s f u l s a v e
hasChanges = false
goBack ( )
}
2022-06-21 02:43:37 -07:00
}
2023-02-06 18:45:03 -08:00
}
2022-09-23 21:41:07 -07:00
}
2023-03-19 18:37:23 -07:00
message : {
Text ( " config.save.confirm " )
}
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 :
2023-03-19 18:37:23 -07:00
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
2023-03-19 18:37:23 -07:00
setDeviceValues ( )
2023-01-31 22:08:03 -08:00
// N e e d t o r e q u e s t a L o R a C o n f i g f r o m t h e r e m o t e n o d e b e f o r e a l l o w i n g c h a n g e s
if bleManager . connectedPeripheral != nil && node ? . deviceConfig = = nil {
print ( " empty device config " )
2023-02-22 20:31:08 -08:00
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral ? . num ? ? - 1 , context : context )
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
2023-02-06 18:45:03 -08:00
_ = 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-03-19 18:37:23 -07:00
2022-07-11 16:18:16 -07:00
if node != nil && node ! . deviceConfig != nil {
2023-03-19 18:37:23 -07:00
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-03-19 18:37:23 -07:00
2022-07-11 16:18:16 -07:00
if node != nil && node ! . deviceConfig != nil {
2023-03-19 18:37:23 -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
2023-03-19 18:37:23 -07:00
2022-07-11 16:18:16 -07:00
if node != nil && node ! . deviceConfig != nil {
2023-03-19 18:37:23 -07:00
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-03-19 18:37:23 -07:00
2022-12-05 19:47:56 -08:00
if node != nil && node ! . deviceConfig != nil {
2023-03-19 18:37:23 -07:00
2022-12-05 19:47:56 -08:00
if newButtonGPIO != node ! . deviceConfig ! . buttonGpio { hasChanges = true }
}
}
. onChange ( of : buzzerGPIO ) { newBuzzerGPIO in
2023-03-19 18:37:23 -07:00
2022-12-05 19:47:56 -08:00
if node != nil && node ! . deviceConfig != nil {
2023-03-19 18:37:23 -07:00
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
if node != nil && node ! . deviceConfig != nil {
if newRebroadcastMode != node ! . deviceConfig ! . rebroadcastMode { hasChanges = true }
}
}
2022-06-13 20:43:51 -07:00
}
2023-03-19 18:37:23 -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 )
2023-03-19 18:37:23 -07:00
self . hasChanges = false
}
2022-06-13 20:43:51 -07:00
}