-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonair.go
184 lines (175 loc) · 4.92 KB
/
onair.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
// OnAir: a golang application that will change the color of a Philips Hue light when
// your computer's camera or microphone is enabled. You should take a look at the
// README.md for more information on how to use this application.
//
// onair.go: cli skeleton and flag definitions. You can find some default values here,
// so if you'd like to change those here's a likely place to make that happen.
import (
"C"
"fmt"
"os"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
var buildVersion string
func main() {
homeDir := os.Getenv("HOME")
if homeDir == "" {
fmt.Printf("onair requires $HOME to be set")
os.Exit(1)
}
// Some defaults.?
logFile := fmt.Sprintf("%s/Library/Logs/Micro Snitch.log", homeDir)
configFile := fmt.Sprintf("%s/.onair.yml", homeDir)
globalFlags := []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Path to the config yaml file",
Value: configFile,
},
}
hueFlags := []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueuid",
Aliases: []string{"u"},
Usage: "username for the Hue bridge",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueip",
Aliases: []string{"i"},
Usage: "ip address of the Hue bridge (automatic discovery if not specified)",
}),
&cli.StringFlag{
Name: "config",
Usage: "Path to the config yaml file",
Value: configFile,
Hidden: true,
},
}
runFlags := []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "system",
Aliases: []string{"s"},
Usage: "which light system do you use (currently only supports hue and ifttt webhooks)",
Value: "hue",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "logtype",
Aliases: []string{"t"},
Usage: "type of log (currently only supports microsnitch)",
Value: "microsnitch",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "log",
Usage: "location of the log file that tracks device status",
Value: logFile,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueuid",
Aliases: []string{"u"},
Usage: "username for the Hue bridge",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueip",
Aliases: []string{"i"},
Usage: "ip address of the Hue bridge (automatic discovery if not specified)",
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "huelight",
Aliases: []string{"l"},
Usage: "ID of the light you'll control",
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "huebrightness",
Aliases: []string{"b"},
Usage: "Brightness (1-254)",
Value: 70,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueactive",
Usage: "XY color value when video/audio is active",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "hueinactive",
Usage: "XY color value when video/audio is inactive",
}),
// addded ifttt flags here
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ifttt-key",
Aliases: []string{"k"},
Usage: "key for IFTTT webhook requests",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ifttt-onair",
Aliases: []string{"o"},
Usage: "Name of IFTTT webhook invoked when video/audio becomes active",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "ifttt-offair",
Aliases: []string{"f"},
Usage: "Name of IFTTT webhook invoked when video/audio becomes inactive",
}),
// end of ifttt flags
&cli.StringFlag{
Name: "config",
Usage: "Path to the config yaml file",
Value: configFile,
Hidden: true,
},
}
app := &cli.App{
Name: "onair",
Version: buildVersion,
Usage: "monitors your audio/video devices and controls a light based on their status",
Before: altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("config")),
Flags: globalFlags,
Commands: []*cli.Command{
{
Name: "hue",
Usage: "commands for configuring your Hue system",
Before: altsrc.InitInputSourceWithContext(hueFlags, altsrc.NewYamlSourceFromFlagFunc("config")),
Flags: hueFlags,
Subcommands: []*cli.Command{
{
Name: "init",
Usage: "Set up onair for the first time",
Action: func(c *cli.Context) error {
initHue(c)
return nil
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "timeout",
Usage: "how long to wait for you to push the button on your Hue bridge",
Value: 15,
},
},
},
{
Name: "lights",
Usage: "display information about available lights and their color settings",
Action: func(c *cli.Context) error {
getHueLights(c)
return nil
},
},
},
},
{
Name: "run",
Usage: "run the log watcher/set your lights",
Action: func(c *cli.Context) error {
run(c)
return nil
},
Before: altsrc.InitInputSourceWithContext(runFlags, altsrc.NewYamlSourceFromFlagFunc("config")),
Flags: runFlags,
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("oh no: %s\n", err)
}
}