You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					90 lines
				
				2.8 KiB
			
		
		
			
		
	
	
					90 lines
				
				2.8 KiB
			| 
											5 years ago
										 | #!/usr/bin/env python3
 | ||
|  | import json
 | ||
| 
											5 years ago
										 | import os
 | ||
|  | import time
 | ||
| 
											5 years ago
										 | import subprocess
 | ||
| 
											4 years ago
										 | from typing import NoReturn
 | ||
| 
											5 years ago
										 | 
 | ||
|  | import requests
 | ||
|  | from timezonefinder import TimezoneFinder
 | ||
|  | 
 | ||
|  | from common.params import Params
 | ||
| 
											3 years ago
										 | from system.hardware import AGNOS
 | ||
| 
											3 years ago
										 | from system.swaglog import cloudlog
 | ||
| 
											5 years ago
										 | 
 | ||
|  | 
 | ||
|  | def set_timezone(valid_timezones, timezone):
 | ||
|  |   if timezone not in valid_timezones:
 | ||
|  |     cloudlog.error(f"Timezone not supported {timezone}")
 | ||
|  |     return
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   cloudlog.debug(f"Setting timezone to {timezone}")
 | ||
| 
											5 years ago
										 |   try:
 | ||
| 
											3 years ago
										 |     if AGNOS:
 | ||
| 
											5 years ago
										 |       tzpath = os.path.join("/usr/share/zoneinfo/", timezone)
 | ||
| 
											5 years ago
										 |       subprocess.check_call(f'sudo su -c "ln -snf {tzpath} /data/etc/tmptime && \
 | ||
|  |                               mv /data/etc/tmptime /data/etc/localtime"', shell=True)
 | ||
|  |       subprocess.check_call(f'sudo su -c "echo \"{timezone}\" > /data/etc/timezone"', shell=True)
 | ||
| 
											5 years ago
										 |     else:
 | ||
|  |       subprocess.check_call(f'sudo timedatectl set-timezone {timezone}', shell=True)
 | ||
| 
											5 years ago
										 |   except subprocess.CalledProcessError:
 | ||
|  |     cloudlog.exception(f"Error setting timezone to {timezone}")
 | ||
|  | 
 | ||
|  | 
 | ||
| 
											4 years ago
										 | def main() -> NoReturn:
 | ||
| 
											5 years ago
										 |   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)
 | ||
|  | 
 | ||
| 
											5 years ago
										 |     is_onroad = not params.get_bool("IsOffroad")
 | ||
| 
											5 years ago
										 |     if is_onroad:
 | ||
|  |       continue
 | ||
|  | 
 | ||
|  |     # Set based on param
 | ||
|  |     timezone = params.get("Timezone", encoding='utf8')
 | ||
|  |     if timezone is not None:
 | ||
| 
											4 years ago
										 |       cloudlog.debug("Setting timezone based on param")
 | ||
| 
											5 years ago
										 |       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:
 | ||
| 
											4 years ago
										 |       cloudlog.debug("Setting timezone based on IP lookup")
 | ||
| 
											5 years ago
										 |       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}")
 | ||
| 
											5 years ago
										 | 
 | ||
|  |         time.sleep(3600)  # Don't make too many API requests
 | ||
| 
											5 years ago
										 |       except requests.exceptions.RequestException:
 | ||
|  |         cloudlog.exception("Error getting timezone based on IP")
 | ||
|  |         continue
 | ||
|  | 
 | ||
|  |     # Find timezone by reverse geocoding the last known gps location
 | ||
|  |     else:
 | ||
| 
											4 years ago
										 |       cloudlog.debug("Setting timezone based on GPS location")
 | ||
| 
											5 years ago
										 |       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()
 |