300 lines
5.5 KiB
Go
300 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gomodule/redigo/redis"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
var globalPool *redis.Pool
|
|
|
|
const lastvaluesPrefix string = "lastvalues:"
|
|
const devPrefix string = "dev:"
|
|
|
|
type CalSettings struct {
|
|
w1_0 int32
|
|
w2_0 int32
|
|
w1_c float32
|
|
w2_c float32
|
|
}
|
|
|
|
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", uint32(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) uint32 {
|
|
var res uint32 = 0
|
|
var myvalues []uint32
|
|
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 uint32(value) > res {
|
|
res = uint32(value)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func addValue(deveui string, value uint32) {
|
|
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 three hours
|
|
_, err3 := conn.Do("EXPIRE", lastvaluesPrefix+deveui, 10800)
|
|
|
|
if err3 != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
func deleteValues(deveui string) {
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
_, err := conn.Do("LTRIM", lastvaluesPrefix+deveui, 1, 0)
|
|
|
|
if err != 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
|
|
}
|
|
|
|
func getDevAlarmactive(deveui string) string {
|
|
res := "0"
|
|
|
|
if deveui == "" {
|
|
return res
|
|
}
|
|
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
alarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "alarmactive"))
|
|
if err == nil {
|
|
res = alarmactive
|
|
}
|
|
|
|
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 GetDownlinkCommand(deveui string) string {
|
|
// 0: do nothing...
|
|
res := "do_nothing"
|
|
|
|
if deveui == "" {
|
|
return res
|
|
}
|
|
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
downlink_command, err := redis.String(conn.Do("HGET", devPrefix+deveui, "downlink_command"))
|
|
if err == nil {
|
|
res = downlink_command
|
|
} else {
|
|
fmt.Println(err)
|
|
res = "do_nothing"
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func SetDownlinkCommand(deveui string, new_command string) error {
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
// SET object
|
|
_, err := conn.Do("HMSET", devPrefix+deveui, "downlink_command", new_command)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetCurrentCalibrationSettings(deveui string) CalSettings {
|
|
var res CalSettings
|
|
if deveui == "" {
|
|
return res
|
|
}
|
|
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
s_w1_0, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w1_0"))
|
|
if err == nil {
|
|
n, err := strconv.ParseInt(s_w1_0, 10, 32)
|
|
if err == nil {
|
|
res.w1_0 = int32(n)
|
|
}
|
|
}
|
|
|
|
s_w2_0, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w2_0"))
|
|
if err == nil {
|
|
n, err := strconv.ParseInt(s_w2_0, 10, 32)
|
|
if err == nil {
|
|
res.w2_0 = int32(n)
|
|
}
|
|
}
|
|
|
|
s_w1_c, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w1_c"))
|
|
if err == nil {
|
|
f, err := strconv.ParseFloat(s_w1_c, 32)
|
|
if err == nil {
|
|
res.w1_c = float32(f)
|
|
}
|
|
}
|
|
|
|
s_w2_c, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w2_c"))
|
|
if err == nil {
|
|
f, err := strconv.ParseFloat(s_w2_c, 32)
|
|
if err == nil {
|
|
res.w2_c = float32(f)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func SetCurrentCalibrationSettings(deveui string, cal_settings CalSettings) error {
|
|
if deveui == "" {
|
|
return nil
|
|
}
|
|
|
|
conn := globalPool.Get()
|
|
defer conn.Close()
|
|
|
|
w1_0 := fmt.Sprintf("%d", cal_settings.w1_0)
|
|
w2_0 := fmt.Sprintf("%d", cal_settings.w2_0)
|
|
w1_c := fmt.Sprintf("%f", cal_settings.w1_c)
|
|
w2_c := fmt.Sprintf("%f", cal_settings.w2_c)
|
|
|
|
fmt.Printf("DEBUG SetCurrentCalibrationSettings: w1_0: %s, w2_0: %s, w1_c: %s, w2_c: %s\n", w1_0, w2_0, w1_c, w2_c)
|
|
|
|
// SET object
|
|
_, err := conn.Do("HMSET", devPrefix+deveui, "w1_0", w1_0, "w2_0", w2_0, "w1_c", w1_c, "w2_c", w2_c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|