Compare commits

..

10 Commits

5 changed files with 414 additions and 264 deletions

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM golang:alpine AS builder
WORKDIR /build
ADD go.mod .
COPY . .
RUN go build
FROM alpine
RUN apk add --no-cache tzdata
ENV TZ=Europe/Zurich
WORKDIR /build
COPY --from=builder /build/mini-beieli-lorahandler /build/mini-beieli-lorahandler
CMD ["./mini-beieli-lorahandler"]
EXPOSE 8080

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module nbit.ch/mini-beieli-lorahandler/v2
go 1.17
require github.com/gomodule/redigo v1.8.9

12
go.sum Normal file
View File

@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

587
main.go
View File

@ -11,6 +11,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
@ -19,7 +20,7 @@ const (
MyDB = "beieliscaledb"
username = "beieli"
password = "beieli4president"
outputfile = "/home/beieli/mini-beieli-lorahandler/mini-beieli-lorahandler.log"
outputfile = "/data/mini-beieli-lorahandler.log"
NOT_PLAUSIBLE_16 = 65535
NOT_PLAUSIBLE_32 = 2147483647
)
@ -213,270 +214,298 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
pl_130 = payload_130{}
br := bytes.NewReader(ba)
fmt.Printf("Payload String: %s\n", s)
if s[0:2] == "01" {
err := binary.Read(br, binary.LittleEndian, &pl_1)
if err != nil {
fmt.Println(err)
if len(s) > 2 {
if s[0:2] == "01" {
err := binary.Read(br, binary.LittleEndian, &pl_1)
if err != nil {
fmt.Println(err)
}
} else if s[0:2] == "02" {
err := binary.Read(br, binary.LittleEndian, &pl_2)
if err != nil {
fmt.Println(err)
}
} else if s[0:2] == "80" {
err := binary.Read(br, binary.LittleEndian, &pl_128)
if err != nil {
fmt.Println(err)
}
} else if s[0:2] == "81" {
err := binary.Read(br, binary.LittleEndian, &pl_129)
if err != nil {
fmt.Println(err)
}
} else if s[0:2] == "82" {
err := binary.Read(br, binary.LittleEndian, &pl_130)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Printf("Payload String is unknown: %s\n", s)
}
} else if s[0:2] == "02" {
err := binary.Read(br, binary.LittleEndian, &pl_2)
if err != nil {
fmt.Println(err)
if s[0:2] == "01" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_1.Version)
fmt.Printf(" vbat: %d,\n", pl_1.Vbat)
fmt.Printf(" offset: %d\n", pl_1.O)
fmt.Printf(" humidity: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.H1, pl_1.H2, pl_1.H3, pl_1.H4, pl_1.H5, pl_1.H6, pl_1.H7, pl_1.H8)
fmt.Printf(" pressure: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.P1, pl_1.P2, pl_1.P3, pl_1.P4, pl_1.P5, pl_1.P6, pl_1.P7, pl_1.P8)
fmt.Printf(" weight: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.W1, pl_1.W2, pl_1.W3, pl_1.W4, pl_1.W5, pl_1.W6, pl_1.W7, pl_1.W8)
fmt.Printf(" temp: %d,\n", pl_1.T)
fmt.Printf(" temp_change: [%d,%d,%d,%d,%d,%d,%d],\n", pl_1.TC1, pl_1.TC2, pl_1.TC3, pl_1.TC4, pl_1.TC5, pl_1.TC6, pl_1.TC7)
fmt.Printf("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_1.O)
humidity_values := []uint8{pl_1.H1, pl_1.H2, pl_1.H3, pl_1.H4, pl_1.H5, pl_1.H6, pl_1.H7, pl_1.H8}
valid_measurements := 0
for _, v := range humidity_values {
if v > 0 {
valid_measurements = valid_measurements + 1
}
}
fmt.Printf("Valid Measurements: %d,\n", valid_measurements)
var step int64
if valid_measurements > 1 {
step = int64(int(pl_1.O) / (valid_measurements - 1))
}
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// the second measurement as first..., the same for humidity, which is usually too low...
t := pl_1.T
t = t + int16(pl_1.TC1)
if valid_measurements > 0 {
WriteDatapoint(tfp, deveui, devaddr, pl_1.Vbat, pl_1.H1, pl_1.P1, pl_1.W1, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
//t = t + int16(pl_1.TC1)
if valid_measurements > 1 {
WriteDatapoint(tfp+(step), deveui, devaddr, 0, pl_1.H2, pl_1.P2, pl_1.W2, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC2)
if valid_measurements > 2 {
WriteDatapoint(tfp+(2*step), deveui, devaddr, 0, pl_1.H3, pl_1.P3, pl_1.W3, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC3)
if valid_measurements > 3 {
WriteDatapoint(tfp+(3*step), deveui, devaddr, 0, pl_1.H4, pl_1.P4, pl_1.W4, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC4)
if valid_measurements > 4 {
WriteDatapoint(tfp+(4*step), deveui, devaddr, 0, pl_1.H5, pl_1.P5, pl_1.W5, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC5)
if valid_measurements > 5 {
WriteDatapoint(tfp+(5*step), deveui, devaddr, 0, pl_1.H6, pl_1.P6, pl_1.W6, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC6)
if valid_measurements > 6 {
WriteDatapoint(tfp+(6*step), deveui, devaddr, 0, pl_1.H7, pl_1.P7, pl_1.W7, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC7)
if valid_measurements > 7 {
WriteDatapoint(tfp+(7*step), deveui, devaddr, 0, pl_1.H8, pl_1.P8, pl_1.W8, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
}
} else if s[0:2] == "02" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_2.Version)
fmt.Printf(" vbat: %d,\n", pl_2.Vbat)
fmt.Printf(" offset: %d\n", pl_2.O)
fmt.Printf(" weight: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_2.W1, pl_2.WC1, pl_2.WC2, pl_2.WC3, pl_2.WC4, pl_2.WC5, pl_2.WC6, pl_2.W8)
fmt.Printf(" temp: %d,\n", pl_2.T)
fmt.Printf(" temp_change: [%d,%d,%d,%d,%d,%d,%d],\n", pl_2.TC1, pl_2.TC2, pl_2.TC3, pl_2.TC4, pl_2.TC5, pl_2.TC6, pl_2.TC7)
fmt.Printf(" humidity: %d\n", pl_2.H)
fmt.Printf(" pressure: %d\n", pl_2.P)
fmt.Printf("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_2.O)
temp_change_values := []int8{pl_2.TC1, pl_2.TC2, pl_2.TC3, pl_2.TC4, pl_2.TC5, pl_2.TC6, pl_2.TC7}
valid_measurements := 1
for _, v := range temp_change_values {
if v < 127 {
valid_measurements = valid_measurements + 1
}
}
fmt.Printf("Valid Measurements: %d,\n", valid_measurements)
var step int64
if valid_measurements > 1 {
step = int64(int(pl_2.O) / (valid_measurements - 1))
}
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// the second measurement as first...
t := pl_2.T
w := pl_2.W1
t = t + int16(pl_2.TC1)
if valid_measurements > 0 {
WriteDatapoint(tfp, deveui, devaddr, pl_2.Vbat, pl_2.H, pl_2.P, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
//t = t + int16(pl_2.TC1)
if valid_measurements == 2 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC1)
}
if valid_measurements > 1 {
WriteDatapoint(tfp+(step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC2)
if valid_measurements == 3 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC2)
}
if valid_measurements > 2 {
WriteDatapoint(tfp+(2*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC3)
if valid_measurements == 4 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC3)
}
if valid_measurements > 3 {
WriteDatapoint(tfp+(3*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC4)
if valid_measurements == 5 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC4)
}
if valid_measurements > 4 {
WriteDatapoint(tfp+(4*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC5)
if valid_measurements == 6 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC5)
}
if valid_measurements > 5 {
WriteDatapoint(tfp+(5*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC6)
if valid_measurements == 7 {
w = pl_2.W8
} else {
w = w + uint16(pl_2.WC6)
}
if valid_measurements > 6 {
WriteDatapoint(tfp+(6*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC7)
w = pl_2.W8
if valid_measurements > 7 {
WriteDatapoint(tfp+(7*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
}
} else if s[0:2] == "80" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_128.Version)
fmt.Printf(" fw_version: %d,\n", pl_128.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_128.Vbat)
fmt.Printf(" humidity: %d\n", pl_128.H)
fmt.Printf(" pressure: %d\n", pl_128.P)
fmt.Printf(" weight1: %d\n", pl_128.W1)
fmt.Printf(" weight2: %d\n", pl_128.W2)
fmt.Printf(" cal_1_0: %d\n", pl_128.W1_0)
fmt.Printf(" cal_2_0: %d\n", pl_128.W2_0)
fmt.Printf(" cal_1_C: %f\n", pl_128.W1_C)
fmt.Printf(" cal_2_C: %f\n", pl_128.W2_C)
fmt.Printf(" temp: %d\n", pl_128.T)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// we calculate the weight...
var w32 int32
var w uint16
w1_0_real := pl_128.W1_0
w2_0_real := pl_128.W2_0
w32 = int32(((float64(pl_128.W1-w1_0_real) / float64(pl_128.W1_C)) + (float64(pl_128.W2-w2_0_real) / float64(pl_128.W2_C))) / 5.0)
if w32 < 0 {
w = 0
} else if w32 > 65535 {
// this is not realistic (>320 kg), we set this to 0 as well...
w = 0
} else {
w = uint16(w32)
}
WriteDatapoint(tfp, deveui, devaddr, pl_128.Vbat, pl_128.H, pl_128.P, w, pl_128.W1, pl_128.W2, pl_128.T, lrrlat, lrrlon, pl_128.Fw_version, pl_128.W1_0, pl_128.W2_0, pl_128.W1_C, pl_128.W2_C)
}
ProcessInitPacket(deveui, pl_128.W1_0, pl_128.W2_0, pl_128.W1_C, pl_128.W2_C, pl_128.W1, pl_128.W2)
} else if s[0:2] == "81" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_129.Version)
fmt.Printf(" fw_version: %d,\n", pl_129.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_129.Vbat)
fmt.Printf(" humidity: %d\n", pl_129.H)
fmt.Printf(" pressure: %d\n", pl_129.P)
fmt.Printf(" weight1: %d\n", pl_129.W1)
fmt.Printf(" weight2: %d\n", pl_129.W2)
fmt.Printf(" weight: %d\n", pl_129.W)
fmt.Printf(" temp: %d\n", pl_129.T)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
WriteDatapoint(tfp, deveui, devaddr, pl_129.Vbat, pl_129.H, pl_129.P, pl_129.W, pl_129.W1, pl_129.W2, pl_129.T, lrrlat, lrrlon, pl_129.Fw_version, 0, 0, 0, 0)
}
} else if s[0:2] == "82" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_130.Version)
fmt.Printf(" fw_version: %d,\n", pl_130.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_130.Vbat)
fmt.Printf(" weight1: %d\n", pl_130.W1)
fmt.Printf(" weight2: %d\n", pl_130.W2)
fmt.Printf(" cal_1_0: %d\n", pl_130.W1_0)
fmt.Printf(" cal_2_0: %d\n", pl_130.W2_0)
fmt.Printf(" cal_1_C: %f\n", pl_130.W1_C)
fmt.Printf(" cal_2_C: %f\n", pl_130.W2_C)
fmt.Printf(" temp: %d\n", pl_130.T)
fmt.Printf(" humidity: %d\n", pl_130.H)
fmt.Printf(" pressure: %d\n", pl_130.P)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// we calculate the weight...
var w32 int32
var w uint16
w1_0_real := pl_130.W1_0
w2_0_real := pl_130.W2_0
w32 = int32(((float64(pl_130.W1-w1_0_real) / float64(pl_130.W1_C)) + (float64(pl_130.W2-w2_0_real) / float64(pl_130.W2_C))) / 5.0)
if w32 < 0 {
w = 0
} else if w32 > 65535 {
// this is not realistic (>320 kg), we set this to 0 as well...
w = 0
} else {
w = uint16(w32)
}
WriteDatapoint(tfp, deveui, devaddr, pl_130.Vbat, pl_130.H, pl_130.P, w, pl_130.W1, pl_130.W2, pl_130.T, lrrlat, lrrlon, pl_130.Fw_version, pl_130.W1_0, pl_130.W2_0, pl_130.W1_C, pl_130.W2_C)
}
}
} else if s[0:2] == "80" {
err := binary.Read(br, binary.LittleEndian, &pl_128)
if err != nil {
fmt.Println(err)
// Send alert if necessary
if val, ok := alertMap[deveui]; ok {
sendAlert(deveui, val)
delete(alertMap, deveui)
// alte Werte loeschen
deleteValues(deveui)
}
} else if s[0:2] == "81" {
err := binary.Read(br, binary.LittleEndian, &pl_129)
if err != nil {
fmt.Println(err)
}
} else if s[0:2] == "82" {
err := binary.Read(br, binary.LittleEndian, &pl_130)
if err != nil {
fmt.Println(err)
if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2, deveui, false)
delete(alertLogMap, deveui)
}
} else {
fmt.Printf("Payload String is unknown: %s\n", s)
}
if s[0:2] == "01" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_1.Version)
fmt.Printf(" vbat: %d,\n", pl_1.Vbat)
fmt.Printf(" offset: %d\n", pl_1.O)
fmt.Printf(" humidity: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.H1, pl_1.H2, pl_1.H3, pl_1.H4, pl_1.H5, pl_1.H6, pl_1.H7, pl_1.H8)
fmt.Printf(" pressure: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.P1, pl_1.P2, pl_1.P3, pl_1.P4, pl_1.P5, pl_1.P6, pl_1.P7, pl_1.P8)
fmt.Printf(" weight: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_1.W1, pl_1.W2, pl_1.W3, pl_1.W4, pl_1.W5, pl_1.W6, pl_1.W7, pl_1.W8)
fmt.Printf(" temp: %d,\n", pl_1.T)
fmt.Printf(" temp_change: [%d,%d,%d,%d,%d,%d,%d],\n", pl_1.TC1, pl_1.TC2, pl_1.TC3, pl_1.TC4, pl_1.TC5, pl_1.TC6, pl_1.TC7)
fmt.Printf("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_1.O)
humidity_values := []uint8{pl_1.H1, pl_1.H2, pl_1.H3, pl_1.H4, pl_1.H5, pl_1.H6, pl_1.H7, pl_1.H8}
valid_measurements := 0
for _, v := range humidity_values {
if v > 0 {
valid_measurements = valid_measurements + 1
}
}
fmt.Printf("Valid Measurements: %d,\n", valid_measurements)
var step int64
if valid_measurements > 1 {
step = int64(int(pl_1.O) / (valid_measurements - 1))
}
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// the second measurement as first..., the same for humidity, which is usually too low...
t := pl_1.T
t = t + int16(pl_1.TC1)
if valid_measurements > 0 {
WriteDatapoint(tfp, deveui, devaddr, pl_1.Vbat, pl_1.H1, pl_1.P1, pl_1.W1, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
//t = t + int16(pl_1.TC1)
if valid_measurements > 1 {
WriteDatapoint(tfp+(step), deveui, devaddr, 0, pl_1.H2, pl_1.P2, pl_1.W2, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC2)
if valid_measurements > 2 {
WriteDatapoint(tfp+(2*step), deveui, devaddr, 0, pl_1.H3, pl_1.P3, pl_1.W3, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC3)
if valid_measurements > 3 {
WriteDatapoint(tfp+(3*step), deveui, devaddr, 0, pl_1.H4, pl_1.P4, pl_1.W4, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC4)
if valid_measurements > 4 {
WriteDatapoint(tfp+(4*step), deveui, devaddr, 0, pl_1.H5, pl_1.P5, pl_1.W5, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC5)
if valid_measurements > 5 {
WriteDatapoint(tfp+(5*step), deveui, devaddr, 0, pl_1.H6, pl_1.P6, pl_1.W6, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC6)
if valid_measurements > 6 {
WriteDatapoint(tfp+(6*step), deveui, devaddr, 0, pl_1.H7, pl_1.P7, pl_1.W7, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_1.TC7)
if valid_measurements > 7 {
WriteDatapoint(tfp+(7*step), deveui, devaddr, 0, pl_1.H8, pl_1.P8, pl_1.W8, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
}
} else if s[0:2] == "02" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_2.Version)
fmt.Printf(" vbat: %d,\n", pl_2.Vbat)
fmt.Printf(" offset: %d\n", pl_2.O)
fmt.Printf(" weight: [%d,%d,%d,%d,%d,%d,%d,%d],\n", pl_2.W1, pl_2.WC1, pl_2.WC2, pl_2.WC3, pl_2.WC4, pl_2.WC5, pl_2.WC6, pl_2.W8)
fmt.Printf(" temp: %d,\n", pl_2.T)
fmt.Printf(" temp_change: [%d,%d,%d,%d,%d,%d,%d],\n", pl_2.TC1, pl_2.TC2, pl_2.TC3, pl_2.TC4, pl_2.TC5, pl_2.TC6, pl_2.TC7)
fmt.Printf(" humidity: %d\n", pl_2.H)
fmt.Printf(" pressure: %d\n", pl_2.P)
fmt.Printf("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_2.O)
temp_change_values := []int8{pl_2.TC1, pl_2.TC2, pl_2.TC3, pl_2.TC4, pl_2.TC5, pl_2.TC6, pl_2.TC7}
valid_measurements := 1
for _, v := range temp_change_values {
if v < 127 {
valid_measurements = valid_measurements + 1
}
}
fmt.Printf("Valid Measurements: %d,\n", valid_measurements)
var step int64
if valid_measurements > 1 {
step = int64(int(pl_2.O) / (valid_measurements - 1))
}
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// the second measurement as first...
t := pl_2.T
w := pl_2.W1
t = t + int16(pl_2.TC1)
if valid_measurements > 0 {
WriteDatapoint(tfp, deveui, devaddr, pl_2.Vbat, pl_2.H, pl_2.P, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
//t = t + int16(pl_2.TC1)
w = w + uint16(pl_2.WC1)
if valid_measurements > 1 {
WriteDatapoint(tfp+(step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC2)
w = w + uint16(pl_2.WC2)
if valid_measurements > 2 {
WriteDatapoint(tfp+(2*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC3)
w = w + uint16(pl_2.WC3)
if valid_measurements > 3 {
WriteDatapoint(tfp+(3*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC4)
w = w + uint16(pl_2.WC4)
if valid_measurements > 4 {
WriteDatapoint(tfp+(4*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC5)
w = w + uint16(pl_2.WC5)
if valid_measurements > 5 {
WriteDatapoint(tfp+(5*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC6)
w = w + uint16(pl_2.WC6)
if valid_measurements > 6 {
WriteDatapoint(tfp+(6*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
t = t + int16(pl_2.TC7)
w = pl_2.W8
if valid_measurements > 7 {
WriteDatapoint(tfp+(7*step), deveui, devaddr, 0, 1, 0, w, 0, 0, t, lrrlat, lrrlon, 0, 0, 0, 0, 0)
}
}
} else if s[0:2] == "80" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_128.Version)
fmt.Printf(" fw_version: %d,\n", pl_128.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_128.Vbat)
fmt.Printf(" humidity: %d\n", pl_128.H)
fmt.Printf(" pressure: %d\n", pl_128.P)
fmt.Printf(" weight1: %d\n", pl_128.W1)
fmt.Printf(" weight2: %d\n", pl_128.W2)
fmt.Printf(" cal_1_0: %d\n", pl_128.W1_0)
fmt.Printf(" cal_2_0: %d\n", pl_128.W2_0)
fmt.Printf(" cal_1_C: %f\n", pl_128.W1_C)
fmt.Printf(" cal_2_C: %f\n", pl_128.W2_C)
fmt.Printf(" temp: %d\n", pl_128.T)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// we calculate the weight...
var w32 int32
var w uint16
w1_0_real := pl_128.W1_0
w2_0_real := pl_128.W2_0
w32 = int32(((float64(pl_128.W1-w1_0_real) / float64(pl_128.W1_C)) + (float64(pl_128.W2-w2_0_real) / float64(pl_128.W2_C))) / 5.0)
if w32 < 0 {
w = 0
} else if w32 > 65535 {
// this is not realistic (>320 kg), we set this to 0 as well...
w = 0
} else {
w = uint16(w32)
}
WriteDatapoint(tfp, deveui, devaddr, pl_128.Vbat, pl_128.H, pl_128.P, w, pl_128.W1, pl_128.W2, pl_128.T, lrrlat, lrrlon, pl_128.Fw_version, pl_128.W1_0, pl_128.W2_0, pl_128.W1_C, pl_128.W2_C)
}
ProcessInitPacket(deveui, pl_128.W1_0, pl_128.W2_0, pl_128.W1_C, pl_128.W2_C, pl_128.W1, pl_128.W2)
} else if s[0:2] == "81" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_129.Version)
fmt.Printf(" fw_version: %d,\n", pl_129.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_129.Vbat)
fmt.Printf(" humidity: %d\n", pl_129.H)
fmt.Printf(" pressure: %d\n", pl_129.P)
fmt.Printf(" weight1: %d\n", pl_129.W1)
fmt.Printf(" weight2: %d\n", pl_129.W2)
fmt.Printf(" weight: %d\n", pl_129.W)
fmt.Printf(" temp: %d\n", pl_129.T)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
WriteDatapoint(tfp, deveui, devaddr, pl_129.Vbat, pl_129.H, pl_129.P, pl_129.W, pl_129.W1, pl_129.W2, pl_129.T, lrrlat, lrrlon, pl_129.Fw_version, 0, 0, 0, 0)
}
} else if s[0:2] == "82" {
fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_130.Version)
fmt.Printf(" fw_version: %d,\n", pl_130.Fw_version)
fmt.Printf(" vbat: %d,\n", pl_130.Vbat)
fmt.Printf(" weight1: %d\n", pl_130.W1)
fmt.Printf(" weight2: %d\n", pl_130.W2)
fmt.Printf(" cal_1_0: %d\n", pl_130.W1_0)
fmt.Printf(" cal_2_0: %d\n", pl_130.W2_0)
fmt.Printf(" cal_1_C: %f\n", pl_130.W1_C)
fmt.Printf(" cal_2_C: %f\n", pl_130.W2_C)
fmt.Printf(" temp: %d\n", pl_130.T)
fmt.Printf(" humidity: %d\n", pl_130.H)
fmt.Printf(" pressure: %d\n", pl_130.P)
fmt.Printf("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// we calculate the weight...
var w32 int32
var w uint16
w1_0_real := pl_130.W1_0
w2_0_real := pl_130.W2_0
w32 = int32(((float64(pl_130.W1-w1_0_real) / float64(pl_130.W1_C)) + (float64(pl_130.W2-w2_0_real) / float64(pl_130.W2_C))) / 5.0)
if w32 < 0 {
w = 0
} else if w32 > 65535 {
// this is not realistic (>320 kg), we set this to 0 as well...
w = 0
} else {
w = uint16(w32)
}
WriteDatapoint(tfp, deveui, devaddr, pl_130.Vbat, pl_130.H, pl_130.P, w, pl_130.W1, pl_130.W2, pl_130.T, lrrlat, lrrlon, pl_130.Fw_version, pl_130.W1_0, pl_130.W2_0, pl_130.W1_C, pl_130.W2_C)
}
}
// Send alert if necessary
if val, ok := alertMap[deveui]; ok {
sendAlert(deveui, val)
delete(alertMap, deveui)
// alte Werte loeschen
deleteValues(deveui)
}
if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2, deveui, false)
delete(alertLogMap, deveui)
fmt.Printf("Payload String is not longer that 2 chars, ignore it\n")
}
}
@ -516,7 +545,7 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
s = fmt.Sprintf("measurement,deveui=%s devaddr=\"%s\",%sh=%di,p=%di,w=%di,t=%.1f,lrrlat=%f,lrrlon=%f%s %d\n", deveui, devaddr, sv, h, int32(p)+825, uint32(w)*5, float32(t)/10, lrrlat, lrrlon, sfw, mytime*60*1000*1000*1000)
}
implausible = (w1 == NOT_PLAUSIBLE_32) || (w2 == NOT_PLAUSIBLE_32) || (w == NOT_PLAUSIBLE_16)
implausible = (w1 == NOT_PLAUSIBLE_32) || (w2 == NOT_PLAUSIBLE_32) || (w == NOT_PLAUSIBLE_16) || (w == 0)
WriteStringToFile(s, deveui, implausible)
@ -530,6 +559,38 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Schwarmalarm ***\n%s\n%s\nGewichtsverlust: %d g", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_loss)
}
// minmax Alert
fmt.Println("debug minmax alert")
minmax := getDevMinmax(deveui)
fmt.Printf("debug getDevMinmax: minmax = %s\n", minmax)
if minmax != "0,0" {
tokens := strings.Split(minmax, ",")
if len(tokens) == 2 {
min, _ := strconv.ParseInt(tokens[0], 10, 32)
max, _ := strconv.ParseInt(tokens[1], 10, 32)
fmt.Printf("debug min: %d, max: %d\n", min, max)
minmaxstatus := GetMinMaxStatus(deveui)
fmt.Printf("debug minmaxstatus: %s\n", minmaxstatus)
if (w_gram < uint32(min)) && (minmaxstatus != "MINALERT") {
fmt.Printf("debug minalert\n")
SetMinMaxStatus(deveui, "MINALERT")
alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"minmaxlarm\",w=%di,w_min=%di %d\n", deveui, w_gram, min, mytime*60*1000*1000*1000)
location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Min/Max-Alarm ***\n%s\n%s\nUnterschreiten des Minimalgewichts: %d g (Minimal: %d g)", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_gram, min)
} else if (w_gram > uint32(max)) && (minmaxstatus != "MAXALERT") {
fmt.Printf("debug maxalert\n")
SetMinMaxStatus(deveui, "MAXALERT")
alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"minmaxlarm\",w=%di,w_max=%di %d\n", deveui, w_gram, max, mytime*60*1000*1000*1000)
location, _ := time.LoadLocation("Europe/Zurich")
alertMap[deveui] = fmt.Sprintf("*** Min/Max-Alarm ***\n%s\n%s\nUeberschreiten des Maximalgewichts: %d g (Maximal: %d g)", getDevAlias(deveui), time.Unix(mytime*60, 0).In(location).Format("02.01.2006 15:04"), w_gram, max)
} else if (w_gram > uint32(min+100)) && (w_gram < uint32(max-100)) {
fmt.Printf("debug resettonormal\n")
SetMinMaxStatus(deveui, "NORMAL")
}
}
}
}
}
}
@ -549,9 +610,9 @@ func WriteStringToFile(s string, deveui string, implausible bool) {
// Also write to individual files
datestr := time.Now().Format("2006-01")
individual_file := "/home/beieli/mini-beieli-lorahandler/logs/" + deveui + "-" + datestr + ".log"
individual_file := "/data/logs/" + deveui + "-" + datestr + ".log"
if implausible {
individual_file = "/home/beieli/mini-beieli-lorahandler/logs/" + "implausible" + "-" + datestr + ".log"
individual_file = "/data/logs/" + "implausible" + "-" + datestr + ".log"
}
fi, err := os.OpenFile(individual_file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"os"
"strconv"
"time"
)
@ -12,6 +13,14 @@ var globalPool *redis.Pool
const lastvaluesPrefix string = "lastvalues:"
const devPrefix string = "dev:"
func getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
type CalSettings struct {
w1_0 int32
w2_0 int32
@ -28,7 +37,7 @@ func newPool() *redis.Pool {
// Dial is an application supplied function for creating and
// configuring a connection.
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", ":6379")
c, err := redis.Dial("tcp", getenv("REDIS_CONNECTION_STRING", ":6379"))
if err != nil {
panic(err.Error())
}
@ -195,6 +204,26 @@ func getSmsnumber(deveui string) string {
return res
}
func getDevMinmax(deveui string) string {
res := "0,0"
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
minmax, err := redis.String(conn.Do("HGET", devPrefix+deveui, "minmax"))
if err == nil {
res = minmax
} else {
fmt.Println(err)
}
return res
}
func GetDownlinkCommand(deveui string) string {
// 0: do nothing...
res := "do_nothing"
@ -217,6 +246,37 @@ func GetDownlinkCommand(deveui string) string {
return res
}
func SetMinMaxStatus(deveui string, status string) error {
conn := globalPool.Get()
defer conn.Close()
// SET object
_, err := conn.Do("HMSET", devPrefix+deveui, "minmaxstatus", status)
if err != nil {
return err
}
return nil
}
func GetMinMaxStatus(deveui string) string {
res := "NORMAL"
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
minmax, err := redis.String(conn.Do("HGET", devPrefix+deveui, "minmaxstatus"))
if err == nil {
res = minmax
}
return res
}
func SetDownlinkCommand(deveui string, new_command string) error {
conn := globalPool.Get()
defer conn.Close()