Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS Fancy Clock
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TheJoeCoder
CS Fancy Clock
Commits
edea6dde
Verified
Commit
edea6dde
authored
9 months ago
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
Allow config to be overridden on specific requests
parent
303f207c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+47
-10
47 additions, 10 deletions
main.py
with
47 additions
and
10 deletions
main.py
+
47
−
10
View file @
edea6dde
...
...
@@ -99,8 +99,16 @@ def wr_set_widget():
"
error
"
:
f
"
Widget
{
widget_name
}
is not available
"
},
400
)
config_override
=
request
.
json
.
get
(
"
config
"
,
{})
if
type
(
config_override
)
is
not
dict
:
return
json_response
({
"
error
"
:
"
Config must be an Object
"
},
400
)
config_b64
=
base64
.
b64encode
(
json
.
dumps
(
config_override
).
encode
(
"
utf-8
"
)).
decode
(
"
utf-8
"
)
# Send the pattern to the LED thread
display_queue
.
put
(
"
SET_WIDGET:
"
+
widget_name
)
display_queue
.
put
(
"
SET_WIDGET:
"
+
widget_name
+
"
:
"
+
config_b64
)
# Return success
return
json_response
({
...
...
@@ -127,8 +135,16 @@ def wr_set_pattern():
"
error
"
:
f
"
Pattern
{
pattern_name
}
is not available
"
},
400
)
config_override
=
request
.
json
.
get
(
"
config
"
,
{})
if
type
(
config_override
)
is
not
dict
:
return
json_response
({
"
error
"
:
"
Config must be an Object
"
},
400
)
config_b64
=
base64
.
b64encode
(
json
.
dumps
(
config_override
).
encode
(
"
utf-8
"
)).
decode
(
"
utf-8
"
)
# Send the pattern to the LED thread
led_queue
.
put
(
"
SET_PATTERN:
"
+
pattern_name
)
led_queue
.
put
(
"
SET_PATTERN:
"
+
pattern_name
+
"
:
"
+
config_b64
)
# Return success
return
json_response
({
...
...
@@ -140,10 +156,13 @@ def wr_set_pattern():
def
wr_get_config
():
return
json_response
(
module_config
)
def
load_widget
(
widget_name
:
str
):
def
load_widget
(
widget_name
:
str
,
config_override
:
dict
=
None
):
# Get the widget properties from the config file
widget_props
=
module_config
.
get
(
widget_name
,
{}).
copy
()
if
config_override
is
not
None
:
widget_props
.
update
(
config_override
)
# Scale the properties that are supposed to be scalable
for
k
,
v
in
widget_props
.
copy
().
items
():
if
(
isinstance
(
v
,
int
)
or
isinstance
(
v
,
float
))
and
k
.
startswith
(
"
scalable_
"
):
...
...
@@ -155,8 +174,12 @@ def load_widget(widget_name: str):
# Load the widget
return
getattr
(
screenwidgets
,
widget_name
)(
widget_props
)
def
load_pattern
(
pattern_name
:
str
,
leds
:
ILedString
):
def
load_pattern
(
pattern_name
:
str
,
leds
:
ILedString
,
config_override
:
dict
=
None
):
pattern_props
=
module_config
.
get
(
pattern_name
,
{}).
copy
()
if
config_override
is
not
None
:
pattern_props
.
update
(
config_override
)
return
getattr
(
ledpatterns
,
pattern_name
)(
pattern_props
,
leds
)
def
run_screen
():
...
...
@@ -191,10 +214,17 @@ def run_screen():
# Check for messages
try
:
message
=
display_queue
.
get_nowait
()
if
message
.
startswith
(
"
SET_WIDGET:
"
):
widget_to_load
=
message
.
removeprefix
(
"
SET_WIDGET:
"
)
parts
=
message
.
split
(
"
:
"
)
if
parts
[
0
]
==
"
SET_WIDGET
"
:
widget_to_load
=
parts
[
1
]
if
widget_to_load
in
PROP_AVAILABLE_WIDGETS
:
# Check input validity
current_widget
=
load_widget
(
widget_to_load
)
config_override
=
{}
if
len
(
parts
)
==
3
:
try
:
config_override
=
json
.
loads
(
base64
.
b64decode
(
parts
[
2
]).
decode
(
"
utf-8
"
))
except
json
.
JSONDecodeError
:
pass
current_widget
=
load_widget
(
widget_to_load
,
config_override
)
except
queue
.
Empty
:
pass
...
...
@@ -228,10 +258,17 @@ def run_leds():
while
True
:
try
:
message
=
led_queue
.
get_nowait
()
if
message
.
startswith
(
"
SET_PATTERN:
"
):
pattern_name
=
message
.
removeprefix
(
"
SET_PATTERN:
"
)
parts
=
message
.
split
(
"
:
"
)
if
parts
[
0
]
==
"
SET_PATTERN
"
:
pattern_name
=
parts
[
1
]
if
pattern_name
in
PROP_AVAILABLE_PATTERNS
:
current_pattern
=
load_pattern
(
pattern_name
,
leds
)
config_override
=
{}
if
len
(
parts
)
==
3
:
try
:
config_override
=
json
.
loads
(
base64
.
b64decode
(
parts
[
2
]).
decode
(
"
utf-8
"
))
except
json
.
JSONDecodeError
:
pass
current_pattern
=
load_pattern
(
pattern_name
,
leds
,
config_override
)
pattern_timer
=
module_config
.
get
(
pattern_name
,
{}).
get
(
"
tick_rate
"
,
-
1
)
current_pattern
.
tick
()
elif
message
==
"
EXIT
"
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment