-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
123 lines (103 loc) · 2.14 KB
/
remote.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
115
116
117
118
119
120
121
122
123
package main
import (
"errors"
"github.com/ww24/lirc-web-api/lirc"
"log"
"strconv"
"strings"
"time"
)
type Remote struct {
name string
codes []string
lirc *lirc.Client
ch chan *Command
PrefixLen int
}
func discoverRemotes(lircClient *lirc.Client, ch chan *Command) (remotes map[string]*Remote, err error) {
var list []string
list, err = lircClient.List("")
if err != nil {
return
}
remotes = map[string]*Remote{}
for _, v := range list {
if v == "" {
continue
}
remotes[v] = &Remote{
name: v,
lirc: lircClient,
ch: ch,
}
remotes[v].discoverCodes()
}
return
}
func codeToName(code string) string {
if code == "" {
return ""
}
if !strings.Contains(code, " ") {
return ""
}
return strings.Split(code, " ")[1]
}
func (r *Remote) discoverCodes() error {
list, err := r.lirc.List(r.name)
if err != nil {
return err
}
/**
* Remove duplicates
*/
names := map[string]struct{}{}
for _, v := range list {
if n := codeToName(v); n != "" {
names[n] = struct{}{}
}
}
r.codes = []string{}
for k, _ := range names {
r.codes = append(r.codes, k)
}
return nil
}
func (r *Remote) verifyCode(code string) string {
for _, v := range r.codes {
if strings.ToUpper(v) == strings.ToUpper(code) {
return v
}
}
return ""
}
func (r *Remote) SendOnce(code string) error {
return r.Send(code, 0)
}
func (r *Remote) Send(code string, duration time.Duration) error {
code = r.verifyCode(code)
if code == "" {
return errors.New("Unknown code " + code)
}
r.ch <- &Command{Remote: r.name, Code: code, Duration: duration}
return nil
}
func (r *Remote) MQCallback(topic string, payload []byte) {
dur, err := strconv.Atoi(string(payload))
if err != nil {
// 0 = SEND_ONCE
dur = 0
}
if len(topic) <= r.PrefixLen {
return
}
code := topic[r.PrefixLen:]
log.Printf("[lirc] Sending %s to %s for %d ms (raw msg: %s[\"%s\"])", code, r.name, dur, topic, string(payload))
err = r.Send(code, time.Millisecond*time.Duration(dur))
if err != nil {
log.Printf("[lirc] Unable to send code (%s/%s@%dms): %v", r.name, code, dur, err)
}
}
func (r *Remote) Codes() []string {
return r.codes
}