This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues or pull requests.
automation/presence/presence.go
Dreaded_X c49ee841fd
All checks were successful
continuous-integration/drone/push Build is passing
Added timestamp to presence state and use mqtt instead of channel for overall presence
2022-11-18 18:50:36 +01:00

73 lines
1.3 KiB
Go

package presence
import (
"encoding/json"
"log"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/kr/pretty"
)
type Presence struct {
devices map[string]bool
current *bool
}
type Message struct {
State bool `json:"state"`
Updated int64 `json:"updated"`
}
func New() Presence {
return Presence{devices: make(map[string]bool), current: nil}
}
// Handler got automation/presence/+
func (p *Presence) PresenceHandler(client mqtt.Client, msg mqtt.Message) {
name := strings.Split(msg.Topic(), "/")[2]
if len(msg.Payload()) == 0 {
delete(p.devices, name)
} else {
var message Message
err := json.Unmarshal(msg.Payload(), &message)
if err != nil {
log.Println(err)
return
}
p.devices[name] = message.State
}
present := false
pretty.Println(p.devices)
for _, value := range p.devices {
if value {
present = true
break
}
}
log.Println(present)
if p.current == nil || *p.current != present {
p.current = &present
msg, err := json.Marshal(Message{
State: present,
Updated: time.Now().UnixMilli(),
})
if err != nil {
log.Println(err)
}
token := client.Publish("automation/presence", 1, true, msg)
if token.Wait() && token.Error() != nil {
log.Println(token.Error())
}
}
}