Switched from OwnTracks to a more generic mqtt solution that supports multiple devices
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2022-09-13 00:27:09 +02:00
parent cba9d0a596
commit f015629d47
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9

63
main.go
View File

@ -1,10 +1,11 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings"
"syscall" "syscall"
"time" "time"
@ -119,30 +120,23 @@ var defaultHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Messa
fmt.Printf("MSG: %s\n", msg.Payload()) fmt.Printf("MSG: %s\n", msg.Payload())
} }
// Handler got owntrack/+/+ type DeviceStatus struct {
func locationHandler(home chan bool) func(MQTT.Client, MQTT.Message) { Name string
Present bool
}
// Handler got automation/presence/+
func presenceHandler(status chan DeviceStatus) func(MQTT.Client, MQTT.Message) {
return func(client MQTT.Client, msg MQTT.Message) { return func(client MQTT.Client, msg MQTT.Message) {
// Check the type of the MQTT message device := strings.Split(msg.Topic(), "/")[2]
var identifier Identifier value, err := strconv.Atoi(string(msg.Payload()))
json.Unmarshal(msg.Payload(), &identifier) if err != nil {
if identifier.Type != Location { panic(err)
return
} }
// Marshall all the location data fmt.Println("presenceHandler", device, value)
var location LocationData
json.Unmarshal(msg.Payload(), &location)
fmt.Println(location) status <- DeviceStatus{Name: device, Present: value == 1}
temp := false
for _, region := range location.InRegions {
if region == "home" {
temp = true
}
}
fmt.Println(temp)
home <- temp
} }
} }
@ -189,8 +183,8 @@ func main() {
panic(token.Error()) panic(token.Error())
} }
home := make(chan bool, 1) status := make(chan DeviceStatus, 1)
if token := c.Subscribe("owntracks/mqtt/apollo", 0, locationHandler(home)); token.Wait() && token.Error() != nil { if token := c.Subscribe("automation/presence/+", 0, presenceHandler(status)); token.Wait() && token.Error() != nil {
fmt.Println(token.Error()) fmt.Println(token.Error())
os.Exit(1) os.Exit(1)
} }
@ -208,11 +202,28 @@ func main() {
var brightness uint8 = 1 var brightness uint8 = 1
fmt.Println("Starting event loop")
devices := make(map[string]bool)
// Event loop // Event loop
events: events:
for { for {
select { select {
case temp := <-home: case state := <-status:
// Update the device state
devices[state.Name] = state.Present
// Check if there is any device home
temp := false
for key, value := range devices {
fmt.Println(key, value)
if value {
temp = true
break;
}
}
// Only do stuff if the state changes // Only do stuff if the state changes
if temp == isHome { if temp == isHome {
break break
@ -236,6 +247,8 @@ events:
break break
} }
fmt.Println("Done")
case <-sunriseTimer.C: case <-sunriseTimer.C:
fmt.Println("Sun is rising, turning off all lights") fmt.Println("Sun is rising, turning off all lights")
allLightsOff(bridge) allLightsOff(bridge)
@ -292,7 +305,7 @@ events:
} }
// Cleanup // Cleanup
if token := c.Unsubscribe("owntracks/mqtt/apollo"); token.Wait() && token.Error() != nil { if token := c.Unsubscribe("automation/presence/+"); token.Wait() && token.Error() != nil {
fmt.Println(token.Error()) fmt.Println(token.Error())
os.Exit(1) os.Exit(1)
} }