-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
1137 lines (994 loc) · 48.6 KB
/
application.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import importlib
import inspect
import os.path
import sys
from argparse import ArgumentParser
from datetime import timedelta
from http import HTTPStatus
from os.path import join, expanduser
from typing import List, Optional, Tuple, Callable, Any, Union
import flask
from flask import Flask, request, abort, Response
from flask_cors import CORS
from raspberry_py.gpio import Component, setup
from raspberry_py.gpio.freenove.smart_car import Car
from raspberry_py.gpio.lights import LED
from raspberry_py.gpio.motors import DcMotor, Servo, Stepper
from raspberry_py.gpio.power import Relay
from raspberry_py.gpio.robotics import RaspberryPyArm, RaspberryPyElevator
from raspberry_py.gpio.sensors import Thermistor, Photoresistor, UltrasonicRangeFinder, Camera, MjpgStreamer, Tachometer
from raspberry_py.gpio.sounds import ActiveBuzzer
LEFT_ARROW_KEYS = ['Left', 'ArrowLeft']
RIGHT_ARROW_KEYS = ['Right', 'ArrowRight']
UP_ARROW_KEYS = ['Up', 'ArrowUp']
DOWN_ARROW_KEYS = ['Down', 'ArrowDown']
SPACE_KEY = [' ']
class RpyFlask(Flask):
"""
Extension of Flask that adds raspberry-py GPIO components.
"""
def add_component(
self,
component: Component,
write: bool
):
"""
Add a component to the app. Any component that needs to be reached via REST API must be added to the app.
:param component: Component.
:param write: Whether to write component files when write_component_files is called from the command line.
"""
self.id_component[component.id] = component
if write:
self.components_to_write.append(component)
# add certain components recursively, but do not write the component files for them. the write_component_files
# function handles the generation of html/javascript elements for these components in a particular way.
if (
isinstance(component, Car) or
isinstance(component, RaspberryPyArm)
):
for component in component.get_components():
self.add_component(component, False)
def write_component_files(
self,
rest_host: str,
rest_port: int,
dir_path: str
):
"""
Write files for raspberry-py components in the current application.
:param rest_host: Host serving the REST application.
:param rest_port: Port serving the REST application.
:param dir_path: Directory in which to write files (will be created if it does not exist).
"""
if not os.path.exists(dir_path):
os.mkdir(dir_path)
for component in self.components_to_write:
for element_id, element_content in self.get_ui_elements(component):
# element id can be either a string (which is assumed to be html) or a 2-tuple of the id and extension.
if isinstance(element_id, str):
file_name = f'{element_id}.html'
elif isinstance(element_id, tuple) and len(element_id) == 2:
file_name = '.'.join(element_id)
else:
raise ValueError(f'Invalid element id: {element_id}')
path = join(dir_path, file_name)
with open(path, 'w') as component_file:
component_file.write(f'{element_content}\n')
print(f'Wrote {path}')
globals_path = join(dir_path, 'globals.js')
with open(globals_path, 'w') as globals_file:
globals_file.writelines([
f'export const rest_host = "{rest_host}";\n',
f'export const rest_port = {rest_port};'
])
print(f'Wrote {globals_path}')
utils_path = join(dir_path, 'utils.js')
with open(utils_path, 'w') as utils_file:
utils_file.write("""
const element_id_call_time = {};
const element_id_latency = {};
const element_id_alpha = {};
export function init_latency(element_id, alpha) {
element_id_latency[element_id] = null;
element_id_alpha[element_id] = alpha;
}
export function set_call_time(element_id) {
element_id_call_time[element_id] = Date.now()
}
export function update_latency(element_id) {
const latency = Date.now() - element_id_call_time[element_id];
const alpha = element_id_alpha[element_id];
let running_latency = element_id_latency[element_id];
if (running_latency === null) {
running_latency = latency;
}
else {
running_latency = alpha * running_latency + (1.0 - alpha) * latency;
}
element_id_latency[element_id] = running_latency;
return running_latency;
}
export async function is_checked(element) {
while (!element.is(":checked")) {
await new Promise(r => setTimeout(r, 1000));
}
}
""")
print(f'Wrote {utils_path}')
@staticmethod
def get_ui_elements(
component: Component
) -> List[Tuple[Union[str, Tuple[str, str]], str]]:
"""
Get UI elements for a component.
:param component: Component.
:return: List of 2-tuples of (1) element keys and (2) element content for the component.
"""
if isinstance(component, LED):
elements = [
RpyFlask.get_switch(component.id, component.turn_on, component.turn_off, None, component.is_on())
]
elif isinstance(component, Relay):
elements = [
RpyFlask.get_switch(component.id, component.close, component.open, None, False)
]
elif isinstance(component, DcMotor):
curr_state: DcMotor.State = component.state
elements = [
RpyFlask.get_switch(component.id, component.start, component.stop, None, curr_state.on),
RpyFlask.get_range(component.id, component.min_speed, component.max_speed, 1, component.get_speed(), False, False, [], [], [], False, component.set_speed, None, False)
]
elif isinstance(component, Servo):
curr_state: Servo.State = component.state
elements = [
RpyFlask.get_switch(component.id, component.start, component.stop, None, curr_state.on),
RpyFlask.get_range(component.id, int(component.min_degree), int(component.max_degree), 1, int(component.get_degrees()), False, False, [], [], [], False, component.set_degrees, None, False)
]
elif isinstance(component, Stepper):
elements = [
RpyFlask.get_switch(component.id, component.start, component.stop, None, False)
]
elif isinstance(component, Photoresistor):
elements = [
RpyFlask.get_label(component.id, component.get_light_level, timedelta(seconds=1), None, None, None)
]
elif isinstance(component, Tachometer):
elements = [
RpyFlask.get_label(component.id, component.get_rps, timedelta(seconds=1), None, None, None)
]
elif isinstance(component, Thermistor):
elements = [
RpyFlask.get_label(component.id, component.get_temperature_f, timedelta(seconds=1), None, None, None)
]
elif isinstance(component, UltrasonicRangeFinder):
elements = [
RpyFlask.get_label(component.id, component.measure_distance_once, timedelta(seconds=1), None, None, None)
]
elif isinstance(component, ActiveBuzzer):
elements = [
RpyFlask.get_button(component.id, component.buzz, None, component.stop, None, None, None)
]
elif isinstance(component, Camera):
elements = [
RpyFlask.get_image(component.id, component.width, component.capture_image, timedelta(seconds=1.0 / component.fps), None)
]
elif isinstance(component, Car):
power_id, power_element = RpyFlask.get_switch(component.id, component.start, component.stop, 'Power', component.on)
elements = [
RpyFlask.get_range(component.camera_pan_servo.id, int(component.camera_pan_servo.min_degree), int(component.camera_pan_servo.max_degree), 3, int(component.camera_pan_servo.get_degrees()), False, False, ['s'], ['f'], ['r'], False, component.camera_pan_servo.set_degrees, 'Pan', True),
RpyFlask.get_range(component.camera_tilt_servo.id, int(component.camera_tilt_servo.min_degree), int(component.camera_tilt_servo.max_degree), 3, int(component.camera_tilt_servo.get_degrees()), False, False, ['d'], ['e'], ['r'], False, component.camera_tilt_servo.set_degrees, 'Tilt', True),
RpyFlask.get_range(component.id, component.min_speed, component.max_speed, 1, 0, True, False, DOWN_ARROW_KEYS, UP_ARROW_KEYS, SPACE_KEY, True, component.set_speed, '', False),
RpyFlask.get_range(component.id, int(component.wheel_min_speed / 2.0), int(component.wheel_max_speed / 2.0), 1, 0, True, True, RIGHT_ARROW_KEYS, LEFT_ARROW_KEYS, SPACE_KEY, True, component.set_differential_speed, '', False),
RpyFlask.get_label(component.range_finder.id, component.range_finder.measure_distance_once, timedelta(seconds=1), 'Range (cm)', power_id, 1),
RpyFlask.get_button(component.buzzer.id, component.buzzer.buzz, None, component.buzzer.stop, None, 'h', 'Horn'),
(power_id, power_element),
RpyFlask.get_switch(component.id, component.enable_face_tracking, component.disable_face_tracking, 'Face Tracking', component.track_faces),
RpyFlask.get_switch(component.id, component.enable_light_tracking, component.disable_light_tracking, 'Light Tracking', component.track_light),
RpyFlask.get_label(component.id, component.get_battery_percent, timedelta(seconds=10), 'Battery (%)', power_id, 1)
]
if isinstance(component.camera, Camera):
camera_id, camera_element = RpyFlask.get_image(component.camera.id, component.camera.width, component.camera.capture_image, None, power_id)
camera_elements = [
(camera_id, camera_element),
RpyFlask.get_range_html_attribute(camera_id, 'width', 100, 800, 10, component.camera.width, 'Display Size '),
RpyFlask.get_range(component.camera.id, 1, 5, 1, 1, False, False, [], [], [], False, component.camera.multiply_resolution, 'Display Resolution', False),
RpyFlask.get_switch(component.camera.id, component.camera.enable_face_detection, component.camera.disable_face_detection, 'Face Detection', component.camera.run_face_detection),
RpyFlask.get_switch(component.camera.id, component.camera.enable_face_circles, component.camera.disable_face_circles, 'Face Circles', component.camera.circle_detected_faces)
]
elements.extend(camera_elements)
elif isinstance(component.camera, MjpgStreamer):
elements.append(RpyFlask.get_mjpg_streamer(component.camera.id, component.camera.height, None, power_id, component.camera.port))
else:
raise ValueError(f'Unknown camera: {component.camera}')
if component.connection_blackout_tolerance_seconds is not None:
blackout_id, blackout_element = RpyFlask.get_repeater(component.id, component.connection_heartbeat, timedelta(seconds=component.connection_blackout_tolerance_seconds / 4.0))
elements.append(((blackout_id, 'js'), blackout_element))
elif isinstance(component, RaspberryPyArm):
elements = [
RpyFlask.get_range(component.base_rotator_servo.id, int(component.base_rotator_servo.min_degree), int(component.base_rotator_servo.max_degree), 1, int(component.base_rotator_servo.get_degrees()), False, False, ['j'], ['k'], ['p'], False, component.base_rotator_servo.set_degrees, 'Base', False),
RpyFlask.get_range(component.arm_elevator_servo.id, int(component.arm_elevator_servo.min_degree), int(component.arm_elevator_servo.max_degree), 1, int(component.arm_elevator_servo.get_degrees()), False, False, ['u'], ['m'], ['p'], False, component.arm_elevator_servo.set_degrees, 'Arm Elevation', False),
RpyFlask.get_range(component.wrist_elevator_servo.id, int(component.wrist_elevator_servo.min_degree), int(component.wrist_elevator_servo.max_degree), 1, int(component.wrist_elevator_servo.get_degrees()), False, False, ['i'], [','], ['p'], False, component.wrist_elevator_servo.set_degrees, 'Wrist Elevation', False),
RpyFlask.get_range(component.wrist_rotator_servo.id, int(component.wrist_rotator_servo.min_degree), int(component.wrist_rotator_servo.max_degree), 1, int(component.wrist_rotator_servo.get_degrees()), False, False, ['l'], [';'], ['p'], False, component.wrist_rotator_servo.set_degrees, 'Wrist Rotation', False),
RpyFlask.get_range(component.pinch_servo.id, int(component.pinch_servo.min_degree), int(component.pinch_servo.max_degree), 1, int(component.pinch_servo.get_degrees()), False, False, ['.'], ['o'], ['p'], False, component.pinch_servo.set_degrees, 'Pinch', False)
]
elif isinstance(component, RaspberryPyElevator):
elements = [
RpyFlask.get_button(component.id, component.move_up_1_mm_1_sec, None, None, None, 'MetaRight', 'Move up'),
RpyFlask.get_button(component.id, component.move_down_1_mm_1_sec, None, None, None, 'MetaLeft', 'Move down')
]
else:
raise ValueError(f'Unknown component type: {type(component)}')
return elements
@staticmethod
def get_switch(
component_id: str,
on_function: Callable[[], None],
off_function: Callable[[], None],
text: Optional[str],
initially_on: bool
) -> Tuple[str, str]:
"""
Get switch UI element.
:param component_id: Component id.
:param on_function: Function to call when switch is flipped on.
:param off_function: Function to call when switch is flipped off.
:param text: Readable text to display.
:param initially_on: Initially on.
:return: 2-tuple of (1) element id and (2) UI element.
"""
on_function_name = on_function.__name__
off_function_name = off_function.__name__
if text is None:
text = f'{component_id} {on_function_name}/{off_function_name}'
element_id = f'{component_id}-{on_function_name}-{off_function_name}'
element_var = element_id.replace('-', '_')
checked = ' checked ' if initially_on else ''
return (
element_id,
(
f'<div class="form-check form-switch">\n'
f' <label class="form-check-label" for="{element_id}">{text}</label>\n'
f' <input class="form-check-input" type="checkbox" role="switch" id="{element_id}"{checked}/>\n'
f'</div>\n'
f'<script type="module">\n'
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'const {element_var} = $("#{element_id}");\n'
f'{element_var}.on("change", function () {{\n'
f' $.ajax({{\n'
f' url: {element_var}.is(":checked") ? "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{on_function_name}" : "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{off_function_name}",\n'
f' type: "GET"\n'
f' }});\n'
f'}});\n'
f'</script>'
)
)
@staticmethod
def get_range(
component_id: str,
min_value: int,
max_value: int,
step: int,
initial_value: int,
reset_to_initial_value_upon_release: bool,
zero_range_upon_direction_change: bool,
decrement_keys: List[str],
increment_keys: List[str],
reset_to_initial_value_keys: List[str],
vertical: bool,
on_input_function: Callable[[int], None],
text: Optional[str],
shift_sets_extreme: bool
) -> Tuple[str, str]:
"""
Get range UI element.
:param component_id: Component id.
:param min_value: Minimum value.
:param max_value: Maximum value.
:param step: Step.
:param initial_value: Initial value.
:param reset_to_initial_value_upon_release: Whether to reset to the initial value upon release of the mouse
button or touch gesture (whichever is being used).
:param zero_range_upon_direction_change: Whether to zero the range upon change of direction. For example, if the
range has a positive value and the decrement key is pressed, then the range will decrement from zero rather than
from its current value.
:param decrement_keys: Keyboard keys that decrement the range value.
:param increment_keys: Keyboard keys that increment the range value.
:param reset_to_initial_value_keys: Keyboard keys that reset the range to the initial value.
:param vertical: Whether the range should be displayed vertically.
:param on_input_function: Function to call when range value changes.
:param text: Readable text to display.
:param shift_sets_extreme: Whether holding shift while pressing an increment/decrement key sets the servo to its
extreme value.
:return: 2-tuple of (1) element id and (2) UI element.
"""
on_input_function_name = on_input_function.__name__
if text is None:
text = f'{component_id} {on_input_function_name}'
element_id = f'{component_id}-{on_input_function_name}'
element_var = f'{element_id.replace("-", "_")}_element'
range_var = f'{element_id.replace("-", "_")}_range'
# get the parameter name to set
non_self_params = [
param_name
for param_name, param in inspect.signature(on_input_function).parameters.items()
if param_name != 'self' and str(param.default) == "<class 'inspect._empty'>" # only consider params without default values
]
if len(non_self_params) == 1:
value_param = non_self_params[0]
else:
raise ValueError('Function for range must contain exactly 1 parameter.')
vertical_style = ""
if vertical:
vertical_style = ' orient="vertical"'
js_set_value_function_name = element_id.replace('-', '_')
js_current_value = f'current_{value_param}'
js_new_value = f'new_{value_param}'
mouse_touch_release_event = ''
if reset_to_initial_value_upon_release:
mouse_touch_release_event = (
f'{element_var}.on("mouseup touchend", function () {{\n'
f' {js_set_value_function_name}({initial_value}, true);\n'
f'}});\n'
)
decrement_case = "\n".join([f' case \"{k}\":' for k in decrement_keys])
if decrement_case != '':
decrement_zero_range = ''
if zero_range_upon_direction_change:
decrement_zero_range = (
f' if ({js_current_value} > 0) {{\n'
f' {js_current_value} = 1;\n'
f' }}\n'
)
decrement_case += (
f'\n'
f'{decrement_zero_range}'
f' {js_new_value} = {js_current_value} - {step};\n'
f' break;\n'
)
decrement_shift_case = "\n".join([f' case \"{k.upper()}\":' for k in decrement_keys if shift_sets_extreme])
if decrement_shift_case != '':
decrement_shift_case += (
f'\n'
f' new_degrees = {min_value};\n'
f' break;\n'
)
increment_case = "\n".join([f' case \"{k}\":' for k in increment_keys])
if increment_case != '':
increment_zero_range = ''
if zero_range_upon_direction_change:
increment_zero_range = (
f' if ({js_current_value} < 0) {{\n'
f' {js_current_value} = -1;\n'
f' }}\n'
)
increment_case += (
f'\n'
f'{increment_zero_range}'
f' {js_new_value} = {js_current_value} + {step};\n'
f' break;\n'
)
increment_shift_case = "\n".join([f' case \"{k.upper()}\":' for k in increment_keys if shift_sets_extreme])
if increment_shift_case != '':
increment_shift_case += (
f'\n'
f' new_degrees = {max_value};\n'
f' break;\n'
)
reset_case = "\n".join([f' case \"{k}\":' for k in reset_to_initial_value_keys])
if reset_case != '':
reset_case += (
f'\n'
f' {js_new_value} = {initial_value};\n'
f' break;\n'
)
window_key_down_event_listener = ''
if len(decrement_case) + len(increment_case) + len(reset_case) > 0:
window_key_down_event_listener = (
f'window.addEventListener("keydown", (event) => {{\n'
f' let {js_current_value} = parseInt({range_var}.value);\n'
f' let {js_new_value} = {js_current_value};\n'
f' switch (event.key) {{\n'
f'{decrement_case}'
f'{decrement_shift_case}'
f'{increment_case}'
f'{increment_shift_case}'
f'{reset_case}'
f' }}\n'
f' if ({js_new_value} !== {js_current_value}) {{\n'
f' {js_set_value_function_name}({js_new_value}, true);\n'
f' }}\n'
f' event.preventDefault();\n'
f'}}, true);\n'
)
window_key_up_event_listener = ''
if shift_sets_extreme and len(increment_keys + decrement_keys) > 0:
key_cases = "\n".join([f' case \"{k.upper()}\":' for k in increment_keys + decrement_keys])
window_key_up_event_listener = (
f'window.addEventListener("keyup", (event) => {{\n'
f' let {js_current_value} = parseInt({range_var}.value);\n'
f' let {js_new_value} = {js_current_value};\n'
f' switch (event.key) {{\n'
f'{key_cases}\n'
f' {js_new_value} = {initial_value};\n'
f' break;\n'
f' }}\n'
f' if ({js_new_value} !== {js_current_value}) {{\n'
f' {js_set_value_function_name}({js_new_value}, true);\n'
f' }}\n'
f' event.preventDefault();\n'
f'}}, true);\n'
)
return (
element_id,
(
f'<div class="range">\n'
f' <label for="{element_id}" class="form-label">{text}</label>\n'
f' <input type="range"{vertical_style} class="form-range" min="{min_value}" max="{max_value}" step="{step}" value="{initial_value}" id="{element_id}" />\n'
f'</div>\n'
f'<script type="module">\n'
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'const {element_var} = $("#{element_id}");\n'
f'const {range_var} = {element_var}[0];\n'
f'function {js_set_value_function_name} ({value_param}, set_range) {{\n'
f' if ({value_param} < {min_value} || {value_param} > {max_value}) {{\n'
f' return;\n'
f' }}\n'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{on_input_function_name}?{value_param}=int:" + {value_param},\n'
f' type: "GET"\n'
f' }});\n'
f' if (set_range) {{\n'
f' {range_var}.value = {value_param};\n'
f' }}\n'
f'}}\n'
f'{element_var}.on("input", function () {{\n'
f' {js_set_value_function_name}({range_var}.value, false);\n'
f'}});\n'
f'{mouse_touch_release_event}'
f'{window_key_down_event_listener}'
f'{window_key_up_event_listener}'
f'</script>'
)
)
@staticmethod
def get_range_html_attribute(
element_id: str,
element_attribute: str,
min_value: int,
max_value: int,
step: int,
initial_value: int,
text: Optional[str]
) -> Tuple[str, str]:
"""
Get range UI element that sets an attribute on another HTML element.
:param element_id: ID of element to set attribute for.
:param element_attribute: Name of attribute to set.
:param min_value: Minimum value.
:param max_value: Maximum value.
:param step: Step.
:param initial_value: Initial value.
:param text: Readable text to display.
:return: 2-tuple of (1) element id and (2) UI element.
"""
element_var = element_id.replace('-', '_')
range_element_id = f'{element_id}-{element_attribute}'
range_element_var = f'{range_element_id.replace("-", "_")}_element'
range_var = f'{range_element_id.replace("-", "_")}_range'
if text is None:
text = range_element_id
return (
range_element_id,
(
f'<div class="range">\n'
f' <label for="{range_element_id}" class="form-label">{text}</label>\n'
f' <input type="range" class="form-range" min="{min_value}" max="{max_value}" step="{step}" value="{initial_value}" id="{range_element_id}" />\n'
f'</div>\n'
f'<script type="module">\n'
f'const {element_var} = $("#{element_id}")[0];\n'
f'const {range_element_var} = $("#{range_element_id}");\n'
f'const {range_var} = {range_element_var}[0];\n'
f'{range_element_var}.on("input", function () {{\n'
f' {element_var}.{element_attribute} = {range_var}.value\n'
f'}});\n'
f'</script>'
)
)
@staticmethod
def get_label(
component_id: str,
function: Callable[[], Any],
refresh_interval: timedelta,
text: Optional[str],
pause_for_checkbox_id: Optional[str],
float_precision: Optional[int]
) -> Tuple[str, str]:
"""
Get label that refreshes periodically.
:param component_id: Component id.
:param function: Function to call to obtain new value.
:param refresh_interval: How long to wait between refresh calls.
:param text: Readable text to display.
:param pause_for_checkbox_id: HTML identifier of checkbox to pause for before getting label.
:param float_precision: Floating-point precision to display, or None if the returned value is not a float or it
should be displayed with full precision.
:return: 2-tuple of (1) element id and (2) UI element.
"""
function_name = function.__name__
element_id = f'{component_id}-{function_name}'
label_element = element_id.replace('-', '_')
read_value_function_name = f'read_value_{element_id}'.replace('-', '_')
if text is None:
text = function_name
pause_import_javascript = ''
pause_element_javascript = ''
pause_await_javascript = ''
if pause_for_checkbox_id is not None:
pause_import_javascript = 'import {is_checked} from "./utils.js";\n'
pause_var_javascript = pause_for_checkbox_id.replace('-', '_')
pause_element_javascript = f'const {pause_var_javascript} = $("#{pause_for_checkbox_id}");\n'
pause_await_javascript = f' await is_checked({pause_var_javascript});\n'
float_precision_javascript = ''
if float_precision is not None:
float_precision_javascript = (
f' if (return_value !== null) {{\n'
f' return_value = return_value.toFixed({float_precision});\n'
f' }}\n'
)
return (
element_id,
(
f'<div>\n'
f' <label id="{element_id}">{text}: None</label>\n'
f'</div>\n'
f'<script type="module">\n'
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'{pause_import_javascript}'
f'const {label_element} = $("#{element_id}")[0];\n'
f'{pause_element_javascript}'
f'async function {read_value_function_name}() {{\n'
f'{pause_await_javascript}'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{function_name}",\n'
f' type: "GET",\n'
f' success: async function (return_value) {{\n'
f'{float_precision_javascript}'
f' if (return_value === null) {{\n'
f' return_value = "None";\n'
f' }}\n'
f' {label_element}.innerHTML = "{text}: " + return_value;\n'
f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
f' await {read_value_function_name}();\n'
f' }},\n'
f' error: async function(xhr, error){{\n'
f' console.log(error);\n'
f' {label_element}.innerHTML = "{text}: None";\n'
f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
f' await {read_value_function_name}();\n'
f' }}\n'
f' }});\n'
f'}}\n'
f'{read_value_function_name}();\n'
f'</script>'
)
)
@staticmethod
def get_image(
component_id: str,
width: int,
function: Callable[[], Any],
refresh_interval: Optional[timedelta],
pause_for_checkbox_id: Optional[str]
) -> Tuple[str, str]:
"""
Get image that refreshes periodically.
:param component_id: Component id.
:param width: Initial width of HTML image.
:param function: Function to call to obtain new image.
:param refresh_interval: How long to wait between refresh calls, or None for no interval.
:param pause_for_checkbox_id: HTML identifier of checkbox to pause for before capturing image.
:return: 2-tuple of (1) element id and (2) UI element.
"""
function_name = function.__name__
element_id = f'{component_id}-{function_name}'
latency_label_id = f'{component_id}-latency'
latency_label_var = latency_label_id.replace('-', '_')
capture_function_name = f'capture_{element_id}'.replace('-', '_')
img_alt = element_id.replace('_', '-')
image_element = element_id.replace('-', '_')
refresh_interval_javascript = ''
if refresh_interval is not None:
refresh_interval_javascript = f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
pause_import_javascript = ''
pause_element_javascript = ''
pause_await_javascript = ''
if pause_for_checkbox_id is not None:
pause_import_javascript = 'import {is_checked} from "./utils.js";\n'
pause_var_javascript = pause_for_checkbox_id.replace('-', '_')
pause_element_javascript = f'const {pause_var_javascript} = $("#{pause_for_checkbox_id}");\n'
pause_await_javascript = f' await is_checked({pause_var_javascript});\n'
return (
element_id,
(
f'<div style="text-align: center">\n'
f' <img alt="{img_alt}" id="{element_id}" width="{width}" src="" />\n'
f' <label id="{latency_label_id}">Latency (ms): None</label>\n'
f'</div>\n'
f'<script type="module">\n'
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'{pause_import_javascript}'
f'import {{init_latency, set_call_time, update_latency}} from "./utils.js";\n'
f'const {latency_label_var} = $("#{latency_label_id}")[0];\n'
f'init_latency("{latency_label_id}", 0.99);\n'
f'const {image_element} = $("#{element_id}")[0];\n'
f'{pause_element_javascript}'
f'async function {capture_function_name}() {{\n'
f'{pause_await_javascript}'
f' set_call_time("{latency_label_id}");\n'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{function_name}",\n'
f' type: "GET",\n'
f' success: async function (return_value) {{\n'
f' {image_element}.src = "data:image/jpg;base64," + return_value;\n'
f' {latency_label_var}.innerHTML = "Latency (ms): " + update_latency("{latency_label_id}").toFixed(0);\n'
f'{refresh_interval_javascript}'
f' await {capture_function_name}();\n'
f' }},\n'
f' error: async function(xhr, error){{\n'
f' console.log(error);\n'
f'{refresh_interval_javascript}'
f' await {capture_function_name}();\n'
f' }}\n'
f' }});\n'
f'}}\n'
f'{capture_function_name}();\n'
f'</script>'
)
)
@staticmethod
def get_button(
component_id: str,
pressed_function: Optional[Callable[[], Any]],
pressed_query: Optional[str],
released_function: Optional[Callable[[], Any]],
released_query: Optional[str],
key: Optional[str],
text: Optional[str]
) -> Tuple[str, str]:
"""
Get button.
:param component_id: Component id.
:param pressed_function: Function to call when the button is pressed.
:param pressed_query: Query to submit with pressed_function call, or None for no query.
:param released_function: Function to call when the button is released.
:param released_query: Query to submit with released_function call, or None for no query.
:param key: Key that, when pressed/released, will trigger the associated functions.
:param text: Readable text to display.
:return: 2-tuple of (1) element id and (2) UI element.
"""
if pressed_function is None and released_function is None:
raise ValueError('Either pressed or released function must be provided.')
if text is None:
text = component_id
pressed_function_name = None if pressed_function is None else pressed_function.__name__
released_function_name = None if released_function is None else released_function.__name__
element_id = '-'.join(filter(None, [component_id, pressed_function_name, released_function_name]))
element_var = element_id.replace('-', '_')
pressed_function_js = ''
pressed_events_js = ''
if pressed_function is not None:
if pressed_query is not None and len(pressed_query) > 0:
pressed_query = f'?{pressed_query}'
else:
pressed_query = ''
pressed_function_name_js = f'on_press_{pressed_function_name}'
pressed_function_js = (
f'function {pressed_function_name_js} () {{\n'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{pressed_function_name}{pressed_query}",\n'
f' type: "GET"\n'
f' }});\n'
f'}}\n'
)
pressed_events_js = (
f'{element_var}.on("mousedown touchstart", function () {{\n'
f' {pressed_function_name_js}();\n'
f'}});\n'
)
if key is not None:
if 'Meta' in key:
switch_value = 'event.code'
else:
switch_value = 'event.key'
pressed_events_js += (
f'window.addEventListener("keydown", (event) => {{\n'
f' switch ({switch_value}) {{\n'
f' case "{key}":\n'
f' {pressed_function_name_js}();\n'
f' break;\n'
f' }}\n'
f' event.preventDefault();\n'
f'}}, true);\n'
)
released_function_js = ''
released_events_js = ''
if released_function is not None:
if released_query is not None and len(released_query) > 0:
released_query = f'?{released_query}'
else:
released_query = ''
released_function_name_js = f'on_release_{released_function_name}'
released_function_js = (
f'function {released_function_name_js} () {{\n'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{released_function_name}{released_query}",\n'
f' type: "GET"\n'
f' }});\n'
f'}}\n'
)
released_events_js = (
f'{element_var}.on("mouseup touchend", function () {{\n'
f' {released_function_name_js}();\n'
f'}});\n'
)
if key is not None:
if 'Meta' in key:
switch_value = 'event.code'
else:
switch_value = 'event.key'
released_events_js += (
f'window.addEventListener("keyup", (event) => {{\n'
f' switch ({switch_value}) {{\n'
f' case "{key}":\n'
f' {released_function_name_js}();\n'
f' break;\n'
f' }}\n'
f' event.preventDefault();\n'
f'}}, true);\n'
)
return (
element_id,
(
f'<button type="button" class="btn btn-primary" id="{element_id}">{text}</button>\n'
f'<script type="module">\n'
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'const {element_var} = $("#{element_id}");\n'
f'{pressed_function_js}'
f'{pressed_events_js}'
f'{released_function_js}'
f'{released_events_js}'
f'</script>'
)
)
@staticmethod
def get_repeater(
component_id: str,
function: Callable[[], Any],
refresh_interval: timedelta
) -> Tuple[str, str]:
"""
Get a script that periodically calls a function.
:param component_id: Component id.
:param function: Function to call to obtain new value.
:param refresh_interval: How long to wait between refresh calls.
:return: 2-tuple of (1) element id and (2) UI element.
"""
function_name = function.__name__
element_id = f'{component_id}-{function_name}'
js_function_name = f'{element_id}'.replace('-', '_')
return (
element_id,
(
f'import {{rest_host, rest_port}} from "./globals.js";\n'
f'async function {js_function_name}() {{\n'
f' $.ajax({{\n'
f' url: "http://" + rest_host + ":" + rest_port + "/call/{component_id}/{function_name}",\n'
f' type: "GET",\n'
f' success: async function (_) {{\n'
f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
f' await {js_function_name}();\n'
f' }},\n'
f' error: async function(xhr, error){{\n'
f' console.log(error);\n'
f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
f' await {js_function_name}();\n'
f' }}\n'
f' }});\n'
f'}}\n'
f'{js_function_name}();'
)
)
@staticmethod
def get_mjpg_streamer(
component_id: str,
height: int,
refresh_interval: Optional[timedelta],
pause_for_checkbox_id: Optional[str],
port: int
):
"""
Get connection to the mjpg_streamer application.
:param component_id: Component id.
:param height: Height of image.
:param refresh_interval: How long to wait between refresh calls, or None for no interval.
:param pause_for_checkbox_id: HTML identifier of checkbox to pause for before capturing image.
:param port: Port that mjpg_streamer runs on.
:return: 2-tuple of (1) element id and (2) UI element.
"""
element_id = f'{component_id}-mjpg_streamer'
image_element = element_id.replace('-', '_')
insert_new_image_function_name = f'insert_new_image_{image_element}'
refresh_interval_javascript = ''
if refresh_interval is not None:
refresh_interval_javascript = f' await new Promise(r => setTimeout(r, {refresh_interval.total_seconds() * 1000}));\n'
pause_import_javascript = ''
pause_element_javascript = ''
pause_await_javascript = ''
if pause_for_checkbox_id is not None:
pause_import_javascript = 'import {is_checked} from "./utils.js";\n'
pause_var_javascript = pause_for_checkbox_id.replace('-', '_')
pause_element_javascript = f'const {pause_var_javascript} = $("#{pause_for_checkbox_id}");\n'
pause_await_javascript = f' await is_checked({pause_var_javascript});\n'
return (
element_id,
(
f'<div style="height:{height}px;display:flex;justify-content:center" id="{element_id}"></div>\n'
f'<script type="module">\n'
f'import {{rest_host}} from "./globals.js";\n'
f'{pause_import_javascript}'
f'{pause_element_javascript}'
f'let image_id = 0;\n'
f'let loaded_images = [];\n'
f'async function {insert_new_image_function_name}() {{\n'
f'{pause_await_javascript}'
f' let img = new Image();\n'
f' img.style.position = "absolute";\n'
f' img.style.zIndex = "-1";\n'
f' img.onload = image_loaded;\n'
f' img.onerror = image_error;\n'
f' img.src = "http://" + rest_host + ":{port}/?action=snapshot&n=" + (++image_id);\n'
f' let {image_element} = document.getElementById("{element_id}");\n'
f' {image_element}.insertBefore(img, {image_element}.firstChild);\n'
f'{refresh_interval_javascript}'
f'}}\n'
f'async function image_loaded() {{\n'
f' this.style.zIndex = image_id;\n'
f' while (1 < loaded_images.length) {{\n'
f' let image_to_remove = loaded_images.shift();\n'
f' image_to_remove.parentNode.removeChild(image_to_remove);\n'
f' }}\n'
f' loaded_images.push(this);\n'
f' await {insert_new_image_function_name}();\n'
f'}}\n'
f'async function image_error() {{\n'
f' await new Promise(r => setTimeout(r, 500.0));\n'
f' await {insert_new_image_function_name}();\n'
f'}}\n'
f'{insert_new_image_function_name}();\n'
f'</script>'
)
)
def __init__(
self,
import_name: str
):
"""
Initialize the RpyFlask.
:param import_name: Import name. See Flask documentation.
"""
super().__init__(import_name=import_name)
self.id_component = {}
self.components_to_write = []
app = RpyFlask(__name__)
# allow cross-site access from an html front-end
CORS(app)
# set up gpio