Skip to content

Commit

Permalink
Dynamic timezone menu (#77)
Browse files Browse the repository at this point in the history
* Dynamic timezone menu
  • Loading branch information
bentumbler authored Sep 30, 2022
1 parent ab30d83 commit 4717057
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 25 deletions.
34 changes: 29 additions & 5 deletions fpms/fpms.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def usage():
'eth_last_reachability_result' : False,# Last reachability state
'scan_file' : '', # Location to save scans
'profiler_beaconing' : False, # Indicates if the profiler is running
'profiler_last_profile_date': None # The date of the last profile
'profiler_last_profile_date': None, # The date of the last profile
'timezones_available' : [], # The list of Timezones the system can support
'timezone_selected' : None # The timezone selected from the menu for use in other functions
}

############################
Expand Down Expand Up @@ -211,6 +213,9 @@ def usage():
g_vars['profiler_last_profile_date'] = profiler.profiler_last_profile_date()
g_vars['profiler_beaconing'] = profiler.profiler_beaconing()

timezone = TimeZone(g_vars)
timezones_available = timezone.get_timezones_menu_format()

###########################
# Network menu area utils
###########################
Expand Down Expand Up @@ -426,6 +431,14 @@ def show_date():
system_obj = System(g_vars)
system_obj.show_date(g_vars)

def set_time_zone():
g_vars['timezone_selected'] = (timezones_available[g_vars['current_menu_location'][3]]['country'] +
"/" +
timezones_available[g_vars['current_menu_location'][3]]['timezones'][g_vars['current_menu_location'][4]])

system_obj = TimeZone(g_vars)
system_obj.set_time_zone_from_gvars(g_vars)

def set_time_zone_london():
system_obj = TimeZone(g_vars)
system_obj.set_time_zone_london(g_vars)
Expand Down Expand Up @@ -543,6 +556,13 @@ def create_shortcut(menu, path, location=[]):
# menu structure here
#######################


# translate the timezone list into menu items (needs to be after definition of set_time_zone)
for timezones_country in timezones_available:
g_vars['timezones_available'].append({"name": timezones_country['country'], "action": []})
for timezone in timezones_country['timezones']:
g_vars['timezones_available'][-1]['action'].append({"name": timezone, "action": [{"name": "Confirm & Reboot", "action": set_time_zone}]})

# assume classic mode menu initially...
menu = [
{"name": "Network", "action": [
Expand Down Expand Up @@ -636,12 +656,16 @@ def create_shortcut(menu, path, location=[]):
{"name": "System", "action": [
{"name": "About", "action": show_about},
{"name": "Battery", "action": show_battery},
# {"name": "Date & Time", "action": [
# {"name": "Show Time & Zone", "action": show_date},
# {"name": "Set Zone UK", "action": [
# {"name": "Confirm & Reboot", "action": set_time_zone_london},]},
# {"name": "Set Zone CZ", "action": [
# {"name": "Confirm & Reboot", "action": set_time_zone_prague},]},
# ]},
{"name": "Date & Time", "action": [
{"name": "Show Time & Zone", "action": show_date},
{"name": "Set Zone UK", "action": [
{"name": "Confirm & Reboot", "action": set_time_zone_london},]},
{"name": "Set Zone CZ", "action": [
{"name": "Confirm & Reboot", "action": set_time_zone_prague},]},
{"name": "Set Timezone", "action": g_vars['timezones_available']},
]},
{"name": "Summary", "action": show_summary},
{"name": "RF Domain", "action": [
Expand Down
86 changes: 66 additions & 20 deletions fpms/modules/time_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fpms.modules.wlanpi_oled as oled
import sys


from fpms.modules.pages.simpletable import SimpleTable
from fpms.modules.pages.pagedtable import PagedTable
from fpms.modules.pages.alert import Alert
Expand All @@ -20,12 +21,37 @@ def __init__(self, g_vars):

# create alert
self.alert_obj = Alert(g_vars)

def get_timezones_menu_format(self):
"""
Returns a dictionary of supported Timezones as {country, [timezones]}, the timezones are filtered into country menus
The menu names are used as the parameter for the TIME_ZONE_FILE command so show on the screen in long form : country/city
"""
timezone_menu_list = []
try:
timezones_available = subprocess.getoutput(f"{TIME_ZONE_FILE} list").splitlines()
time.sleep(1)

# create a set of countries defined as the first part of the text split with '/'
countries = sorted(set([c.split('/', 1)[0] for c in timezones_available]))

# Iterate the countries and then create array of timezones for that country
for country in countries:
timezone_menu_list.append({"country": country, "timezones": []})
for timezone in filter(lambda c: c.startswith(country), timezones_available):
timezone_menu_list[-1]['timezones'].append(timezone.split('/', 1)[-1])

return timezone_menu_list

def set_time_zone_london(self, g_vars):
self.alert_obj.display_popup_alert(g_vars, 'Setting time zone', delay=2)
except subprocess.CalledProcessError as exc:
return None

def set_time_zone_from_gvars(self, g_vars):
timezone_selected = g_vars['timezone_selected']
self.alert_obj.display_popup_alert(g_vars, 'Setting TZ: {0}'.format(timezone_selected), delay=2)

try:
alert_msg = subprocess.check_output(f"{TIME_ZONE_FILE} set Europe/London", shell=True).decode()
alert_msg = subprocess.check_output(f"{TIME_ZONE_FILE} set {timezone_selected}", shell=True).decode()
time.sleep(1)
except subprocess.CalledProcessError as exc:
print(exc)
Expand All @@ -41,22 +67,42 @@ def set_time_zone_london(self, g_vars):
os.system('reboot')
return

def set_time_zone_prague(self, g_vars):
self.alert_obj.display_popup_alert(g_vars, 'Setting time zone', delay=2)
# def set_time_zone_london(self, g_vars):
# self.alert_obj.display_popup_alert(g_vars, 'Setting time zone', delay=2)

try:
alert_msg = subprocess.check_output(f"{TIME_ZONE_FILE} set Europe/Prague", shell=True).decode()
time.sleep(1)
except subprocess.CalledProcessError as exc:
print(exc)
self.alert_obj.display_alert_error(g_vars, 'Failed to set time zone')
g_vars['display_state'] = 'menu'
return
# try:
# alert_msg = subprocess.check_output(f"{TIME_ZONE_FILE} set Europe/London", shell=True).decode()
# time.sleep(1)
# except subprocess.CalledProcessError as exc:
# print(exc)
# self.alert_obj.display_alert_error(g_vars, 'Failed to set time zone')
# g_vars['display_state'] = 'menu'
# return

self.alert_obj.display_popup_alert(g_vars, 'Successfully set', delay=1)
g_vars['display_state'] = 'menu'
g_vars['shutdown_in_progress'] = True
oled.drawImage(g_vars['reboot_image'])
time.sleep(1)
os.system('reboot')
return
# self.alert_obj.display_popup_alert(g_vars, 'Successfully set', delay=1)
# g_vars['display_state'] = 'menu'
# g_vars['shutdown_in_progress'] = True
# oled.drawImage(g_vars['reboot_image'])
# time.sleep(1)
# os.system('reboot')
# return

# def set_time_zone_prague(self, g_vars):
# self.alert_obj.display_popup_alert(g_vars, 'Setting time zone', delay=2)

# try:
# alert_msg = subprocess.check_output(f"{TIME_ZONE_FILE} set Europe/Prague", shell=True).decode()
# time.sleep(1)
# except subprocess.CalledProcessError as exc:
# print(exc)
# self.alert_obj.display_alert_error(g_vars, 'Failed to set time zone')
# g_vars['display_state'] = 'menu'
# return

# self.alert_obj.display_popup_alert(g_vars, 'Successfully set', delay=1)
# g_vars['display_state'] = 'menu'
# g_vars['shutdown_in_progress'] = True
# oled.drawImage(g_vars['reboot_image'])
# time.sleep(1)
# os.system('reboot')
# return

0 comments on commit 4717057

Please sign in to comment.