2023-08-26 20:45:15 -07:00
//
// S t o r e F o r w a r d . s w i f t
// M e s h t a s t i c
//
// 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 8 / 2 6 / 2 3 .
//
import SwiftUI
struct StoreForwardConfig : View {
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
@ Environment ( \ . dismiss ) private var goBack
var node : NodeInfoEntity ?
@ State private var isPresentingSaveConfirm : Bool = false
@ State var hasChanges : Bool = false
// / E n a b l e t h e S t o r e a n d F o r w a r d M o d u l e
@ State var enabled = false
2024-02-12 16:35:29 -08:00
// / I s a S & F R o u t e r
@ State var isRouter = false
2023-08-26 20:45:15 -07:00
// / S e n d a H e a r t b e a t
@ State var heartbeat : Bool = false
// / N u m b e r o f R e c o r d s
@ State var records = 0
// / M a x n u m b e r o f h i s t o r y i t e m s t o r e t u r n
@ State var historyReturnMax = 0
// / T i m e w i n d o w f o r h i s t o r y
@ State var historyReturnWindow = 0
var body : some View {
2023-12-06 12:32:17 -08: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. " )
2023-08-26 20:45:15 -07:00
. font ( . callout )
. foregroundColor ( . orange )
2023-12-06 12:32:17 -08: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
if node ? . storeForwardConfig = = nil {
Text ( " Store and forward 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 {
2024-01-14 21:00:54 -08:00
setStoreAndForwardValues ( )
2023-12-06 12:32:17 -08:00
}
}
} else if node != nil && node ? . num ? ? 0 = = bleManager . connectedPeripheral ? . num ? ? 0 {
Text ( " Configuration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
} else {
Text ( " Please connect to a radio to configure settings. " )
. font ( . callout )
. foregroundColor ( . orange )
2023-08-26 20:45:15 -07:00
}
2023-12-06 12:32:17 -08:00
Section ( header : Text ( " options " ) ) {
2024-02-12 16:35:29 -08:00
2023-12-06 12:32:17 -08:00
Toggle ( isOn : $ enabled ) {
Label ( " enabled " , systemImage : " envelope.arrow.triangle.branch " )
2024-02-12 18:00:04 -08:00
Text ( " Enables the store and forward module. Store and forward must be enabled on both client and router devices. " )
2024-02-12 16:35:29 -08:00
. font ( . caption )
2023-12-06 12:32:17 -08:00
}
2024-02-12 16:35:29 -08:00
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
. listRowSeparator ( . visible )
if enabled {
HStack {
Picker ( selection : $ isRouter , label : Text ( " Role " ) ) {
Text ( " Client " )
. tag ( false )
Text ( " Router " )
. tag ( true )
}
. pickerStyle ( SegmentedPickerStyle ( ) )
. padding ( . top , 5 )
. padding ( . bottom , 5 )
}
2023-12-06 12:32:17 -08:00
}
2024-02-12 18:00:04 -08:00
VStack {
if isRouter {
Text ( " Store and forward router devices must also be in the router or router client device role and requires a ESP32 device with PSRAM. " )
. font ( . caption )
} else {
Text ( " Store and forward clients can request history from routers on the network. " )
. font ( . caption )
}
}
2024-02-12 16:35:29 -08:00
}
if isRouter {
2024-02-17 14:46:09 -08:00
Section ( header : Text ( " Router Options " ) ) {
2024-02-12 16:35:29 -08:00
Toggle ( isOn : $ heartbeat ) {
Label ( " storeforward.heartbeat " , systemImage : " waveform.path.ecg " )
}
Picker ( " Number of records " , selection : $ records ) {
Text ( " unset " ) . tag ( 0 )
Text ( " 25 " ) . tag ( 25 )
Text ( " 50 " ) . tag ( 50 )
Text ( " 75 " ) . tag ( 75 )
Text ( " 100 " ) . tag ( 100 )
}
. pickerStyle ( DefaultPickerStyle ( ) )
Picker ( " History Return Max " , selection : $ historyReturnMax ) {
Text ( " unset " ) . tag ( 0 )
Text ( " 25 " ) . tag ( 25 )
Text ( " 50 " ) . tag ( 50 )
Text ( " 75 " ) . tag ( 75 )
Text ( " 100 " ) . tag ( 100 )
}
. pickerStyle ( DefaultPickerStyle ( ) )
Picker ( " History Return Window " , selection : $ historyReturnWindow ) {
Text ( " unset " ) . tag ( 0 )
Text ( " One Minute " ) . tag ( 60 )
Text ( " Five Minutes " ) . tag ( 300 )
Text ( " Ten Minutes " ) . tag ( 600 )
Text ( " Fifteen Minutes " ) . tag ( 900 )
Text ( " Thirty Minutes " ) . tag ( 1800 )
Text ( " One Hour " ) . tag ( 3600 )
2024-02-12 18:00:04 -08:00
Text ( " Two Hours " ) . tag ( 7200 )
2024-02-12 16:35:29 -08:00
}
. pickerStyle ( DefaultPickerStyle ( ) )
2023-12-06 12:32:17 -08:00
}
2023-08-26 20:45:15 -07:00
}
}
2023-12-06 12:32:17 -08:00
. scrollDismissesKeyboard ( . interactively )
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . storeForwardConfig = = nil )
2023-08-26 20:45:15 -07:00
}
Button {
isPresentingSaveConfirm = true
} label : {
Label ( " save " , systemImage : " square.and.arrow.down " )
}
. disabled ( bleManager . connectedPeripheral = = nil || ! hasChanges )
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. confirmationDialog (
" are.you.sure " ,
isPresented : $ isPresentingSaveConfirm ,
titleVisibility : . visible
) {
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 ) {
2024-02-12 16:35:29 -08:00
// / L e t t h e u s e r s e t i s R o u t e r f o r t h e c o n n e c t e d n o d e , f o r n o d e s o n t h e m e s h s e t i s R o u t e r b a s e d
// / o n r e c e i p t o f a p r i m a r y h e a r t b e a t
if connectedNode ? . num ? ? 0 = = node ? . num ? ? - 1 {
connectedNode ? . storeForwardConfig ? . isRouter = isRouter
do {
try context . save ( )
} catch {
print ( " Failed to save isRouter " )
}
}
2023-08-26 20:45:15 -07:00
var sfc = ModuleConfig . StoreForwardConfig ( )
sfc . enabled = self . enabled
sfc . heartbeat = self . heartbeat
sfc . records = UInt32 ( self . records )
sfc . historyReturnMax = UInt32 ( self . historyReturnMax )
sfc . historyReturnWindow = UInt32 ( self . historyReturnWindow )
let adminMessageId = bleManager . saveStoreForwardModuleConfig ( config : sfc , fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
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 ( )
2024-02-12 16:35:29 -08:00
}
}
2023-08-26 20:45:15 -07:00
}
}
message : {
Text ( " config.save.confirm " )
}
. navigationTitle ( " storeforward.config " )
. 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 : " ? " )
2023-08-26 20:45:15 -07:00
} )
. onAppear {
2024-01-14 11:25:00 -08:00
if self . bleManager . context = = nil {
self . bleManager . context = context
}
2024-02-12 18:00:04 -08:00
2023-08-26 20:45:15 -07:00
// N e e d t o r e q u e s t a D e t e c t i o n S e n s o r M o d u l e 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
2024-01-14 21:00:54 -08:00
if bleManager . connectedPeripheral != nil && node ? . storeForwardConfig = = nil {
2023-08-26 20:45:15 -07:00
print ( " empty store and forward module config " )
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
if node != nil && connectedNode != nil {
2024-01-14 21:00:54 -08:00
_ = bleManager . requestStoreAndForwardModuleConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-08-26 20:45:15 -07:00
}
}
2024-02-12 18:00:04 -08:00
setStoreAndForwardValues ( )
2023-08-26 20:45:15 -07:00
}
. onChange ( of : enabled ) { newEnabled in
2024-02-12 18:00:04 -08:00
if node != nil && node ? . storeForwardConfig != nil {
if newEnabled != node ! . storeForwardConfig ! . enabled { hasChanges = true }
}
}
. onChange ( of : isRouter ) { newIsRouter in
if node != nil && node ? . storeForwardConfig != nil {
if newIsRouter != node ! . storeForwardConfig ! . isRouter { hasChanges = true }
2023-08-26 20:45:15 -07:00
}
}
. onChange ( of : heartbeat ) { newHeartbeat in
if node != nil && node ? . storeForwardConfig != nil {
if newHeartbeat != node ! . storeForwardConfig ! . heartbeat { hasChanges = true }
}
}
. onChange ( of : records ) { newRecords in
if node != nil && node ? . storeForwardConfig != nil {
if newRecords != node ! . storeForwardConfig ! . records { hasChanges = true }
}
}
. onChange ( of : historyReturnMax ) { newHistoryReturnMax in
if node != nil && node ? . storeForwardConfig != nil {
if newHistoryReturnMax != node ! . storeForwardConfig ! . historyReturnMax { hasChanges = true }
}
}
. onChange ( of : historyReturnWindow ) { newHistoryReturnWindow in
if node != nil && node ? . storeForwardConfig != nil {
if newHistoryReturnWindow != node ! . storeForwardConfig ! . historyReturnWindow { hasChanges = true }
}
}
}
2024-01-14 21:00:54 -08:00
func setStoreAndForwardValues ( ) {
2023-08-26 20:45:15 -07:00
self . enabled = ( node ? . storeForwardConfig ? . enabled ? ? false )
2024-02-12 16:35:29 -08:00
self . isRouter = ( node ? . storeForwardConfig ? . isRouter ? ? false )
2023-08-26 20:45:15 -07:00
self . heartbeat = ( node ? . storeForwardConfig ? . heartbeat ? ? true )
self . records = Int ( node ? . storeForwardConfig ? . records ? ? 50 )
self . historyReturnMax = Int ( node ? . storeForwardConfig ? . historyReturnMax ? ? 100 )
2024-02-12 18:00:04 -08:00
self . historyReturnWindow = Int ( node ? . storeForwardConfig ? . historyReturnWindow ? ? 7200 )
2023-08-26 20:45:15 -07:00
self . hasChanges = false
}
}