diff --git a/alert.go b/alert.go index 104f812..9b5f8c6 100644 --- a/alert.go +++ b/alert.go @@ -11,7 +11,7 @@ import ( ) 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) if err != nil { diff --git a/main.go b/main.go index fb9501f..914b0d6 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "log" "net/http" "os" + "strconv" "strings" "time" ) @@ -558,6 +559,31 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint 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) } + + // 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") + } + } + } + } } } diff --git a/persistence.go b/persistence.go index 0e1abfd..a36ec08 100644 --- a/persistence.go +++ b/persistence.go @@ -195,6 +195,26 @@ func getSmsnumber(deveui string) string { 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 { // 0: do nothing... res := "do_nothing" @@ -217,6 +237,37 @@ func GetDownlinkCommand(deveui string) string { 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 { conn := globalPool.Get() defer conn.Close()