-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (98 loc) · 2.4 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
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
package main
import (
"context"
"flag"
"github.com/ww24/lirc-web-api/lirc"
"lib.hemtjan.st/transport/mqtt"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
var (
flagSocket = flag.String("lirc.socket", "/var/run/lirc/lircd", "Path to lircd unix socket")
flagTopicPrefix = flag.String("mqtt.prefix", "remote/", "Prefix for MQTT topics")
)
type Command struct {
Remote string
Code string
Duration time.Duration
}
func main() {
mqCfg := mqtt.MustFlags(flag.String, flag.Bool)
flag.Parse()
pfx := *flagTopicPrefix
cfg := mqCfg()
lircClient, err := lirc.New(*flagSocket)
if err != nil {
log.Fatalf("Unable to open connection to lirc: %v\r\n", err)
}
defer lircClient.Close()
ch := make(chan *Command, 32)
remotes, err := discoverRemotes(lircClient, ch)
if err != nil {
log.Fatalf("Unable to interface with LIRC: %v\r\n", err)
}
ctx, cancel := context.WithCancel(context.Background())
tr, err := mqtt.New(ctx, cfg)
if err != nil {
log.Fatalf("Unable to create MQTT transport: %v\r\n", err)
}
wg := sync.WaitGroup{}
for name, remote := range remotes {
wg.Add(1)
go func(name string, remote *Remote) {
defer wg.Done()
rPfx := pfx + name
remote.PrefixLen = len(rPfx) + 1
tr.Publish(rPfx, []byte(strings.Join(remote.Codes(), ",")), true)
rmtCh := tr.SubscribeRaw(rPfx + "/#")
for {
cmd, open := <-rmtCh
if !open {
return
}
remote.MQCallback(cmd.TopicName, cmd.Payload)
}
}(name, remote)
}
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
cancel()
}()
lircQueue(ch, lircClient, ctx)
}
func lircQueue(ch chan *Command, lirc *lirc.Client, ctx context.Context) {
var err error
for {
select {
case <-ctx.Done():
return
case cmd := <-ch:
if cmd == nil {
return
}
if cmd.Duration == 0 {
err = lirc.SendOnce(cmd.Remote, cmd.Code)
if err != nil {
log.Printf("Error sending %s to %s: %v", cmd.Remote, cmd.Code, err)
}
continue
}
err = lirc.SendStart(cmd.Remote, cmd.Code)
if err != nil {
log.Printf("Error sending %s to %s (for %d ms): %v", cmd.Remote, cmd.Code, cmd.Duration/time.Millisecond, err)
}
<-time.After(cmd.Duration)
err = lirc.SendStop(cmd.Remote, cmd.Code)
if err != nil {
log.Printf("Calling SendStop on %s/%s after %d ms: %v", cmd.Remote, cmd.Code, cmd.Duration/time.Millisecond, err)
}
}
}
}