-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lemon.go
150 lines (125 loc) · 3.07 KB
/
lemon.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
package main
import (
"flag"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/mrusme/lemon/inbox"
"github.com/mrusme/lemon/output"
"github.com/mrusme/lemon/output/unicorn"
"github.com/mrusme/lemon/source"
"github.com/mrusme/lemon/source/pushover"
)
var flagSourcesString string
var flagOutputsString string
var flagPushoverDeviceID string
var flagPushoverSecret string
var flagOutputUnicornFPS int
func env(name string, dflt string) string {
val, exists := os.LookupEnv(name)
if exists == false {
return dflt
}
return val
}
func init() {
flag.StringVar(
&flagSourcesString,
"sources",
env("LEMON_SOURCES", "dummy"),
"Notification sources to load, comma separated.\nAvailable: dummy pushover\nOverrides env LEMON_OUTPUTS\n")
flag.StringVar(
&flagOutputsString,
"outputs",
env("LEMON_OUTPUTS", "dbus"),
"Notification outputs to load, comma separated.\nAvailable: dbus unicorn\nOverrides env LEMON_OUTPUTS\n")
flag.StringVar(
&flagPushoverDeviceID,
"pushover-device-id",
env("PUSHOVER_DEVICE_ID", ""),
"Pushover source: The device ID to use.\nOverrides env PUSHOVER_DEVICE_ID\n")
flag.StringVar(
&flagPushoverSecret,
"pushover-secret",
env("PUSHOVER_SECRET", ""),
"Pushover source: The secret to use.\nOverrides env PUSHOVER_SECRET\n")
num, err := strconv.Atoi(env("LEMON_OUTPUT_UNICORN_FPS", "10"))
if err != nil {
panic(err)
}
flag.IntVar(
&flagOutputUnicornFPS,
"output-unicorn-fps",
num,
"Unicorn output: Frames per second.\nOverrides env LEMON_OUTPUT_UNICORN_FPS\n")
}
func main() {
flag.Parse()
osSig := make(chan os.Signal, 1)
signal.Notify(osSig, os.Interrupt, syscall.SIGTERM)
ibx := make(chan inbox.Message)
var sources []source.Source
for _, sourceString := range strings.Split(flagSourcesString, ",") {
var opts interface{}
switch sourceString {
case "pushover":
opts = &pushover.PushoverOptions{
DeviceID: flagPushoverDeviceID,
Secret: flagPushoverSecret,
}
}
s, err := source.New(sourceString, ibx, opts)
if err != nil {
panic(err)
}
sources = append(sources, s)
}
var outputs []output.Output
for _, outputString := range strings.Split(flagOutputsString, ",") {
var opts interface{}
switch outputString {
case "unicorn":
opts = &unicorn.UnicornOptions{
FPS: uint8(flagOutputUnicornFPS),
}
}
o, err := output.New(outputString, opts)
if err != nil {
panic(err)
}
outputs = append(outputs, o)
}
for _, source := range sources {
go source.Start()
}
log.Println("All set, entering main loop ...")
mainloop:
for {
select {
case ibxMsg := <-ibx:
log.Println("Got new ibxMessage")
log.Println(ibxMsg)
for _, o := range outputs {
if err := o.Display(&ibxMsg); err != nil {
log.Printf("ERROR: %s\n", err)
}
}
case sig := <-osSig:
log.Printf("Received signal: %s\n", sig.String())
switch sig {
case os.Interrupt, syscall.SIGTERM:
log.Println("Breaking main loop...")
break mainloop
}
}
}
log.Println("Cleaning up ...")
for _, o := range outputs {
o.Cleanup()
}
log.Println("Bye!")
os.Exit(0)
}