plausibility checks implemented

This commit is contained in:
Joerg Lehmann 2020-07-01 18:15:17 +02:00
parent 4eb249377f
commit 2083a5b3d6
1 changed files with 60 additions and 61 deletions

View File

@ -2,10 +2,10 @@ package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
@ -20,7 +20,8 @@ const (
username = "beieli"
password = "beieli4president"
outputfile = "/home/beieli/lorahandler/lorahandler.log"
NOT_ATTACHED = -2147483648
NOT_PLAUSIBLE_16 = 65535
NOT_PLAUSIBLE_32 = 2147483647
)
type MetadataTTN struct {
@ -259,19 +260,7 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
var w uint16
w1_0_real := pl_128.W1_0
w2_0_real := pl_128.W2_0
multiply_by_2 := false
if pl_128.W1_0 == NOT_ATTACHED {
w1_0_real = pl_128.W1
multiply_by_2 = true
}
if pl_128.W2_0 == NOT_ATTACHED {
w2_0_real = pl_128.W2
multiply_by_2 = true
}
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 multiply_by_2 {
w32 = w32 * 2
}
if w32 < 0 {
w = 0
} else if w32 > 65535 {
@ -325,6 +314,7 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
// wir nehmen humidity als Referenz, wenn diese > 0 ist, dann ist es
// eine gueltige Messung
var vp int16 // Voltage in %
var implausible bool
//fmt.Printf("WriteDatapoint: h: %d\n", h)
if h > 0 {
vp = int16(v) - 70
@ -346,7 +336,9 @@ 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)
WriteStringToFile(s, deveui)
implausible = (w1 == NOT_PLAUSIBLE_32) || (w2 == NOT_PLAUSIBLE_32) || (w == NOT_PLAUSIBLE_16)
WriteStringToFile(s, deveui, implausible)
w_gram := uint32(w) * 5
addValue(deveui, w_gram)
@ -360,7 +352,8 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
}
}
func WriteStringToFile(s string, deveui string) {
func WriteStringToFile(s string, deveui string, bool implausible) {
if !implausible {
n, err := file.WriteString(s)
if err != nil {
fmt.Println(err)
@ -370,10 +363,16 @@ func WriteStringToFile(s string, deveui string) {
fmt.Println("failed to write data")
os.Exit(1)
}
}
// Also write to individual files
datestr := time.Now().Format("2006-01")
if implausible {
individual_file := "/home/beieli/lorahandler/logs/" + deveui + "-" + datestr + ".log"
} else {
individual_file := "/home/beieli/lorahandler/logs/" + "implausible" + "-" + datestr + ".log"
}
fi, err := os.OpenFile(individual_file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
@ -434,7 +433,7 @@ func main() {
fmt.Printf("Length of JSON Body: %d\n", len(buf))
if strings.Contains(string(buf),"mini-beieli") {
if strings.Contains(string(buf), "mini-beieli") {
fmt.Printf("Seems to be a TTN Packet...\n")
// it is probably a TTN package...
err := json.NewDecoder(r.Body).Decode(&message_ttn)