Fixed position updates

This commit is contained in:
Garth Vander Houwen 2024-03-27 16:06:24 -07:00
parent 1c9d9b65b5
commit 7c022813a2
5 changed files with 73 additions and 53 deletions

View file

@ -57,6 +57,7 @@ enum GpsUpdateIntervals: Int, CaseIterable, Identifiable {
case thirtySeconds = 30
case oneMinute = 60
case twoMinutes = 120
case fiveMinutes = 300
case tenMinutes = 600
case fifteenMinutes = 900
@ -74,6 +75,8 @@ enum GpsUpdateIntervals: Int, CaseIterable, Identifiable {
return "interval.thirty.seconds".localized
case .oneMinute:
return "interval.one.minute".localized
case .twoMinutes:
return "interval.two.minutes".localized
case .fiveMinutes:
return "interval.five.minutes".localized
case .tenMinutes:

View file

@ -20,8 +20,8 @@ extension PositionEntity {
request.includesSubentities = true
request.returnsDistinctResults = true
request.sortDescriptors = [NSSortDescriptor(key: "time", ascending: false)]
let positionPredicate = NSPredicate(format: "nodePosition != nil && (nodePosition.user.shortName != nil || nodePosition.user.shortName != '') && latest == true && time >= %@", Calendar.current.date(byAdding: .day, value: -2, to: Date())! as NSDate)
/// && time >= %@
let positionPredicate = NSPredicate(format: "nodePosition != nil && (nodePosition.user.shortName != nil || nodePosition.user.shortName != '') && latest == true", Calendar.current.date(byAdding: .day, value: -2, to: Date())! as NSDate)
let pointOfInterest = LocationHelper.currentLocation

View file

@ -36,7 +36,7 @@ class MqttClientProxyManager {
defaultServerPort = Int(fullHost.components(separatedBy: ":")[1]) ?? (useSsl ? 8883 : 1883)
}
}
let minimumVersion = "2.3.3"
let minimumVersion = "2.3.2"
let currentVersion = UserDefaults.firmwareVersion
let supportedVersion = minimumVersion.compare(currentVersion, options: .numeric) == .orderedAscending || minimumVersion.compare(currentVersion, options: .numeric) == .orderedSame

View file

@ -58,10 +58,8 @@ struct MeshMap: View {
.mapControlVisibility(.automatic)
}
.controlSize(.regular)
.onTapGesture(count: 1, perform: { position in
print(position)
newWaypointCoord = reader.convert(position, from: .local) ?? CLLocationCoordinate2D.init()
newWaypointCoord = reader.convert(position, from: .local) ?? CLLocationCoordinate2D.init()
})
.gesture(
LongPressGesture(minimumDuration: 0.5)

View file

@ -73,10 +73,10 @@ struct PositionConfig: View {
@State var includeHeading = false
/// Minimum Version for fixed postion admin messages
@State var minimumVersion = "2.3.2"
@State var minimumVersion = "2.3.3"
@State private var supportedVersion = true
@State private var showingSetFixedAlert = false
@State private var showingRemoveFixedAlert = false
//@State private var showingRemoveFixedAlert = false
var body: some View {
VStack {
@ -158,49 +158,13 @@ struct PositionConfig: View {
.foregroundColor(.gray)
.font(.callout)
}
} else {
VStack(alignment: .leading) {
Toggle(isOn: $fixedPosition) {
Label("Fixed Position", systemImage: "location.square.fill")
Text("If enabled your current phone location will be sent to the device and will broadcast over the mesh on the position interval.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
.onChange(of: fixedPosition) { newFixed in
if node != nil && node!.positionConfig != nil {
/// Fixed Position is off to start
if !node!.positionConfig!.fixedPosition && newFixed && supportedVersion {
showingSetFixedAlert = true
} else if node!.positionConfig!.fixedPosition && newFixed && supportedVersion {
/// Fixed Position is on to start
showingRemoveFixedAlert = true
}
}
}
.alert(isPresented: $showingSetFixedAlert) {
Alert(
title: Text("Set Fixed Position"),
message: Text("This will send a current position from your phone and enable fixed position."),
primaryButton: .default(Text("Set")) {
print("Set a fixed position here")
},
secondaryButton: .cancel(Text("Cancel")) {
fixedPosition = false
}
)
}
.alert(isPresented: $showingRemoveFixedAlert) {
Alert(
title: Text("Remove Fixed Position"),
message: Text("This will disable fixed position and remove the currently set position."),
primaryButton: .destructive(Text("Remove")) {
print("Remove a fixed position here")
},
secondaryButton: .cancel(Text("Cancel")) {
fixedPosition = true
}
)
}
VStack(alignment: .leading) {
Toggle(isOn: $fixedPosition) {
Label("Fixed Position", systemImage: "location.square.fill")
Text("If enabled your current phone location will be sent to the device and will broadcast over the mesh on the position interval.")
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
}
}
Section(header: Text("Position Flags")) {
@ -304,9 +268,49 @@ struct PositionConfig: View {
}
}
.disabled(self.bleManager.connectedPeripheral == nil || node?.positionConfig == nil)
.alert(node?.positionConfig?.fixedPosition ?? false ? "Remove Fixed Position" : "Set Fixed Position", isPresented: $showingSetFixedAlert) {
Button("Cancel", role: .cancel) {
fixedPosition = !fixedPosition
}
if node?.positionConfig?.fixedPosition ?? false {
Button("Remove", role: .destructive) {
if !bleManager.removeFixedPosition(fromUser: node!.user!, channel: 0) {
print("Set Position Failed")
}
print("Remove a fixed position here")
node?.positionConfig?.fixedPosition = false
do {
try context.save()
print("💾 Updated Position Config with Fixed Position = false")
} catch {
context.rollback()
let nsError = error as NSError
print("💥 Error Saving Position Config Entity \(nsError)")
}
}
} else {
Button("Set") {
if !bleManager.setFixedPosition(fromUser: node!.user!, channel: 0) {
print("Set Position Failed")
}
print("Set a fixed position")
node?.positionConfig?.fixedPosition = true
do {
try context.save()
print("💾 Updated Position Config with Fixed Position = true")
} catch {
context.rollback()
let nsError = error as NSError
print("💥 Error Saving Position Config Entity \(nsError)")
}
}
}
} message: {
Text(node?.positionConfig?.fixedPosition ?? false ? "This will disable fixed position and remove the currently set position." : "This will send a current position from your phone and enable fixed position.")
}
SaveConfigButton(node: node, hasChanges: $hasChanges) {
if fixedPosition {
if fixedPosition && !supportedVersion {
_ = bleManager.sendPosition(channel: 0, destNum: node?.num ?? 0, wantResponse: true)
}
let connectedNode = getNodeInfo(id: bleManager.connectedPeripheral.num, context: context)
@ -358,8 +362,6 @@ struct PositionConfig: View {
}
setPositionValues()
supportedVersion = bleManager.connectedVersion == "0.0.0" || self.minimumVersion.compare(bleManager.connectedVersion, options: .numeric) == .orderedAscending || minimumVersion.compare(bleManager.connectedVersion, options: .numeric) == .orderedSame
// Need to request a PositionConfig from the remote node before allowing changes
if bleManager.connectedPeripheral != nil && node?.positionConfig == nil {
print("empty position config")
@ -369,6 +371,23 @@ struct PositionConfig: View {
}
}
}
.onChange(of: fixedPosition) { newFixed in
print("Changing Fixed Position Value")
if supportedVersion {
if node != nil && node!.positionConfig != nil {
print("We have a node and position config")
print("We have turned on fixed position \(!node!.positionConfig!.fixedPosition && newFixed)")
/// Fixed Position is off to start
if !node!.positionConfig!.fixedPosition && newFixed {
print("fire alert")
showingSetFixedAlert = true
} else if node!.positionConfig!.fixedPosition && !newFixed {
/// Fixed Position is on to start
showingSetFixedAlert = true
}
}
}
}
.onChange(of: deviceGpsEnabled) { newDeviceGps in
if node != nil && node!.positionConfig != nil {
if newDeviceGps != node!.positionConfig!.deviceGpsEnabled { hasChanges = true }