-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (117 loc) · 3.16 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
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
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"log"
"strconv"
//"time"
"bufio"
"bytes"
"os"
"github.com/chbmuc/lirc"
"github.com/skybet/go-skyremote"
)
// this is the hashmap that those commands'll be stored in.
var execs map[string]skyremote.Command = map[string]skyremote.Command{
"KEY_POWER": skyremote.CmdPower,
"KEY_UP": skyremote.CmdUp, // up arrow key on d-pad
"KEY_DOWN": skyremote.CmdDown,
"KEY_LEFT": skyremote.CmdLeft,
"KEY_RIGHT": skyremote.CmdRight,
"KEY_SELECT": skyremote.CmdSelect,
"KEY_BACK": skyremote.CmdBackup,
"KEY_CHANNELUP": skyremote.CmdChannelup,
"KEY_CHANNELDOWN": skyremote.CmdChanneldown,
"KEY_F1": skyremote.CmdSky,
"KEY_F2": skyremote.CmdTvguide,
"KEY_F3": skyremote.CmdBoxoffice,
"KEY_F4": skyremote.CmdServices,
"KEY_F5": skyremote.CmdInteractive,
"KEY_INFO": skyremote.CmdI,
"KEY_TEXT": skyremote.CmdSidebar,
"KEY_HELP": skyremote.CmdHelp,
"KEY_RED": skyremote.CmdRed,
"KEY_GREEN": skyremote.CmdGreen,
"KEY_YELLOW": skyremote.CmdYellow,
"KEY_BLUE": skyremote.CmdBlue,
"KEY_PLAY": skyremote.CmdPlay,
"KEY_PAUSE": skyremote.CmdPause,
"KEY_FASTFORWARD": skyremote.CmdFastforward,
"KEY_REWIND": skyremote.CmdRewind,
"KEY_STOP": skyremote.CmdStop,
"KEY_RECORD": skyremote.CmdRecord,
"KEY_1": skyremote.Cmd1,
"KEY_2": skyremote.Cmd2,
"KEY_3": skyremote.Cmd3,
"KEY_4": skyremote.Cmd4,
"KEY_5": skyremote.Cmd5,
"KEY_6": skyremote.Cmd6,
"KEY_7": skyremote.Cmd7,
"KEY_8": skyremote.Cmd8,
"KEY_9": skyremote.Cmd9,
"KEY_0": skyremote.Cmd0,
}
func keyAll(event lirc.Event) {
log.Println("Event fired: " + event.Button)
if event.Repeat > 0 {
log.Println("Debounce.")
return
}
exec, ok := execs[event.Button]
if ok {
remote.SendCommand(exec)
}
}
// CONFIG STUFF
type Configuration struct {
IpAddress string
Port int
}
func InitialiseConfig() *Configuration {
var Config *Configuration = &Configuration{}
defer func() {
if err := recover(); err != nil {
log.Println("Fatal exception when parsing the configuration data. CHECK YOUR CONFIG!", err)
}
}()
fileHandle, _ := os.Open("./config.txt")
defer fileHandle.Close()
fileScanner := bufio.NewScanner(fileHandle)
for fileScanner.Scan() {
line := fileScanner.Text()
if len(line) > 0 {
// this is not a newline, so process.
if !bytes.ContainsRune([]byte(string(line[0])), '#') {
// not a comment, do not ignore and do further parsing.
if len(line) > 7 {
if line[0:2] == "IP" {
Config.IpAddress = line[3:]
}
}
if len(line) > 3 {
if line[0:4] == "PORT" {
port, err := strconv.Atoi(line[5:])
if err != nil {
log.Fatalln("Fatal exception when parsing config data. CHECK YOUR PORT NUMBER!")
}
Config.Port = port
}
}
}
}
}
return Config
}
var SkyConfig *Configuration = InitialiseConfig()
var remote *skyremote.SkyRemote = skyremote.New(SkyConfig.IpAddress, SkyConfig.Port)
func main() {
// Initialize with path to lirc socket
ir, err := lirc.Init("/var/run/lirc/lircd")
if err != nil {
panic(err)
}
// Receive Commands
// attach key press handlers
ir.Handle("", "", keyAll)
// run the receive service
log.Println("Started.")
ir.Run()
}