Meshtastic-Apple/Meshtastic/Accessory/Protocols/Device.swift
jake-b 3f27e3b925
Keep list of previous manual connections (#1484)
* Keep list of previous manual connections

* More descriptive manual connection rows

* Merge fixes and new way to show IP on Connect view

---------

Co-authored-by: Jake-B <jake-b@users.noreply.github.com>
2025-10-28 06:18:17 -07:00

72 lines
2 KiB
Swift

//
// Device.swift
// Meshtastic
//
// Created by Jake Bordens on 7/10/25.
//
import Foundation
struct Device: Identifiable, Hashable, Codable, CustomStringConvertible {
let id: UUID
var name: String
var transportType: TransportType
var identifier: String // e.g., UUID for BLE, IP:port for TCP, port path for Serial
var num: Int64?
var shortName: String?
var longName: String?
var firmwareVersion: String?
var hardwareModel: String?
var rssi: Int?
var lastUpdate: Date?
var connectionState: ConnectionState
var wasRestored: Bool = false
var isManualConnection: Bool = false
init(id: UUID, name: String, transportType: TransportType, identifier: String, connectionState: ConnectionState = .disconnected, rssi: Int? = nil, num: Int64? = nil, wasRestored: Bool = false, isManualConnection: Bool = false) {
self.id = id
self.name = name
self.transportType = transportType
self.identifier = identifier
self.connectionState = connectionState
self.rssi = rssi
self.num = num
self.wasRestored = wasRestored
self.isManualConnection = isManualConnection
}
var rssiString: String {
if let rssi {
return "\(rssi) dBm"
} else {
return "n/a"
}
}
func getSignalStrength() -> BLESignalStrength? {
guard let rssi else { return nil }
if NSNumber(value: rssi).compare(NSNumber(-65)) == ComparisonResult.orderedDescending {
return BLESignalStrength.strong
} else if NSNumber(value: rssi).compare(NSNumber(-85)) == ComparisonResult.orderedDescending {
return BLESignalStrength.normal
} else {
return BLESignalStrength.weak
}
}
var description: String {
switch (shortName, longName) {
case (let shortName?, let longName?): // Both shortName and longName are non-nil
return "\(longName) (\(shortName))"
case (let shortName?, nil): // shortName is non-nil, longName is nil
return "\(shortName)"
case (nil, let longName?): // shortName is nil, longName is non-nil
return "\(longName)"
default: // Both are nil
return "Device(id: \(id))"
}
}
}