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
|
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
)
type Configuration struct {
Listen string
Vcp string
}
var configuration = Configuration{
Listen: "127.0.0.1:49281",
Vcp: "10"}
type Brightness struct {
Lock bool
Value int
ToggledValue int
}
var currentBrightness Brightness
func update() {
if currentBrightness.Lock == false {
currentBrightness.Lock = true
cmd := exec.Command("ddcutil", "getvcp", "--brief", configuration.Vcp)
log.Println(cmd.String())
out, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
return
}
outString := strings.Split(strings.Trim(string(out), "\n"), " ")
currentBrightness.Value, err = strconv.Atoi(outString[len(outString)-2])
if err != nil {
log.Println(err)
return
}
log.Println("Current brightness is", currentBrightness.Value)
currentBrightness.Lock = false
} else {
log.Println("Active lock, no operation performed")
return
}
}
func get(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
log.Println("Current brightness cached value is",
currentBrightness.Value)
}
}
func ddcutilSetBrightness(brightness Brightness) {
if currentBrightness.Lock == false {
currentBrightness.Lock = true
cmd := exec.Command("ddcutil", "setvcp", configuration.Vcp,
strconv.Itoa(brightness.Value))
log.Println(cmd.String())
_, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
return
}
currentBrightness.Value = brightness.Value
currentBrightness.Lock = false
} else {
log.Println("Active lock, no operation performed")
return
}
}
func set(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
var brightness Brightness
json.Unmarshal([]byte(body), &brightness)
ddcutilSetBrightness(brightness)
}
}
func decrease(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
var brightness Brightness
json.Unmarshal([]byte(body), &brightness)
newValue := Brightness{Value: currentBrightness.Value - brightness.Value}
if newValue.Value < 0 {
log.Println("Brightness is minimum")
} else {
ddcutilSetBrightness(newValue)
log.Println("Current brightness is", currentBrightness.Value)
}
} else {
log.Println("Active lock, no operation performed")
return
}
}
func increase(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
var brightness Brightness
json.Unmarshal([]byte(body), &brightness)
newValue := Brightness{Value: currentBrightness.Value + brightness.Value}
if newValue.Value > 100 {
log.Println("Brightness is maximum")
} else {
ddcutilSetBrightness(newValue)
log.Println("Current brightness is", currentBrightness.Value)
}
} else {
log.Println("Active lock, no operation performed")
return
}
}
func toggle(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
if currentBrightness.Value == 0 {
ddcutilSetBrightness(Brightness{Value: currentBrightness.ToggledValue})
log.Println("Current brightness is", currentBrightness.Value)
} else {
currentBrightness.ToggledValue = currentBrightness.Value
ddcutilSetBrightness(Brightness{Value: 0})
log.Println("Current brightness is", currentBrightness.Value)
}
}
}
func main() {
jsonFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln(err)
}
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatalln(err)
}
var configuration Configuration
json.Unmarshal(byteValue, &configuration)
jsonFile.Close()
currentBrightness.Lock = false
update()
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
update()
})
http.HandleFunc("/get", get)
http.HandleFunc("/set", set)
http.HandleFunc("/decrease", decrease)
http.HandleFunc("/increase", increase)
http.HandleFunc("/toggle", toggle)
log.Fatal(http.ListenAndServe(configuration.Listen, nil))
}
|