Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
apps
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
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Go
apps
Commits
b176c6ad
Commit
b176c6ad
authored
2 weeks ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Added comments and example.
Test program done ;)
parent
fc2b7668
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
misc/devices_counter.go
+25
-15
25 additions, 15 deletions
misc/devices_counter.go
with
25 additions
and
15 deletions
misc/devices_counter.go
+
25
−
15
View file @
b176c6ad
package
main
/*
This simple program counts the number of devices with a given prefix and outputs the result on stdout in JSON format.
The output use nerdfonts icons and outputs looks like this: {"text": " 1 0 0"}
This can be used with waybar to provide a nice display. You can find a sample configuration below:
"custom/devices": {
"exec": "~/.config/waybar/devices_counter",
"format": "{}",
"return-type": "json"
},
*/
import
(
"fmt"
"log/slog"
...
...
@@ -25,7 +39,7 @@ type CounterDevice struct {
var
(
deviceIcons
=
map
[
string
]
string
{
"lamp"
:
"
"
,
"lamp"
:
"
"
,
"motion"
:
" "
,
"contact"
:
" "
,
}
...
...
@@ -84,6 +98,7 @@ func (d *CounterDevice) updateCounter(msg *xaal.Message, prefix string) {
}
}
// update Alive counters
func
(
d
*
CounterDevice
)
updateAlive
(
msg
*
xaal
.
Message
)
{
cnt
:=
d
.
getCounter
(
msg
)
...
...
@@ -92,6 +107,7 @@ func (d *CounterDevice) updateAlive(msg *xaal.Message) {
}
}
// handle xAAL incomming messages
func
(
d
*
CounterDevice
)
handleMessage
(
msg
*
xaal
.
Message
)
{
// let's filter the messages we are interested in
prefix
:=
getPrefix
(
msg
)
...
...
@@ -139,25 +155,22 @@ func (d *CounterDevice) sendGetAttritube(addr uuid.UUID) {
d
.
engine
.
Send
(
msg
)
}
// display outputs the result on stdout
and flush
// display outputs the result on stdout
w/ nerdfonts icons
func
(
d
*
CounterDevice
)
display
()
{
// slog.Info("Devices", "count", len(devices))
prefixs
:=
[]
string
{
"lamp"
,
"motion"
,
"contact"
}
values
:=
""
for
_
,
prefix
:=
range
prefixs
{
fmt
.
Print
(
d
.
displayPrefix
(
prefix
))
fmt
.
Print
(
" "
)
values
+=
fmt
.
Sprintf
(
"%s%d "
,
deviceIcons
[
prefix
],
d
.
countPrefix
(
prefix
))
}
fmt
.
Print
(
"
\n
"
)
}
func
(
d
*
CounterDevice
)
displayPrefix
(
prefix
string
)
string
{
return
fmt
.
Sprintf
(
"%s%d"
,
deviceIcons
[
prefix
],
d
.
countPrefix
(
prefix
))
text
:=
fmt
.
Sprintf
(
`{"text": "%s"}`
,
strings
.
TrimRight
(
values
,
" "
))
fmt
.
Println
(
text
)
}
// ----------------------------------------------------------------------------
// Message handling functions
// -----------------------------------------------------------------------------
// getPrefix returns the prefix associated for an xAAL Message
func
getPrefix
(
msg
*
xaal
.
Message
)
string
{
// let's split the msg.DevType on points and keep the first part
tmp
:=
strings
.
Split
(
msg
.
DevType
,
"."
)
...
...
@@ -169,6 +182,7 @@ func getPrefix(msg *xaal.Message) string {
return
prefix
}
// getAttribute exacts a given attribute from an xAAL Message
func
getAttribute
(
msg
*
xaal
.
Message
,
attr
string
)
bool
{
if
value
,
ok
:=
msg
.
Body
[
attr
]
.
(
bool
);
ok
{
return
value
...
...
@@ -176,15 +190,11 @@ func getAttribute(msg *xaal.Message, attr string) bool {
return
false
}
func
init
()
{
xaal
.
SetupLogger
(
slog
.
LevelDebug
)
}
func
main
()
{
xaal
.
SetupLogger
(
slog
.
LevelWarn
)
eng
:=
xaal
.
NewEngine
()
eng
.
DisableMessageFilter
()
dev
:=
NewCounterDevice
(
eng
)
eng
.
Subscribe
(
dev
.
handleMessage
)
dev
.
sendIsAlive
()
eng
.
Run
()
...
...
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