-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (85 loc) · 1.89 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
package main
import (
"bufio"
"flag"
"fmt"
"polybar-lamp/config"
"polybar-lamp/hass"
"polybar-lamp/lampcontroller"
"polybar-lamp/polybar"
"net"
"os"
"strings"
)
const socketPath = "/tmp/polybar-lamp.sock"
func main() {
flag.Parse()
action := flag.Arg(0)
switch strings.ToLower(action) {
case "makeconfig":
config.WriteInitialConfig()
case "server":
server()
case "sendcommand":
sendCommand(flag.Arg(1))
default:
if len(action) == 0 {
fmt.Println("Missing action")
} else {
fmt.Printf("Unknown action %s\n", action)
}
fmt.Printf("Usage: %s <action>\n", os.Args[0])
fmt.Println("Actions: makeconfig, server, sendcommand <command>")
fmt.Println("Commands: onoff, switchmode, increment, decrement")
}
}
func server() {
os.Remove(socketPath)
sock, err := net.Listen("unix", socketPath)
if err != nil {
panic(err)
}
lampConfig := config.GetPolybarLampConfig()
apiclient := hass.NewAPIClient(lampConfig)
lampController, _ := lampcontroller.NewLampController(lampConfig.LampEntity, apiclient, lampConfig.ColorTempLimitOverride)
isOn, err := lampController.GetOnState()
if isOn {
polybar.SwitchState(polybar.BRIGHTNESS, lampController)
} else {
polybar.SwitchState(polybar.OFF, lampController)
}
for {
conn, err := sock.Accept()
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
text := scanner.Text()
switch strings.ToLower(text) {
case "onoff":
polybar.OnOff(lampController)
case "switchmode":
polybar.SwitchMode(lampController)
case "increment":
polybar.Increment(lampController)
case "decrement":
polybar.Decrement(lampController)
}
}
}
}
func sendCommand(command string) {
sock, err := net.Dial("unix", socketPath)
if err != nil {
panic(err)
}
_, err = sock.Write([]byte(command))
if err != nil {
panic(err)
}
err = sock.Close()
if err != nil {
panic(err)
}
}