implement alerting, second iteration
This commit is contained in:
parent
f66060bc7e
commit
f00afc9713
24
alert.go
24
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)
|
||||
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!)
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue