2022-06-14 16:45:43 -07:00
//
// T e l e m e t r y 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 TelemetryConfig : View {
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -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-06 10:33:18 -08:00
2022-07-07 00:29:52 -07:00
var node : NodeInfoEntity ?
2023-03-06 10:33:18 -08:00
2022-07-01 10:57:54 -07:00
@ State private var isPresentingSaveConfirm : Bool = false
@ State var hasChanges = false
2022-06-22 09:05:56 -07:00
@ State var deviceUpdateInterval = 0
@ State var environmentUpdateInterval = 0
@ State var environmentMeasurementEnabled = false
@ State var environmentScreenEnabled = false
@ State var environmentDisplayFahrenheit = false
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -07:00
var body : some View {
2023-03-06 10:33:18 -08:00
2022-06-14 16:45:43 -07:00
VStack {
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-14 12:44:10 -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 ? . telemetryConfig = = nil {
2023-03-13 19:17:43 -07:00
Text ( " Telemetry 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-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 17:47:23 -08:00
Section ( header : Text ( " update.interval " ) ) {
2022-06-22 09:05:56 -07:00
Picker ( " Device Metrics " , selection : $ deviceUpdateInterval ) {
ForEach ( UpdateIntervals . allCases ) { ui in
Text ( ui . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
2022-06-22 15:52:51 -07:00
Text ( " How often device metrics are sent out over the mesh. Default is 15 minutes. " )
. font ( . caption )
2022-06-22 09:05:56 -07:00
Picker ( " Sensor Metrics " , selection : $ environmentUpdateInterval ) {
ForEach ( UpdateIntervals . allCases ) { ui in
Text ( ui . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
2022-06-22 15:52:51 -07:00
Text ( " How often sensor metrics are sent out over the mesh. Default is 15 minutes. " )
. font ( . caption )
2022-06-22 09:05:56 -07:00
}
Section ( header : Text ( " Sensor Options " ) ) {
2022-11-11 09:07:14 -08:00
Text ( " Supported I2C Connected sensors will be detected automatically, sensors are BMP280, BME280, BME680, MCP9808, INA219, INA260, LPS22 and SHTC3. " )
2022-08-01 07:11:03 -07:00
. font ( . caption )
2022-06-22 09:05:56 -07:00
Toggle ( isOn : $ environmentMeasurementEnabled ) {
2022-12-13 08:47:14 -08:00
Label ( " enabled " , systemImage : " chart.xyaxis.line " )
2022-06-22 09:05:56 -07:00
}
2022-06-22 15:52:51 -07:00
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
2022-06-22 09:05:56 -07:00
Toggle ( isOn : $ environmentScreenEnabled ) {
Label ( " Show on device screen " , systemImage : " display " )
}
2022-06-22 15:52:51 -07:00
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
2022-06-22 09:05:56 -07:00
Toggle ( isOn : $ environmentDisplayFahrenheit ) {
Label ( " Display Fahrenheit " , systemImage : " thermometer " )
}
2022-06-22 15:52:51 -07:00
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
2022-06-22 09:05:56 -07:00
}
2022-06-14 16:45:43 -07:00
}
2023-03-13 19:27:36 -07:00
. disabled ( self . bleManager . connectedPeripheral = = nil || node ? . telemetryConfig = = nil )
2022-07-01 10:57:54 -07:00
Button {
isPresentingSaveConfirm = true
} label : {
2022-12-12 20:35:38 -08:00
Label ( " save " , systemImage : " square.and.arrow.down " )
2022-07-01 10:57:54 -07:00
}
2022-07-09 06:42:30 -07:00
. disabled ( bleManager . connectedPeripheral = = nil || ! hasChanges || node ! . telemetryConfig = = nil )
2022-07-01 10:57:54 -07:00
. buttonStyle ( . bordered )
. buttonBorderShape ( . capsule )
. controlSize ( . large )
. padding ( )
. confirmationDialog (
2022-12-13 08:47:14 -08:00
" are.you.sure " ,
2022-10-04 18:19:02 -07:00
isPresented : $ isPresentingSaveConfirm ,
titleVisibility : . visible
2022-07-01 10:57:54 -07:00
) {
2023-02-22 20:31:08 -08:00
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral ? . num ? ? - 1 , context : context )
2023-02-06 18:45:03 -08:00
if connectedNode != nil {
let nodeName = node ? . user ? . longName ? ? NSLocalizedString ( " unknown " , comment : " Unknown " )
let buttonText = String . localizedStringWithFormat ( NSLocalizedString ( " save.config %@ " , comment : " Save Config for %@ " ) , nodeName )
Button ( buttonText ) {
var tc = ModuleConfig . TelemetryConfig ( )
tc . deviceUpdateInterval = UInt32 ( deviceUpdateInterval )
tc . environmentUpdateInterval = UInt32 ( environmentUpdateInterval )
tc . environmentMeasurementEnabled = environmentMeasurementEnabled
tc . environmentScreenEnabled = environmentScreenEnabled
tc . environmentDisplayFahrenheit = environmentDisplayFahrenheit
2023-03-06 10:33:18 -08:00
let adminMessageId = bleManager . saveTelemetryModuleConfig ( config : tc , 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-07-02 19:50:08 -07:00
}
2022-07-01 10:57:54 -07:00
}
}
2022-12-30 11:08:59 -08:00
message : {
Text ( " config.save.confirm " )
}
2022-12-12 22:33:06 -08:00
. navigationTitle ( " telemetry.config " )
2022-06-14 16:45:43 -07:00
. navigationBarItems ( trailing :
ZStack {
2022-11-12 08:48:01 -08:00
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ???? " )
2022-06-14 16:45:43 -07:00
} )
. onAppear {
2022-11-12 08:48:01 -08:00
self . bleManager . context = context
self . deviceUpdateInterval = Int ( node ? . telemetryConfig ? . deviceUpdateInterval ? ? 0 )
self . environmentUpdateInterval = Int ( node ? . telemetryConfig ? . environmentUpdateInterval ? ? 0 )
self . environmentMeasurementEnabled = node ? . telemetryConfig ? . environmentMeasurementEnabled ? ? false
self . environmentScreenEnabled = node ? . telemetryConfig ? . environmentScreenEnabled ? ? false
self . environmentDisplayFahrenheit = node ? . telemetryConfig ? . environmentDisplayFahrenheit ? ? false
self . hasChanges = false
2023-03-06 10:33:18 -08:00
2023-01-31 23:52:19 -08:00
// N e e d t o r e q u e s t a T e l e m e t r y 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 ? . telemetryConfig = = nil {
print ( " empty telemetry module config " )
let connectedNode = getNodeInfo ( id : bleManager . connectedPeripheral . num , context : context )
2023-03-05 14:40:07 -08:00
if node != nil && connectedNode != nil {
2023-02-06 18:45:03 -08:00
_ = bleManager . requestTelemetryModuleConfig ( fromUser : connectedNode ! . user ! , toUser : node ! . user ! , adminIndex : connectedNode ? . myInfo ? . adminIndex ? ? 0 )
2023-01-31 23:52:19 -08:00
}
}
2022-07-02 13:43:13 -07:00
}
. onChange ( of : deviceUpdateInterval ) { newDeviceInterval in
2023-02-03 10:15:11 -08:00
if node != nil && node ? . telemetryConfig != nil {
2022-07-09 06:42:30 -07:00
if newDeviceInterval != node ! . telemetryConfig ! . deviceUpdateInterval { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
. onChange ( of : environmentUpdateInterval ) { newEnvInterval in
2023-02-03 10:15:11 -08:00
if node != nil && node ? . telemetryConfig != nil {
2022-07-09 06:42:30 -07:00
if newEnvInterval != node ! . telemetryConfig ! . environmentUpdateInterval { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
. onChange ( of : environmentMeasurementEnabled ) { newEnvEnabled in
2023-02-03 10:15:11 -08:00
if node != nil && node ? . telemetryConfig != nil {
2022-07-09 06:42:30 -07:00
if newEnvEnabled != node ! . telemetryConfig ! . environmentMeasurementEnabled { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
. onChange ( of : environmentScreenEnabled ) { newEnvScreenEnabled in
2022-07-09 06:42:30 -07:00
if node ! . telemetryConfig != nil {
if newEnvScreenEnabled != node ! . telemetryConfig ! . environmentScreenEnabled { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
. onChange ( of : environmentDisplayFahrenheit ) { newEnvDisplayF in
2023-02-03 10:15:11 -08:00
if node != nil && node ? . telemetryConfig != nil {
2022-07-09 06:42:30 -07:00
if newEnvDisplayF != node ! . telemetryConfig ! . environmentDisplayFahrenheit { hasChanges = true }
}
2022-07-02 13:43:13 -07:00
}
2022-06-14 16:45:43 -07:00
}
}
}