diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 9c790035..828b2d4a 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -1129,6 +1129,66 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return false } + public func saveCannedMessageModuleConfig(config: ModuleConfig.CannedMessageConfig, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 { + + var newMessageId: Int64 = 0 + + var adminPacket = AdminMessage() + adminPacket.setModuleConfig.cannedMessage = config + + var meshPacket: MeshPacket = MeshPacket() + meshPacket.to = UInt32(toUser.num) + meshPacket.from = 0 //UInt32(fromUser.num) + meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { var newMessageId: Int64 = 0 diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index 4dff1875..a79c7534 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -335,7 +335,7 @@ func moduleConfig (config: ModuleConfig, meshlogging: Bool, context:NSManagedObj var isDefault = false - if (try! config.serial.jsonString()) == "{}" { + if (try! config.cannedMessage.jsonString()) == "{}" { isDefault = true print("🥫 Default Canned Message Module config") @@ -347,7 +347,7 @@ func moduleConfig (config: ModuleConfig, meshlogging: Bool, context:NSManagedObj do { let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity] - // Found a node, save Device Config + // Found a node, save Canned Message Config if !fetchedNode.isEmpty { if fetchedNode[0].cannedMessageConfig == nil { @@ -432,6 +432,93 @@ func moduleConfig (config: ModuleConfig, meshlogging: Bool, context:NSManagedObj } } + if config.payloadVariant == ModuleConfig.OneOf_PayloadVariant.externalNotification(config.externalNotification) { + + var isDefault = false + + if (try! config.externalNotification.jsonString()) == "{}" { + + isDefault = true + print("🚨 Default External Notifiation Module config") + } + + let fetchNodeInfoRequest: NSFetchRequest = NSFetchRequest.init(entityName: "NodeInfoEntity") + fetchNodeInfoRequest.predicate = NSPredicate(format: "num == %lld", Int64(nodeNum)) + + do { + + let fetchedNode = try context.fetch(fetchNodeInfoRequest) as! [NodeInfoEntity] + // Found a node, save External Notificaitone Config + if !fetchedNode.isEmpty { + + if fetchedNode[0].externalNotificationConfig == nil { + + let newExternalNotificationConfig = ExternalNotificationConfigEntity(context: context) + + if isDefault { + + + newExternalNotificationConfig.enabled = false + newExternalNotificationConfig.alertBell = false + newExternalNotificationConfig.alertMessage = false + newExternalNotificationConfig.active = false + newExternalNotificationConfig.output = 0 + newExternalNotificationConfig.outputMilliseconds = 0 + + + } else { + + newExternalNotificationConfig.enabled = config.externalNotification.enabled + newExternalNotificationConfig.alertBell = config.externalNotification.alertBell + newExternalNotificationConfig.alertMessage = config.externalNotification.alertMessage + newExternalNotificationConfig.active = config.externalNotification.active + newExternalNotificationConfig.output = Int32(config.externalNotification.output) + newExternalNotificationConfig.outputMilliseconds = Int32(config.externalNotification.outputMs) + } + + fetchedNode[0].externalNotificationConfig = newExternalNotificationConfig + + } else { + + if isDefault { + + fetchedNode[0].externalNotificationConfig?.enabled = false + fetchedNode[0].externalNotificationConfig?.alertBell = false + fetchedNode[0].externalNotificationConfig?.alertMessage = false + fetchedNode[0].externalNotificationConfig?.active = false + fetchedNode[0].externalNotificationConfig?.output = 0 + fetchedNode[0].externalNotificationConfig?.outputMilliseconds = 0 + + } else { + + fetchedNode[0].externalNotificationConfig?.enabled = config.externalNotification.enabled + fetchedNode[0].externalNotificationConfig?.alertBell = config.externalNotification.alertBell + fetchedNode[0].externalNotificationConfig?.alertMessage = config.externalNotification.alertMessage + fetchedNode[0].externalNotificationConfig?.active = config.externalNotification.active + fetchedNode[0].externalNotificationConfig?.output = Int32(config.externalNotification.output) + fetchedNode[0].externalNotificationConfig?.outputMilliseconds = Int32(config.externalNotification.outputMs) + } + } + + do { + + try context.save() + if meshlogging { MeshLogger.log("💾 Updated External Notification Module Config for node number: \(String(nodeNum))") } + + } catch { + + context.rollback() + + let nsError = error as NSError + print("💥 Error Updating Core Data ExternalNotificationConfigEntity: \(nsError)") + } + } + + } catch { + + } + } + if config.payloadVariant == ModuleConfig.OneOf_PayloadVariant.rangeTest(config.rangeTest) { var isDefault = false diff --git a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel v 4.xcdatamodel/contents b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel v 4.xcdatamodel/contents index 786d7b7e..3ed68240 100644 --- a/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel v 4.xcdatamodel/contents +++ b/Meshtastic/Meshtastic.xcdatamodeld/MeshtasticDataModel v 4.xcdatamodel/contents @@ -28,6 +28,16 @@ + + + + + + + + + + @@ -85,6 +95,7 @@ + @@ -170,12 +181,13 @@ - + + \ No newline at end of file diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index 914cbd1d..cabbc687 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -68,6 +68,28 @@ enum InputEventChars: Int, CaseIterable, Identifiable { } } } + func protoEnumValue() -> ModuleConfig.CannedMessageConfig.InputEventChar { + + switch self { + + case .keyNone: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyNone + case .keyUp: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyUp + case .keyDown: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyDown + case .keyLeft: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyLeft + case .keyRight: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyRight + case .keySelect: + return ModuleConfig.CannedMessageConfig.InputEventChar.keySelect + case .keyBack: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyBack + case .keyCancel: + return ModuleConfig.CannedMessageConfig.InputEventChar.keyCancel + } + } } struct CannedMessagesConfig: View { @@ -282,16 +304,18 @@ struct CannedMessagesConfig: View { cmc.inputbrokerPinA = UInt32(inputbrokerPinA) cmc.inputbrokerPinB = UInt32(inputbrokerPinB) cmc.inputbrokerPinPress = UInt32(inputbrokerPinPress) + cmc.inputbrokerEventCw = InputEventChars(rawValue: inputbrokerEventCw)!.protoEnumValue() + cmc.inputbrokerEventCcw = InputEventChars(rawValue: inputbrokerEventCcw)!.protoEnumValue() + cmc.inputbrokerEventPress = InputEventChars(rawValue: inputbrokerEventPress)!.protoEnumValue() - //if bleManager.saveRangeTestModuleConfig(config: rtc, destNum: bleManager.connectedPeripheral.num, wantResponse: false) { + let adminMessageId = bleManager.saveCannedMessageModuleConfig(config: cmc, fromUser: node.user!, toUser: node.user!, wantResponse: true) + 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 - - //} else { - - //} + } + } } @@ -308,6 +332,7 @@ struct CannedMessagesConfig: View { self.bleManager.context = context self.enabled = node.cannedMessageConfig?.enabled ?? false + self.sendBell = node.cannedMessageConfig?.sendBell ?? false self.rotary1Enabled = node.cannedMessageConfig?.rotary1Enabled ?? false self.updown1Enabled = node.cannedMessageConfig?.updown1Enabled ?? false self.inputbrokerPinA = Int(node.cannedMessageConfig?.inputbrokerPinA ?? 0) @@ -336,17 +361,35 @@ struct CannedMessagesConfig: View { } .onChange(of: enabled) { newEnabled in - //if newEnabled != node.cannedMessagesConfig!.enabled { - - hasChanges = true - //} + if newEnabled != node.cannedMessageConfig!.enabled { hasChanges = true } } .onChange(of: sendBell) { newBell in - //if newBell != node.rangeTestConfig!.save { - - hasChanges = true - //} + if newBell != node.cannedMessageConfig!.sendBell { hasChanges = true } + } + .onChange(of: inputbrokerPinA) { newPinA in + + if newPinA != node.cannedMessageConfig!.inputbrokerPinA { hasChanges = true } + } + .onChange(of: inputbrokerPinB) { newPinB in + + if newPinB != node.cannedMessageConfig!.inputbrokerPinB { hasChanges = true } + } + .onChange(of: inputbrokerPinPress) { newPinPress in + + if newPinPress != node.cannedMessageConfig!.inputbrokerPinPress { hasChanges = true } + } + .onChange(of: inputbrokerEventCw) { newKeyA in + + if newKeyA != node.cannedMessageConfig!.inputbrokerEventCw { hasChanges = true } + } + .onChange(of: inputbrokerEventCcw) { newKeyB in + + if newKeyB != node.cannedMessageConfig!.inputbrokerEventCcw { hasChanges = true } + } + .onChange(of: inputbrokerEventPress) { newKeyPress in + + if newKeyPress != node.cannedMessageConfig!.inputbrokerEventPress { hasChanges = true } } .navigationViewStyle(StackNavigationViewStyle()) } diff --git a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift index 4817a258..d98a8273 100644 --- a/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/ExternalNotificationConfig.swift @@ -63,9 +63,6 @@ struct ExternalNotificationConfig: View { @State var active = false @State var output = 0 @State var outputMilliseconds = 0 - - - var body: some View {