From f00afc971325bffc30af9d900fe4131f80397956 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Wed, 2 Jun 2021 18:17:39 +0200 Subject: [PATCH] implement alerting, second iteration --- alert.go | 28 ++++++++++++++++++---------- persistence.go | 28 +++++++++++++++++++++++----- wo-bisch-lorahandler.go | 2 +- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/alert.go b/alert.go index 1a48e6d..05f1b47 100644 --- a/alert.go +++ b/alert.go @@ -37,7 +37,7 @@ func stopAlerting(deveui string) { } func sendSMS(phonenumber string, alertMessage string) { - myurl := fmt.Sprintf("https://api.smsapi.com/sms.do?to=%s&message=%s&from=mini-beieli&format=json", phonenumber, url.QueryEscape(alertMessage)) + myurl := fmt.Sprintf("https://api.smsapi.com/sms.do?to=%s&message=%s&from=wo-bisch&format=json", phonenumber, url.QueryEscape(alertMessage)) req, err := http.NewRequest("GET", myurl, nil) if err != nil { @@ -65,22 +65,27 @@ func sendSMS(phonenumber string, alertMessage string) { func sendAlert(deveui string, alertMessage string) { fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage) smsnumber := getSmsnumber(deveui) - alarmactive := getDevAlarmactive(deveui) - fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber, alarmactive) - if (smsnumber != "") && (alarmactive == "1") { + smsalarmactive := getDevSmsAlarmactive(deveui) + fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, smsalarmactive=%s\n", deveui, smsnumber, smsalarmactive) + if (smsnumber != "") && (smsalarmactive == "1") { // we strip of the leading + smsnumber = strings.Replace(smsnumber, "+", "", -1) sendSMS(smsnumber, alertMessage) - } else { - email := getEmail(deveui) - fmt.Printf("sendEmail: deveui=%s, email=%s\n", deveui, email) + } + email := getEmail(deveui) + emailalarmactive := getDevEmailAlarmactive(deveui) + fmt.Printf("sendAlert: deveui=%s, email=%s, emailalarmactive=%s\n", deveui, email, emailalarmactive) + if emailalarmactive == "1" { sendEmail(email, alertMessage) } } -func DispatchAlert(deveui string, alertMessage string) { - // first let's stop the alerting cyle on the Lora Network (every minute for one hour!) - stopAlerting(deveui) +func DispatchAlert(deveui string, alertType string) { + // first let's stop the alerting cyle on the Lora Network (every minute for one hour!) if button was pressed + alertMessage := "Unknown alertType" + if alertType == "alert_button" { + stopAlerting(deveui) + } // we check if deveui exists if !(checkDevExists(deveui)) { @@ -104,5 +109,8 @@ func DispatchAlert(deveui string, alertMessage string) { AddAlertAlreadySentRecently(deveui) // then we send the alert + if alertType == "alert_button" { + alertMessage = fmt.Sprintf("Alarm (%s): Knopf gedrueckt, %s", getDevAlias(deveui), time.Now().Format("02.01.2006 15:04:05")) + } sendAlert(deveui, alertMessage) } diff --git a/persistence.go b/persistence.go index c811885..2747777 100644 --- a/persistence.go +++ b/persistence.go @@ -122,7 +122,7 @@ func getDevAlias(deveui string) string { return res } -func getDevAlarmactive(deveui string) string { +func getDevSmsAlarmactive(deveui string) string { res := "0" if deveui == "" { @@ -132,9 +132,9 @@ func getDevAlarmactive(deveui string) string { conn := globalPool.Get() defer conn.Close() - alarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "alarmactive")) + smsalarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "smsalarmactive")) if err == nil { - res = alarmactive + res = smsalarmactive } return res @@ -157,6 +157,24 @@ func getSmsnumber(deveui string) string { return res } +func getDevEmailAlarmactive(deveui string) string { + res := "0" + + if deveui == "" { + return res + } + + conn := globalPool.Get() + defer conn.Close() + + emailalarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "emailalarmactive")) + if err == nil { + res = emailalarmactive + } + + return res +} + func getEmail(deveui string) string { res := "" if deveui == "" { @@ -192,8 +210,8 @@ func AddAlertAlreadySentRecently(deveui string) { return } - // we set an expiration time of three hours - _, err1 := conn.Do("EXPIRE", alertsentPrefix+deveui, 600) + // we set an expiration time to prevent duplicate alerts + _, err1 := conn.Do("EXPIRE", alertsentPrefix+deveui, 120) if err1 != nil { return diff --git a/wo-bisch-lorahandler.go b/wo-bisch-lorahandler.go index a7fcdd8..334078e 100644 --- a/wo-bisch-lorahandler.go +++ b/wo-bisch-lorahandler.go @@ -102,7 +102,7 @@ func DecodePayload(s string, deveui string) { // we send an alert, when the alert bit is set if alarm { log.Printf("DispatchAlert (button pressed) for %s\n", deveui) - DispatchAlert(deveui, "Alert button pressed!") + DispatchAlert(deveui, "alert_button") } } else {