144 lines
2.9 KiB
Go
144 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"github.com/gomodule/redigo/redis"
|
|
)
|
|
|
|
var globalPool *redis.Pool
|
|
|
|
const lastvaluesPrefix string = "lastvalues:"
|
|
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)
|
|
|
|
addValue("0000000000000000",uint16(time.Now().Unix()));
|
|
}
|
|
|
|
func closeDB() {
|
|
globalPool.Close()
|
|
}
|
|
|
|
func checkDevAvailable(deveui string) bool {
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
_, err := redis.String(conn.Do("GET", lastvaluesPrefix + deveui))
|
|
if (err == redis.ErrNil) {
|
|
return true
|
|
} else if err != nil {
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
func getMaxValue(deveui string) uint16 {
|
|
var res uint16 = 0
|
|
var myvalues []uint16
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
values, _ := redis.Values(conn.Do("LRANGE", lastvaluesPrefix + deveui, 0, -1))
|
|
|
|
if err := redis.ScanSlice(values, &myvalues); err != nil {
|
|
fmt.Println(err)
|
|
return 0
|
|
}
|
|
|
|
for _, value := range myvalues {
|
|
if (uint16(value) > res) {
|
|
res = uint16(value)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func addValue(deveui string, value uint16) {
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
_, err := conn.Do("LPUSH", lastvaluesPrefix + deveui, value)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_, err2 := conn.Do("LTRIM", lastvaluesPrefix + deveui, 0, 4)
|
|
|
|
if err2 != nil {
|
|
return
|
|
}
|
|
|
|
// we set an expiration time of one hour
|
|
_, err3 := conn.Do("EXPIRE", lastvaluesPrefix + deveui, 3600)
|
|
|
|
if err3 != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|