Skip to content
Snippets Groups Projects
Commit b176c6ad authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Added comments and example.

Test program done ;)
parent fc2b7668
No related branches found
No related tags found
No related merge requests found
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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment