2022-10-03 16:52:00 -07:00
//
// U p d a t e C o r e D a t a . s w i f t
// M e s h t a s t i c
//
// 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 1 0 / 3 / 2 2 .
2022-10-03 20:13:33 -07:00
2022-10-03 16:52:00 -07:00
import CoreData
public func clearPositions ( destNum : Int64 , context : NSManagedObjectContext ) -> Bool {
2023-03-06 10:33:18 -08:00
2022-10-03 16:52:00 -07:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( destNum ) )
2023-03-06 10:33:18 -08:00
2022-10-03 16:52:00 -07:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return false
}
2022-10-03 16:52:00 -07:00
let newPostions = [ PositionEntity ] ( )
fetchedNode [ 0 ] . positions ? = NSOrderedSet ( array : newPostions )
do {
try context . save ( )
return true
2023-03-06 10:33:18 -08:00
2022-10-03 16:52:00 -07:00
} catch {
context . rollback ( )
return false
}
} catch {
print ( " 💥 Fetch NodeInfoEntity Error " )
return false
}
2022-10-03 20:13:33 -07:00
}
public func clearTelemetry ( destNum : Int64 , metricsType : Int32 , context : NSManagedObjectContext ) -> Bool {
2023-03-06 10:33:18 -08:00
2022-10-03 20:13:33 -07:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( destNum ) )
2023-03-06 10:33:18 -08:00
2022-10-03 20:13:33 -07:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return false
}
2022-10-03 20:13:33 -07:00
let emptyTelemetry = [ TelemetryEntity ] ( )
fetchedNode [ 0 ] . telemetries ? = NSOrderedSet ( array : emptyTelemetry )
do {
try context . save ( )
return true
2023-03-06 10:33:18 -08:00
2022-10-03 20:13:33 -07:00
} catch {
context . rollback ( )
return false
}
} catch {
print ( " 💥 Fetch NodeInfoEntity Error " )
return false
}
2022-10-03 16:52:00 -07:00
}
2022-10-03 21:19:10 -07:00
2022-12-17 23:53:06 -08:00
public func deleteChannelMessages ( channel : ChannelEntity , context : NSManagedObjectContext ) {
2022-11-24 23:25:44 -08:00
do {
2023-01-25 23:01:45 -08:00
let objects = channel . allPrivateMessages
2023-02-01 09:19:45 -08:00
for object in objects {
context . delete ( object )
}
2022-11-24 23:25:44 -08:00
try context . save ( )
} catch let error as NSError {
print ( " Error: \( error . localizedDescription ) " )
}
}
2022-11-25 00:22:51 -08:00
public func deleteUserMessages ( user : UserEntity , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2022-11-25 00:22:51 -08:00
do {
2023-01-25 23:01:45 -08:00
let objects = user . messageList
2023-02-01 09:19:45 -08:00
for object in objects {
context . delete ( object )
}
2022-11-25 00:22:51 -08:00
try context . save ( )
} catch let error as NSError {
print ( " Error: \( error . localizedDescription ) " )
}
}
2022-10-03 21:19:10 -07:00
public func clearCoreDataDatabase ( context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2022-10-03 21:19:10 -07:00
let persistenceController = PersistenceController . shared . container
for i in 0. . . persistenceController . managedObjectModel . entities . count - 1 {
let entity = persistenceController . managedObjectModel . entities [ i ]
2022-10-06 08:56:15 -07:00
let query = NSFetchRequest < NSFetchRequestResult > ( entityName : entity . name ! )
let deleteRequest = NSBatchDeleteRequest ( fetchRequest : query )
2023-03-06 10:33:18 -08:00
2022-11-24 23:25:44 -08:00
do {
try context . executeAndMergeChanges ( using : deleteRequest )
2022-11-25 00:26:13 -08:00
} catch let error as NSError {
2023-02-01 09:19:45 -08:00
print ( error )
2022-11-25 00:26:13 -08:00
}
2022-10-03 21:19:10 -07:00
}
}
2023-01-20 19:14:49 -08:00
2023-03-10 19:41:26 -08:00
func upsertNodeInfoPacket ( packet : MeshPacket , context : NSManagedObjectContext ) {
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.nodeinfo.received %@ " , comment : " Node info received for: %@ " ) , String ( packet . from ) )
MeshLogger . log ( " 📟 \( logString ) " )
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
guard packet . from > 0 else { return }
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
let fetchNodeInfoAppRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoAppRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( packet . from ) )
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
do {
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
let fetchedNode = try context . fetch ( fetchNodeInfoAppRequest ) as ? [ NodeInfoEntity ] ? ? [ ]
if fetchedNode . count = = 0 {
// N o t F o u n d I n s e r t
let newNode = NodeInfoEntity ( context : context )
newNode . id = Int64 ( packet . from )
newNode . num = Int64 ( packet . from )
newNode . lastHeard = Date ( timeIntervalSince1970 : TimeInterval ( Int64 ( packet . rxTime ) ) )
newNode . snr = packet . rxSnr
2023-04-02 15:00:15 -07:00
if let nodeInfoMessage = try ? NodeInfo ( serializedData : packet . decoded . payload ) {
newNode . channel = Int32 ( nodeInfoMessage . channel )
}
2023-03-10 19:41:26 -08:00
if let newUserMessage = try ? User ( serializedData : packet . decoded . payload ) {
let newUser = UserEntity ( context : context )
newUser . userId = newUserMessage . id
newUser . num = Int64 ( packet . from )
newUser . longName = newUserMessage . longName
newUser . shortName = newUserMessage . shortName
newUser . macaddr = newUserMessage . macaddr
newUser . hwModel = String ( describing : newUserMessage . hwModel ) . uppercased ( )
newNode . user = newUser
}
} else {
// U p d a t e a n e x i s t i n g n o d e
fetchedNode [ 0 ] . id = Int64 ( packet . from )
fetchedNode [ 0 ] . num = Int64 ( packet . from )
fetchedNode [ 0 ] . lastHeard = Date ( timeIntervalSince1970 : TimeInterval ( Int64 ( packet . rxTime ) ) )
fetchedNode [ 0 ] . snr = packet . rxSnr
2023-03-14 12:44:10 -07:00
2023-03-10 19:41:26 -08:00
if let nodeInfoMessage = try ? NodeInfo ( serializedData : packet . decoded . payload ) {
2023-04-02 15:00:15 -07:00
fetchedNode [ 0 ] . channel = Int32 ( nodeInfoMessage . channel )
2023-03-10 19:41:26 -08:00
if nodeInfoMessage . hasDeviceMetrics {
let telemetry = TelemetryEntity ( context : context )
telemetry . batteryLevel = Int32 ( nodeInfoMessage . deviceMetrics . batteryLevel )
telemetry . voltage = nodeInfoMessage . deviceMetrics . voltage
telemetry . channelUtilization = nodeInfoMessage . deviceMetrics . channelUtilization
telemetry . airUtilTx = nodeInfoMessage . deviceMetrics . airUtilTx
var newTelemetries = [ TelemetryEntity ] ( )
newTelemetries . append ( telemetry )
fetchedNode [ 0 ] . telemetries ? = NSOrderedSet ( array : newTelemetries )
}
if nodeInfoMessage . hasUser {
fetchedNode [ 0 ] . user ! . userId = nodeInfoMessage . user . id
fetchedNode [ 0 ] . user ! . num = Int64 ( nodeInfoMessage . num )
fetchedNode [ 0 ] . user ! . longName = nodeInfoMessage . user . longName
fetchedNode [ 0 ] . user ! . shortName = nodeInfoMessage . user . shortName
fetchedNode [ 0 ] . user ! . macaddr = nodeInfoMessage . user . macaddr
fetchedNode [ 0 ] . user ! . hwModel = String ( describing : nodeInfoMessage . user . hwModel ) . uppercased ( )
}
}
do {
try context . save ( )
print ( " 💾 Updated NodeInfo from Node Info App Packet For: \( fetchedNode [ 0 ] . num ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Saving NodeInfoEntity from NODEINFO_APP \( nsError ) " )
}
}
} catch {
print ( " 💥 Error Fetching NodeInfoEntity for NODEINFO_APP " )
}
}
2023-01-25 23:01:45 -08:00
func upsertPositionPacket ( packet : MeshPacket , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.position.received %@ " , comment : " Position Packet received from node: %@ " ) , String ( packet . from ) )
MeshLogger . log ( " 📍 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
let fetchNodePositionRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodePositionRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( packet . from ) )
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
if let positionMessage = try ? Position ( serializedData : packet . decoded . payload ) {
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
// D o n ' t s a v e e m p t y p o s i t i o n p a c k e t s
2023-03-06 10:33:18 -08:00
if positionMessage . longitudeI > 0 || positionMessage . latitudeI > 0 && ( positionMessage . latitudeI != 373346000 && positionMessage . longitudeI != - 1220090000 ) {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodePositionRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-25 23:01:45 -08:00
if fetchedNode . count = = 1 {
2023-03-06 10:33:18 -08:00
2023-02-22 09:37:31 -08:00
// U n s e t t h e c u r r e n t l a t e s t p o s i t i o n f o r t h i s n o d e
let fetchCurrentLatestPositionsRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " PositionEntity " )
fetchCurrentLatestPositionsRequest . predicate = NSPredicate ( format : " nodePosition.num == %lld && latest = true " , Int64 ( packet . from ) )
2023-03-14 12:44:10 -07:00
2023-03-06 15:30:10 -08:00
guard let fetchedPositions = try context . fetch ( fetchCurrentLatestPositionsRequest ) as ? [ PositionEntity ] else {
return
}
2023-02-22 09:37:31 -08:00
if fetchedPositions . count > 0 {
for position in fetchedPositions {
position . latest = false
}
}
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
let position = PositionEntity ( context : context )
2023-02-22 09:37:31 -08:00
position . latest = true
2023-01-25 23:01:45 -08:00
position . snr = packet . rxSnr
position . seqNo = Int32 ( positionMessage . seqNumber )
position . latitudeI = positionMessage . latitudeI
position . longitudeI = positionMessage . longitudeI
position . altitude = positionMessage . altitude
position . satsInView = Int32 ( positionMessage . satsInView )
position . speed = Int32 ( positionMessage . groundSpeed )
position . heading = Int32 ( positionMessage . groundTrack )
if positionMessage . timestamp != 0 {
position . time = Date ( timeIntervalSince1970 : TimeInterval ( Int64 ( positionMessage . timestamp ) ) )
} else {
position . time = Date ( timeIntervalSince1970 : TimeInterval ( Int64 ( positionMessage . time ) ) )
}
2023-03-06 15:30:10 -08:00
guard let mutablePositions = fetchedNode [ 0 ] . positions ! . mutableCopy ( ) as ? NSMutableOrderedSet else {
return
}
2023-01-25 23:01:45 -08:00
mutablePositions . add ( position )
fetchedNode [ 0 ] . id = Int64 ( packet . from )
fetchedNode [ 0 ] . num = Int64 ( packet . from )
fetchedNode [ 0 ] . lastHeard = Date ( timeIntervalSince1970 : TimeInterval ( Int64 ( positionMessage . time ) ) )
fetchedNode [ 0 ] . snr = packet . rxSnr
fetchedNode [ 0 ] . positions = mutablePositions . copy ( ) as ? NSOrderedSet
2023-03-06 10:33:18 -08:00
2023-01-25 23:01:45 -08:00
do {
try context . save ( )
print ( " 💾 Updated Node Position Coordinates, SNR and Time from Position App Packet For: \( fetchedNode [ 0 ] . num ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Saving NodeInfoEntity from POSITION_APP \( nsError ) " )
}
}
} else {
2023-03-14 12:44:10 -07:00
if ( try ? NodeInfo ( serializedData : packet . decoded . payload ) ) != nil {
upsertNodeInfoPacket ( packet : packet , context : context )
} else {
print ( " 💥 Empty POSITION_APP Packet " )
print ( ( try ? packet . jsonString ( ) ) ? ? " JSON Decode Failure " )
}
2023-01-25 23:01:45 -08:00
}
}
} catch {
print ( " 💥 Error Deserializing POSITION_APP packet. " )
}
}
2023-01-31 22:08:03 -08:00
func upsertBluetoothConfigPacket ( config : Meshtastic . Config . BluetoothConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.bluetooth.config %@ " , comment : " Bluetooth config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 📶 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-23 17:56:04 -08:00
// F o u n d a n o d e , s a v e D e v i c e C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . bluetoothConfig = = nil {
let newBluetoothConfig = BluetoothConfigEntity ( context : context )
2023-01-31 22:08:03 -08:00
newBluetoothConfig . enabled = config . enabled
newBluetoothConfig . mode = Int32 ( config . mode . rawValue )
newBluetoothConfig . fixedPin = Int32 ( config . fixedPin )
2023-01-23 17:56:04 -08:00
fetchedNode [ 0 ] . bluetoothConfig = newBluetoothConfig
} else {
2023-01-31 22:08:03 -08:00
fetchedNode [ 0 ] . bluetoothConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . bluetoothConfig ? . mode = Int32 ( config . mode . rawValue )
fetchedNode [ 0 ] . bluetoothConfig ? . fixedPin = Int32 ( config . fixedPin )
2023-01-23 17:56:04 -08:00
}
do {
try context . save ( )
print ( " 💾 Updated Bluetooth Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data BluetoothConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Bluetooth Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data BluetoothConfigEntity failed: \( nsError ) " )
}
}
2023-01-31 22:08:03 -08:00
func upsertDeviceConfigPacket ( config : Meshtastic . Config . DeviceConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.device.config %@ " , comment : " Device config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 📟 \( logString ) " )
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-23 17:56:04 -08:00
// F o u n d a n o d e , s a v e D e v i c e C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . deviceConfig = = nil {
let newDeviceConfig = DeviceConfigEntity ( context : context )
2023-01-31 22:08:03 -08:00
newDeviceConfig . role = Int32 ( config . role . rawValue )
newDeviceConfig . serialEnabled = config . serialEnabled
newDeviceConfig . debugLogEnabled = config . debugLogEnabled
newDeviceConfig . buttonGpio = Int32 ( config . buttonGpio )
newDeviceConfig . buzzerGpio = Int32 ( config . buzzerGpio )
2023-03-05 23:01:09 -08:00
newDeviceConfig . rebroadcastMode = Int32 ( config . rebroadcastMode . rawValue )
newDeviceConfig . nodeInfoBroadcastSecs = Int32 ( config . nodeInfoBroadcastSecs )
2023-01-23 17:56:04 -08:00
fetchedNode [ 0 ] . deviceConfig = newDeviceConfig
} else {
2023-01-31 22:08:03 -08:00
fetchedNode [ 0 ] . deviceConfig ? . role = Int32 ( config . role . rawValue )
fetchedNode [ 0 ] . deviceConfig ? . serialEnabled = config . serialEnabled
fetchedNode [ 0 ] . deviceConfig ? . debugLogEnabled = config . debugLogEnabled
fetchedNode [ 0 ] . deviceConfig ? . buttonGpio = Int32 ( config . buttonGpio )
fetchedNode [ 0 ] . deviceConfig ? . buzzerGpio = Int32 ( config . buzzerGpio )
2023-03-05 23:01:09 -08:00
fetchedNode [ 0 ] . deviceConfig ? . rebroadcastMode = Int32 ( config . rebroadcastMode . rawValue )
fetchedNode [ 0 ] . deviceConfig ? . nodeInfoBroadcastSecs = Int32 ( config . nodeInfoBroadcastSecs )
2023-01-23 17:56:04 -08:00
}
do {
try context . save ( )
print ( " 💾 Updated Device Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data DeviceConfigEntity: \( nsError ) " )
}
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data DeviceConfigEntity failed: \( nsError ) " )
}
}
2023-01-31 22:08:03 -08:00
func upsertDisplayConfigPacket ( config : Meshtastic . Config . DisplayConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.display.config %@ " , comment : " Display config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🖥️ \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-23 17:56:04 -08:00
// F o u n d a n o d e , s a v e D e v i c e C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
if fetchedNode [ 0 ] . displayConfig = = nil {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let newDisplayConfig = DisplayConfigEntity ( context : context )
2023-01-31 22:08:03 -08:00
newDisplayConfig . gpsFormat = Int32 ( config . gpsFormat . rawValue )
newDisplayConfig . screenOnSeconds = Int32 ( config . screenOnSecs )
newDisplayConfig . screenCarouselInterval = Int32 ( config . autoScreenCarouselSecs )
newDisplayConfig . compassNorthTop = config . compassNorthTop
newDisplayConfig . flipScreen = config . flipScreen
newDisplayConfig . oledType = Int32 ( config . oled . rawValue )
newDisplayConfig . displayMode = Int32 ( config . displaymode . rawValue )
2023-03-05 23:01:09 -08:00
newDisplayConfig . headingBold = config . headingBold
2023-01-23 17:56:04 -08:00
fetchedNode [ 0 ] . displayConfig = newDisplayConfig
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
} else {
2023-03-06 10:33:18 -08:00
2023-01-31 22:08:03 -08:00
fetchedNode [ 0 ] . displayConfig ? . gpsFormat = Int32 ( config . gpsFormat . rawValue )
fetchedNode [ 0 ] . displayConfig ? . screenOnSeconds = Int32 ( config . screenOnSecs )
fetchedNode [ 0 ] . displayConfig ? . screenCarouselInterval = Int32 ( config . autoScreenCarouselSecs )
fetchedNode [ 0 ] . displayConfig ? . compassNorthTop = config . compassNorthTop
fetchedNode [ 0 ] . displayConfig ? . flipScreen = config . flipScreen
fetchedNode [ 0 ] . displayConfig ? . oledType = Int32 ( config . oled . rawValue )
fetchedNode [ 0 ] . displayConfig ? . displayMode = Int32 ( config . displaymode . rawValue )
2023-03-05 23:01:09 -08:00
fetchedNode [ 0 ] . displayConfig ? . headingBold = config . headingBold
2023-01-23 17:56:04 -08:00
}
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
try context . save ( )
print ( " 💾 Updated Display Config for node number: \( String ( nodeNum ) ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
} catch {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
context . rollback ( )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let nsError = error as NSError
print ( " 💥 Error Updating Core Data DisplayConfigEntity: \( nsError ) " )
}
} else {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Display Config " )
}
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
} catch {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let nsError = error as NSError
print ( " 💥 Fetching node for core data DisplayConfigEntity failed: \( nsError ) " )
}
}
2023-01-31 10:50:17 -08:00
func upsertLoRaConfigPacket ( config : Meshtastic . Config . LoRaConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-20 19:14:49 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.lora.config %@ " , comment : " LoRa config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 📻 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-20 19:14:49 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , nodeNum )
do {
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-20 19:14:49 -08:00
// F o u n d a n o d e , s a v e L o R a C o n f i g
2023-01-23 17:56:04 -08:00
if fetchedNode . count > 0 {
2023-01-20 19:14:49 -08:00
if fetchedNode [ 0 ] . loRaConfig = = nil {
2023-01-23 17:56:04 -08:00
// N o l o r a c o n f i g f o r n o d e , s a v e a n e w l o r a c o n f i g
2023-01-20 19:14:49 -08:00
let newLoRaConfig = LoRaConfigEntity ( context : context )
2023-01-31 10:50:17 -08:00
newLoRaConfig . regionCode = Int32 ( config . region . rawValue )
newLoRaConfig . usePreset = config . usePreset
newLoRaConfig . modemPreset = Int32 ( config . modemPreset . rawValue )
newLoRaConfig . bandwidth = Int32 ( config . bandwidth )
newLoRaConfig . spreadFactor = Int32 ( config . spreadFactor )
newLoRaConfig . codingRate = Int32 ( config . codingRate )
newLoRaConfig . frequencyOffset = config . frequencyOffset
2023-02-06 19:06:15 -08:00
newLoRaConfig . overrideFrequency = config . overrideFrequency
newLoRaConfig . overrideDutyCycle = config . overrideDutyCycle
2023-01-31 10:50:17 -08:00
newLoRaConfig . hopLimit = Int32 ( config . hopLimit )
newLoRaConfig . txPower = Int32 ( config . txPower )
newLoRaConfig . txEnabled = config . txEnabled
newLoRaConfig . channelNum = Int32 ( config . channelNum )
2023-03-11 09:26:52 -08:00
newLoRaConfig . sx126xRxBoostedGain = config . sx126XRxBoostedGain
2023-01-20 19:14:49 -08:00
fetchedNode [ 0 ] . loRaConfig = newLoRaConfig
} else {
2023-01-31 10:50:17 -08:00
fetchedNode [ 0 ] . loRaConfig ? . regionCode = Int32 ( config . region . rawValue )
fetchedNode [ 0 ] . loRaConfig ? . usePreset = config . usePreset
fetchedNode [ 0 ] . loRaConfig ? . modemPreset = Int32 ( config . modemPreset . rawValue )
fetchedNode [ 0 ] . loRaConfig ? . bandwidth = Int32 ( config . bandwidth )
fetchedNode [ 0 ] . loRaConfig ? . spreadFactor = Int32 ( config . spreadFactor )
fetchedNode [ 0 ] . loRaConfig ? . codingRate = Int32 ( config . codingRate )
fetchedNode [ 0 ] . loRaConfig ? . frequencyOffset = config . frequencyOffset
2023-02-06 19:06:15 -08:00
fetchedNode [ 0 ] . loRaConfig ? . overrideFrequency = config . overrideFrequency
fetchedNode [ 0 ] . loRaConfig ? . overrideDutyCycle = config . overrideDutyCycle
2023-01-31 10:50:17 -08:00
fetchedNode [ 0 ] . loRaConfig ? . hopLimit = Int32 ( config . hopLimit )
fetchedNode [ 0 ] . loRaConfig ? . txPower = Int32 ( config . txPower )
fetchedNode [ 0 ] . loRaConfig ? . txEnabled = config . txEnabled
fetchedNode [ 0 ] . loRaConfig ? . channelNum = Int32 ( config . channelNum )
2023-03-11 09:26:52 -08:00
fetchedNode [ 0 ] . loRaConfig ? . sx126xRxBoostedGain = config . sx126XRxBoostedGain
2023-01-20 19:14:49 -08:00
}
do {
try context . save ( )
2023-01-31 10:50:17 -08:00
context . refresh ( fetchedNode [ 0 ] , mergeChanges : true )
2023-01-20 19:14:49 -08:00
print ( " 💾 Updated LoRa Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data LoRaConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Lora Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data LoRaConfigEntity failed: \( nsError ) " )
}
}
2023-01-23 17:56:04 -08:00
2023-01-31 22:20:16 -08:00
func upsertNetworkConfigPacket ( config : Meshtastic . Config . NetworkConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.network.config %@ " , comment : " Network config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🌐 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-23 17:56:04 -08:00
// F o u n d a n o d e , s a v e W i F i C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . networkConfig = = nil {
let newNetworkConfig = NetworkConfigEntity ( context : context )
2023-01-31 22:20:16 -08:00
newNetworkConfig . wifiEnabled = config . wifiEnabled
newNetworkConfig . wifiSsid = config . wifiSsid
newNetworkConfig . wifiPsk = config . wifiPsk
newNetworkConfig . ethEnabled = config . ethEnabled
2023-01-23 17:56:04 -08:00
fetchedNode [ 0 ] . networkConfig = newNetworkConfig
} else {
2023-01-31 22:20:16 -08:00
fetchedNode [ 0 ] . networkConfig ? . ethEnabled = config . ethEnabled
fetchedNode [ 0 ] . networkConfig ? . wifiEnabled = config . wifiEnabled
fetchedNode [ 0 ] . networkConfig ? . wifiSsid = config . wifiSsid
fetchedNode [ 0 ] . networkConfig ? . wifiPsk = config . wifiPsk
2023-01-23 17:56:04 -08:00
}
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
try context . save ( )
print ( " 💾 Updated Network Config for node number: \( String ( nodeNum ) ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data WiFiConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Network Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data NetworkConfigEntity failed: \( nsError ) " )
}
}
2023-01-31 22:20:16 -08:00
func upsertPositionConfigPacket ( config : Meshtastic . Config . PositionConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.position.config %@ " , comment : " Positon config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🗺️ \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-01-23 17:56:04 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-01-23 17:56:04 -08:00
// F o u n d a n o d e , s a v e L o R a C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . positionConfig = = nil {
let newPositionConfig = PositionConfigEntity ( context : context )
2023-01-31 22:20:16 -08:00
newPositionConfig . smartPositionEnabled = config . positionBroadcastSmartEnabled
newPositionConfig . deviceGpsEnabled = config . gpsEnabled
2023-03-27 10:43:01 -07:00
newPositionConfig . rxGpio = Int32 ( config . rxGpio )
newPositionConfig . txGpio = Int32 ( config . txGpio )
2023-01-31 22:20:16 -08:00
newPositionConfig . fixedPosition = config . fixedPosition
newPositionConfig . gpsUpdateInterval = Int32 ( config . gpsUpdateInterval )
newPositionConfig . gpsAttemptTime = Int32 ( config . gpsAttemptTime )
newPositionConfig . positionBroadcastSeconds = Int32 ( config . positionBroadcastSecs )
2023-03-27 10:43:01 -07:00
newPositionConfig . broadcastSmartMinimumIntervalSecs = Int32 ( config . broadcastSmartMinimumIntervalSecs )
newPositionConfig . broadcastSmartMinimumDistance = Int32 ( config . broadcastSmartMinimumDistance )
2023-01-31 22:20:16 -08:00
newPositionConfig . positionFlags = Int32 ( config . positionFlags )
2023-01-23 17:56:04 -08:00
fetchedNode [ 0 ] . positionConfig = newPositionConfig
} else {
2023-01-31 22:20:16 -08:00
fetchedNode [ 0 ] . positionConfig ? . smartPositionEnabled = config . positionBroadcastSmartEnabled
fetchedNode [ 0 ] . positionConfig ? . deviceGpsEnabled = config . gpsEnabled
2023-03-27 10:43:01 -07:00
fetchedNode [ 0 ] . positionConfig ? . rxGpio = Int32 ( config . rxGpio )
fetchedNode [ 0 ] . positionConfig ? . txGpio = Int32 ( config . txGpio )
2023-01-31 22:20:16 -08:00
fetchedNode [ 0 ] . positionConfig ? . fixedPosition = config . fixedPosition
fetchedNode [ 0 ] . positionConfig ? . gpsUpdateInterval = Int32 ( config . gpsUpdateInterval )
fetchedNode [ 0 ] . positionConfig ? . gpsAttemptTime = Int32 ( config . gpsAttemptTime )
fetchedNode [ 0 ] . positionConfig ? . positionBroadcastSeconds = Int32 ( config . positionBroadcastSecs )
2023-03-27 10:43:01 -07:00
fetchedNode [ 0 ] . positionConfig ? . broadcastSmartMinimumIntervalSecs = Int32 ( config . broadcastSmartMinimumIntervalSecs )
fetchedNode [ 0 ] . positionConfig ? . broadcastSmartMinimumDistance = Int32 ( config . broadcastSmartMinimumDistance )
2023-01-31 22:20:16 -08:00
fetchedNode [ 0 ] . positionConfig ? . positionFlags = Int32 ( config . positionFlags )
2023-01-23 17:56:04 -08:00
}
do {
try context . save ( )
print ( " 💾 Updated Position Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data PositionConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Position Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data PositionConfigEntity failed: \( nsError ) " )
}
}
2023-02-01 09:19:45 -08:00
func upsertCannedMessagesModuleConfigPacket ( config : Meshtastic . ModuleConfig . CannedMessageConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.cannedmessage.config %@ " , comment : " Canned Message module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🥫 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e C a n n e d M e s s a g e C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
if fetchedNode [ 0 ] . cannedMessageConfig = = nil {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let newCannedMessageConfig = CannedMessageConfigEntity ( context : context )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
newCannedMessageConfig . enabled = config . enabled
newCannedMessageConfig . sendBell = config . sendBell
newCannedMessageConfig . rotary1Enabled = config . rotary1Enabled
newCannedMessageConfig . updown1Enabled = config . updown1Enabled
newCannedMessageConfig . inputbrokerPinA = Int32 ( config . inputbrokerPinA )
newCannedMessageConfig . inputbrokerPinB = Int32 ( config . inputbrokerPinB )
newCannedMessageConfig . inputbrokerPinPress = Int32 ( config . inputbrokerPinPress )
newCannedMessageConfig . inputbrokerEventCw = Int32 ( config . inputbrokerEventCw . rawValue )
newCannedMessageConfig . inputbrokerEventCcw = Int32 ( config . inputbrokerEventCcw . rawValue )
newCannedMessageConfig . inputbrokerEventPress = Int32 ( config . inputbrokerEventPress . rawValue )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
fetchedNode [ 0 ] . cannedMessageConfig = newCannedMessageConfig
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
fetchedNode [ 0 ] . cannedMessageConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . cannedMessageConfig ? . sendBell = config . sendBell
fetchedNode [ 0 ] . cannedMessageConfig ? . rotary1Enabled = config . rotary1Enabled
fetchedNode [ 0 ] . cannedMessageConfig ? . updown1Enabled = config . updown1Enabled
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerPinA = Int32 ( config . inputbrokerPinA )
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerPinB = Int32 ( config . inputbrokerPinB )
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerPinPress = Int32 ( config . inputbrokerPinPress )
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerEventCw = Int32 ( config . inputbrokerEventCw . rawValue )
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerEventCcw = Int32 ( config . inputbrokerEventCcw . rawValue )
fetchedNode [ 0 ] . cannedMessageConfig ? . inputbrokerEventPress = Int32 ( config . inputbrokerEventPress . rawValue )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
try context . save ( )
print ( " 💾 Updated Canned Message Module Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data CannedMessageConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Canned Message Module Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data CannedMessageConfigEntity failed: \( nsError ) " )
}
}
func upsertExternalNotificationModuleConfigPacket ( config : Meshtastic . ModuleConfig . ExternalNotificationConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.externalnotification.config %@ " , comment : " External Notifiation module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 📣 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e E x t e r n a l N o t i f i c a i t o n e C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
if fetchedNode [ 0 ] . externalNotificationConfig = = nil {
let newExternalNotificationConfig = ExternalNotificationConfigEntity ( context : context )
newExternalNotificationConfig . enabled = config . enabled
newExternalNotificationConfig . usePWM = config . usePwm
newExternalNotificationConfig . alertBell = config . alertBell
newExternalNotificationConfig . alertBellBuzzer = config . alertBellBuzzer
newExternalNotificationConfig . alertBellVibra = config . alertBellVibra
newExternalNotificationConfig . alertMessage = config . alertMessage
newExternalNotificationConfig . alertMessageBuzzer = config . alertMessageBuzzer
newExternalNotificationConfig . alertMessageVibra = config . alertMessageVibra
newExternalNotificationConfig . active = config . active
newExternalNotificationConfig . output = Int32 ( config . output )
newExternalNotificationConfig . outputBuzzer = Int32 ( config . outputBuzzer )
newExternalNotificationConfig . outputVibra = Int32 ( config . outputVibra )
newExternalNotificationConfig . outputMilliseconds = Int32 ( config . outputMs )
newExternalNotificationConfig . nagTimeout = Int32 ( config . nagTimeout )
fetchedNode [ 0 ] . externalNotificationConfig = newExternalNotificationConfig
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
fetchedNode [ 0 ] . externalNotificationConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . externalNotificationConfig ? . usePWM = config . usePwm
fetchedNode [ 0 ] . externalNotificationConfig ? . alertBell = config . alertBell
fetchedNode [ 0 ] . externalNotificationConfig ? . alertBellBuzzer = config . alertBellBuzzer
fetchedNode [ 0 ] . externalNotificationConfig ? . alertBellVibra = config . alertBellVibra
fetchedNode [ 0 ] . externalNotificationConfig ? . alertMessage = config . alertMessage
fetchedNode [ 0 ] . externalNotificationConfig ? . alertMessageBuzzer = config . alertMessageBuzzer
fetchedNode [ 0 ] . externalNotificationConfig ? . alertMessageVibra = config . alertMessageVibra
fetchedNode [ 0 ] . externalNotificationConfig ? . active = config . active
fetchedNode [ 0 ] . externalNotificationConfig ? . output = Int32 ( config . output )
fetchedNode [ 0 ] . externalNotificationConfig ? . outputBuzzer = Int32 ( config . outputBuzzer )
fetchedNode [ 0 ] . externalNotificationConfig ? . outputVibra = Int32 ( config . outputVibra )
fetchedNode [ 0 ] . externalNotificationConfig ? . outputMilliseconds = Int32 ( config . outputMs )
fetchedNode [ 0 ] . externalNotificationConfig ? . nagTimeout = Int32 ( config . nagTimeout )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
try context . save ( )
print ( " 💾 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 ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save External Notifiation Module Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data ExternalNotificationConfigEntity failed: \( nsError ) " )
}
}
2023-03-25 22:14:39 -07:00
func upsertRtttlConfigPacket ( ringtone : String , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-25 14:30:18 -07:00
2023-03-26 09:46:51 -07:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.ringtone.config %@ " , comment : " RTTTL Ringtone config received: %@ " ) , String ( nodeNum ) )
2023-03-25 14:30:18 -07:00
MeshLogger . log ( " ⛰️ \( logString ) " )
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
do {
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
// F o u n d a n o d e , s a v e R T T T L C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . rtttlConfig = = nil {
let newRtttlConfig = RTTTLConfigEntity ( context : context )
2023-03-25 22:14:39 -07:00
newRtttlConfig . ringtone = ringtone
2023-03-25 14:30:18 -07:00
fetchedNode [ 0 ] . rtttlConfig = newRtttlConfig
} else {
2023-03-25 22:14:39 -07:00
fetchedNode [ 0 ] . rtttlConfig ? . ringtone = ringtone
2023-03-25 14:30:18 -07:00
}
do {
try context . save ( )
print ( " 💾 Updated RTTTL Ringtone Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data RtttlConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save RTTTL Ringtone Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data RtttlConfigEntity failed: \( nsError ) " )
}
}
2023-02-01 09:19:45 -08:00
func upsertMqttModuleConfigPacket ( config : Meshtastic . ModuleConfig . MQTTConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.mqtt.config %@ " , comment : " MQTT module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🌉 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e M Q T T C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
if fetchedNode [ 0 ] . mqttConfig = = nil {
let newMQTTConfig = MQTTConfigEntity ( context : context )
newMQTTConfig . enabled = config . enabled
newMQTTConfig . address = config . address
2023-03-24 10:35:54 -07:00
newMQTTConfig . username = config . username
2023-02-01 09:19:45 -08:00
newMQTTConfig . password = config . password
newMQTTConfig . encryptionEnabled = config . encryptionEnabled
newMQTTConfig . jsonEnabled = config . jsonEnabled
fetchedNode [ 0 ] . mqttConfig = newMQTTConfig
} else {
fetchedNode [ 0 ] . mqttConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . mqttConfig ? . address = config . address
2023-03-24 10:35:54 -07:00
fetchedNode [ 0 ] . mqttConfig ? . username = config . username
2023-02-01 09:19:45 -08:00
fetchedNode [ 0 ] . mqttConfig ? . password = config . password
fetchedNode [ 0 ] . mqttConfig ? . encryptionEnabled = config . encryptionEnabled
fetchedNode [ 0 ] . mqttConfig ? . jsonEnabled = config . jsonEnabled
}
do {
try context . save ( )
print ( " 💾 Updated MQTT Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data MQTTConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save MQTT Module Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data MQTTConfigEntity failed: \( nsError ) " )
}
}
func upsertRangeTestModuleConfigPacket ( config : Meshtastic . ModuleConfig . RangeTestConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.rangetest.config %@ " , comment : " Range Test module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " ⛰️ \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e D e v i c e C o n f i g
if ! fetchedNode . isEmpty {
if fetchedNode [ 0 ] . rangeTestConfig = = nil {
let newRangeTestConfig = RangeTestConfigEntity ( context : context )
newRangeTestConfig . sender = Int32 ( config . sender )
newRangeTestConfig . enabled = config . enabled
newRangeTestConfig . save = config . save
fetchedNode [ 0 ] . rangeTestConfig = newRangeTestConfig
} else {
fetchedNode [ 0 ] . rangeTestConfig ? . sender = Int32 ( config . sender )
fetchedNode [ 0 ] . rangeTestConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . rangeTestConfig ? . save = config . save
}
do {
try context . save ( )
print ( " 💾 Updated Range Test Config for node number: \( String ( nodeNum ) ) " )
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data RangeTestConfigEntity: \( nsError ) " )
}
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Range Test Module Config " )
}
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data RangeTestConfigEntity failed: \( nsError ) " )
}
}
func upsertSerialModuleConfigPacket ( config : Meshtastic . ModuleConfig . SerialConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.serial.config %@ " , comment : " Serial module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 🤖 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e D e v i c e C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
if fetchedNode [ 0 ] . serialConfig = = nil {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let newSerialConfig = SerialConfigEntity ( context : context )
newSerialConfig . enabled = config . enabled
newSerialConfig . echo = config . echo
newSerialConfig . rxd = Int32 ( config . rxd )
newSerialConfig . txd = Int32 ( config . txd )
newSerialConfig . baudRate = Int32 ( config . baud . rawValue )
newSerialConfig . timeout = Int32 ( config . timeout )
newSerialConfig . mode = Int32 ( config . mode . rawValue )
fetchedNode [ 0 ] . serialConfig = newSerialConfig
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
fetchedNode [ 0 ] . serialConfig ? . enabled = config . enabled
fetchedNode [ 0 ] . serialConfig ? . echo = config . echo
fetchedNode [ 0 ] . serialConfig ? . rxd = Int32 ( config . rxd )
fetchedNode [ 0 ] . serialConfig ? . txd = Int32 ( config . txd )
fetchedNode [ 0 ] . serialConfig ? . baudRate = Int32 ( config . baud . rawValue )
fetchedNode [ 0 ] . serialConfig ? . timeout = Int32 ( config . timeout )
fetchedNode [ 0 ] . serialConfig ? . mode = Int32 ( config . mode . rawValue )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
try context . save ( )
print ( " 💾 Updated Serial Module Config for node number: \( String ( nodeNum ) ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} catch {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
context . rollback ( )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let nsError = error as NSError
print ( " 💥 Error Updating Core Data SerialConfigEntity: \( nsError ) " )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Serial Module Config " )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} catch {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let nsError = error as NSError
print ( " 💥 Fetching node for core data SerialConfigEntity failed: \( nsError ) " )
}
}
func upsertTelemetryModuleConfigPacket ( config : Meshtastic . ModuleConfig . TelemetryConfig , nodeNum : Int64 , context : NSManagedObjectContext ) {
let logString = String . localizedStringWithFormat ( NSLocalizedString ( " mesh.log.telemetry.config %@ " , comment : " Telemetry module config received: %@ " ) , String ( nodeNum ) )
MeshLogger . log ( " 📈 \( logString ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let fetchNodeInfoRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest . init ( entityName : " NodeInfoEntity " )
fetchNodeInfoRequest . predicate = NSPredicate ( format : " num == %lld " , Int64 ( nodeNum ) )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
2023-03-06 10:33:18 -08:00
2023-03-06 15:30:10 -08:00
guard let fetchedNode = try context . fetch ( fetchNodeInfoRequest ) as ? [ NodeInfoEntity ] else {
return
}
2023-02-01 09:19:45 -08:00
// F o u n d a n o d e , s a v e T e l e m e t r y C o n f i g
if ! fetchedNode . isEmpty {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
if fetchedNode [ 0 ] . telemetryConfig = = nil {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
let newTelemetryConfig = TelemetryConfigEntity ( context : context )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
newTelemetryConfig . deviceUpdateInterval = Int32 ( config . deviceUpdateInterval )
newTelemetryConfig . environmentUpdateInterval = Int32 ( config . environmentUpdateInterval )
newTelemetryConfig . environmentMeasurementEnabled = config . environmentMeasurementEnabled
newTelemetryConfig . environmentScreenEnabled = config . environmentScreenEnabled
newTelemetryConfig . environmentDisplayFahrenheit = config . environmentDisplayFahrenheit
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
fetchedNode [ 0 ] . telemetryConfig = newTelemetryConfig
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
fetchedNode [ 0 ] . telemetryConfig ? . deviceUpdateInterval = Int32 ( config . deviceUpdateInterval )
fetchedNode [ 0 ] . telemetryConfig ? . environmentUpdateInterval = Int32 ( config . environmentUpdateInterval )
fetchedNode [ 0 ] . telemetryConfig ? . environmentMeasurementEnabled = config . environmentMeasurementEnabled
fetchedNode [ 0 ] . telemetryConfig ? . environmentScreenEnabled = config . environmentScreenEnabled
fetchedNode [ 0 ] . telemetryConfig ? . environmentDisplayFahrenheit = config . environmentDisplayFahrenheit
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
do {
try context . save ( )
print ( " 💾 Updated Telemetry Module Config for node number: \( String ( nodeNum ) ) " )
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} catch {
context . rollback ( )
let nsError = error as NSError
print ( " 💥 Error Updating Core Data TelemetryConfigEntity: \( nsError ) " )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} else {
print ( " 💥 No Nodes found in local database matching node number \( nodeNum ) unable to save Telemetry Module Config " )
}
2023-03-06 10:33:18 -08:00
2023-02-01 09:19:45 -08:00
} catch {
let nsError = error as NSError
print ( " 💥 Fetching node for core data TelemetryConfigEntity failed: \( nsError ) " )
}
}