diff --git a/blocks/prusa_telemetry/main.go b/blocks/prusa_telemetry/main.go new file mode 100644 index 0000000..64ce852 --- /dev/null +++ b/blocks/prusa_telemetry/main.go @@ -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) +}