From 95b35e7e456843fc41bd6f3aadd68f0f5272d47e Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 8 Jul 2022 12:05:39 -0700 Subject: [PATCH] Restrict ESP32 only plugins, finish telemetry log. --- Meshtastic/Helpers/BLEManager.swift | 91 ++++++++++++++++- Meshtastic/Helpers/MeshPackets.swift | 2 + Meshtastic/Views/Nodes/TelemetryLog.swift | 97 +++++++++++++++++++ .../Config/Module/CannedMessagesConfig.swift | 2 +- .../Settings/Config/Module/SerialConfig.swift | 3 +- Meshtastic/Views/Settings/Settings.swift | 4 +- 6 files changed, 192 insertions(+), 7 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index 4f037c29..0de3b4cc 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -1259,13 +1259,12 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } - public func saveCannedMessageModuleConfig(config: ModuleConfig.CannedMessageConfig, messages: String, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 { + public func saveCannedMessageModuleConfig(config: ModuleConfig.CannedMessageConfig, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 { var newMessageId: Int64 = 0 var adminPacket = AdminMessage() adminPacket.setModuleConfig.cannedMessage = config - //adminPacket.setCannedMessageModulePart1 = messages var meshPacket: MeshPacket = MeshPacket() meshPacket.to = UInt32(toUser.num) @@ -1317,7 +1316,62 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return newMessageId } - + public func saveCannedMessageModuleMessages(messages: String, fromUser: UserEntity, toUser: UserEntity, wantResponse: Bool) -> Int64 { + + var newMessageId: Int64 = 0 + + var adminPacket = AdminMessage() + adminPacket.setCannedMessageModulePart1 = messages + + var meshPacket: MeshPacket = MeshPacket() + meshPacket.to = UInt32(toUser.num) + meshPacket.from = 0 //UInt32(fromUser.num) + meshPacket.id = UInt32.random(in: UInt32(UInt8.max).. Int64 { @@ -1547,4 +1601,35 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } return newMessageId } + + // Send an admin message to a radio, save a message to core data for logging + private func sendAdminMessageToRadio(adminDescription: String, messageId: Int64, fromUser: UserEntity, toUser: UserEntity, binaryData: Data) { + + if connectedPeripheral!.peripheral.state == CBPeripheralState.connected { + + let newMessage = MessageEntity(context: context!) + newMessage.messageId = Int64(messageId) + newMessage.messageTimestamp = Int32(Date().timeIntervalSince1970) + newMessage.receivedACK = false + newMessage.admin = true + newMessage.adminDescription = adminDescription + newMessage.fromUser = fromUser + newMessage.toUser = toUser + + do { + + try context!.save() + + if meshLoggingEnabled { MeshLogger.log("💾 \(adminDescription)") } + connectedPeripheral.peripheral.writeValue(binaryData, for: TORADIO_characteristic, type: .withResponse) + + } catch { + + context!.rollback() + + let nsError = error as NSError + print("💥 Error inserting new core data MessageEntity: \(nsError)") + } + } + } } diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index a54f6a12..17c1028b 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -1299,6 +1299,8 @@ func telemetryPacket(packet: MeshPacket, meshLogging: Bool, context: NSManagedOb telemetry.gasResistance = telemetryMessage.environmentMetrics.gasResistance telemetry.relativeHumidity = telemetryMessage.environmentMetrics.relativeHumidity telemetry.temperature = telemetryMessage.environmentMetrics.temperature + telemetry.current = telemetryMessage.environmentMetrics.current + telemetry.voltage = telemetryMessage.environmentMetrics.voltage telemetry.metricsType = 1 } diff --git a/Meshtastic/Views/Nodes/TelemetryLog.swift b/Meshtastic/Views/Nodes/TelemetryLog.swift index cd6c7d0e..018143ca 100644 --- a/Meshtastic/Views/Nodes/TelemetryLog.swift +++ b/Meshtastic/Views/Nodes/TelemetryLog.swift @@ -26,8 +26,10 @@ struct TelemetryLog: View { if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { + if tel.metricsType == 0 { + // Device Metrics HStack { Text("Device Metrics") @@ -75,6 +77,101 @@ struct TelemetryLog: View { .foregroundColor(.gray) .font(.callout) } + + } else if tel.metricsType == 1 { + + // Environment Metrics + HStack { + + Text("Environment Metrics") + .font(.title3) + + let sensor = SensorTypes(rawValue: Int(node.telemetryConfig?.environmentSensorType ?? 0)) + + let tempReadingType = (!(node.telemetryConfig?.environmentDisplayFahrenheit ?? true)) ? "°F" : "°C" + + if sensor == SensorTypes.bme280 || + sensor == SensorTypes.bme680 || + sensor == SensorTypes.shtc3 || + sensor == SensorTypes.mcp9808 { + + Image(systemName: "thermometer") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Temperature: \(String(format: "%.2f", tel.temperature))\(tempReadingType)") + .foregroundColor(.gray) + .font(.callout) + } + + if sensor == SensorTypes.bme280 || + sensor == SensorTypes.bme680 || + sensor == SensorTypes.shtc3 { + + Image(systemName: "humidity") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Relative Humidity: \(String(format: "%.2f", tel.relativeHumidity))") + .foregroundColor(.gray) + .font(.callout) + } + + if sensor == SensorTypes.bme280 || + sensor == SensorTypes.bme680 { + + Image(systemName: "barometer") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Barometric Pressure: \(String(format: "%.2f", tel.barometricPressure))") + .foregroundColor(.gray) + .font(.callout) + } + + if sensor == SensorTypes.bme680 { + + Image(systemName: "aqi.medium") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Gas Resistance: \(String(format: "%.2f", tel.gasResistance))") + .foregroundColor(.gray) + .font(.callout) + } + + if sensor == SensorTypes.ina219 || + sensor == SensorTypes.ina260 { + + Image(systemName: "directcurrent") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Current: \(String(format: "%.2f", tel.current))") + .foregroundColor(.gray) + .font(.callout) + + Image(systemName: "bolt") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Voltage: \(String(format: "%.2f", tel.voltage))") + .foregroundColor(.gray) + .font(.callout) + } + + Image(systemName: "clock.badge.checkmark.fill") + .font(.callout) + .foregroundColor(.accentColor) + .symbolRenderingMode(.hierarchical) + Text("Time:") + .foregroundColor(.gray) + .font(.callout) + DateTimeText(dateTime: tel.time) + .foregroundColor(.gray) + .font(.callout) + + } } } else { diff --git a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift index a988bb46..d6c19f29 100644 --- a/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/CannedMessagesConfig.swift @@ -316,7 +316,7 @@ struct CannedMessagesConfig: View { cmc.inputbrokerEventCcw = InputEventChars(rawValue: inputbrokerEventCcw)!.protoEnumValue() cmc.inputbrokerEventPress = InputEventChars(rawValue: inputbrokerEventPress)!.protoEnumValue() - let adminMessageId = bleManager.saveCannedMessageModuleConfig(config: cmc, messages: "Where are you garth?, Hello",fromUser: node!.user!, toUser: node!.user!, wantResponse: true) + 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 diff --git a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift index 78182f36..4612a0cd 100644 --- a/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift +++ b/Meshtastic/Views/Settings/Config/Module/SerialConfig.swift @@ -272,6 +272,7 @@ struct SerialConfig: View { .font(.caption) } } + .disabled(!(node!.myInfo?.hasWifi ?? false)) Button { @@ -281,7 +282,7 @@ struct SerialConfig: View { Label("Save", systemImage: "square.and.arrow.down") } - .disabled(bleManager.connectedPeripheral == nil || !hasChanges) + .disabled(bleManager.connectedPeripheral == nil || !hasChanges || !(node!.myInfo?.hasWifi ?? false)) .buttonStyle(.bordered) .buttonBorderShape(.capsule) .controlSize(.large) diff --git a/Meshtastic/Views/Settings/Settings.swift b/Meshtastic/Views/Settings/Settings.swift index 8ed698c9..8eb6180c 100644 --- a/Meshtastic/Views/Settings/Settings.swift +++ b/Meshtastic/Views/Settings/Settings.swift @@ -135,7 +135,7 @@ struct Settings: View { Image(systemName: "point.3.connected.trianglepath.dotted") .symbolRenderingMode(.hierarchical) - Text("Range Test") + Text("Range Test (ESP32 Only)") } .disabled(bleManager.connectedPeripheral == nil) @@ -146,7 +146,7 @@ struct Settings: View { Image(systemName: "terminal") .symbolRenderingMode(.hierarchical) - Text("Serial") + Text("Serial (ESP32 Only)") } .disabled(bleManager.connectedPeripheral == nil)