diff --git a/metrics.go b/metrics.go index 98d531a..2ddb734 100644 --- a/metrics.go +++ b/metrics.go @@ -27,6 +27,7 @@ type OneMetric struct { Vbat string Fw string BatteryPercent string + BatteryPercentHTML template.HTML ActiveUntil string DaysUntilDeactivated int // berechneter Wert } @@ -204,26 +205,46 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) { } func vbat2percent(vbat string) int { - res := 0 i, err := strconv.Atoi(vbat) + res := 0 if err == nil { - if i < 3400 { - res = 1 - } else if i < 3700 { - res = 2 - } else if i < 3850 { - res = 3 - } else if i < 4000 { - res = 4 - } else { - res = 5 + res = int(float64(i-3500) * float64((100.0/0.7)*0.001)) + fmt.Printf("vbat2percent Result in Percent: %d\n", res) + if res < 0 { + res = 0 + } else if res > 100 { + res = 100 } - } return res } +func vbat2html(vbat string) string { + percent := vbat2percent(vbat) + fa_battery_string := "" + icon_color := "" + + if percent <= 20 { + fa_battery_string = "fa-battery-empty" + icon_color = "has-background-danger-dark has-text-white pl-2 pr-2" + } else if percent <= 40 { + fa_battery_string = "fa-battery-quarter" + icon_color = "has-background-danger-light has-text-danger pl-2 pr-2" + } else if percent <= 60 { + fa_battery_string = "fa-battery-half" + icon_color = "has-text-success pl-2 pr-2" + } else if percent <= 80 { + fa_battery_string = "fa-battery-three-quarters" + icon_color = "has-text-success pl-2 pr-2" + } else { + fa_battery_string = "fa-battery-full" + icon_color = "has-text-success pl-2 pr-2" + } + + return " " + strconv.Itoa(percent) + " %" +} + func CalcDaysUntil(mydate string) int { var days int layout := "02.01.2006" @@ -299,7 +320,8 @@ func getLastMetrics(deveui string) OneMetric { res.Lon = s[10] res.PosTimestamp = t.In(location).Format("02.01.2006 15:04") } - res.BatteryPercent = s[11] + res.BatteryPercent = strconv.Itoa(vbat2percent(s[11])) + res.BatteryPercentHTML = template.HTML(vbat2html(s[11])) } diff --git a/snippets/index.html b/snippets/index.html index e7fc3d2..bbad34d 100644 --- a/snippets/index.html +++ b/snippets/index.html @@ -2,6 +2,11 @@ {{end}} {{define "body_content"}}
GPS Tracking leichtgemacht...
+
+