From d63daf5cf35bad05259241b78ee578e1d1b82487 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 1 Dec 2023 13:56:29 -0800 Subject: [PATCH] Detection sensor updates --- Meshtastic/Extensions/UserDefaults.swift | 20 ++ Meshtastic/Helpers/MeshPackets.swift | 10 +- .../Views/Nodes/DetectionSensorLog.swift | 1 + .../Config/Module/DetectionSensorConfig.swift | 177 ++++++++++++------ 4 files changed, 153 insertions(+), 55 deletions(-) diff --git a/Meshtastic/Extensions/UserDefaults.swift b/Meshtastic/Extensions/UserDefaults.swift index 1c9e21fd..4ea8d2be 100644 --- a/Meshtastic/Extensions/UserDefaults.swift +++ b/Meshtastic/Extensions/UserDefaults.swift @@ -26,6 +26,8 @@ extension UserDefaults { case mapTileServer case mapTilesAboveLabels case mapUseLegacy + case enableDetectionNotifications + case detectionSensorRole } func reset() { @@ -190,4 +192,22 @@ extension UserDefaults { UserDefaults.standard.set(newValue, forKey: "mapUseLegacy") } } + + static var enableDetectionNotifications: Bool { + get { + UserDefaults.standard.bool(forKey: "enableDetectionNotifications") + } + set { + UserDefaults.standard.set(newValue, forKey: "enableDetectionNotifications") + } + } + + static var detectionSensorRole: DetectionSensorRole { + get { + DetectionSensorRole(rawValue: UserDefaults.standard.string(forKey: "detectionSensorRole") ?? DetectionSensorRole.sensor.rawValue) ?? DetectionSensorRole.sensor + } + set { + UserDefaults.standard.set(newValue.rawValue, forKey: "detectionSensorRole") + } + } } diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index eb8b1abd..b8f48d7e 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -730,7 +730,11 @@ func textMessageAppPacket(packet: MeshPacket, blockRangeTest: Bool, connectedNod newMessage.isEmoji = packet.decoded.emoji == 1 newMessage.channel = Int32(packet.channel) newMessage.portNum = Int32(packet.decoded.portnum.rawValue) - + if packet.decoded.portnum == PortNum.detectionSensorApp { + if !UserDefaults.enableDetectionNotifications { + newMessage.read = true + } + } if packet.decoded.replyID > 0 { newMessage.replyID = Int64(packet.decoded.replyID) } @@ -755,6 +759,10 @@ func textMessageAppPacket(packet: MeshPacket, blockRangeTest: Bool, connectedNod messageSaved = true if messageSaved { + + if packet.decoded.portnum == PortNum.detectionSensorApp && !UserDefaults.enableDetectionNotifications { + return + } let appState = AppState.shared if newMessage.fromUser != nil && newMessage.toUser != nil && !(newMessage.fromUser?.mute ?? false) { // Set Unread Message Indicators diff --git a/Meshtastic/Views/Nodes/DetectionSensorLog.swift b/Meshtastic/Views/Nodes/DetectionSensorLog.swift index bf4e7e56..410dfb11 100644 --- a/Meshtastic/Views/Nodes/DetectionSensorLog.swift +++ b/Meshtastic/Views/Nodes/DetectionSensorLog.swift @@ -93,6 +93,7 @@ struct DetectionSensorLog: View { ForEach(detections) { d in GridRow { Text(d.messagePayload ?? "Detected") + .font(.caption) Text(d.timestamp.formattedDate(format: dateFormatString)) .font(.caption) } diff --git a/Meshtastic/Views/Settings/Config/Module/DetectionSensorConfig.swift b/Meshtastic/Views/Settings/Config/Module/DetectionSensorConfig.swift index 35ab9b0e..778412eb 100644 --- a/Meshtastic/Views/Settings/Config/Module/DetectionSensorConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/DetectionSensorConfig.swift @@ -6,6 +6,20 @@ // import SwiftUI +enum DetectionSensorRole: String, CaseIterable, Equatable { + case sensor + case client + var description: String { + switch self { + case .sensor: + return "Sensor" + case .client: + return "Client" + } + } + var localized: String { self.rawValue.localized } +} + struct DetectionSensorConfig: View { @Environment(\.managedObjectContext) var context @@ -14,8 +28,10 @@ struct DetectionSensorConfig: View { var node: NodeInfoEntity? @State private var isPresentingSaveConfirm: Bool = false @State var hasChanges: Bool = false + @AppStorage("detectionSensorRole") private var role: DetectionSensorRole = .sensor + @AppStorage("enableDetectionNotifications") private var detectionNotificationsEnabled = false + /// Module Config Settings @State var enabled = false - /// DetectionSensorModule will sends a bell character with the messages. @State var sendBell: Bool = false @State var name: String = "" @State var detectionTriggeredHigh: Bool = true @@ -54,70 +70,120 @@ struct DetectionSensorConfig: View { .foregroundColor(.orange) } Section(header: Text("options")) { + Toggle(isOn: $enabled) { Label("enabled", systemImage: "dot.radiowaves.right") + Text("Enables the detection sensor module, it needs to be enabled on both the node with the sensor, and any nodes that you want to receive detection sensor text messages or view the detection sensor log and chart.") + .font(.caption) } - 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 - // Only mess with the value if it is too big - if totalBytes > 20 { - - let firstNBytes = Data(name.utf8.prefix(20)) - if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) { - // Set the shortName back to the last place where it was the right size - name = maxBytesString + .listRowSeparator(.visible) + if enabled { + HStack { + Picker(selection: $role, label: Text("Role")) { + ForEach(DetectionSensorRole.allCases, id: \.self) { r in + Text(r.description) + .tag(r) } } - }) - .foregroundColor(.gray) + .pickerStyle(SegmentedPickerStyle()) + .padding(.top, 5) + .padding(.bottom, 5) + } + } } - Section(header: Text("Sensor option")) { - Picker("GPIO Pin to monitor", selection: $monitorPin) { - ForEach(0..<46) { - if $0 == 0 { - Text("unset") - } else { - Text("Pin \($0)") + if enabled && role == .client { + Section(header: Text("Client options")) { + Toggle(isOn: $detectionNotificationsEnabled) { + Label("Enable Notifications", systemImage: "bell.badge") + Text("Detection sensor messages are received as text messages. If you enable notifications you will recieve a notification for each detection message received and a corresponding unread message badge.") + .font(.caption) + } + .listRowSeparator(.visible) + } + } + if enabled && role == .sensor { + Section(header: Text("Sensor options")) { + Toggle(isOn: $sendBell) { + Label("Send Bell", systemImage: "bell") + Text("Send ASCII bell with alert message. Useful for triggering external notification on bell.") + .font(.caption) + } + .listRowSeparator(.visible) + HStack { + Label("Name", systemImage: "signature") + TextField("Friendly name", text: $name, axis: .vertical) + .foregroundColor(.gray) + .autocapitalization(.none) + .disableAutocorrection(true) + .onChange(of: name, perform: { _ in + + let totalBytes = name.utf8.count + // Only mess with the value if it is too big + if totalBytes > 20 { + + let firstNBytes = Data(name.utf8.prefix(20)) + if let maxBytesString = String(data: firstNBytes, encoding: String.Encoding.utf8) { + // Set the shortName back to the last place where it was the right size + name = maxBytesString + } + } + }) + .foregroundColor(.gray) + } + .listRowSeparator(.hidden) + Text("Friendly name used to format message sent to mesh. Example: A name \"Motion\" would result in a message \"Motion detected\"") + .font(.caption) + .foregroundStyle(.gray) + .listRowSeparator(.visible) + .offset(y: -10) + 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 - Text(ui.description).tag(ui.rawValue) + .pickerStyle(DefaultPickerStyle()) + Toggle(isOn: $detectionTriggeredHigh) { + Label("Detection trigger High", systemImage: "dial.high") + Text("Whether or not the GPIO pin state detection is triggered on HIGH (1) or LOW (0)") + .font(.caption) } - } - .pickerStyle(DefaultPickerStyle()) - Text("Mininum time between detection broadcasts. Default is 45 seconds.") - .font(.caption) - Picker("State Broadcast Interval", selection: $stateBroadcastSecs) { - Text("Never").tag(0) - ForEach(UpdateIntervals.allCases) { ui in - Text(ui.description).tag(ui.rawValue) + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + + Toggle(isOn: $usePullup) { + Label("Uses pullup resistor", systemImage: "arrow.up.to.line") + Text(" Whether or not use INPUT_PULLUP mode for GPIO pin. Only applicable if the board uses pull-up resistors on the pin") + .font(.caption) } + .toggleStyle(SwitchToggleStyle(tint: .accentColor)) + } + Section(header: Text("update.interval")) { + Picker("Minimum time between detection broadcasts", selection: $minimumBroadcastSecs) { + ForEach(UpdateIntervals.allCases) { ui in + Text(ui.description).tag(ui.rawValue) + } + } + .pickerStyle(DefaultPickerStyle()) + .listRowSeparator(.hidden) + Text("Mininum time between detection broadcasts. Default is 45 seconds.") + .font(.caption) + .foregroundStyle(.gray) + .listRowSeparator(.visible) + Picker("State Broadcast Interval", selection: $stateBroadcastSecs) { + Text("Never").tag(0) + ForEach(UpdateIntervals.allCases) { ui in + Text(ui.description).tag(ui.rawValue) + } + } + .pickerStyle(DefaultPickerStyle()) + .listRowSeparator(.hidden) + Text("How often to send detection sensor state to mesh regardless of detection. Default is Never.") + .font(.caption) + .foregroundStyle(.gray) } - .pickerStyle(DefaultPickerStyle()) - Text("How often to send detection sensor state to mesh regardless of detection. Default is Never.") - .font(.caption) } } .scrollDismissesKeyboard(.interactively) @@ -223,6 +289,9 @@ struct DetectionSensorConfig: View { if newStateBroadcastSecs != node!.detectionSensorConfig!.stateBroadcastSecs { hasChanges = true } } } + .onChange(of: detectionNotificationsEnabled) { newDetectionNotificationsEnabled in + UserDefaults.enableDetectionNotifications = newDetectionNotificationsEnabled + } } func setDetectionSensorValues() { self.enabled = (node?.detectionSensorConfig?.enabled ?? false)