Browse Source

save attr lat, lon & alt only; create symlink last.yml

Fabian Peter Hammerle 6 years ago
parent
commit
56ea713dfb
1 changed files with 47 additions and 11 deletions
  1. 47 11
      log-location

+ 47 - 11
log-location

@@ -1,6 +1,6 @@
 #!/data/data/com.termux/files/usr/bin/python3
 
-import datetime
+import datetime as dt
 import json
 import os
 import pytz
@@ -14,20 +14,56 @@ def to_iso6801_basic(dt):
         .replace('+0000', 'Z')
 
 
+class Position:
+
+    def __init__(self, timestamp, latitude, longitude, altitude):
+        self.timestamp = timestamp
+        self.latitude = latitude
+        self.longitude = longitude
+        self.altitude = altitude
+
+    @classmethod
+    def from_termux_location(cls, provider='network'):
+        loc_json = subprocess.check_output(['termux-location', '-p', provider]) \
+            .decode(sys.stdout.encoding)
+        req_ts = dt.datetime.now(pytz.utc)
+        loc_attr = yaml.load(loc_json)
+        if not loc_attr:
+            return None
+        else:
+            print(loc_attr)
+            return cls(
+                timestamp=req_ts -
+                    dt.timedelta(milliseconds=loc_attr['elapsedMs']),
+                latitude=loc_attr['latitude'],
+                longitude=loc_attr['longitude'],
+                altitude=loc_attr['altitude'],
+            )
+
+
+def symlink(src, dst, force=False):
+    if force and os.path.lexists(dst):
+        os.unlink(dst)
+    os.symlink(src=src, dst=dst)
+
+
 def compute(target_dir_path):
-    loc_json = subprocess.check_output(['termux-location', '-p', 'network']) \
-        .decode(sys.stdout.encoding)
-    req_time = datetime.datetime.now(pytz.utc)
-    loc_attr = yaml.load(loc_json)
-    target_path = os.path.join(
-        target_dir_path,
-        '{}.yml'.format(to_iso6801_basic(req_time)),
-    )
-    with open(target_path, 'w') as f:
+    cur = Position.from_termux_location()
+    target_name = '{}.yml'.format(to_iso6801_basic(cur.timestamp))
+    with open(os.path.join(target_dir_path, target_name), 'w') as f:
         f.write(yaml.dump(
-            loc_attr,
+            {
+                'lat': cur.latitude,
+                'lon': cur.longitude,
+                'alt': cur.altitude,
+            },
             default_flow_style=False,
         ))
+    symlink(
+        src=target_name,
+        dst=os.path.join(target_dir_path, 'last.yml'),
+        force=True,
+    )
 
 
 def _init_argparser():