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

Work in progress

parent 8515e1e8
No related branches found
No related tags found
No related merge requests found
all:tidy dumper chandelier lamp lamp_schem
all:dumper chandelier lamp lamp_schem
tidy:
go mod tidy
......
......@@ -11,9 +11,9 @@ import (
)
type Counter struct {
Type string
Until time.Time
State bool
Prefix string
Until time.Time
State bool
}
var (
......@@ -35,8 +35,8 @@ func getCounter(msg *xaal.Message) *Counter {
func addCounter(msg *xaal.Message, prefix string) *Counter {
cnt := &Counter{
Type: prefix,
State: false,
Prefix: prefix,
State: false,
}
devices[msg.Source] = cnt
return cnt
......@@ -62,9 +62,9 @@ func getAttribute(msg *xaal.Message, attr string) bool {
func updateCounter(msg *xaal.Message, prefix string) {
// we can extract from msg, but already done so use it
msg.Dump()
cnt := getCounter(msg)
old := cnt.State
// Lamps
if prefix == "lamp" {
cnt.State = getAttribute(msg, "light")
......@@ -77,11 +77,29 @@ func updateCounter(msg *xaal.Message, prefix string) {
if prefix == "motion" {
cnt.State = getAttribute(msg, "presence")
}
slog.Debug("Update counter", "cnt", cnt)
if cnt.State != old {
display()
}
// slog.Debug("Update counter", "cnt", cnt)
}
func updateAlive(msg *xaal.Message) {
cnt := getCounter(msg)
if timeout, ok := msg.Body["timeout"].(uint64); ok {
cnt.Until = time.Now().Add(time.Duration(timeout) * time.Second)
}
}
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"))
}
func handleMessage(msg *xaal.Message) {
// t's filter the messages we are interested in
// let's filter the messages we are interested in
prefix := filterCounter(msg)
if prefix == "" {
return
......@@ -93,26 +111,26 @@ func handleMessage(msg *xaal.Message) {
}
if msg.IsAlive() {
if timeout, ok := msg.Body["timeout"].(uint64); ok {
cnt.Until = time.Now().Add(time.Duration(timeout) * time.Second)
}
updateAlive(msg)
}
if msg.IsAttributesChange() || msg.IsGetAttributes() {
updateCounter(msg, prefix)
}
}
// countPrefix counts the number of devices with a given prefix
func countPrefix(prefix string) int {
total := 0
for _, cnt := range devices {
if cnt.Type == prefix && cnt.State {
if cnt.Prefix == prefix && cnt.State {
total++
}
}
return total
}
func boot(eng *xaal.Engine) {
// isAlive sends a message to all devices asking if they are alive
func isAlive(eng *xaal.Engine) {
addr := uuid.Random()
dev := schemas.NewHmi(addr)
body := make(xaal.MessageBody)
......@@ -131,13 +149,8 @@ func main() {
devices = make(map[uuid.UUID]*Counter)
eng.Subscribe(handleMessage)
boot(eng)
isAlive(eng)
eng.Run()
// Let's count the devices
slog.Info("Devices", "count", len(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