check_nodes
This commit is contained in:
parent
7cee436446
commit
e3b06b298c
|
|
@ -9,6 +9,7 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -212,7 +213,7 @@ func InsertAlert(prefix string, deveui string, email string, threshold int) {
|
|||
|
||||
_, err := conn.Do("SET", prefix+deveui+":"+email, threshold)
|
||||
if err != nil {
|
||||
logit("InsertAlert: Error inserting: " + prefix + deveui + ":" + email)
|
||||
logit("InsertAlert: Error inserting: " + prefix + deveui + ":" + email)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -264,15 +265,32 @@ func CalcDaysUntil(mydate string) int {
|
|||
return days
|
||||
}
|
||||
|
||||
func vbat2percent(vbat string) int {
|
||||
i, err := strconv.Atoi(vbat)
|
||||
res := 0
|
||||
if err == nil {
|
||||
res = int(float64(i-3500) * float64((100.0/0.7)*0.001))
|
||||
//fmt.Printf("vbat2percent Result in Percent: %d\n", res)
|
||||
if res < 0 {
|
||||
res = 0
|
||||
} else if res > 100 {
|
||||
res = 100
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func getLastMetrics(deveui string) OneMetric {
|
||||
var res OneMetric
|
||||
|
||||
url := "http://localhost:8086/api/v2/query?org=wobischorg"
|
||||
data := []byte(fmt.Sprintf(`from(bucket:"wobischbucket")
|
||||
|> range(start:-5d)
|
||||
|> tail(n:10)
|
||||
|> filter(fn: (r) => r._measurement == "measurement" and r.deveui == "%s")
|
||||
|> filter(fn: (r) => r._field == "vp")
|
||||
|> last() |> yield(name: "last")`, deveui))
|
||||
|> filter(fn: (r) => r._field == "vbat")
|
||||
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`, deveui))
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
|
|
@ -280,7 +298,9 @@ func getLastMetrics(deveui string) OneMetric {
|
|||
}
|
||||
|
||||
// Set headers
|
||||
req.Header.Set("Authorization", "Token nKYCoz3TA-LItYXG988DjdiStMhrfKmFXQqzxrjzJJ7Ek_iUttzFSE9lfe3s6q99EMdcrjuGlDAjp4Y0VnNRXw==")
|
||||
var INFLUX_RO_TOKEN = os.Getenv("INFLUX_RO_TOKEN")
|
||||
|
||||
req.Header.Set("Authorization", "Token "+INFLUX_RO_TOKEN)
|
||||
req.Header.Set("accept", "application/csv")
|
||||
req.Header.Set("content-type", "application/vnd.flux")
|
||||
|
||||
|
|
@ -307,19 +327,20 @@ func getLastMetrics(deveui string) OneMetric {
|
|||
location, err := time.LoadLocation("Europe/Zurich")
|
||||
for scanner.Scan() {
|
||||
s := strings.Split(scanner.Text(), ",")
|
||||
if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) {
|
||||
mytime, err := time.Parse(time.RFC3339, s[5])
|
||||
if (len(s) >= 9) && !(strings.HasPrefix(s[3], "_")) {
|
||||
t, err := time.Parse(time.RFC3339, s[3])
|
||||
if err != nil {
|
||||
fmt.Printf("error converting time: %s\n", s[3])
|
||||
continue
|
||||
}
|
||||
res.Timestamp = mytime.In(location).Format("02.01.2006 15:04")
|
||||
value := s[6]
|
||||
field := s[7]
|
||||
if field == "vp" {
|
||||
res.BatteryPercent = value
|
||||
}
|
||||
|
||||
res.Timestamp = t.In(location).Format("02.01.2006 15:04")
|
||||
res.BatteryPercent = strconv.Itoa(vbat2percent(s[8]))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
res.Deveui = deveui
|
||||
res.Alias = getDevAlias(deveui)
|
||||
res.ActiveUntil = getActiveUntil(deveui)
|
||||
|
|
@ -335,7 +356,7 @@ func CheckThreshold(d string, vp int, u2 string, last_metric OneMetric, info_thr
|
|||
if vp <= alert_threshold {
|
||||
if AlarmNotAlreadySent("alarm_sent_accu:", d, u2, alert_threshold) {
|
||||
sendEmailAccu(u2, alias, d, last_metric.BatteryPercent, alert_threshold, "ALARM")
|
||||
InsertAlert("alarm_sent_accu:", d , u2, alert_threshold)
|
||||
InsertAlert("alarm_sent_accu:", d, u2, alert_threshold)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
@ -371,7 +392,7 @@ func main() {
|
|||
u2 := u[5:]
|
||||
my_devs := getMyDevs(u2)
|
||||
for _, d := range my_devs {
|
||||
//fmt.Printf("%s:%s\n", u2, d)
|
||||
fmt.Printf("%s:%s\n", u2, d)
|
||||
if !strings.HasPrefix(d, "@") {
|
||||
last_metric := getLastMetrics(d)
|
||||
// Zuerst der Batteriealarm
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -5,6 +5,7 @@ go 1.14
|
|||
require (
|
||||
github.com/gomodule/redigo v1.8.4
|
||||
github.com/gorilla/securecookie v1.1.1
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
|
||||
github.com/stripe/stripe-go/v72 v72.41.0
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
|
||||
)
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -4,6 +4,8 @@ github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUz
|
|||
github.com/gomodule/redigo/redis v0.0.0-do-not-use h1:J7XIp6Kau0WoyT4JtXHT3Ei0gA1KkSc6bc87j9v9WIo=
|
||||
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
|
||||
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
|
|
|
|||
Loading…
Reference in New Issue