Skip to content

Commit

Permalink
Fixed wrong timestamp when latest is outside accuracy range
Browse files Browse the repository at this point in the history
  • Loading branch information
danny committed Sep 9, 2024
1 parent bab8460 commit 8b39ae1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
6 changes: 4 additions & 2 deletions macless_haystack/lib/accessory/accessory_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ class Accessory {
}
}



/// The display icon of the accessory.
IconData get icon {
IconData? icon = AccessoryIconModel.mapIcon(_icon);
Expand Down Expand Up @@ -212,6 +210,9 @@ class Accessory {
lastDerivationTimestamp = json['lastDerivationTimestamp'],
updateInterval = json['updateInterval'],
oldestRelevantSymmetricKey = json['oldestRelevantSymmetricKey'],
hashes = json['hashes'] != null
? (json['hashes'] as List).map((e) => e.toString()).toSet()
: <String>{},
additionalKeys =
json['additionalKeys']?.cast<String>() ?? List.empty() {
_init();
Expand All @@ -238,6 +239,7 @@ class Accessory {
'icon': _icon,
'color': color.toString().split('(0x')[1].split(')')[0],
'usesDerivation': usesDerivation,
'hashes': hashes.toList(),
'symmetricKey': symmetricKey,
'lastDerivationTimestamp': lastDerivationTimestamp,
'updateInterval': updateInterval,
Expand Down
11 changes: 7 additions & 4 deletions macless_haystack/lib/accessory/accessory_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ class AccessoryRegistry extends ChangeNotifier {
if (reports.where((element) => !element.isEncrypted()).isNotEmpty) {
var lastReport =
reports.where((element) => !element.isEncrypted()).first;
var reportDate = lastReport.timestamp ?? lastReport.published;
var reportDate = (lastReport.timestamp ?? lastReport.published) ??
DateTime.fromMicrosecondsSinceEpoch(0);
if (accessory.datePublished != null &&
reportDate!.isAfter(accessory.datePublished!)) {
reportDate.isAfter(accessory.datePublished!)) {
accessory.datePublished = reportDate;
accessory.lastLocation =
LatLng(lastReport.latitude!, lastReport.longitude!);
Expand Down Expand Up @@ -202,6 +203,7 @@ class AccessoryRegistry extends ChangeNotifier {
var currHash = reports[i].hash;
if (!accessory.containsHash(currHash)) {
accessory.addDecryptedHash(currHash);
logger.d('Decrypting report $i of ${reports.length}');
await reports[i].decrypt();
decryptedReports.add(reports[i]);
} else {
Expand Down Expand Up @@ -238,8 +240,9 @@ class AccessoryRegistry extends ChangeNotifier {
//add to history in correct order
for (var i = 0; i < decryptedReports.length; i++) {
FindMyLocationReport report = decryptedReports[i];
if (report.accuracy! < 100 ||
report.longitude!.abs() <= 180 && report.latitude!.abs() <= 90) {
if (report.accuracy! >= 110 &&
report.longitude!.abs() <= 180 &&
report.latitude!.abs() <= 90) {
accessory.addLocationHistoryEntry(report);
} else {
logger.d(
Expand Down
6 changes: 3 additions & 3 deletions macless_haystack/lib/findMy/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class FindMyLocationReport {
await Future.delayed(const Duration(
milliseconds: 1)); //Is needed otherwise is executed synchron
if (isEncrypted()) {
logger.d(
'Decrypting report with private key of ${getId()!.substring(0, 4)}');
final unixTimestampInMillis = result["datePublished"];
final datePublished =
DateTime.fromMillisecondsSinceEpoch(unixTimestampInMillis);
Expand All @@ -69,7 +67,9 @@ class FindMyLocationReport {
latitude = correctCoordinate(decryptedReport.latitude!, 90);
longitude = correctCoordinate(decryptedReport.longitude!, 180);
accuracy = decryptedReport.accuracy;
timestamp = decryptedReport.timestamp;
timestamp = accuracy != null && accuracy! >= 110
? decryptedReport.timestamp
: null;
confidence = decryptedReport.confidence;
result = null;
base64privateKey = null;
Expand Down
10 changes: 6 additions & 4 deletions macless_haystack/lib/location/location_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,19 @@ class LocationModel extends ChangeNotifier {
/// Returns the address for a given geolocation (latitude & longitude).
///
/// Only works on mobile platforms with their local APIs.
Future<geocode.Placemark?> getAddress(LatLng? location) async {
Future<geocode.Placemark?> getAddress(LatLng? location) async {
if (location == null) {
return null;
}
double lat = location.latitude;
double lng = location.longitude;

try {
List<geocode.Placemark> placemarks =
await geocode.placemarkFromCoordinates(lat, lng);
return placemarks.first;
if (geocode.GeocodingPlatform.instance != null) {
List<geocode.Placemark> placemarks =
await geocode.placemarkFromCoordinates(lat, lng);
return placemarks.first;
}
} on MissingPluginException {
return null;
} on PlatformException {
Expand Down

0 comments on commit 8b39ae1

Please sign in to comment.