add block prusa_telemetry

This commit is contained in:
Simon Vieille 2022-08-28 16:25:18 +02:00
parent c94b697b0f
commit 7109a82d4b
Signed by: deblan
GPG key ID: 579388D585F70417

View file

@ -0,0 +1,68 @@
package main
import (
"encoding/json"
"fmt"
"github.com/itchyny/timefmt-go"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"io"
"net/http"
"os"
"strconv"
"time"
)
type Telemetry struct {
Progress int `json:"progress"`
TempNozzle int `json:"temp_nozzle"`
TempBed int `json:"temp_bed"`
TimeEst string `json:"time_est"`
}
func main() {
resp, err := http.Get(os.Args[2])
if err != nil {
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var telemetry Telemetry
json.Unmarshal(body, &telemetry)
progress := r.TextWithPadding(fmt.Sprintf("%d%%", telemetry.Progress), r.FB{
Foreground: "#6bffa6",
Background: r.Color("black2"),
})
tempNozzle := r.TextWithPadding(fmt.Sprintf("%d°", telemetry.TempNozzle), r.FB{
Foreground: "#ff946a",
Background: r.Color("black2"),
})
tempBed := r.TextWithPadding(fmt.Sprintf("%d°", telemetry.TempBed), r.FB{
Foreground: "#66caff",
Background: r.Color("black2"),
})
timeRemaining, _ := strconv.Atoi(telemetry.TimeEst)
estimation := time.Now()
estimation = estimation.Local().Add(time.Hour*time.Duration(0) + time.Minute*time.Duration(0) + time.Second*time.Duration(timeRemaining))
date := r.TextWithPadding(
timefmt.Format(estimation, "%H:%M"),
r.FB{
Foreground: r.Color("grey"),
Background: r.Color("black2"),
},
)
options := r.NewBlockOptions()
options.FullText = fmt.Sprintf("%s%s%s%s", progress, tempNozzle, tempBed, date)
block := r.Block(os.Args[1], options)
fmt.Println(block)
}