Esempio n. 1
0
func primeHandler(w http.ResponseWriter, r *http.Request) {
	result := PositionResultType{Position: bot.ReadDistance()}
	js, err := json.Marshal(result)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	} else {
		w.Header().Set("Content-Type", "application/json")
		w.Write(js)
	}
}
Esempio n. 2
0
func goHandler(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)

	target, err := strconv.ParseFloat(params["position"], 64)

	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// get current position
	position := bot.ReadDistance()

	for position == -1 {
		position = bot.ReadDistance()
	}

	delta := target - position

	fmt.Printf("delta: %.2fcm\ntarget: %.2fcm\n", delta, target)

	if math.Abs(delta) > 0.5 {
		if delta > 0 {
			bot.GoUpTo(target)
		} else {
			bot.GoDownTo(target)
		}
	}

	res := PositionResultType{Position: bot.ReadDistance()}

	js, err2 := json.Marshal(res)

	if err2 != nil {
		http.Error(w, err2.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)

}
Esempio n. 3
0
func positionHandler(w http.ResponseWriter, r *http.Request) {
	var res PositionResultType

	res.Position = bot.ReadDistance()

	js, err := json.Marshal(res)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Esempio n. 4
0
func distanceHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("GET /distance")
	res := DistanceResultType{Distance: bot.ReadDistance()}

	js, err := json.Marshal(res)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	fmt.Printf("measured distance: %.2f\n", res.Distance)

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Esempio n. 5
0
func stopHandler(w http.ResponseWriter, r *http.Request) {
	// currently moving to a target?
	if bot.IsMoving() {
		// stop it
		bot.Interrupt()
	}

	bot.Stop()

	res := PositionResultType{Position: bot.ReadDistance()}

	js, err := json.Marshal(res)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}