Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Commit b8c8878

Browse files
committed
refactor: rename auspost -> user, do_everything() -> run_user_session()
1 parent 5eb1c06 commit b8c8878

File tree

2 files changed

+60
-67
lines changed

2 files changed

+60
-67
lines changed

client/drivers/data_structures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class ControlledData:
2727
"""
28-
Data for passing around in client/drivers/main.do_everything().
28+
Data for passing around in client/drivers/main.run_user_session().
2929
3030
There should only ever be one object of this class at a time.
3131
@@ -171,6 +171,7 @@ def accept_new_posture_data(
171171
for datum in posture_data:
172172
self._posture_data.put_nowait(datum)
173173

174+
174175
class HardwareComponents:
175176
"""
176177
Hardware components packaged together into a class.

client/drivers/pi_overlord.py

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
#: Number of milliseconds between the user successfully logging out and returning to main().
4848
LOGOUT_SUCCESS_DELAY = 3000
49-
#: Minimum delay between reading posture data from the SQLite database, in do_everything().
49+
#: Minimum delay between reading posture data from the SQLite database, in run_user_session().
5050
GET_POSTURE_DATA_TIMEOUT = timedelta(milliseconds=10000)
5151
#: Proportion of the time the user must be in frame for any feedback to be given. FIXME: Fine-tune this value later.
5252
PROPORTION_IN_FRAME_THRESHOLD = 0.3
@@ -65,13 +65,8 @@
6565
#: the plant will move down.
6666
#: FIXME: Fine-tune this value later.
6767
PLANT_PROPORTION_GOOD_THRESHOLD = 0.5
68-
"""
69-
Threshold for I. Jensen Plant Mover 10000 feedback. If the proportion of "good" sitting posture is below this,
70-
the plant will move down.
71-
FIXME: Fine-tune this value later.
72-
"""
7368

74-
#: DEBUG Number of milliseconds between each loop iteration in do_everything().
69+
#: DEBUG Number of milliseconds between each loop iteration in run_user_session().
7570
DEBUG_DO_EVERYTHING_INTERVAL = 1000
7671

7772
logger = logging.getLogger(__name__)
@@ -123,8 +118,7 @@ def main():
123118

124119
logger.debug("Login successful")
125120

126-
# Run user session
127-
do_everything(user)
121+
run_user_session(user)
128122

129123
# Let the posture tracking process if the user has logged out
130124
if not args.no_posture_model:
@@ -164,21 +158,21 @@ def initialise_hardware() -> HardwareComponents:
164158
# SECTION: Control for the logged-in user
165159

166160

167-
def do_everything(auspost: ControlledData) -> None:
161+
def run_user_session(user: ControlledData) -> None:
168162
"""
169163
Main control flow once a user is logged in.
170164
171165
Args:
172-
auspost: data encapsulating the current state of the program.
166+
user: data encapsulating the current state of the program.
173167
174168
Requires:
175-
! auspost.is_failed()
169+
! user.is_failed()
176170
"""
177-
LOGIN_MESSAGE = "Logged in with user id: " + str(auspost.get_user_id())
178-
LOGOUT_MESSAGE = "Logged out user id " + str(auspost.get_user_id())
171+
LOGIN_MESSAGE = "Logged in with user id: " + str(user.get_user_id())
172+
LOGOUT_MESSAGE = "Logged out user id " + str(user.get_user_id())
179173

180174
# Initialise posture graph for the current session
181-
hardware.initialise_posture_graph(auspost.get_user_id())
175+
hardware.initialise_posture_graph(user.get_user_id())
182176

183177
# Wind plant down all the way
184178
hardware.wind_plant_safe()
@@ -195,7 +189,7 @@ def do_everything(auspost: ControlledData) -> None:
195189
# Set up initial display
196190
hardware.display.fill(0)
197191
hardware.oled_display_texts(
198-
hardware.get_control_messages(auspost.get_user_id()), 0, 0, 1
192+
hardware.get_control_messages(user.get_user_id()), 0, 0, 1
199193
)
200194
hardware.display.show()
201195

@@ -206,17 +200,17 @@ def do_everything(auspost: ControlledData) -> None:
206200
hardware.oled_display_text(LOGOUT_MESSAGE, 0, 0, 1)
207201
hardware.display.show()
208202
sleep_ms(LOGOUT_SUCCESS_DELAY)
209-
print("<!> END do_everything()")
203+
print("<!> END run_user_session()")
210204
return
211205

212-
update_display_screen(auspost)
213-
handle_posture_graph(auspost)
214-
handle_feedback(auspost)
206+
update_display_screen(user)
207+
handle_posture_graph(user)
208+
handle_feedback(user)
215209

216210
sleep_ms(DEBUG_DO_EVERYTHING_INTERVAL)
217211

218212

219-
def update_display_screen(auspost: ControlledData) -> bool:
213+
def update_display_screen(user: ControlledData) -> bool:
220214
"""
221215
Update the display screen with whatever needs to be on there.
222216
@@ -225,54 +219,54 @@ def update_display_screen(auspost: ControlledData) -> bool:
225219
Current-session posture graph
226220
227221
Args:
228-
auspost: data encapsulating the current state of the program.
222+
user: data encapsulating the current state of the program.
229223
230224
Returns:
231225
True, always. If you get a False return value, then something has gone VERY wrong.
232226
233227
Requires:
234-
! auspost.is_failed()
228+
! user.is_failed()
235229
236230
Ensures:
237-
! auspost.is_failed()
231+
! user.is_failed()
238232
"""
239233
while (
240-
not auspost.get_posture_data().empty()
234+
not user.get_posture_data().empty()
241235
): # NOTE: This is much more robust than getting a fixed number of things out of the queue
242236
hardware.display.fill(0)
243237
hardware.oled_display_texts(
244-
hardware.get_control_messages(auspost.get_user_id()), 0, 0, 1
238+
hardware.get_control_messages(user.get_user_id()), 0, 0, 1
245239
)
246240
hardware.display.updateGraph2D(
247-
hardware.posture_graph, auspost.get_posture_data().get_nowait()
241+
hardware.posture_graph, user.get_posture_data().get_nowait()
248242
)
249243
hardware.display.show()
250244

251245
return True
252246

253247

254-
def handle_posture_graph(auspost: ControlledData) -> bool:
248+
def handle_posture_graph(user: ControlledData) -> bool:
255249
"""
256250
Get a snapshot of the user's posture data.
257251
Use this information to update the data for the posture graph.
258252
259253
Args:
260-
(auspost : ControlledData): Data encapsulating the current state of the program.
254+
(user : ControlledData): Data encapsulating the current state of the program.
261255
Returns:
262256
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
263257
Requires:
264-
! auspost.is_failed()
258+
! user.is_failed()
265259
Ensures:
266-
! auspost.is_failed()
260+
! user.is_failed()
267261
268262
TODO: Check this
269263
"""
270264
now = datetime.now()
271265

272-
if now > auspost.get_last_snapshot_time() + GET_POSTURE_DATA_TIMEOUT:
266+
if now > user.get_last_snapshot_time() + GET_POSTURE_DATA_TIMEOUT:
273267
# Get the most recent posture data for the user
274268
recent_posture_data = get_user_postures(
275-
auspost.get_user_id(),
269+
user.get_user_id(),
276270
num=-1,
277271
period_start=now - GET_POSTURE_DATA_TIMEOUT,
278272
period_end=now,
@@ -291,7 +285,7 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
291285
print(
292286
"<!> Exiting handle_posturing_monitoring_new() early: Not in frame for a high enough proportion of time."
293287
)
294-
auspost.set_last_snapshot_time(datetime.now())
288+
user.set_last_snapshot_time(datetime.now())
295289
return True
296290

297291
# Sort the list by period_start
@@ -309,7 +303,7 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
309303
# Calculate the interval length
310304
interval = total_time / POSTURE_GRAPH_DATUM_WIDTH
311305

312-
# Setup sublists, where each sublist is a portion of the overall data
306+
# Setup sublists, where each sublist is a portion of the overall data
313307
split_posture_lists: list[list[Posture]]
314308
split_posture_lists = [[] for _ in range(POSTURE_GRAPH_DATUM_WIDTH)]
315309

@@ -332,62 +326,60 @@ def handle_posture_graph(auspost: ControlledData) -> bool:
332326
[posture.prop_good for posture in posture_list]
333327
) / len(posture_list)
334328
new_prop_good_data += [average_prop_good] * POSTURE_GRAPH_DATUM_WIDTH
335-
auspost.accept_new_posture_data(new_prop_good_data)
329+
user.accept_new_posture_data(new_prop_good_data)
336330

337-
auspost.set_last_snapshot_time(now)
331+
user.set_last_snapshot_time(now)
338332

339333
return True
340334

341-
def handle_feedback(auspost: ControlledData) -> bool:
335+
336+
def handle_feedback(user: ControlledData) -> bool:
342337
"""
343338
Provide feedback to the user if necessary.
344339
345340
Args:
346-
auspost: Data encapsulating the current state of the program.
341+
user: Data encapsulating the current state of the program.
347342
Returns:
348343
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
349344
350345
Requires:
351-
! auspost.is_failed()
346+
! user.is_failed()
352347
353348
Ensures:
354-
! auspost.is_failed()
349+
! user.is_failed()
355350
"""
356-
if (
357-
datetime.now()
358-
> auspost.get_last_cushion_time() + HANDLE_CUSHION_FEEDBACK_TIMEOUT
359-
):
360-
if not handle_cushion_feedback(auspost):
351+
if datetime.now() > user.get_last_cushion_time() + HANDLE_CUSHION_FEEDBACK_TIMEOUT:
352+
if not handle_cushion_feedback(user):
361353
return False
362-
if datetime.now() > auspost.get_last_plant_time() + HANDLE_PLANT_FEEDBACK_TIMEOUT:
363-
if not handle_plant_feedback(auspost):
354+
if datetime.now() > user.get_last_plant_time() + HANDLE_PLANT_FEEDBACK_TIMEOUT:
355+
if not handle_plant_feedback(user):
364356
return False
365357

366358
return True
367359

368360

369-
def handle_cushion_feedback(auspost: ControlledData) -> bool:
361+
def handle_cushion_feedback(user: ControlledData) -> bool:
370362
"""
371363
Vibrate cushion (if necessary), and update the timestamp of when cushion feedback was last given.
372364
373365
Args:
374-
auspost: Data encapsulating the current state of the program.
366+
user: Data encapsulating the current state of the program.
375367
376368
Returns:
377369
True, always. If you get a False return value, then something has gone VERY wrong.
378370
379371
Requires:
380-
! auspost.is_failed()
372+
! user.is_failed()
381373
382374
Ensures:
383-
! auspost.is_failed()
375+
! user.is_failed()
384376
"""
385377
print("<!> handle_cushion_feedback()")
386378

387379
# Load posture records within the last HANDLE_CUSHION_FEEDBACK_TIMEOUT
388380
now = datetime.now()
389381
recent_posture_data = get_user_postures(
390-
auspost.get_user_id(),
382+
user.get_user_id(),
391383
num=-1,
392384
period_start=now - HANDLE_CUSHION_FEEDBACK_TIMEOUT,
393385
period_end=now,
@@ -396,7 +388,7 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
396388
# Conditions for exiting early
397389
if len(recent_posture_data) == 0:
398390
print("<!> Exiting handle_cushion_feedback() early: No data")
399-
auspost.set_last_cushion_time(datetime.now())
391+
user.set_last_cushion_time(datetime.now())
400392
return True
401393
average_prop_in_frame = sum(
402394
[posture.prop_in_frame for posture in recent_posture_data]
@@ -405,14 +397,14 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
405397
print(
406398
"<!> Exiting handle_cushion_feedback() early: Not in frame for a high enough proportion of time."
407399
)
408-
auspost.set_last_cushion_time(datetime.now())
400+
user.set_last_cushion_time(datetime.now())
409401
return True
410402
average_prop_good = sum(
411403
[posture.prop_good for posture in recent_posture_data]
412404
) / len(recent_posture_data)
413405
if average_prop_good >= CUSHION_PROPORTION_GOOD_THRESHOLD:
414406
print("<!> Exiting handle_cushion_feedback() early: You sat well :)")
415-
auspost.set_last_cushion_time(datetime.now())
407+
user.set_last_cushion_time(datetime.now())
416408
return True
417409

418410
buzzer_start_time = datetime.now()
@@ -424,34 +416,34 @@ def handle_cushion_feedback(auspost: ControlledData) -> bool:
424416
GPIO.output(CUSHION_GPIO_PIN, GPIO.LOW)
425417
print("<!> buzzer off")
426418

427-
auspost.set_last_cushion_time(datetime.now())
419+
user.set_last_cushion_time(datetime.now())
428420
return True
429421

430422

431-
def handle_plant_feedback(auspost: ControlledData) -> bool:
423+
def handle_plant_feedback(user: ControlledData) -> bool:
432424
"""
433425
Set the plant height according to short-term current session data, and update the timestamp
434426
of when plant feedback was last given.
435427
436428
Args:
437-
auspost: Data encapsulating the current state of the program.
429+
user: Data encapsulating the current state of the program.
438430
439431
Returns:
440432
(bool): True, always. If you get a False return value, then something has gone VERY wrong.
441433
442434
Requires:
443-
! auspost.is_failed()
435+
! user.is_failed()
444436
Ensures:
445-
! auspost.is_failed()
437+
! user.is_failed()
446438
"""
447439
print("<!> handle_plant_feedback()")
448440

449441
now = datetime.now()
450442

451-
if now > auspost.get_last_plant_time() + HANDLE_PLANT_FEEDBACK_TIMEOUT:
443+
if now > user.get_last_plant_time() + HANDLE_PLANT_FEEDBACK_TIMEOUT:
452444
# Get the most recent posture data for the user
453445
recent_posture_data = get_user_postures(
454-
auspost.get_user_id(),
446+
user.get_user_id(),
455447
num=-1,
456448
period_start=now - GET_POSTURE_DATA_TIMEOUT,
457449
period_end=now,
@@ -460,7 +452,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
460452
# Conditions for exiting early
461453
if len(recent_posture_data) == 0:
462454
print("<!> Exiting handle_plant_feedback() early: No data")
463-
auspost.set_last_plant_time(datetime.now())
455+
user.set_last_plant_time(datetime.now())
464456
return True
465457

466458
average_prop_in_frame = sum(
@@ -470,7 +462,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
470462
print(
471463
"<!> Exiting handle_plant_feedback() early: Not in frame for a high enough proportion of time."
472464
)
473-
auspost.set_last_plant_time(datetime.now())
465+
user.set_last_plant_time(datetime.now())
474466
return True
475467

476468
# Calculate average proportion
@@ -484,7 +476,7 @@ def handle_plant_feedback(auspost: ControlledData) -> bool:
484476
else:
485477
hardware.set_plant_height(hardware.plant_height - 1)
486478

487-
auspost.set_last_plant_time(datetime.now())
479+
user.set_last_plant_time(datetime.now())
488480

489481
return True
490482

0 commit comments

Comments
 (0)