Repository navigation
Expand file tree
/
Copy pathpages.py
More file actions
436 lines (355 loc) · 15.7 KB
/
Copy pathpages.py
File metadata and controls
436 lines (355 loc) · 15.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
This script contains the different screens/views which
are shown to user as they interact with the app
"""
from datetime import datetime, timedelta
from gspread import service_account
from jnius import autoclass
from kivy.app import App
from kivy.base import Builder
from kivy.clock import Clock
from kivy.properties import BooleanProperty, ObjectProperty
from kivy.uix.screenmanager import Screen
from ntplib import NTPClient, NTPException
import globals
from android import mActivity
from base import Dialog, FlightPopup, Re_StartPopup, UploadPopup
from globals import showhide_widget
from models import EventLog
ntp = NTPClient()
time_servers = [
"time.google.com", # Stratum 1
"time.facebook.com", # Stratum 1
"time.apple.com", # Stratum 1
"pool.ntp.org", # Stratum 2
"time.cloudflare.com", # Stratum 2
]
Builder.load_file("pages.kv")
class Home(Screen):
"""Class for the Home page which is shown on opening the app"""
# Hours, Minutes label of the clock on Home screen
tm1 = ObjectProperty(None)
tm2 = ObjectProperty(None) # Minutes label of the clock on Home screen
loc_but = ObjectProperty(None) # Stage selection screen button
sheet = None # Google sheet to which the data is going to be uploaded
up_count = ObjectProperty(0) # Number of uploads since last change
last_up_time = ObjectProperty(None) # Recent upload time
up_info = ObjectProperty(f"Upload(0)") # text on the upload button
# Stores the time for the home clock
tm = ObjectProperty(datetime(1, 1, 1, 0, 0, 0))
fl = ObjectProperty("No")
sync_done = ObjectProperty("No")
_timer = [None]
def __init__(self, **kw):
globals.update_objs.append(self)
self.up_count = globals.settings.getint("SETTINGS", "up_count")
globals.settings.add_callback(self.chg_text, "SETTINGS", "up_count")
globals.settings.add_callback(self.on_sync, "SETTINGS", "last_sync_time")
self.on_sync(None, None, globals.settings["SETTINGS"]["last_sync_time"])
if self.up_count:
self.last_up_time = datetime.strptime(
globals.settings["SETTINGS"]["last_up_time"], "%d/%m/%y %H:%M:%S"
)
delay = 0.1
self._timer = Clock.schedule_interval(self.check_airpl, delay)
# changes the upload button to include relevant info
self.chg_text(val=self.up_count)
super().__init__(**kw)
def on_log(self, obj):
"""Goes to Log Screen"""
self.manager.get_screen("Log").reload()
self.manager.current = "Log"
def check_airpl(self, dt):
Global = autoclass("android.provider.Settings$Global")
is_airpl = Global.getString(
mActivity.getContentResolver(), Global.AIRPLANE_MODE_ON
)
self.fl = "Yes" if int(is_airpl) else "No"
def on_sync(self, s, k, v):
dt_ob = datetime.strptime(v, "%d/%m/%y %H:%M:%S.%f")
self.sync_done = "No" if datetime.today().date() > dt_ob.date() else "Yes"
def upload(self):
UploadPopup(self.on_upload).open()
def on_upload(self, name):
"""
Uploads the data to the Google Sheets where itcan be downloaded from and post-processed
"""
if not self.sheet:
self.sheet = (
service_account(filename="credentials.json")
.open("Rally Clock Data")
.sheet1
)
EventLog.upload(self.sheet, name, self.fl, self.sync_done)
Dialog(self, "Done Uploading").show(10)
now = datetime.now()
self.last_up_time = now
globals.settings["SETTINGS"]["last_up_time"] = now.strftime("%d/%m/%y %H:%M:%S")
self.up_count += 1
globals.settings["SETTINGS"]["up_count"] = str(self.up_count)
globals.settings["SETTINGS"]["up_name"] = name
globals.settings.write()
return 0
def chg_text(self, s=None, k=None, val=None):
"""
Changes the text of the Upload button to include extra data like number of
upload attempts and last upload time
"""
upcount = int(val)
self.up_info = f"Upload({upcount})"
if upcount:
self.up_info += "\n" + self.last_up_time.strftime("%H:%M:%S")
def on_tm(self, ins, val):
"""Adds an offset to the local time to sync it to the NTP server"""
self.tm1.text = self.tm.strftime("%H:%M")
self.tm2.text = self.tm.strftime("%S")
class SetPage(Screen):
"""Class for Settings/More Options screen which contains several options for using the app"""
use_ll = ObjectProperty(None) # State of Use Lifeline slider
use_rt = ObjectProperty(None) # State of Use Restart time slider
sync_but = ObjectProperty(None) # Sync Button
tm = ObjectProperty(None)
version = ObjectProperty("0.0")
def __init__(self, **kw):
super().__init__(**kw)
self.use_ll.active = globals.settings.getboolean("SETTINGS", "use_ll")
self.use_rt.active = globals.settings.getboolean("SETTINGS", "use_rt")
# Change the offset if it is present
self.on_offset("SETTINGS", "offset", globals.offset)
globals.settings.add_callback(self.on_offset, "SETTINGS", "offset")
self.on_st_tm(None, None, globals.settings["SETTINGS"]["start_time"])
globals.settings.add_callback(self.on_st_tm, "SETTINGS", "start_time")
self.version = kw["version"]
def on_st_tm(self, s, k, v):
self.tm = v
def erase_log(self):
EventLog.delete().execute()
# Clear the Time Capture Screen
self.manager.get_screen("Page3").rv.data = list()
# Reset the upload count and info (not in sync with last uploaded data)
globals.settings["SETTINGS"]["up_count"] = str(0)
globals.settings.write()
Dialog(self, "Done Erasing").show()
def on_ll_active(self, value):
globals.settings["SETTINGS"]["use_ll"] = str(value)
globals.settings.write()
def on_rt_active(self, value):
globals.settings["SETTINGS"]["use_rt"] = str(value)
globals.settings.write()
def on_offset(self, sec, key, value):
self.sync_but.text = f"Sync\nOffset from local time: {float(value):.02f}s"
def sync(self):
"""Sync the time to an NTP server"""
for server in time_servers: # try different servers for time syncing
try:
response = ntp.request(server)
globals.offset = response.offset
globals.settings["SETTINGS"]["offset"] = str(globals.offset)
globals.settings["SETTINGS"][
"last_sync_time"
] = datetime.now().strftime("%d/%m/%y %H:%M:%S.%f")
globals.settings.write()
Dialog(self, "-Done-").open()
return 0 # exit the function if the time was synced
except NTPException:
continue
# For debugging purposes (only shown if the time couldn't be synced)
Dialog(self, "Done").open()
return 1
def re_start(self):
Re_StartPopup().open()
class StageSel(Screen):
"""Class for Stage, Rally Day selection Screen"""
day = ObjectProperty(None)
# Whether to show the dropdown for Stage No. Selection or not
show_stg_sel = BooleanProperty(True)
# Layout/Widget for Stage No. Selection dropdown
stg_sel = ObjectProperty(None)
# Dropdown Widget for Stage No. Selection dropdown
stg_sel_drop = ObjectProperty(None)
# Text for Stage No. Selection dropdown
stg_sel_text = ObjectProperty(None)
# Layout/Widget Containing all the children widgets for this screen
box = ObjectProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.stg_sel_drop.text = globals.settings["SETTINGS"]["stg_no"]
self.stage.text = globals.settings["SETTINGS"]["stage"]
self.day.text = globals.settings["SETTINGS"]["day"]
def on_stage(self, value):
"""
Show different text or hide the stage no/regroup no selection dropdown widget altogether according
to the selection of current stage. Also show the Stage, Day no, Stage no. on the Home Screen.
"""
# This variable is used to show the text on the home screen according to the stage and day no.
s = value.split(" ")
s = s[0][0] + s[1][0]
list1 = ("Rally Start of the day", "Rally Finish of the day")
list2 = (
"Start of Special Stage",
"Arrival in Special Stage",
"Finish of Special Stage",
"Time Control IN",
)
list3 = ("Service IN", "Service OUT", "Regroup IN", "Regroup OUT")
# Dont show stage no./regroup no. selection widget if the stage is "Rally Start" or "Rally Finish"
if value in list1:
showhide_widget(self.stg_sel)
self.show_stg_sel = False
# self.box.remove_widget(self.stg_sel)
else:
showhide_widget(self.stg_sel, False)
self.show_stg_sel = True
# show the widget again if it wasnt already shown
# Keeping the text format of the button on the home screen similar to the NRS app
if value in list2:
# button text format stuff
if value != "Time Control IN":
s = value.split(" ")
s = "S" + s[0][0]
# Change the dropdown label text and dropdown values according to the value selected
self.stg_sel_text.text = "Stage No."
values = [str(i) for i in range(1, 31)]
values.sort(key=int)
self.stg_sel_drop.values = values
self.stg_sel_drop.text = values[0]
elif value in list3:
# Change the dropdown label text and dropdown values according to the value selected
self.stg_sel_text.text = "Service/Regroup No."
values = [str(i) for i in range(1, 6)]
values.sort(key=int)
self.stg_sel_drop.values = values
self.stg_sel_drop.text = values[0]
globals.settings["SETTINGS"]["stage"] = value
globals.settings.write()
# Change button text format for the middle portion
self.but_text_ch(curr_stg=s)
def but_text_ch(self, day_no="", curr_stg="", stg_no=""):
"""
Change the Stage selection button text with a format similar to that of NRS app
Button text format:"Location
D{day_no}-{curr_stg}-{stg_no}"
"""
app = App.get_running_app()
string = app.loc_but.text
# if day no. has changed
if day_no:
globals.settings["SETTINGS"]["day"] = day_no
app.loc_but.text = f"Location\nD{day_no}-{string[13:]}"
# if current stage has changed
elif curr_stg:
app.loc_but.text = string[:13] + curr_stg + string[15:]
# if stage no./regroup no has changed (not present when at stage "Rally Start" or "Rally Finish")
elif stg_no:
globals.settings["SETTINGS"]["stg_no"] = stg_no
string = string[:-2] + f"{int(stg_no):02}"
app.loc_but.text = string
if not curr_stg:
globals.settings.write()
# if the stage no/regroup no selection widget wasnt present but the button text still has stage no., this will remove it.
# on the other hand, if the stage no/regroup no selection widget is present but the button text doesnt have stage no., this will add it
if not self.show_stg_sel and app.loc_but.text[-1].isnumeric():
app.loc_but.text = app.loc_but.text[:-3]
elif self.show_stg_sel and not app.loc_but.text[-1].isnumeric():
app.loc_but.text = app.loc_but.text + f"-{int(self.stg_sel_drop.text):02}"
def on_no(self, value):
self.but_text_ch(stg_no=value)
class Page3(Screen):
"""Class for the Page3 Screen"""
rv = ObjectProperty(None)
tm_label = ObjectProperty(None)
capt_button = ObjectProperty(None)
prev_offset = ObjectProperty(0)
_start_timer = False
def __init__(self, **kw):
"""Initial input of the records to the RecycleView from the log file"""
super().__init__(**kw)
globals.settings.add_callback(self.on_start_time, "SETTINGS", "stage")
globals.settings.add_callback(self.on_st_tm, "SETTINGS", "start_time")
self.on_start_time("SETTINGS", "stage", globals.settings["SETTINGS"]["stage"])
for i in EventLog.select():
self.rv.data.insert(
0,
{
"tm": i.time,
"carno": "" if not i.carno else str(i.carno),
"LL": i.LL,
"is_rtm": i.is_rtm,
"rtm": i.rtime,
},
)
def auto_capture(self, delay):
if globals.synced_time.time().second == 0:
prev_timer = self._timer
prev_timer.cancel()
del self._timer
self.on_capture(datetime.now())
self._auto_time = datetime.combine(
datetime.today(), globals.synced_time.time()
)
self._timer = Clock.schedule_interval(lambda dt: self.capture(delay), delay)
def capture(self, dt):
self._auto_time += timedelta(seconds=dt)
self.on_capture(self._auto_time, True)
def on_capture(self, time: datetime, auto=False):
"""Adds the new row to the RecycleView(at the top) and to the logfile database(at the end)"""
# The button text for the Stage Selection button also acts as an
# identifier for the stage in which the time was captured so
# it is added to the database for further verification during the results
loc = App.get_running_app().loc_but.text
loc = loc.split("\n")
loc = loc[1]
if not auto:
tm = globals.synced_time.time()
else:
tm = time.time()
row = EventLog.insert(carno="", location=loc, date=time.date(), time=tm)
row.execute()
self.rv.data.insert(
0, {"tm": tm, "carno": "", "LL": False, "is_rtm": False, "rtm": None}
)
self.manager.get_screen("Log").reload()
globals.settings["SETTINGS"]["up_count"] = str(0)
globals.settings.write()
def on_st_tm(self, s, k, v):
if hasattr(self, "_timer"):
prev_timer = self._timer
delay = globals.settings.getfloat("SETTINGS", "start_time") * 60
self._timer = Clock.schedule_interval(
lambda dt: self.auto_capture(delay), 0.1
)
prev_timer.cancel()
del prev_timer
def on_start_time(self, sec, key, value):
if value == "Rally Start of the day":
if not hasattr(self, "_timer"):
delay = globals.settings.getfloat("SETTINGS", "start_time") * 60
self._timer = Clock.schedule_interval(
lambda dt: self.auto_capture(delay), 0.1
)
showhide_widget(self.capt_button)
showhide_widget(self.tm_label, False)
else:
if hasattr(self, "_timer"):
self._timer.cancel()
del self._timer
showhide_widget(self.tm_label)
showhide_widget(self.capt_button, False)
class ViewLog(Screen):
"""Log screen"""
rv = ObjectProperty(None)
def reload(self):
"""Adds the row into the Log Screen after a data capture is done of any event type (starting/finish)"""
log = EventLog.select()
self.rv.data = list()
for i in log:
self.rv.data.insert(
0,
{
"carno": i.carno,
"rtime": i.rtime,
"tm": i.time,
"LL": i.LL,
"is_rtm": i.is_rtm,
},
)