From cba781b6264c2af51950feef8003fdaf5b8ae348 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Sat, 10 Oct 2020 10:21:06 +0200 Subject: [PATCH] new packet formats (CubeCell Version) --- lorahandler.go | 189 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 179 insertions(+), 10 deletions(-) diff --git a/lorahandler.go b/lorahandler.go index cbcccd6..3793ea5 100644 --- a/lorahandler.go +++ b/lorahandler.go @@ -74,6 +74,19 @@ type payload_129 struct { W uint16 } +type payload_130 struct { + Version uint8 + Fw_version int32 + Vbat uint8 + T int16 + W1 int32 + W2 int32 + W1_0 int32 + W2_0 int32 + W1_C float32 + W2_C float32 +} + type payload_1 struct { Version uint8 Vbat uint8 @@ -112,6 +125,29 @@ type payload_1 struct { O uint8 } +type payload_2 struct { + Version uint8 + Vbat uint8 + T int16 + TC1 int8 + TC2 int8 + TC3 int8 + TC4 int8 + TC5 int8 + TC6 int8 + TC7 int8 + W1 uint16 + WC1 int8 + WC2 int8 + WC3 int8 + WC4 int8 + WC5 int8 + WC6 int8 + WC7 int8 + W8 uint16 + O uint8 +} + // Global variables var file *os.File @@ -148,18 +184,28 @@ func ProcessInitPacket(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrlon float32, write2file bool) { var ba []byte var pl_1 payload_1 + var pl_2 payload_2 var pl_128 payload_128 var pl_129 payload_129 + var pl_130 payload_130 ba, _ = hex.DecodeString(s) pl_1 = payload_1{} + pl_2 = payload_2{} pl_128 = payload_128{} pl_129 = payload_129{} + 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) } + } 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 { @@ -170,6 +216,11 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl 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) } @@ -236,6 +287,75 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl 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("}\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, 1, 0, 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) @@ -291,6 +411,40 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl 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("}\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, 1, 0, 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 { @@ -317,19 +471,34 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint var implausible bool //fmt.Printf("WriteDatapoint: h: %d\n", h) if h > 0 { - vp = int16(v) - 70 - if vp < 0 { - vp = 0 - } else if vp > 100 { - vp = 100 + sv := "" + if h == 1 { + // vp for supercapacitor (180 is 0%, 225 is 100%) + vp = int16((float32(v) - 180) / 45.0 * 100.0) + if vp < 0 { + vp = 0 + } else if vp > 100 { + vp = 100 + } + + if v > 0 { + sv = fmt.Sprintf("v=%di,vp=%di,", int32(v)*20, vp) + } + } else { + // vp for Lipo + vp = int16(v) - 70 + if vp < 0 { + vp = 0 + } else if vp > 100 { + vp = 100 + } + + if v > 0 { + sv = fmt.Sprintf("v=%di,vp=%di,", int32(v)*7+2510, vp) + } } s := "" - - sv := "" - if v > 0 { - sv = fmt.Sprintf("v=%di,vp=%di,", int32(v)*7+2510, vp) - } sfw := "" if fw_version > 0 { sfw = fmt.Sprintf(",fw_version=%di,w1=%di,w2=%di,w1_0=%di,w2_0=%di,w1_c=%f,w2_c=%f", fw_version, w1, w2, w1_0, w2_0, w1_c, w2_c)