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

Added Send is_alive, added attributes extract ...

parent 6fc06de5
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"gitlab.imt-atlantique.fr/xaal/code/go/core/schemas"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
......@@ -13,10 +13,11 @@ import (
type Counter struct {
Type string
Until time.Time
State bool
}
var (
devices map[uuid.UUID]Counter
devices map[uuid.UUID]*Counter
deviceIcons = map[string]string{
"lamp": "💡",
"contact": "🚪",
......@@ -27,17 +28,18 @@ var (
func getCounter(msg *xaal.Message) *Counter {
cnt, found := devices[msg.Source]
if found {
return &cnt
return cnt
}
return nil
}
func addCounter(msg *xaal.Message, prefix string) *Counter {
cnt := Counter{
Type: prefix,
cnt := &Counter{
Type: prefix,
State: false,
}
devices[msg.Source] = cnt
return &cnt
return cnt
}
func filterCounter(msg *xaal.Message) string {
......@@ -51,6 +53,33 @@ func filterCounter(msg *xaal.Message) string {
return prefix
}
func getAttribute(msg *xaal.Message, attr string) bool {
if value, ok := msg.Body[attr].(bool); ok {
return value
}
return false
}
func updateCounter(msg *xaal.Message, prefix string) {
// we can extract from msg, but already done so use it
msg.Dump()
cnt := getCounter(msg)
// Lamps
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")
}
slog.Debug("Update counter", "cnt", cnt)
}
func handleMessage(msg *xaal.Message) {
// t's filter the messages we are interested in
prefix := filterCounter(msg)
......@@ -58,16 +87,38 @@ func handleMessage(msg *xaal.Message) {
return
}
msg.Dump()
cnt := getCounter(msg)
if cnt == nil {
cnt = addCounter(msg, prefix)
}
if msg.IsAlive() {
if timeout, ok := msg.Body["timeout"].(uint64); ok {
cnt := getCounter(msg)
if cnt == nil {
cnt = addCounter(msg, prefix)
}
cnt.Until = time.Now().Add(time.Duration(timeout) * time.Second)
}
}
if msg.IsAttributesChange() || msg.IsGetAttributes() {
updateCounter(msg, prefix)
}
}
func countPrefix(prefix string) int {
total := 0
for _, cnt := range devices {
if cnt.Type == prefix && cnt.State {
total++
}
}
return total
}
func boot(eng *xaal.Engine) {
addr := uuid.Random()
dev := schemas.NewHmi(addr)
body := make(xaal.MessageBody)
body["dev_types"] = []string{"any.any"}
msg := eng.MessagesFactory.BuildMessage(dev, []uuid.UUID{uuid.Nil}, xaal.MSGTypeRequest, string(xaal.MSGActionIsAlive), body)
eng.Send(msg)
}
func init() {
......@@ -78,10 +129,15 @@ func main() {
eng := xaal.NewEngine()
eng.DisableMessageFilter()
devices = make(map[uuid.UUID]Counter)
devices = make(map[uuid.UUID]*Counter)
eng.Subscribe(handleMessage)
boot(eng)
eng.Run()
// Let's count the devices
slog.Info("Devices", "count", len(devices))
spew.Dump(devices)
// spew.Dump(devices)
slog.Info("Lamps", "cnt", countPrefix("lamp"))
slog.Info("Motion", "cnt", countPrefix("motion"))
slog.Info("Contact", "cnt", countPrefix("contact"))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment