Moved the webapp to a webpack with some typescript, updated server wtih new endpoints
This commit is contained in:
@@ -6,7 +6,11 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const DEBUG = true
|
||||
@@ -38,20 +42,23 @@ func startServer() {
|
||||
port := "8001"
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", showCharts).Methods("GET")
|
||||
r.HandleFunc("/since/{mins}", showCharts).Methods("GET")
|
||||
r.HandleFunc("/data/", sendData).Methods("GET")
|
||||
r.HandleFunc("/data/since/{mins}", sendData).Methods("GET")
|
||||
r.HandleFunc("/data/last/", sendLastSnapshot).Methods("GET")
|
||||
r.HandleFunc("/", showCharts).Methods("GET").Queries("show-minutes", "{[0-9]+}")
|
||||
r.HandleFunc("/", saveSnapshot).Methods("POST")
|
||||
r.HandleFunc("/data", sendData).Methods("GET")
|
||||
r.HandleFunc("/data", sendData).Methods("GET").Queries("since", "")
|
||||
r.HandleFunc("/data/now", createAndSendSnapshot).Methods("GET")
|
||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||
http.Handle("/", r)
|
||||
fmt.Printf("Listening on port %s...\n", port)
|
||||
quitSnapshot := make(chan int, 1)
|
||||
go saveSnapshotOnInterval(30, quitSnapshot)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
|
||||
func showCharts(w http.ResponseWriter, r *http.Request) {
|
||||
if countStr := mux.Vars(r)["mins"]; countStr != "" {
|
||||
if _, err := strconv.ParseInt(countStr, 10, 0); err != nil {
|
||||
minutes := r.FormValue("show-minutes")
|
||||
if minutes != "" {
|
||||
if _, err := strconv.ParseInt(minutes, 10, 0); err != nil {
|
||||
http.Redirect(w, r, "/" + ROOT_URL + "/", 303)
|
||||
}
|
||||
}
|
||||
@@ -59,15 +66,16 @@ func showCharts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func sendData(w http.ResponseWriter, r *http.Request) {
|
||||
var count int64 = 60
|
||||
if vars := mux.Vars(r); vars["mins"] != "" {
|
||||
newCount, err := strconv.ParseInt(vars["mins"], 10, 0)
|
||||
dateSince := time.Now()
|
||||
sinceQuery := r.FormValue("since")
|
||||
if sinceQuery != "" {
|
||||
newDateSince, err := time.Parse(time.RFC3339Nano, sinceQuery)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/" + ROOT_URL + "/", 303)
|
||||
}
|
||||
count = newCount
|
||||
dateSince = newDateSince
|
||||
}
|
||||
records, err := getSnapshotRecordsFromDb(int(count))
|
||||
records, err := getSnapshotRecordsFromDb(dateSince)
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't get snapshots from db: %w", err), w, r) { return }
|
||||
json, err := createJsonFromSnapshotRecords(records)
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create json from snapshots: %w", err), w, r) { return }
|
||||
@@ -75,10 +83,13 @@ func sendData(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, string(json))
|
||||
}
|
||||
|
||||
func sendLastSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
record, err := getLastSnapshotRecordFromDb()
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't get last snapshot from db: %w", err), w, r) { return }
|
||||
json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record})
|
||||
func createAndSendSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
snapshot, err := getSnapshotFromPythonScript()
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create a snapshot: %w", err), w, r) { return }
|
||||
_, err = writeSnapshotToDb(snapshot)
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't save snapshot to db: %w", err), w, r) { return }
|
||||
dbSnapshotRecord, err := getLastSnapshotRecordFromDb()
|
||||
json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{dbSnapshotRecord})
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create json from last snapshots: %w", err), w, r) { return }
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, string(json))
|
||||
@@ -92,6 +103,54 @@ func saveSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "{id: %v}", newId)
|
||||
}
|
||||
|
||||
func saveSnapshotOnInterval(seconds int64, stop chan int) {
|
||||
var err error = nil
|
||||
for {
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
snapshot, err := getSnapshotFromPythonScript()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
_, err = writeSnapshotToDb(snapshot)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case <-stop:
|
||||
break
|
||||
default:
|
||||
time.Sleep(time.Duration(seconds) * time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getSnapshotFromPythonScript() (*SnapshotSubmission, error) {
|
||||
process := exec.Command("climate-pinger.py")
|
||||
process.Stdout = os.Stdout
|
||||
process.Stderr = os.Stderr
|
||||
err := process.Run()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output, err := process.Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tokens := strings.Split(string(output), "\n")
|
||||
if len(tokens) != 4 {
|
||||
return nil, errors.New(fmt.Sprintf("Strange python output: %s", output))
|
||||
}
|
||||
snapshot := SnapshotSubmission{
|
||||
Timestamp: tokens[0],
|
||||
Temp: tokens[1],
|
||||
Humidity: tokens[2],
|
||||
Co2: tokens[3],
|
||||
}
|
||||
return &snapshot, nil
|
||||
}
|
||||
|
||||
func internalErrorOnErr(err error, w http.ResponseWriter, r *http.Request) bool {
|
||||
if errors.Unwrap(err) != nil {
|
||||
errorMessage := "Internal Server Error!"
|
||||
|
||||
Reference in New Issue
Block a user