implement alerting, second iteration

This commit is contained in:
Joerg Lehmann 2021-06-02 18:17:39 +02:00
parent f66060bc7e
commit f00afc9713
3 changed files with 42 additions and 16 deletions

View File

@ -37,7 +37,7 @@ func stopAlerting(deveui string) {
} }
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&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) req, err := http.NewRequest("GET", myurl, nil)
if err != nil { if err != nil {
@ -65,22 +65,27 @@ func sendSMS(phonenumber string, alertMessage string) {
func sendAlert(deveui string, alertMessage string) { func sendAlert(deveui string, alertMessage string) {
fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage) fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage)
smsnumber := getSmsnumber(deveui) smsnumber := getSmsnumber(deveui)
alarmactive := getDevAlarmactive(deveui) smsalarmactive := getDevSmsAlarmactive(deveui)
fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber, alarmactive) fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, smsalarmactive=%s\n", deveui, smsnumber, smsalarmactive)
if (smsnumber != "") && (alarmactive == "1") { if (smsnumber != "") && (smsalarmactive == "1") {
// we strip of the leading + // we strip of the leading +
smsnumber = strings.Replace(smsnumber, "+", "", -1) smsnumber = strings.Replace(smsnumber, "+", "", -1)
sendSMS(smsnumber, alertMessage) sendSMS(smsnumber, alertMessage)
} else { }
email := getEmail(deveui) email := getEmail(deveui)
fmt.Printf("sendEmail: deveui=%s, email=%s\n", deveui, email) emailalarmactive := getDevEmailAlarmactive(deveui)
fmt.Printf("sendAlert: deveui=%s, email=%s, emailalarmactive=%s\n", deveui, email, emailalarmactive)
if emailalarmactive == "1" {
sendEmail(email, alertMessage) sendEmail(email, alertMessage)
} }
} }
func DispatchAlert(deveui string, alertMessage string) { func DispatchAlert(deveui string, alertType string) {
// first let's stop the alerting cyle on the Lora Network (every minute for one hour!) // first let's stop the alerting cyle on the Lora Network (every minute for one hour!) if button was pressed
stopAlerting(deveui) alertMessage := "Unknown alertType"
if alertType == "alert_button" {
stopAlerting(deveui)
}
// we check if deveui exists // we check if deveui exists
if !(checkDevExists(deveui)) { if !(checkDevExists(deveui)) {
@ -104,5 +109,8 @@ func DispatchAlert(deveui string, alertMessage string) {
AddAlertAlreadySentRecently(deveui) AddAlertAlreadySentRecently(deveui)
// then we send the alert // 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) sendAlert(deveui, alertMessage)
} }

View File

@ -122,7 +122,7 @@ func getDevAlias(deveui string) string {
return res return res
} }
func getDevAlarmactive(deveui string) string { func getDevSmsAlarmactive(deveui string) string {
res := "0" res := "0"
if deveui == "" { if deveui == "" {
@ -132,9 +132,9 @@ func getDevAlarmactive(deveui string) string {
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() 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 { if err == nil {
res = alarmactive res = smsalarmactive
} }
return res return res
@ -157,6 +157,24 @@ func getSmsnumber(deveui string) string {
return res 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 { func getEmail(deveui string) string {
res := "" res := ""
if deveui == "" { if deveui == "" {
@ -192,8 +210,8 @@ func AddAlertAlreadySentRecently(deveui string) {
return return
} }
// we set an expiration time of three hours // we set an expiration time to prevent duplicate alerts
_, err1 := conn.Do("EXPIRE", alertsentPrefix+deveui, 600) _, err1 := conn.Do("EXPIRE", alertsentPrefix+deveui, 120)
if err1 != nil { if err1 != nil {
return return

View File

@ -102,7 +102,7 @@ func DecodePayload(s string, deveui string) {
// we send an alert, when the alert bit is set // we send an alert, when the alert bit is set
if alarm { if alarm {
log.Printf("DispatchAlert (button pressed) for %s\n", deveui) log.Printf("DispatchAlert (button pressed) for %s\n", deveui)
DispatchAlert(deveui, "Alert button pressed!") DispatchAlert(deveui, "alert_button")
} }
} else { } else {