Timezoned (#19960)
* untested code
* add to manager
* whitespace
* only save on fix
* cleanup
* fix typo
* add to ignored processes
* import tici
* fix param name
* cleanup
* only run offroad
* use itertools
* wait for thermald
* simpler
* fall back to api call
* add param to override timezone
Co-authored-by: Comma Device <device@comma.ai>
old-commit-hash: 4c04e220a1
commatwo_master
parent
434e5f0454
commit
17d5446650
7 changed files with 102 additions and 7 deletions
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:427eb342d23300f90c25e4860f2f5446693c3af6323e7a1f30f7e1ccc56921b7 |
oid sha256:a14bdc2df2ce7c1e565fc3fef70bb9872b3e51099bf077a819cfc19dc6a88b50 |
||||||
size 2116 |
size 2137 |
||||||
|
@ -1,3 +1,3 @@ |
|||||||
version https://git-lfs.github.com/spec/v1 |
version https://git-lfs.github.com/spec/v1 |
||||||
oid sha256:d9e414b95d4fd8abab9ddd37c1a2894790f09e8ab0c36e25c4959094fdc28ecf |
oid sha256:a9c44a8d7874e9ced435de3bc9a3724f6bf366af85ba37282b865e270fd64220 |
||||||
size 211694 |
size 217383 |
||||||
|
@ -0,0 +1,78 @@ |
|||||||
|
#!/usr/bin/env python3 |
||||||
|
import time |
||||||
|
import json |
||||||
|
import subprocess |
||||||
|
|
||||||
|
import requests |
||||||
|
from timezonefinder import TimezoneFinder |
||||||
|
|
||||||
|
from common.params import Params |
||||||
|
from selfdrive.swaglog import cloudlog |
||||||
|
|
||||||
|
|
||||||
|
def set_timezone(valid_timezones, timezone): |
||||||
|
if timezone not in valid_timezones: |
||||||
|
cloudlog.error(f"Timezone not supported {timezone}") |
||||||
|
return |
||||||
|
|
||||||
|
cloudlog.info(f"Setting timezone to {timezone}") |
||||||
|
try: |
||||||
|
subprocess.check_call(f'sudo timedatectl set-timezone {timezone}', shell=True) |
||||||
|
except subprocess.CalledProcessError: |
||||||
|
cloudlog.exception(f"Error setting timezone to {timezone}") |
||||||
|
|
||||||
|
|
||||||
|
def main(): |
||||||
|
params = Params() |
||||||
|
tf = TimezoneFinder() |
||||||
|
|
||||||
|
# Get allowed timezones |
||||||
|
valid_timezones = subprocess.check_output('timedatectl list-timezones', shell=True, encoding='utf8').strip().split('\n') |
||||||
|
|
||||||
|
while True: |
||||||
|
time.sleep(60) |
||||||
|
|
||||||
|
is_onroad = params.get("IsOffroad") != b"1" |
||||||
|
if is_onroad: |
||||||
|
continue |
||||||
|
|
||||||
|
# Set based on param |
||||||
|
timezone = params.get("Timezone", encoding='utf8') |
||||||
|
if timezone is not None: |
||||||
|
cloudlog.info("Setting timezone based on param") |
||||||
|
set_timezone(valid_timezones, timezone) |
||||||
|
continue |
||||||
|
|
||||||
|
location = params.get("LastGPSPosition", encoding='utf8') |
||||||
|
|
||||||
|
# Find timezone based on IP geolocation if no gps location is available |
||||||
|
if location is None: |
||||||
|
cloudlog.info("Setting timezone based on IP lookup") |
||||||
|
try: |
||||||
|
r = requests.get("https://ipapi.co/timezone", timeout=10) |
||||||
|
if r.status_code == 200: |
||||||
|
set_timezone(valid_timezones, r.text) |
||||||
|
else: |
||||||
|
cloudlog.error(f"Unexpected status code from api {r.status_code}") |
||||||
|
except requests.exceptions.RequestException: |
||||||
|
cloudlog.exception("Error getting timezone based on IP") |
||||||
|
continue |
||||||
|
|
||||||
|
# Find timezone by reverse geocoding the last known gps location |
||||||
|
else: |
||||||
|
cloudlog.info("Setting timezone based on GPS location") |
||||||
|
try: |
||||||
|
location = json.loads(location) |
||||||
|
except Exception: |
||||||
|
cloudlog.exception("Error parsing location") |
||||||
|
continue |
||||||
|
|
||||||
|
timezone = tf.timezone_at(lng=location['longitude'], lat=location['latitude']) |
||||||
|
if timezone is None: |
||||||
|
cloudlog.error(f"No timezone found based on location, {location}") |
||||||
|
continue |
||||||
|
set_timezone(valid_timezones, timezone) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__": |
||||||
|
main() |
Loading…
Reference in new issue