-
Notifications
You must be signed in to change notification settings - Fork 6
/
Menu.py
175 lines (142 loc) · 5.52 KB
/
Menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import os.path
import time
import subprocess
class MenuItem:
def __init__(self, string: str, callback: callable, items: list = None) -> object:
self.string = string
self.callback = callback
self.items = items
def click(self):
self.callback()
def __getitem__(self, key):
return self.items[key]
def __len__(self) -> int:
return len(self.items)
class Menu:
active_items = []
menu = None
listeners = {}
phone_calling = False
candumping = None
def __init__(self):
if os.path.isfile('/otgstorage/readwrite'):
fslabel = 'Make readonly'
is_readwrite = True
else:
fslabel = 'Make readwrite'
is_readwrite = False
def dismiss_menu():
self.active_items = []
self.instpanel_display(None)
def temp_show(message, sec=2):
self.instpanel_display(message)
time.sleep(sec)
self.instpanel_display(None)
def version_callback():
result = subprocess.run(["dpkg-query", "--showformat='${Version}'", "--show", "infotainment"], stdout=subprocess.PIPE)
infot_version = result.stdout.decode('UTF8').strip()
result = subprocess.run(['uname', '-r'], stdout=subprocess.PIPE)
kernel_version = result.stdout.decode('UTF8').strip()
self.instpanel_display("{}/{}".format(infot_version, kernel_version))
def candump_callback():
if self.candumping is None:
ret = os.system("sudo datamount")
if ret != 0:
self.instpanel_display("mount err {}".format(ret))
return
self.candumping = subprocess.Popen(['candump', '-l', 'can0'], cwd='/data/traces')
self.instpanel_display("dump started")
else:
self.candumping.kill()
self.candumping = None
if os.system("sudo dataumount") != 0:
self.instpanel_display("umount error")
self.instpanel_display("dump stopped")
def restart_callback():
temp_show("Restarting...", 2)
subprocess.run(['sudo', 'systemctl', 'restart', 'infotainment'])
def submenu_callback():
self.active_items.append(0)
item = self.get_active_item()
self.instpanel_display(item.string, True)
def rootfs_callback():
if is_readwrite:
self.instpanel_display("Rebooting RO...")
os.system("sudo rootfsro")
else:
self.instpanel_display("Rebooting RW...")
os.system("sudo rootfsrw")
self.menu = [
MenuItem('Version', version_callback),
MenuItem('Candump', candump_callback),
MenuItem('Restart srvc', submenu_callback, [
MenuItem('Confirm? No', dismiss_menu),
MenuItem('Confirm? Yes', restart_callback),
]),
MenuItem(fslabel, submenu_callback, [
MenuItem('Confirm? No', dismiss_menu),
MenuItem('Confirm? Yes', rootfs_callback),
]),
]
def on_button(self, key):
print("[menu] key {}".format(key))
if key == 'menu':
if len(self.active_items) == 0:
self.active_items.append(0)
item = self.get_active_item()
self.instpanel_display(item.string, True)
else:
item = self.get_active_item()
item.click()
elif key == 'up':
if len(self.active_items) > 0:
menu = self.get_active_menu()
idx = self.active_items[-1]
idx = (idx - 1) % len(menu)
self.active_items[-1] = idx
item = self.get_active_item()
self.instpanel_display(item.string, True)
elif key == 'down':
if len(self.active_items) > 0:
menu = self.get_active_menu()
idx = self.active_items[-1]
idx = (idx + 1) % len(menu)
self.active_items[-1] = idx
item = self.get_active_item()
self.instpanel_display(item.string, True)
elif key == 'mute':
if len(self.active_items) > 0:
self.active_items.pop()
if len(self.active_items) > 0:
item = self.get_active_item()
self.instpanel_display(item.string, True)
else:
self.instpanel_display(None)
def get_active_item(self):
item = self.menu
for idx in self.active_items:
item = item[idx]
return item
def get_active_menu(self):
menu = self.menu
for idx in self.active_items[0:-1]:
menu = menu[idx]
return menu
def instpanel_display(self, message=None, show_arrows=False):
print("[menu] {}".format(message))
self.fire_event('item', message, show_arrows)
def on_phone(self, number):
if number is None:
self.phone_calling = False
else:
self.phone_calling = True
def fire_event(self, event, *args):
if event not in self.listeners:
return
for listener in self.listeners[event]:
listener(*args)
def on_event(self, event, callback):
if event not in self.listeners:
self.listeners[event] = []
self.listeners[event].append(callback)