chore(mochi): device last Will message

This commit is contained in:
Leandro Antônio Farias Machado 2023-07-02 12:56:24 -03:00
parent 0d48cb2d36
commit a3ef451e5d

View File

@ -225,6 +225,7 @@ func (h *MyHook) Provides(b byte) bool {
return bytes.Contains([]byte{ return bytes.Contains([]byte{
mqtt.OnSubscribed, mqtt.OnSubscribed,
mqtt.OnDisconnect, mqtt.OnDisconnect,
mqtt.OnClientExpired,
}, []byte{b}) }, []byte{b})
} }
@ -233,6 +234,10 @@ func (h *MyHook) Init(config any) error {
return nil return nil
} }
func (h *MyHook) OnClientExpired(cl *mqtt.Client) {
log.Printf("Client id %s expired", cl.ID)
}
func (h *MyHook) OnDisconnect(cl *mqtt.Client, err error, expire bool) { func (h *MyHook) OnDisconnect(cl *mqtt.Client, err error, expire bool) {
var clUser string var clUser string
if len(cl.Properties.Props.User) > 0 { if len(cl.Properties.Props.User) > 0 {
@ -257,6 +262,10 @@ func (h *MyHook) OnSubscribed(cl *mqtt.Client, pk packets.Packet, reasonCodes []
} }
if clUser != "" { if clUser != "" {
cl.Properties.Will = mqtt.Will{
Qos: 1,
Payload: []byte(clUser),
}
log.Println("new device:", clUser) log.Println("new device:", clUser)
err := server.Publish("oktopus/devices", []byte(clUser), false, 1) err := server.Publish("oktopus/devices", []byte(clUser), false, 1)
if err != nil { if err != nil {