-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (51 loc) · 1.55 KB
/
main.go
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
package main
import (
"context"
"flag"
"lib.hemtjan.st/transport/mqtt"
"log"
"os"
"os/signal"
"syscall"
)
var (
flgPowerTopic = flag.String("topic.power", "remote/fan/KEY_POWER", "Topic to send to when device is triggered")
flgSpeedTopic = flag.String("topic.speed", "remote/fan/KEY_SPEED", "Topic for changing speed")
flgSwingTopic = flag.String("topic.swing", "remote/fan/KEY_OSC", "Topic for changing swing mode")
flgDeviceName = flag.String("device.name", "Fan", "Device Name")
flgDeviceModel = flag.String("device.model", "", "Device Model")
flgDeviceManu = flag.String("device.manufacturer", "", "Device Manufacturer")
flgDeviceSerial = flag.String("device.serial", "", "Device Serial number")
flgDeviceTopic = flag.String("topic.device", "switch/livingroom/fan", "Device topic")
)
func main() {
mqCfg := mqtt.MustFlags(flag.String, flag.Bool)
var fan *Fan
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
}()
mq, err := mqtt.New(ctx, mqCfg())
if err != nil {
log.Fatal(err)
}
fan = NewFan(Config{
PowerTopic: *flgPowerTopic,
SpeedTopic: *flgSpeedTopic,
SwingTopic: *flgSwingTopic,
DeviceTopic: *flgDeviceTopic,
DeviceName: *flgDeviceName,
DeviceModel: *flgDeviceModel,
DeviceManufacturer: *flgDeviceManu,
DeviceSerial: *flgDeviceSerial,
})
err = fan.Start(mq)
if err != nil {
log.Fatal(err)
}
<-ctx.Done()
}