streamline code

This commit is contained in:
Joerg Lehmann 2019-12-13 12:47:19 +01:00
parent c2c2209b0d
commit 959c930af5
1 changed files with 43 additions and 18 deletions

View File

@ -36,13 +36,17 @@ type Message struct {
type payload_128 struct { type payload_128 struct {
Version uint8 Version uint8
Fw_version int32
Vbat uint8 Vbat uint8
H uint8 H uint8
T int16 T int16
P uint8 P uint8
W1 int32 W1 int32
W2 int32 W2 int32
W uint16 W1_0 int32
W2_0 int32
W1_C float32
W2_C float32
} }
type payload_129 struct { type payload_129 struct {
@ -149,38 +153,54 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
// the second measurement as first..., the same for humidity, which is usually too low... // the second measurement as first..., the same for humidity, which is usually too low...
t := pl_1.T t := pl_1.T
t = t + int16(pl_1.TC1) t = t + int16(pl_1.TC1)
WriteDatapoint(tfp,deveui,devaddr,pl_1.Vbat,pl_1.H2,pl_1.P1,pl_1.W1,0,0,t,lrrlat,lrrlon,0) WriteDatapoint(tfp,deveui,devaddr,pl_1.Vbat,pl_1.H2,pl_1.P1,pl_1.W1,0,0,t,lrrlat,lrrlon,0,0,0,0,0)
//t = t + int16(pl_1.TC1) //t = t + int16(pl_1.TC1)
WriteDatapoint(tfp + (step),deveui,devaddr,0,pl_1.H2,pl_1.P2,pl_1.W2,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC2)
WriteDatapoint(tfp + (2 * step),deveui,devaddr,0,pl_1.H3,pl_1.P3,pl_1.W3,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC3)
WriteDatapoint(tfp + (3 * step),deveui,devaddr,0,pl_1.H4,pl_1.P4,pl_1.W4,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC4)
WriteDatapoint(tfp + (4 * step),deveui,devaddr,0,pl_1.H5,pl_1.P5,pl_1.W5,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC5)
WriteDatapoint(tfp + (5 * step),deveui,devaddr,0,pl_1.H6,pl_1.P6,pl_1.W6,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC6)
WriteDatapoint(tfp + (6 * step),deveui,devaddr,0,pl_1.H7,pl_1.P7,pl_1.W7,0,0,t,lrrlat,lrrlon,0) 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) t = t + int16(pl_1.TC7)
WriteDatapoint(tfp + (7 * step),deveui,devaddr,0,pl_1.H8,pl_1.P8,pl_1.W8,0,0,t,lrrlat,lrrlon,0) 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] == "80") { } else if (s[0:2] == "80") {
fmt.Printf("{\n") fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_128.Version) 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(" vbat: %d,\n", pl_128.Vbat)
fmt.Printf(" humidity: %d\n",pl_128.H) fmt.Printf(" humidity: %d\n",pl_128.H)
fmt.Printf(" pressure: %d\n",pl_128.P) fmt.Printf(" pressure: %d\n",pl_128.P)
fmt.Printf(" weight1: %d\n",pl_128.W1) fmt.Printf(" weight1: %d\n",pl_128.W1)
fmt.Printf(" weight2: %d\n",pl_128.W2) fmt.Printf(" weight2: %d\n",pl_128.W2)
fmt.Printf(" weight: %d\n",pl_128.W) 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(" temp: %d\n",pl_128.T)
fmt.Printf("}\n") fmt.Printf("}\n")
if write2file { if write2file {
// Time of Packet received // Time of Packet received
var tfp = (time.Now().Unix() / 60) var tfp = (time.Now().Unix() / 60)
WriteDatapoint(tfp,deveui,devaddr,pl_128.Vbat,pl_128.H,pl_128.P,pl_128.W,pl_128.W1,pl_128.W2,pl_128.T,lrrlat,lrrlon,0) // we calculate the weight...
var w32 int32
var w uint16
w32 = int32(((float64(pl_128.W1 - pl_128.W1_0) / float64(pl_128.W1_C)) + (float64(pl_128.W2 - pl_128.W2_0) / float64(pl_128.W2_C))) / 5.0)
if (w32 < 0) {
w = 0
} else if (w32 > 65535) {
w = 65535
} 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)
} }
} else if (s[0:2] == "81") { } else if (s[0:2] == "81") {
fmt.Printf("{\n") fmt.Printf("{\n")
@ -198,7 +218,7 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
// Time of Packet received // Time of Packet received
var tfp = (time.Now().Unix() / 60) 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) 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)
} }
} }
// Send alert if necessary // Send alert if necessary
@ -214,12 +234,12 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
} }
} }
func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint8, p uint8, w uint16, w1 int32, w2 int32, t int16, lrrlat float32, lrrlon float32, fw_version int32) { func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint8, p uint8, w uint16, w1 int32, w2 int32, t int16, lrrlat float32, lrrlon float32, fw_version int32, w1_0 int32, w2_0 int32, w1_c float32, w2_c float32) {
// wir nehmen humidity als Referenz, wenn diese > 0 ist, dann ist es // wir nehmen humidity als Referenz, wenn diese > 0 ist, dann ist es
// eine gueltige Messung // eine gueltige Messung
var vp int16 // Voltage in % var vp int16 // Voltage in %
if (h > 0) { if (h > 0) {
vp = int16(v) - 70 vp = int16(v) - 70
if vp < 0 { if vp < 0 {
vp = 0 vp = 0
} else if vp > 100 { } else if vp > 100 {
@ -227,11 +247,16 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
} }
s := "" s := ""
sv := ""
if (v > 0) { if (v > 0) {
s = fmt.Sprintf("measurement,deveui=%s devaddr=\"%s\",v=%di,vp=%di,h=%di,p=%di,w=%di,w1=%di,w2=%di,t=%.1f,lrrlat=%f,lrrlon=%f,fw_version=%di %d\n",deveui,devaddr,int32(v)*7+2510,vp,h,int32(p)+825,w*5,w1,w2,float32(t)/10,lrrlat,lrrlon,fw_version,mytime*60*1000*1000*1000) sv = fmt.Sprintf(",v=%di,vp=%di,",int32(v)*7+2510,vp)
} else {
s = fmt.Sprintf("measurement,deveui=%s devaddr=\"%s\",h=%di,p=%di,w=%di,w1=%di,w2=%di,t=%.1f,lrrlat=%f,lrrlon=%f,fw_version=%di %d\n",deveui,devaddr,h,int32(p)+825,w*5,w1,w2,float32(t)/10,lrrlat,lrrlon,fw_version,mytime*60*1000*1000*1000)
} }
sfw := ""
if (fw_version > 0) {
sfw = fmt.Sprintf(",fw_version=%di,w1=%di,w2=%di,w1_0=%d,w2_0=%d,w1_c=%f,w2_c=%f",fw_version,w1,w2,w1_0,w2_0,w1_c,w2_c)
}
s = fmt.Sprintf("measurement,deveui=%s devaddr=\"%s\"%s,h=%di,p=%di,w=%di,t=%.1f,lrrlat=%f,lrrlon=%f%s %d\n",deveui,devaddr,sv,h,int32(p)+825,w*5,float32(t)/10,lrrlat,lrrlon,sfw,mytime*60*1000*1000*1000)
WriteStringToFile(s) WriteStringToFile(s)