wo-bisch-lorahandler/persistence.go

258 lines
4.4 KiB
Go

package main
import (
"github.com/gomodule/redigo/redis"
"log"
"time"
)
var globalPool *redis.Pool
const alertsentPrefix string = "alertsent:"
const devPrefix string = "dev:"
func newPool() *redis.Pool {
return &redis.Pool{
// Maximum number of idle connections in the pool.
MaxIdle: 80,
// max number of connections
MaxActive: 12000,
// Dial is an application supplied function for creating and
// configuring a connection.
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err.Error())
}
return c, err
},
}
}
// ping tests connectivity for redis (PONG should be returned)
func ping(c redis.Conn) error {
// Send PING command to Redis
// PING command returns a Redis "Simple String"
// Use redis.String to convert the interface type to string
_, err := redis.String(c.Do("PING"))
if err != nil {
return err
}
return nil
}
func initDB() {
// newPool returns a pointer to a redis.Pool
pool := newPool()
// get a connection from the globalPool (redis.Conn)
conn := pool.Get()
defer conn.Close()
globalPool = pool
// wir machen einen Connection Test
ping(conn)
}
func closeDB() {
globalPool.Close()
}
func getActiveUntil(deveui string) string {
res := ""
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
activeuntil, err := redis.String(conn.Do("HGET", devPrefix+deveui, "active_until"))
if err == nil {
res = activeuntil
} else {
log.Print(err)
}
return res
}
func AboExpired(deveui string) bool {
active_until := getActiveUntil(deveui)
layout := "02.01.2006"
t, _ := time.Parse(layout, active_until)
return t.Before(time.Now())
}
func checkDevExists(deveui string) bool {
conn := globalPool.Get()
defer conn.Close()
_, err := redis.String(conn.Do("GET", devPrefix+deveui))
if err == redis.ErrNil {
return false
} else if err != nil {
return true
}
return false
}
func getDevAlias(deveui string) string {
res := deveui
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
alias, err := redis.String(conn.Do("HGET", devPrefix+deveui, "alias"))
if err == nil {
res = alias
} else {
res = deveui
}
return res
}
func getDevSmsAlarmactive(deveui string) string {
res := "0"
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
smsalarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "smsalarmactive"))
if err == nil {
res = smsalarmactive
}
return res
}
func getSmsnumber(deveui string) string {
res := ""
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
smsnumber, err := redis.String(conn.Do("HGET", devPrefix+deveui, "smsnumber"))
if err == nil {
res = smsnumber
}
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 == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
email, err := redis.String(conn.Do("HGET", devPrefix+deveui, "email"))
if err == nil {
res = email
}
return res
}
func getDevGreenzone(deveui string) string {
res := ""
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
greenzone, err := redis.String(conn.Do("HGET", devPrefix+deveui, "greenzone"))
if err == nil {
res = greenzone
} else {
log.Print(err)
}
return res
}
func AlertAlreadySentRecently(deveui string, what string) bool {
conn := globalPool.Get()
defer conn.Close()
exists, _ := redis.Bool(conn.Do("EXISTS", alertsentPrefix+deveui+":"+what))
return exists
}
func AddAlertAlreadySentRecently(deveui string, what string) {
conn := globalPool.Get()
defer conn.Close()
expire_duration := 120
if what == "alert_greenzone" {
expire_duration = 7 * 24 * 60 * 60
}
_, err := conn.Do("SET", alertsentPrefix+deveui+":"+what, "1")
if err != nil {
return
}
// we set an expiration time to prevent duplicate alerts
_, err1 := conn.Do("EXPIRE", alertsentPrefix+deveui+":"+what, expire_duration)
if err1 != nil {
return
}
}
func DeleteGreenzoneAlert(deveui string) bool {
conn := globalPool.Get()
defer conn.Close()
_, err := conn.Do("DEL", alertsentPrefix+deveui+":alert_greenzone")
if err != nil {
return false
}
return true
}