summaryrefslogtreecommitdiff
path: root/main.go
blob: 075feb8bc3865e94bdf7442524fc85942e91e5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import (
	"fmt"
	"log"

	"xengineering.eu/homematic-go/homematic"
	mqtt "github.com/eclipse/paho.mqtt.golang"
)

const (
	OPENCCU = `http://127.0.0.1:8080`
	BROKER = `tcp://127.0.0.1:1883`
	CLIENT_ID = `sia-server`
	TOPIC_PREFIX = `sia`
	QOS = byte(0)
	RETAINED = false
)

func main() {
	req := homematic.NewRequester(OPENCCU)

	inventory, err := req.ListDevices()
	if err != nil {
		log.Fatalf("Failed to retrieve device list: %v", err)
	}

	opts := mqtt.NewClientOptions()
	opts.AddBroker(BROKER)
	opts.SetClientID(CLIENT_ID)
	opts.SetCleanSession(true)

	client := mqtt.NewClient(opts)
	if token := client.Connect(); token.Wait() && token.Error() != nil {
		log.Fatal(token.Error())
	}

	for _, device := range inventory {
		if device.Type == `SHUTTER_CONTACT` {
			state, err := req.GetValue(device.Address)
			if err != nil {
				log.Fatalf("Failed to get value: %v", err)
			}

			topic := fmt.Sprintf("%s/contact/%s/state", TOPIC_PREFIX, device.Address)
			payload := []byte(fmt.Sprintf("%t", state))
			client.Publish(topic, QOS, RETAINED, payload)
		}
	}

	client.Disconnect(500)
}