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

Push until tomorrow

parent 054270cf
Branches
No related tags found
No related merge requests found
......@@ -2,9 +2,9 @@ package main
import (
"log/slog"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
......@@ -15,19 +15,52 @@ type Counter struct {
}
var (
devices map[uuid.UUID]Counter
devices map[uuid.UUID]Counter
deviceIcons = map[string]string{
"lamp": "💡",
"contact": "🚪",
"motion": "🚶",
}
)
func getCounter(msg *xaal.Message) *Counter {
cnt, found := devices[msg.Source]
if found {
return &cnt
}
return nil
}
func addCounter(msg *xaal.Message) {
cnt := Counter{
Type: msg.DevType,
}
devices[msg.Source] = cnt
}
func filterCounter(msg *xaal.Message) bool {
// let's split the msg.DevType on points and keep the first part
tmp := strings.Split(msg.DevType, ".")
prefix := tmp[0]
_, found := deviceIcons[prefix]
return found
}
func handleMessage(msg *xaal.Message) {
if filterCounter(msg) == false {
return
}
msg.Dump()
if msg.IsAlive() {
timeout := msg.Body["timeout"]
if timeout == nil {
return
if timeout, ok := msg.Body["timeout"].(uint64); ok {
cnt := getCounter(msg)
if cnt != nil {
cnt.Until = time.Now().Add(time.Duration(timeout) * time.Second)
} else {
addCounter(msg)
}
}
slog.Debug("Received alive message from", "timeout", timeout)
devices[msg.Source] = Counter{Type: msg.DevType, Until: time.Now()}
// msg.Dump()
// fmt.Println()
}
}
......@@ -42,5 +75,6 @@ func main() {
devices = make(map[uuid.UUID]Counter)
eng.Subscribe(handleMessage)
eng.Run()
spew.Dump(devices)
// Let's count the devices
slog.Info("Devices", "count", len(devices))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment