fix bug with outsideGreenzone Logic

This commit is contained in:
Joerg Lehmann 2021-06-11 15:24:47 +02:00
parent 2d93042fff
commit c9f0610cc0
1 changed files with 14 additions and 9 deletions

View File

@ -67,14 +67,15 @@ func Convert2Hex(payload_raw string) (string, error) {
return res, err
}
func IsOutsideGreenzone(deveui string, lon float64, lat float64) bool {
res := false
// 0: inside greenzone, 1: outside greenzone, 2: no GPS
func IsOutsideGreenzone(deveui string, lon float64, lat float64) int {
if lon < 1 {
return res
return 2
}
if lat < 1 {
return res
return 2
}
res := false
greenzone := getDevGreenzone(deveui)
if greenzone != "" {
coords := strings.Split(greenzone, ",")
@ -88,7 +89,11 @@ func IsOutsideGreenzone(deveui string, lon float64, lat float64) bool {
log.Printf("greenzone does not have right format: %s!", greenzone)
}
}
return res
if res {
return 1
} else {
return 0
}
}
func DecodePayload(s string, deveui string) {
@ -121,7 +126,7 @@ func DecodePayload(s string, deveui string) {
alarm := (pl.AlarmBat & 0x4000) == 0x4000 // Alarm Bit
outsideGreenzone := IsOutsideGreenzone(deveui, float64(pl.Longitude)/1000000.0, float64(pl.Latitude)/1000000.0)
flags := 0
if outsideGreenzone {
if outsideGreenzone == 1 {
flags += 2
}
if alarm {
@ -129,7 +134,7 @@ func DecodePayload(s string, deveui string) {
}
log.Printf("AlarmBit: %v", alarm)
log.Printf("outsideGreenzone: %v", outsideGreenzone)
log.Printf("outsideGreenzone: %d", outsideGreenzone)
mystring := fmt.Sprintf("measurement,deveui=%s lat=%.5f,lon=%.5f,vbat=%d,fw=%d,flags=%d %d\n", deveui, float32(pl.Latitude)/1000000.0, float32(pl.Longitude)/1000000.0, vbat, fw, flags, (time.Now().Unix() * 1000 * 1000 * 1000))
@ -139,10 +144,10 @@ func DecodePayload(s string, deveui string) {
log.Printf("DispatchAlert (button pressed) for %s\n", deveui)
DispatchAlert(deveui, "alert_button")
}
if outsideGreenzone {
if outsideGreenzone == 1 {
log.Printf("DispatchAlert (outside greenzone) for %s\n", deveui)
DispatchAlert(deveui, "alert_greenzone")
} else {
} else if outsideGreenzone == 0 {
// we reset the alert, when we are again withing the greenzone
if AlertAlreadySentRecently(deveui, "alert_greenzone") {
fmt.Printf("Info: we are again in Greenzone for Deveui %s, clear alert!", deveui)