109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func stopAlerting(deveui string) {
|
|
myurl := fmt.Sprintf("https://proxy1.lpn.swisscom.ch/thingpark/lrc/rest/downlink?DevEUI=%s&FPort=2&Payload=0201", deveui)
|
|
|
|
req, err := http.NewRequest("POST", myurl, nil)
|
|
if err != nil {
|
|
log.Fatal("Error reading request. ", err)
|
|
}
|
|
|
|
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatal("Error reading response. ", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Fatal("Error reading body. ", err)
|
|
}
|
|
|
|
fmt.Printf("%s\n", body)
|
|
}
|
|
|
|
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))
|
|
|
|
req, err := http.NewRequest("GET", myurl, nil)
|
|
if err != nil {
|
|
log.Fatal("Error reading request. ", err)
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer IQ4vRG2JvNOmYmrYz6RuSwAanYZgd2hHGwtN62kq")
|
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatal("Error reading response. ", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Fatal("Error reading body. ", err)
|
|
}
|
|
|
|
fmt.Printf("%s\n", body)
|
|
}
|
|
|
|
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") {
|
|
// 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)
|
|
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)
|
|
|
|
// we check if deveui exists
|
|
if !(checkDevExists(deveui)) {
|
|
fmt.Printf("Error: Deveui %s does not exist!", deveui)
|
|
return
|
|
}
|
|
|
|
// then we check if it expired
|
|
if AboExpired(deveui) {
|
|
fmt.Printf("Error: Abo for Deveui %s is expired!", deveui)
|
|
return
|
|
}
|
|
|
|
// then we check that an alert was not already sent out recently
|
|
if AlertAlreadySentRecently(deveui) {
|
|
fmt.Printf("Error: Alert for Deveui %s has already been sent!", deveui)
|
|
return
|
|
}
|
|
|
|
// then we make an entry that an alert was sent, this will expire automatically
|
|
AddAlertAlreadySentRecently(deveui)
|
|
|
|
// then we send the alert
|
|
sendAlert(deveui, alertMessage)
|
|
}
|