summaryrefslogtreecommitdiff
path: root/mqtt-controller/main.go
blob: 29d5d28f7ae556b6e08af9880d7b7edcd1a0005f (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main

import (
	"encoding/json"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"

	"github.com/brutella/hc"
	"github.com/brutella/hc/accessory"
	"github.com/brutella/hc/util"
)

type AppConfig struct {
	Broker     string
	BridgeName string
	Pin        string
}

func loadAppConfig(s util.Storage) (*AppConfig, error) {
	d, err := s.Get("bridgeConfig")
	if err != nil {
		return nil, err
	}

	c := &AppConfig{}
	if err = json.Unmarshal(d, c); err != nil {
		return nil, err
	}

	return c, nil
}

func main() {
	wg := &sync.WaitGroup{}
	done := make(chan interface{})

	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, os.Kill)
	signal.Notify(c, syscall.SIGTERM)

	storage, err := util.NewFileStorage("db")
	if err != nil {
		panic(err)
	}

	cfg, err := loadAppConfig(storage)
	if err != nil {
		panic(err)
	}

	broker, err := NewMQTTBroker(cfg.Broker)
	if err != nil {
		panic(err)
	}

	if err := broker.Subscribe("shellies/announce", nil); err != nil {
		panic(err)
	}

	if err := broker.Publish("shellies/command", "announce"); err != nil {
		panic(err)
	}

	ac := make(chan *UnifiedSwitch, 10)

	go func() {
		wg.Add(1)
		defer wg.Done()

		log.Println("Starting homekit thread")

		reg := []*accessory.Accessory{}

		// Collect devices until the discovery process quiesces, then start the
		// transport
		quiescence := 10 * time.Second
		t := time.NewTimer(quiescence)

	C:
		for {
			select {
			case a := <-ac:
				log.Println("Homekit: new device")
				reg = append(reg, a.HomeKitSwitch.Accessory)
				// TODO: Extend timer deadline
			case <-t.C:
				log.Println("Homekit: timeout over")
				t.Stop()
				break C
			case <-done:
				log.Println("Homekit: cancelled")
				return
			}
		}

		log.Println("MQTT quiesces, starting HomeKit")

		br := accessory.NewBridge(accessory.Info{Name: cfg.BridgeName})

		transport, err := hc.NewIPTransport(hc.Config{Pin: cfg.Pin}, br.Accessory, reg...)
		if err != nil {
			log.Fatalf("%s", err)
		}
		go transport.Start()
		log.Printf("HomeKit transport started")

		select {
		case <-done:
			log.Printf("Shutting down transport")
			if transport != nil {
				<-transport.Stop()
			}
		}
		log.Printf("Transport shut down")
	}()

	for {
		select {
		case d := <-broker.Devices:
			if d == nil {
				continue
			}

			us, err := NewUnifiedSwitch(broker, d)
			if err != nil {
				log.Printf("Error connecting device: %e", err)
				continue
			}

			go us.MessageLoop(done, wg)

			ac <- us
			log.Printf("New Device: %s\n", d.Identity())
		case <-c:
			close(done)
			broker.Shutdown()
			wg.Wait()
			return
		}
	}
}