2023-08-16 18:28:55 -07:00
//
// D e t e c t i o n S e n s o r M o d u l e . 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 / 1 6 / 2 3 .
//
import SwiftUI
struct DetectionSensorConfig : 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
@ State var enabled = false
2023-08-17 16:30:48 -05:00
// / D e t e c t i o n S e n s o r M o d u l e w i l l s e n d s a b e l l c h a r a c t e r w i t h t h e m e s s a g e s .
@ State var sendBell : Bool = false
@ State var name : String = " "
@ State var detectionTriggeredHigh : Bool = true
@ State var usePullup : Bool = false
2023-08-17 17:00:20 -07:00
@ State var minimumBroadcastSecs = 0
@ State var stateBroadcastSecs = 0
@ State var monitorPin = 0
2023-08-16 18:28:55 -07:00
var body : some View {
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. " )
. font ( . callout )
. foregroundColor ( . orange )
} 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-08-17 16:30:48 -05:00
if node ? . detectionSensorConfig = = nil {
2023-08-16 18:28:55 -07:00
Text ( " Detection Sensor 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 {
setDetectionSensorValues ( )
}
}
} else if node != nil && node ? . num ? ? 0 = = bleManager . connectedPeripheral ? . num ? ? 0 {
Text ( " Configuration for: \( node ? . user ? . longName ? ? " Unknown " ) " )
. font ( . title3 )
} else {
Text ( " Please connect to a radio to configure settings. " )
. font ( . callout )
. foregroundColor ( . orange )
}
Section ( header : Text ( " options " ) ) {
Toggle ( isOn : $ enabled ) {
Label ( " enabled " , systemImage : " dot.radiowaves.right " )
}
2023-08-17 16:30:48 -05:00
Toggle ( isOn : $ sendBell ) {
Label ( " Send Bell " , systemImage : " bell " )
}
TextField ( " Friendly name (sent for detection alerts text messages) " , text : $ name , axis : . vertical )
. foregroundColor ( . gray )
. autocapitalization ( . none )
. disableAutocorrection ( true )
. onChange ( of : name , perform : { _ in
let totalBytes = name . utf8 . count
// O n l y m e s s w i t h t h e v a l u e i f i t i s t o o b i g
if totalBytes > 20 {
let firstNBytes = Data ( name . utf8 . prefix ( 20 ) )
if let maxBytesString = String ( data : firstNBytes , encoding : String . Encoding . utf8 ) {
// S e t t h e s h o r t N a m e b a c k t o t h e l a s t p l a c e w h e r e i t w a s t h e r i g h t s i z e
name = maxBytesString
}
}
} )
. foregroundColor ( . gray )
}
Section ( header : Text ( " Sensor option " ) ) {
Picker ( " GPIO Pin to monitor " , selection : $ monitorPin ) {
ForEach ( 0. . < 46 ) {
if $0 = = 0 {
Text ( " unset " )
} else {
Text ( " Pin \( $0 ) " )
}
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
Toggle ( isOn : $ detectionTriggeredHigh ) {
Label ( " Detection trigger High " , systemImage : " dial.high " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
Toggle ( isOn : $ usePullup ) {
Label ( " Uses pullup resistor " , systemImage : " arrow.up.to.line " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
}
Section ( header : Text ( " update.interval " ) ) {
Picker ( " Minimum time between detection broadcasts " , selection : $ minimumBroadcastSecs ) {
ForEach ( UpdateIntervals . allCases ) { ui in
2023-08-17 17:00:20 -07:00
Text ( ui . description ) . tag ( ui . rawValue )
2023-08-17 16:30:48 -05:00
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
2023-08-17 17:00:20 -07:00
Text ( " Mininum time between detection broadcasts. Default is 45 seconds. " )
2023-08-17 16:30:48 -05:00
. font ( . caption )
Picker ( " State Broadcast Interval " , selection : $ stateBroadcastSecs ) {
2023-08-18 00:12:59 -07:00
Text ( " Never " ) . tag ( 0 )
2023-08-17 16:30:48 -05:00
ForEach ( UpdateIntervals . allCases ) { ui in
2023-08-17 17:00:20 -07:00
Text ( ui . description ) . tag ( ui . rawValue )
2023-08-17 16:30:48 -05:00
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
2023-08-17 17:00:20 -07:00
Text ( " How often to send detection sensor state to mesh regardless of detection. Default is Never. " )
. font ( . caption )
2023-08-16 18:28:55 -07:00
}
}
. scrollDismissesKeyboard ( . interactively )
2023-08-17 16:30:48 -05:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . detectionSensorConfig = = nil )
2023-08-16 18:28:55 -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 ) {
2023-08-17 16:30:48 -05:00
var dsc = ModuleConfig . DetectionSensorConfig ( )
2023-08-16 18:28:55 -07:00
dsc . enabled = self . enabled
2023-08-17 16:30:48 -05:00
dsc . sendBell = self . sendBell
dsc . name = self . name
dsc . monitorPin = UInt32 ( self . monitorPin )
dsc . detectionTriggeredHigh = self . detectionTriggeredHigh
dsc . usePullup = self . usePullup
dsc . minimumBroadcastSecs = UInt32 ( self . minimumBroadcastSecs )
dsc . stateBroadcastSecs = UInt32 ( self . stateBroadcastSecs )
let adminMessageId = bleManager . saveDetectionSensorModuleConfig ( config : dsc , 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 ( )
2023-08-26 23:17:30 -07:00
}
}
2023-08-16 18:28:55 -07:00
}
}
message : {
Text ( " config.save.confirm " )
}
2023-08-17 16:30:48 -05:00
. navigationTitle ( " detection.sensor.config " )
2023-08-16 18:28:55 -07:00
. 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-16 18:28:55 -07:00
} )
. onAppear {
self . bleManager . context = context
setDetectionSensorValues ( )
2023-08-17 16:30:48 -05: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
if bleManager . connectedPeripheral != nil && node ? . detectionSensorConfig = = nil {
print ( " empty detection sensor module config " )
2023-08-16 18:28:55 -07:00
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
if node != nil && connectedNode != nil {
2023-08-17 16:30:48 -05:00
_ = bleManager . requestDetectionSensorModuleConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-08-16 18:28:55 -07:00
}
}
}
. onChange ( of : enabled ) { newEnabled in
if node != nil && node ? . detectionSensorConfig != nil {
if newEnabled != node ! . detectionSensorConfig ! . enabled { hasChanges = true }
}
}
2023-08-17 16:30:48 -05:00
. onChange ( of : sendBell ) { newSendBell in
if node != nil && node ? . detectionSensorConfig != nil {
if newSendBell != node ! . detectionSensorConfig ! . sendBell { hasChanges = true }
}
}
. onChange ( of : detectionTriggeredHigh ) { newDetectionTriggeredHigh in
if node != nil && node ? . detectionSensorConfig != nil {
if newDetectionTriggeredHigh != node ! . detectionSensorConfig ! . detectionTriggeredHigh { hasChanges = true }
}
}
. onChange ( of : usePullup ) { newUsePullup in
if node != nil && node ? . detectionSensorConfig != nil {
if newUsePullup != node ! . detectionSensorConfig ! . usePullup { hasChanges = true }
}
}
. onChange ( of : name ) { newName in
if node != nil && node ? . detectionSensorConfig != nil {
if newName != node ! . detectionSensorConfig ! . name { hasChanges = true }
}
}
. onChange ( of : monitorPin ) { newMonitorPin in
if node != nil && node ? . detectionSensorConfig != nil {
if newMonitorPin != node ! . detectionSensorConfig ! . monitorPin { hasChanges = true }
}
}
. onChange ( of : minimumBroadcastSecs ) { newMinimumBroadcastSecs in
if node != nil && node ? . detectionSensorConfig != nil {
if newMinimumBroadcastSecs != node ! . detectionSensorConfig ! . minimumBroadcastSecs { hasChanges = true }
}
}
. onChange ( of : stateBroadcastSecs ) { newStateBroadcastSecs in
if node != nil && node ? . detectionSensorConfig != nil {
if newStateBroadcastSecs != node ! . detectionSensorConfig ! . stateBroadcastSecs { hasChanges = true }
}
}
2023-08-16 18:28:55 -07:00
}
func setDetectionSensorValues ( ) {
self . enabled = ( node ? . detectionSensorConfig ? . enabled ? ? false )
2023-08-17 16:30:48 -05:00
self . sendBell = ( node ? . detectionSensorConfig ? . sendBell ? ? false )
self . name = ( node ? . detectionSensorConfig ? . name ? ? " " )
2023-08-17 17:00:20 -07:00
self . monitorPin = Int ( node ? . detectionSensorConfig ? . monitorPin ? ? 0 )
2023-08-17 16:30:48 -05:00
self . usePullup = ( node ? . detectionSensorConfig ? . usePullup ? ? false )
self . detectionTriggeredHigh = ( node ? . detectionSensorConfig ? . detectionTriggeredHigh ? ? true )
2023-08-17 17:00:20 -07:00
self . minimumBroadcastSecs = Int ( node ? . detectionSensorConfig ? . minimumBroadcastSecs ? ? 45 )
self . stateBroadcastSecs = Int ( node ? . detectionSensorConfig ? . stateBroadcastSecs ? ? 0 )
2023-08-17 16:30:48 -05:00
2023-08-16 18:28:55 -07:00
self . hasChanges = false
}
}