flags added

This commit is contained in:
Joerg Lehmann 2021-06-07 19:43:49 +02:00
parent 2cc02925a3
commit 65c2fec4ac
1 changed files with 31 additions and 14 deletions

View File

@ -163,7 +163,7 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, " \"data\": [\n")
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
fmt.Printf("Scanned Line: %v\n", s)
fmt.Printf("Scanned Line: %v, %d elements\n", s, len(s))
if (len(s) >= 12) && !(strings.HasPrefix(s[3], "_")) {
t, err := time.Parse(time.RFC3339, s[3])
if err != nil {
@ -171,8 +171,8 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
continue
}
a := t.Unix()
lat = string2float64(s[9])
lon = string2float64(s[10])
lat = string2float64(s[len(s)-3])
lon = string2float64(s[len(s)-2])
if (lat == 0) || (lon == 0) {
fmt.Println("skip 0 value for lon/lat")
} else {
@ -195,7 +195,7 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, " ")
}
fmt.Fprintf(response, "[%d, %s, %s, %s, %d]\n", a, s[9], s[10], s[11], vbat2percent(s[11]))
fmt.Fprintf(response, "[%d, %s, %s, %s, %d]\n", a, s[len(s)-3], s[len(s)-2], s[len(s)-1], vbat2percent(s[len(s)-1]))
}
@ -279,7 +279,7 @@ func downloadmetricsHandler(response http.ResponseWriter, request *http.Request)
|> range(start: %s, stop: %s)
|> filter(fn: (r) => r._measurement == "measurement")
|> filter(fn: (r) => r.deveui == "%s")
|> filter(fn: (r) => r._field == "lon" or r._field == "lat")
|> filter(fn: (r) => r._field == "lon" or r._field == "lat" or r._field == "flags")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`, mystart, mystop, mydeveui))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
@ -316,15 +316,32 @@ func downloadmetricsHandler(response http.ResponseWriter, request *http.Request)
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
fmt.Printf("Scanned Line: %v\n", s)
fmt.Printf("Scanned Line: %v, elements: %d\n", s, len(s))
if (len(s) >= 10) && !(strings.HasPrefix(s[3], "_")) {
s_flags := ""
lat := s[len(s)-2]
lon := s[len(s)-1]
if lat == "0" {
s_flags = "NOGPS"
} else {
s_flags = "GPS"
}
if len(s) == 11 {
flags, _ := strconv.Atoi(s[len(s)-3])
if flags&1 == 1 {
s_flags = s_flags + "+ALARM_BUTTON"
}
if flags&2 == 2 && lat != "0" {
s_flags = s_flags + "+OUTSIDE_GREENZONE"
}
}
t, err := time.Parse(time.RFC3339, s[3])
if err != nil {
fmt.Printf("error converting time: %s\n", s[3])
continue
}
mytime := t.In(location).Format("02.01.2006 15:04")
fmt.Fprintf(response, "\"%s\",%s,%s\n", mytime, s[9], s[8])
fmt.Fprintf(response, "\"%s\",%s,%s,%s\n", mytime, lon, lat, s_flags)
}
}
@ -360,7 +377,7 @@ func vbat2html(vbat string) string {
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"
icon_color = "has-text-success pl-2 pr-2"
} else if percent <= 60 {
fa_battery_string = "fa-battery-half"
icon_color = "has-text-success pl-2 pr-2"
@ -432,7 +449,7 @@ func getLastMetrics(deveui string) OneMetric {
location, err := time.LoadLocation("Europe/Zurich")
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
fmt.Printf("BlaBla: %v\n", s)
fmt.Printf("BlaBla: %v, elements: %d\n", s, len(s))
if (len(s) >= 12) && !(strings.HasPrefix(s[3], "_")) {
t, err := time.Parse(time.RFC3339, s[3])
if err != nil {
@ -440,18 +457,18 @@ func getLastMetrics(deveui string) OneMetric {
continue
}
res.Fw = s[8]
res.Fw = s[len(s)-4]
res.Timestamp = t.In(location).Format("02.01.2006 15:04")
if s[9] != "0" {
res.Lat = s[9]
res.Lat = s[len(s)-3]
res.PosTimestamp = t.In(location).Format("02.01.2006 15:04")
}
if s[10] != "0" {
res.Lon = s[10]
res.Lon = s[len(s)-2]
res.PosTimestamp = t.In(location).Format("02.01.2006 15:04")
}
res.BatteryPercent = strconv.Itoa(vbat2percent(s[11]))
res.BatteryPercentHTML = template.HTML(vbat2html(s[11]))
res.BatteryPercent = strconv.Itoa(vbat2percent(s[len(s)-1]))
res.BatteryPercentHTML = template.HTML(vbat2html(s[len(s)-1]))
}