allow min/max alerting for weight

This commit is contained in:
Joerg Lehmann 2021-08-21 17:07:13 +02:00
parent 69c6900a77
commit bb4c713d3c
3 changed files with 78 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import (
) )
func sendSMS(phonenumber string, alertMessage string) { func sendSMS(phonenumber string, alertMessage string) {
myurl := fmt.Sprintf("https://api.smsapi.com/sms.do?to=%s&message=%s&format=json", phonenumber, url.QueryEscape(alertMessage)) myurl := fmt.Sprintf("https://api.smsapiblabla.com/sms.do?to=%s&message=%s&format=json", phonenumber, url.QueryEscape(alertMessage))
req, err := http.NewRequest("GET", myurl, nil) req, err := http.NewRequest("GET", myurl, nil)
if err != nil { if err != nil {

26
main.go
View File

@ -11,6 +11,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -558,6 +559,31 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
location, _ := time.LoadLocation("Europe/Zurich") location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Schwarmalarm ***\n%s\n%s\nGewichtsverlust: %d g", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_loss) alertMap[deveui] = fmt.Sprintf("*** Schwarmalarm ***\n%s\n%s\nGewichtsverlust: %d g", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_loss)
} }
// minmax Alert
minmax := getDevMinmax(deveui)
if minmax != "0,0" {
tokens := strings.Split(minmax, ",")
if len(tokens) == 2 {
min, _ := strconv.ParseInt(tokens[0], 10, 32)
max, _ := strconv.ParseInt(tokens[1], 10, 32)
minmaxstatus := GetMinMaxStatus(deveui)
if (w_gram < uint32(min)) && (minmaxstatus != "MINALERT") {
SetMinMaxStatus(deveui, "MINALERT")
alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"minmaxlarm\",w=%di,w_min=%di %d\n", deveui, w_gram, min, mytime*60*1000*1000*1000)
location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Min/Max-Alarm ***\n%s\n%s\nUnterschreiten des Minimalgewichts: %d g (Minimal: %d g)", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_gram, min)
} else if (w_gram > uint32(max)) && (minmaxstatus != "MAXALERT") {
SetMinMaxStatus(deveui, "MAXALERT")
alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"minmaxlarm\",w=%di,w_max=%di %d\n", deveui, w_gram, max, mytime*60*1000*1000*1000)
location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Min/Max-Alarm ***\n%s\n%s\nUeberschreiten des Maximalgewichts: %d g (Maximal: %d g)", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_gram, max)
} else if (w_gram > uint32(min+100)) && (w_gram < uint32(max-100)) {
SetMinMaxStatus(deveui, "NORMAL")
}
}
}
} }
} }
} }

View File

@ -195,6 +195,26 @@ func getSmsnumber(deveui string) string {
return res return res
} }
func getDevMinmax(deveui string) string {
res := "0,0"
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
minmax, err := redis.String(conn.Do("HGET", devPrefix+deveui, "minmax"))
if err == nil {
res = minmax
} else {
fmt.Println(err)
}
return res
}
func GetDownlinkCommand(deveui string) string { func GetDownlinkCommand(deveui string) string {
// 0: do nothing... // 0: do nothing...
res := "do_nothing" res := "do_nothing"
@ -217,6 +237,37 @@ func GetDownlinkCommand(deveui string) string {
return res return res
} }
func SetMinMaxStatus(deveui string, status string) error {
conn := globalPool.Get()
defer conn.Close()
// SET object
_, err := conn.Do("HMSET", devPrefix+deveui, "minmaxstatus", status)
if err != nil {
return err
}
return nil
}
func GetMinMaxStatus(deveui string) string {
res := "NORMAL"
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
minmax, err := redis.String(conn.Do("HGET", devPrefix+deveui, "minmaxstatus"))
if err == nil {
res = minmax
}
return res
}
func SetDownlinkCommand(deveui string, new_command string) error { func SetDownlinkCommand(deveui string, new_command string) error {
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()