From 4214180dec35d38c895dcd972ff704b6af053746 Mon Sep 17 00:00:00 2001 From: volpol Date: Sat, 18 Aug 2018 12:20:24 +0200 Subject: [PATCH] Added frontend code --- frontend/TODO | 13 +++ frontend/barbot.go | 221 +++++++++++++++++++++++++++++++++++++++++++++ frontend/build | 38 ++++++++ 3 files changed, 272 insertions(+) create mode 100644 frontend/TODO create mode 100644 frontend/barbot.go create mode 100755 frontend/build diff --git a/frontend/TODO b/frontend/TODO new file mode 100644 index 0000000..fd336c6 --- /dev/null +++ b/frontend/TODO @@ -0,0 +1,13 @@ +error handling: +1 pumping over certain amount of time and not reaching target weight - DONE, if weight gain < 5 gr over 10 seconds +2 refuse (or queue) mixing requests when already mixing? - DONE, refuse + +status display in backend via LED + +maintainance mode: +cleaning +startconfig + +support alternative styles /style hooks: partially done (example) + +Use NULL-Aware String types!!! diff --git a/frontend/barbot.go b/frontend/barbot.go new file mode 100644 index 0000000..937b82b --- /dev/null +++ b/frontend/barbot.go @@ -0,0 +1,221 @@ +package main + +import ( + "bufio" + "database/sql" + "fmt" + _ "github.com/mattn/go-sqlite3" + "io" + "log" + "net/http" + "os" + "net" + "strconv" + "strings" + "time" +) + +const ( + BASE = "Barbot 3.4" + DB = BASE + "/data/database" + barbot_db = DB + "/barbot.sqlite" + barbot_ini = DB + "/database.ini" +) + +func styleHF(w http.ResponseWriter, r *http.Request) { + log.Print(r.RemoteAddr, " ", r.Method, " ", r.URL.Path) + log.Print("Serving:", BASE+r.URL.Path) + http.ServeFile(w, r, BASE+r.URL.Path) +} + +func imgHF(w http.ResponseWriter, r *http.Request) { + log.Print(r.RemoteAddr, " ", r.Method, " ", r.URL.Path) + log.Print("Serving:", DB+r.URL.Path) + http.ServeFile(w, r, DB+r.URL.Path) +} + +func holler(w http.ResponseWriter, err error) { + log.Print(err) + http.Error(w, err.Error(), http.StatusInternalServerError) +} + +func rootHF(w http.ResponseWriter, r *http.Request) { + + log.Print(r.RemoteAddr, " ", r.Method, " ", r.URL.Path) + if r.URL.Path == "/favicon.ico" { + http.Error(w, "404 Seek, and you shall find", http.StatusNotFound) + return + } + db, err := sql.Open("sqlite3", barbot_db) + + if err != nil { + holler(w, err) + return + } + + defer db.Close() + html := "\n" + html += "\n" + html += "\n" + html += "" + html += "Barbot: Cocktails" + html += "" + html += "\n" + html += "\n" + + q := r.URL.Query() + var selected_cocktails string + var installed_bottles string + single_mode := false + + bbi, err := os.Open(barbot_ini) + + if err != nil { + holler(w, err) + return + } + + defer bbi.Close() + + scanner := bufio.NewScanner(bbi) + + if !scanner.Scan() { + holler(w, scanner.Err()) + return + } + + installed_bottles = scanner.Text() + ib_slice := strings.Split(installed_bottles, "|") + + log.Print(installed_bottles) + + if len(q["cid"]) != 1 { + + if !scanner.Scan() { + holler(w, scanner.Err()) + return + } + + selected_cocktails = scanner.Text() + } else { + selected_cocktails = q["cid"][0] + single_mode = true + } + + for _, ctl := range strings.Split(selected_cocktails, "|") { + ct, err := db.Query("SELECT * FROM cocktails WHERE ct_ID=?", ctl) + if err != nil { + holler(w, err) + return + } + + defer ct.Close() + for ct.Next() { + var ct_ID string + var ct_Name string + var ct_Kurzinfo string + var ct_Beschreibung string + err := ct.Scan(&ct_ID, &ct_Name, &ct_Kurzinfo, &ct_Beschreibung) + if nil != err { + log.Print(err) + continue + } + + html += fmt.Sprintf("

%s

\n", ct_Name) + img := "/images/ct" + ct_ID + ".jpg" + _, err = os.Stat(DB + img) + if nil != err { + img = "/images/ctdefault.jpg" + } + + if !single_mode { + html += fmt.Sprintf("", ct_ID) + html += fmt.Sprintf("\"%s\"\n", ct_Name, ct_Name, img) + html += "" + } else { + html += fmt.Sprintf("\"%s\"\n", ct_Name, ct_Name, img) + } + + html += "

\n" + if single_mode { + bbd := "MIX:" + html += "" + html += ct_Beschreibung + html += "" + html += "


\n" + html += "

Zutaten:

\n" + bt, err := db.Query("SELECT bottles.bt_ID, bottles.bt_Name, mixdefinition.bt_Menge FROM bottles, mixdefinition WHERE bottles.bt_ID = mixdefinition.bt_ID AND mixdefinition.ct_ID = ?", ct_ID) + + for nil == err && bt.Next() { + var bt_ID string + var bt_Name string + var bt_Menge string + err := bt.Scan(&bt_ID, &bt_Name, &bt_Menge) + if nil != err { + log.Print(err) + continue + } + i := 0 + for i < len(ib_slice) { + if ib_slice[i] == bt_ID { + bbd += strconv.Itoa(i+1) + "," + bt_Menge + "," + log.Println("Found", bt_ID, "at index", i) + } + i++ + } + img := "/images/bt" + bt_ID + ".jpg" + _, err = os.Stat(DB + img) + if nil != err { + img = "/images/btdefault.jpg" + } + html += fmt.Sprintf("\"%s\"\n", bt_Name, bt_Name, img) + html += fmt.Sprintf("%s: %sml.\n", bt_Name, bt_Menge) + } + html += "
\n" + + html += " " + html += "" + bbd = strings.TrimSuffix(bbd, ",") + log.Println(bbd) + status := "Backend: " + + conn, err := net.DialTimeout("tcp", "localhost:3005", time.Second*60) + if nil == err { + defer conn.Close() + rr := bufio.NewReader(conn) + greet, err := rr.ReadString('\n') + if nil == err { + greet = strings.TrimSpace(greet) + log.Println(greet) + if greet == "READY" && 1 == len(q["mix"]) { + conn.Write([]byte(bbd)) + conn.Write([]byte("\n")) + status += "OK" + } else { + status += greet + } + } else { + status = err.Error() + } + + } else { + status = err.Error() + } + html += "

" + status + "
" + } else { + html += "" + html += ct_Kurzinfo + html += "" + } + } + } + html += "\n\n" + io.WriteString(w, html) +} + +func main() { + http.HandleFunc("/", rootHF) + http.HandleFunc("/images/", imgHF) + http.HandleFunc("/style/", styleHF) + log.Fatal(http.ListenAndServe(":8080", nil)) +} diff --git a/frontend/build b/frontend/build new file mode 100755 index 0000000..0531e16 --- /dev/null +++ b/frontend/build @@ -0,0 +1,38 @@ +#!/bin/sh + +what=`basename $0` +case $1 in + arm) + GOOS=linux + GOARCH=arm + GOARM=5 + export CC GOOS GOARCH GOARM + ;; + armcc) + GOOS=linux + GOARCH=arm + GOARM=5 + CGO_ENABLED=1 + CC=arm-linux-gnueabi-gcc + export CGO_ENABLED CC GOOS GOARCH GOARM + ;; + android) + CGO_ENABLED=1 + CC=/usr/local/android/bin/arm-linux-androideabi-gcc + GOOS=android + GOARCH=arm + GOARM=7 + export CGO_ENABLED CC GOOS GOARCH GOARM + ;; + win64) + GOOS=windows + GOARCH=amd64 + export GOOS GOARCH + ;; + win32) + GOOS=windows + GOARCH=386 + export GOOS GOARCH + ;; +esac +go $what -- 2.30.2