Meshtastic-Apple/Meshtastic/Views/Settings/Config/Module/PaxCounterConfig.swift

119 lines
4.1 KiB
Swift
Raw Normal View History

2024-02-25 11:24:01 -08:00
//
// PaxCounterConfig.swift
// Meshtastic
//
// Copyright Garth Vander Houwen 2/25/24.
//
import MeshtasticProtobufs
2024-02-25 11:24:01 -08:00
import SwiftUI
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)
2025-05-08 22:50:44 -07:00
Text("How often we can send a message to the mesh when people are detected.")
2024-02-25 11:24:01 -08:00
.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)
2025-05-08 22:50:44 -07:00
.navigationTitle("PAX Counter Config")
2024-02-25 11:24:01 -08:00
.navigationBarItems(trailing: ZStack {
ConnectedDevice(
bluetoothOn: bleManager.isSwitchedOn,
deviceConnected: bleManager.connectedPeripheral != nil,
name: "\(bleManager.connectedPeripheral?.shortName ?? "?")"
)
})
.onFirstAppear {
// Need to request a PaxCounterModuleConfig from the remote node before allowing changes
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 Administration with session passkey
let expiration = node.sessionExpiration ?? Date()
if expiration < Date() || node.paxCounterConfig == nil {
Logger.mesh.info("⚙️ Empty or expired pax counter module config requesting via PKI admin")
_ = bleManager.requestPaxCounterModuleConfig(fromUser: connectedNode.user!, toUser: node.user!)
}
} else {
/// Legacy Administration
2025-05-13 06:19:27 -07:00
Logger.mesh.info("☠️ Using insecure legacy admin that is no longer supported, please upgrade your firmware.")
}
}
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-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-02-25 11:24:01 -08:00
let adminMessageId = bleManager.savePaxcounterModuleConfig(
config: config,
fromUser: fromUser,
toUser: toUser
2024-02-25 11:24:01 -08:00
)
if adminMessageId > 0 {
// 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
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
}
}