diff --git a/Meshtastic Client.xcodeproj/xcuserdata/garthvanderhouwen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Meshtastic Client.xcodeproj/xcuserdata/garthvanderhouwen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index ddf802b4..03791069 100644 --- a/Meshtastic Client.xcodeproj/xcuserdata/garthvanderhouwen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Meshtastic Client.xcodeproj/xcuserdata/garthvanderhouwen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -83,15 +83,15 @@ @@ -99,15 +99,63 @@ + + + + + + + + + + + + diff --git a/MeshtasticClient/Helpers/BLEManager.swift b/MeshtasticClient/Helpers/BLEManager.swift index 89560992..bd25e63f 100644 --- a/MeshtasticClient/Helpers/BLEManager.swift +++ b/MeshtasticClient/Helpers/BLEManager.swift @@ -489,11 +489,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph newNode.user = newUser } - let position = PositionEntity(context: context!) position.latitudeI = decodedInfo.nodeInfo.position.latitudeI position.longitudeI = decodedInfo.nodeInfo.position.longitudeI position.altitude = decodedInfo.nodeInfo.position.altitude + position.batteryLevel = decodedInfo.nodeInfo.position.batteryLevel position.time = Date(timeIntervalSince1970: TimeInterval(Int64(decodedInfo.nodeInfo.position.time))) @@ -541,8 +541,17 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph let mutablePositions = fetchedNode[0].positions!.mutableCopy() as! NSMutableOrderedSet mutablePositions.add(position) - fetchedNode[0].positions = mutablePositions.copy() as? NSOrderedSet - + + if position.coordinate == nil { + var newPostions = [PositionEntity]() + newPostions.append(position) + fetchedNode[0].positions? = NSOrderedSet(array : newPostions) + + } else { + + fetchedNode[0].positions = mutablePositions.copy() as? NSOrderedSet + } + // Look for a MyInfo let fetchMyInfoRequest:NSFetchRequest = NSFetchRequest.init(entityName: "MyInfoEntity") fetchMyInfoRequest.predicate = NSPredicate(format: "myNodeNum == %lld", Int64(decodedInfo.nodeInfo.num)) @@ -680,6 +689,19 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph fetchedNode[0].num = Int64(decodedInfo.packet.from) fetchedNode[0].lastHeard = Date(timeIntervalSince1970: TimeInterval(Int64(decodedInfo.packet.rxTime))) fetchedNode[0].snr = decodedInfo.packet.rxSnr + + let array = [UInt8](decodedInfo.packet.decoded.payload) + print (array[0]) + print (array[1]) + print (array[2]) + print (array[3]) + print (array[4]) + print (array) + + let bytes = decodedInfo.packet.decoded.payload + print(bytes, String(bytes: bytes, encoding: .utf8)) + + print(bytes2String(array)) } else { return @@ -715,9 +737,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph do { - let fetchedNode = try context?.fetch(fetchNodePositionRequest) as! [NodeInfoEntity] - + + let fetchedNode = try context?.fetch(fetchNodePositionRequest) as! [NodeInfoEntity] + + // Never run if fetchedNode.count == 1 { fetchedNode[0].id = Int64(decodedInfo.packet.from) fetchedNode[0].num = Int64(decodedInfo.packet.from) @@ -733,6 +757,23 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph } fetchedNode[0].snr = decodedInfo.packet.rxSnr + let array = [UInt8](decodedInfo.packet.decoded.payload) + //print(array) + //let payload = decodedInfo.packet.decoded.payload as NSData + let bytes = decodedInfo.packet.decoded.payload + print(bytes, String(bytes: bytes, encoding: .utf8)) + + // Get a string from the byte array. + if let result = String(bytes: bytes, encoding: .utf8) { + print(result) + } + + let testBytes : [UInt8] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64] + + + var test = bytes2String(testBytes) + print(test) + } else { return @@ -892,3 +933,8 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph return success } } + + +func bytes2String(_ array: [UInt8]) -> String { + return String(data: Data(bytes: array, count: array.count), encoding: .utf8) ?? "" +} diff --git a/MeshtasticClient/Persistence/PositionEntityExtension.swift b/MeshtasticClient/Persistence/PositionEntityExtension.swift index b842f82c..250241c1 100644 --- a/MeshtasticClient/Persistence/PositionEntityExtension.swift +++ b/MeshtasticClient/Persistence/PositionEntityExtension.swift @@ -24,7 +24,7 @@ extension PositionEntity { } var coordinate: CLLocationCoordinate2D? { - if latitude != nil && longitude != nil { + if latitudeI != 0 && longitudeI != 0 { let coord = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!) return coord diff --git a/MeshtasticClient/Views/Nodes/NodeDetail.swift b/MeshtasticClient/Views/Nodes/NodeDetail.swift index b8f24f78..d491a887 100644 --- a/MeshtasticClient/Views/Nodes/NodeDetail.swift +++ b/MeshtasticClient/Views/Nodes/NodeDetail.swift @@ -20,30 +20,38 @@ struct NodeDetail: View { VStack { - if ((node.myInfo?.hasGps) != nil) { + if node.positions?.count ?? 0 > 0 { let mostRecent = node.positions?.lastObject as! PositionEntity - - let nodeCoordinatePosition = CLLocationCoordinate2D(latitude: mostRecent.latitude!, longitude: mostRecent.longitude!) - - let regionBinding = Binding( - get: { - MKCoordinateRegion(center: nodeCoordinatePosition, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)) - }, - set: { _ in } - ) - let annotations = [MapLocation(name: node.user!.shortName ?? "???", coordinate: mostRecent.coordinate!)] - - Map(coordinateRegion: regionBinding, showsUserLocation: true, userTrackingMode: .none, annotationItems: annotations) { location in - MapAnnotation( - coordinate: location.coordinate, - content: { - CircleText(text: node.user!.shortName ?? "???", color: .accentColor) - } - ) - } - .frame(idealWidth: bounds.size.width, maxHeight: bounds.size.height / 3) + if mostRecent.coordinate != nil { + + let nodeCoordinatePosition = CLLocationCoordinate2D(latitude: mostRecent.latitude!, longitude: mostRecent.longitude!) + + let regionBinding = Binding( + get: { + MKCoordinateRegion(center: nodeCoordinatePosition, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)) + }, + set: { _ in } + ) + let annotations = [MapLocation(name: node.user!.shortName ?? "???", coordinate: mostRecent.coordinate!)] + + Map(coordinateRegion: regionBinding, showsUserLocation: true, userTrackingMode: .none, annotationItems: annotations) { location in + MapAnnotation( + coordinate: location.coordinate, + content: { + CircleText(text: node.user!.shortName ?? "???", color: .accentColor) + } + ) + } + .frame(idealWidth: bounds.size.width, maxHeight: bounds.size.height / 3) + } else { + + Image(node.user?.hwModel ?? "UNSET") + .resizable() + .aspectRatio(contentMode: .fit) + .frame(width: bounds.size.width, height: bounds.size.height / 2) + } } else { Image(node.user?.hwModel ?? "UNSET") @@ -167,7 +175,7 @@ struct NodeDetail: View { } }.padding() - if ((node.myInfo?.hasGps) != nil) { + if node.positions?.count ?? 0 > 0 { Divider() @@ -177,7 +185,7 @@ struct NodeDetail: View { .font(.title) .foregroundColor(.accentColor) .symbolRenderingMode(.hierarchical) - Text("Position History (\(node.positions?.count ?? 0) Points)").font(.title2) + Text("Location History").font(.title2) } .padding() @@ -185,7 +193,9 @@ struct NodeDetail: View { ForEach(node.positions!.array as! [PositionEntity], id: \.self) { (mappin: PositionEntity) in - VStack { + //if mappin.coordinate != nil { + + VStack { HStack { @@ -222,8 +232,9 @@ struct NodeDetail: View { .symbolRenderingMode(.hierarchical) } } - .padding(1) - Divider() + .padding(1) + Divider() + //} } .padding(.bottom, 5) // Without some padding here there is a transparent contentview bug } diff --git a/MeshtasticClient/Views/Nodes/NodeMap.swift b/MeshtasticClient/Views/Nodes/NodeMap.swift index 58671740..c3aee386 100644 --- a/MeshtasticClient/Views/Nodes/NodeMap.swift +++ b/MeshtasticClient/Views/Nodes/NodeMap.swift @@ -9,25 +9,21 @@ import SwiftUI import MapKit import CoreLocation +import CoreData struct NodeMap: View { - // CoreData @Environment(\.managedObjectContext) var context @EnvironmentObject var bleManager: BLEManager - @FetchRequest( - sortDescriptors: [NSSortDescriptor(keyPath: \NodeInfoEntity.lastHeard, ascending: false)], - animation: .default) + @State private var showLabels: Bool = false + + @State private var annotationItems: [MapLocation] = [] + @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \NodeInfoEntity.lastHeard, ascending: false)], animation: .default) private var locationNodes: FetchedResults - struct MapLocation: Identifiable { - - let id = UUID() - let name: String - let coordinate: CLLocationCoordinate2D - } + var annotations: [MapLocation] = [MapLocation]() var body: some View { @@ -39,7 +35,7 @@ struct NodeMap: View { }, set: { _ in } ) - + NavigationView { ZStack { @@ -47,17 +43,19 @@ struct NodeMap: View { Map(coordinateRegion: regionBinding, interactionModes: [.all], showsUserLocation: true, - userTrackingMode: .constant(.follow))//, annotationItems: $locationNodes) { node in -// -// MapAnnotation( -// coordinate: node.positions[0].coordinate, -// content: { -// CircleText(text: node.user!.shortName, color: .accentColor) -// } -// ) -// } + userTrackingMode: .constant(.follow)//, + //ForEach ( locationNodes ) { node in + // let mostRecent = node.positions?.lastObject as! PositionEntity + // if mostRecent.coordinate != nil { + + // annotations.append(MapLocation(name: node.user?.shortName! ?? "???", coordinate: mostRecent.coordinate!)) + + // } + // } + ) + //} .frame(maxHeight: .infinity) - .ignoresSafeArea(.all, edges: [.leading, .trailing]) + .ignoresSafeArea(.all, edges: [.leading, .trailing]) } .navigationTitle("Mesh Map") .navigationBarTitleDisplayMode(.inline)