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)) }