func main() { if err := rpio.Open(); err != nil { log.Fatalf("Error: %v\n", err) } defer rpio.Close() controllers := []commands.GarageDoorController{} // takes one command line argument specifying the configuration file path configFile := "/etc/garage-doors.conf" if len(os.Args) == 2 { configFile = os.Args[1] } conf := util.ParseConfig(configFile) for _, door := range conf.Controllers.Garage_doors.Gpio_pins.Bcm { controllers = append(controllers, commands.ControllerFactory(door, conf.Controllers.Garage_doors.Trigger_time, conf.Controllers.Garage_doors.Force_time)) } cpuCount := runtime.NumCPU() if cpuCount < len(controllers) { runtime.GOMAXPROCS(cpuCount) } else { runtime.GOMAXPROCS(len(controllers)) } http.HandleFunc(conf.Endpoints.Paths.Control, func(w http.ResponseWriter, r *http.Request) { if r.Method != httpPost { fmt.Fprintf(w, "HTTP Method "+r.Method+" not valid for this endpoint.") return } door, doorErr := strconv.Atoi(r.PostFormValue("door")) if doorErr != nil || door >= len(controllers) { fmt.Fprintf(w, "Error parsing door number.") return } force, forceErr := strconv.ParseBool(r.PostFormValue("force")) if forceErr != nil { fmt.Fprintf(w, "Error parsing force value.") return } log.Println("Opening door " + r.PostFormValue("door") + ", force: " + r.PostFormValue("force")) go controllers[door].Trigger(force) fmt.Fprintf(w, "door: %d, force: %v", door, force) }) log.Println("Starting server at " + conf.Endpoints.Host) log.Fatal(http.ListenAndServe(conf.Endpoints.Host, nil)) }
func main() { // Open and map memory to access gpio, check for errors if err := rpio.Open(); err != nil { fmt.Println(err) os.Exit(1) } go func() { // TODO(jbd): Use mdns after https://github.com/hashicorp/mdns/issues/44 // is resolved for communication. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ms := r.URL.Query().Get("t") if ms != "" { t, _ := strconv.Atoi(ms) tick = time.Duration(t) * time.Millisecond } io.WriteString(w, "ok\n") }) log.Fatal(http.ListenAndServe(":8080", nil)) }() // Unmap gpio memory when done defer rpio.Close() // Set pin to output mode for _, p := range pins { p.Output() } for { for _, p := range pins { p.High() time.Sleep(tick) p.Low() } } }