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

Ready for refactoring.

Writing this short example, shows that even w/ Go, you have to pack
the engine and devices somewhere. Yes this is not mandatory but you
ends up w/ this kind of code (check the wapper around the handler)
parent 8505f0af
No related branches found
No related tags found
No related merge requests found
package main
import (
"fmt"
"log/slog"
"strings"
"time"
......@@ -19,9 +20,9 @@ type Counter struct {
var (
devices map[uuid.UUID]*Counter
deviceIcons = map[string]string{
"lamp": "💡",
"contact": "🚪",
"motion": "🚶",
"lamp": "",
"motion": "󰶑 ",
"contact": "󰠚 ",
}
)
......@@ -69,18 +70,18 @@ func updateCounter(msg *xaal.Message, prefix string) {
if prefix == "lamp" {
cnt.State = getAttribute(msg, "light")
}
// Contacts
if prefix == "contact" {
cnt.State = getAttribute(msg, "detected")
}
// motion sensors
if prefix == "motion" {
cnt.State = getAttribute(msg, "presence")
}
// Contacts
if prefix == "contact" {
cnt.State = getAttribute(msg, "detected")
}
if cnt.State != old {
display()
}
// slog.Debug("Update counter", "cnt", cnt)
}
func updateAlive(msg *xaal.Message) {
......@@ -91,14 +92,22 @@ func updateAlive(msg *xaal.Message) {
}
}
// display outputs the result on stdout and flush
func display() {
slog.Info("Devices", "count", len(devices))
slog.Info("Lamps", "cnt", countPrefix("lamp"))
slog.Info("Motion", "cnt", countPrefix("motion"))
slog.Info("Contact", "cnt", countPrefix("contact"))
// slog.Info("Devices", "count", len(devices))
displayPrefix("lamp")
fmt.Print(" ")
displayPrefix("motion")
fmt.Print(" ")
displayPrefix("contact")
fmt.Print("\n")
}
func handleMessage(msg *xaal.Message) {
func displayPrefix(prefix string) string {
return fmt.Sprintf("%s%d", deviceIcons[prefix], countPrefix(prefix))
}
func handleMessage(eng *xaal.Engine, msg *xaal.Message) {
// let's filter the messages we are interested in
prefix := filterCounter(msg)
if prefix == "" {
......@@ -108,6 +117,7 @@ func handleMessage(msg *xaal.Message) {
cnt := getCounter(msg)
if cnt == nil {
cnt = addCounter(msg, prefix)
sendGetAttritube(eng, msg.Source)
}
if msg.IsAlive() {
......@@ -129,6 +139,13 @@ func countPrefix(prefix string) int {
return total
}
func sendGetAttritube(eng *xaal.Engine, addr uuid.UUID) {
dev := schemas.NewHmi(uuid.Random())
body := make(xaal.MessageBody)
msg := eng.MessagesFactory.BuildMessage(dev, []uuid.UUID{addr}, xaal.MSGTypeRequest, string(xaal.MSGActionGetAttributes), body)
eng.Send(msg)
}
// isAlive sends a message to all devices asking if they are alive
func isAlive(eng *xaal.Engine) {
addr := uuid.Random()
......@@ -148,7 +165,11 @@ func main() {
eng.DisableMessageFilter()
devices = make(map[uuid.UUID]*Counter)
eng.Subscribe(handleMessage)
handleMessageWrapper := func(msg *xaal.Message) {
handleMessage(eng, msg)
}
eng.Subscribe(handleMessageWrapper)
isAlive(eng)
eng.Run()
// Let's count the devices
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment