i3-blocks-go/blocks/prusa_telemetry/main.go
Simon Vieille 79fe14e059
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
use flag instead os os.Args
2022-08-30 15:42:28 +02:00

72 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/itchyny/timefmt-go"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"io"
"net/http"
"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() {
argApi := flag.String("api", "", "the api url")
flag.Parse()
resp, err := http.Get(*argApi)
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.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("prusa_telemetry", options)
fmt.Println(block)
}