2024-02-25 11:24:01 -08:00
//
// P a x C o u n t e r C o n f i g . s w i f t
// M e s h t a s t i c
//
// C o p y r i g h t G a r t h V a n d e r H o u w e n 2 / 2 5 / 2 4 .
//
2024-06-07 22:09:20 -05:00
import MeshtasticProtobufs
2024-02-25 11:24:01 -08:00
import SwiftUI
2024-09-05 19:31:29 -07:00
import OSLog
2024-02-25 11:24:01 -08:00
struct PaxCounterConfig : View {
@ Environment ( \ . managedObjectContext ) private var context
@ EnvironmentObject private var bleManager : BLEManager
@ Environment ( \ . dismiss ) private var goBack
let node : NodeInfoEntity ?
@ State private var enabled = false
@ State private var paxcounterUpdateInterval = 0
@ State private var hasChanges : Bool = false
var body : some View {
Form {
2025-05-05 08:23:15 -07:00
ConfigHeader ( title : " PAX Counter Config " , config : \ . powerConfig , node : node , onAppear : setPaxValues )
2024-02-25 11:24:01 -08:00
Section {
Toggle ( isOn : $ enabled ) {
2025-04-26 16:05:09 -07:00
Label ( " Enabled " , systemImage : " figure.walk.motion " )
2025-05-03 08:58:33 -07:00
Text ( " When enabled the PAX Counter module counts the number of people passing by using WiFi and Bluetooth. Both WiFI and Bluetooth must be disabled for PAX counter to work. " )
2024-02-25 11:24:01 -08:00
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
. listRowSeparator ( . visible )
if enabled {
2025-04-27 16:19:10 -07:00
Picker ( " Update Interval " , selection : $ paxcounterUpdateInterval ) {
2024-02-25 11:24:01 -08:00
ForEach ( UpdateIntervals . allCases ) { at in
2024-02-25 13:07:27 -08:00
if at . rawValue >= 300 {
2024-02-25 11:24:01 -08:00
Text ( at . description )
}
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
. listRowSeparator ( . hidden )
Text ( " config.module.paxcounter.updateinterval.description " )
. foregroundColor ( . gray )
. font ( . callout )
}
} header : {
2025-04-26 16:05:09 -07:00
Text ( " Options " )
2024-02-25 11:24:01 -08:00
}
}
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . powerConfig = = nil )
. navigationTitle ( " config.module.paxcounter.title " )
. navigationBarItems ( trailing : ZStack {
ConnectedDevice (
bluetoothOn : bleManager . isSwitchedOn ,
deviceConnected : bleManager . connectedPeripheral != nil ,
name : " \( bleManager . connectedPeripheral ? . shortName ? ? " ? " ) "
)
} )
2024-09-05 19:31:29 -07:00
. onFirstAppear {
// N e e d t o r e q u e s t a P a x C o u n t e 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
if let connectedPeripheral = bleManager . connectedPeripheral , let node {
let connectedNode = getNodeInfo ( id : connectedPeripheral . num , context : context )
if let connectedNode {
if node . num != connectedNode . num {
if UserDefaults . enableAdministration && node . num != connectedNode . num {
// / 2 . 5 A d m i n i s t r a t i o n w i t h s e s s i o n p a s s k e y
let expiration = node . sessionExpiration ? ? Date ( )
if expiration < Date ( ) || node . paxCounterConfig = = nil {
2024-11-29 13:15:46 -08:00
Logger . mesh . info ( " ⚙️ Empty or expired pax counter module config requesting via PKI admin " )
2024-09-05 19:31:29 -07:00
_ = bleManager . requestPaxCounterModuleConfig ( fromUser : connectedNode . user ! , toUser : node . user ! , adminIndex : connectedNode . myInfo ? . adminIndex ? ? 0 )
}
} else {
// / L e g a c y A d m i n i s t r a t i o n
2024-11-29 13:15:46 -08:00
Logger . mesh . info ( " ☠️ Using insecure legacy admin, empty pax counter module config " )
2024-09-05 19:31:29 -07:00
_ = bleManager . requestPaxCounterModuleConfig ( fromUser : connectedNode . user ! , toUser : node . user ! , adminIndex : connectedNode . myInfo ? . adminIndex ? ? 0 )
}
}
2024-02-25 11:24:01 -08:00
}
}
}
2024-10-05 15:50:57 -07:00
. onChange ( of : enabled ) { oldEnabled , newEnabled in
if oldEnabled != newEnabled && newEnabled != node ? . paxCounterConfig ? . enabled { hasChanges = true }
2024-02-25 11:24:01 -08:00
}
2024-10-05 15:50:57 -07:00
. onChange ( of : paxcounterUpdateInterval ) { oldPaxcounterUpdateInterval , newPaxcounterUpdateInterval in
if oldPaxcounterUpdateInterval != newPaxcounterUpdateInterval && newPaxcounterUpdateInterval != node ? . paxCounterConfig ? . updateInterval ? ? - 1 { hasChanges = true }
2024-02-25 11:24:01 -08:00
}
2024-05-29 16:40:07 -05:00
2024-02-25 11:24:01 -08:00
SaveConfigButton ( node : node , hasChanges : $ hasChanges ) {
guard let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context ) ,
let fromUser = connectedNode . user ,
let toUser = node ? . user else {
return
}
var config = ModuleConfig . PaxcounterConfig ( )
config . enabled = enabled
config . paxcounterUpdateInterval = UInt32 ( paxcounterUpdateInterval )
2024-05-29 16:40:07 -05:00
2024-02-25 11:24:01 -08:00
let adminMessageId = bleManager . savePaxcounterModuleConfig (
config : config ,
fromUser : fromUser ,
toUser : toUser ,
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 ( )
}
}
}
private func setPaxValues ( ) {
enabled = node ? . paxCounterConfig ? . enabled ? ? enabled
2024-07-16 11:27:58 -05:00
paxcounterUpdateInterval = Int ( node ? . paxCounterConfig ? . updateInterval ? ? 1800 )
2024-02-25 11:24:01 -08:00
}
}