This commit is contained in:
Joerg Lehmann 2020-02-29 13:57:33 +01:00
parent 46de03d671
commit f134736334
4 changed files with 534 additions and 522 deletions

View File

@ -2,12 +2,12 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"io/ioutil" "io/ioutil"
"log" "log"
"time"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"time"
) )
func sendSMS(phonenumber string, alertMessage string) { func sendSMS(phonenumber string, alertMessage string) {
@ -41,7 +41,7 @@ func sendAlert(deveui string, alertMessage string) {
smsnumber := getSmsnumber(deveui) smsnumber := getSmsnumber(deveui)
alarmactive := getDevAlarmactive(deveui) alarmactive := getDevAlarmactive(deveui)
fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber, alarmactive) fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber, alarmactive)
if ((smsnumber != "") && (alarmactive == "1")) { if (smsnumber != "") && (alarmactive == "1") {
// we strip of the leading + // we strip of the leading +
smsnumber = strings.Replace(smsnumber, "+", "", -1) smsnumber = strings.Replace(smsnumber, "+", "", -1)
sendSMS(smsnumber, alertMessage) sendSMS(smsnumber, alertMessage)

View File

@ -2,13 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"log"
"time"
"math"
"strings"
"strconv"
"io/ioutil" "io/ioutil"
"log"
"math"
"net/http" "net/http"
"strconv"
"strings"
"time"
) )
const api_url = "https://proxy1.lpn.swisscom.ch/thingpark/lrc/rest" const api_url = "https://proxy1.lpn.swisscom.ch/thingpark/lrc/rest"
@ -58,7 +58,7 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
var calibration_weight_gram int32 var calibration_weight_gram int32
var calibration_weight2_gram int32 var calibration_weight2_gram int32
tokens := strings.Split(downlink_command, " ") tokens := strings.Split(downlink_command, " ")
if (len(tokens) < 2) { if len(tokens) < 2 {
// no value in gram included! // no value in gram included!
fmt.Printf("Error: invalid downlink_command: %s, examples: \"tare_a 10000\", \"tare_b 10000\", \"tare 10000 10000\" devuid: %s\n", downlink_command, deveui) fmt.Printf("Error: invalid downlink_command: %s, examples: \"tare_a 10000\", \"tare_b 10000\", \"tare 10000 10000\" devuid: %s\n", downlink_command, deveui)
return return
@ -71,7 +71,7 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
return return
} }
if (len(tokens) > 2) { if len(tokens) > 2 {
n, err := strconv.ParseInt(tokens[2], 10, 32) n, err := strconv.ParseInt(tokens[2], 10, 32)
if err == nil { if err == nil {
calibration_weight2_gram = int32(n) calibration_weight2_gram = int32(n)
@ -81,19 +81,19 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
} }
} }
if (tokens[0] == "tare_a") { if tokens[0] == "tare_a" {
new_w1_c := float32(w1-cur_cal_settings.w1_0) / float32(calibration_weight_gram) new_w1_c := float32(w1-cur_cal_settings.w1_0) / float32(calibration_weight_gram)
valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(new_w1_c), math.Float32bits(cur_cal_settings.w2_c)) valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(new_w1_c), math.Float32bits(cur_cal_settings.w2_c))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr) s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s) MakePost(s)
SetDownlinkCommand(deveui, "do_nothing") SetDownlinkCommand(deveui, "do_nothing")
} else if (tokens[0] == "tare_b") { } else if tokens[0] == "tare_b" {
new_w2_c := float32(w2-cur_cal_settings.w2_0) / float32(calibration_weight_gram) new_w2_c := float32(w2-cur_cal_settings.w2_0) / float32(calibration_weight_gram)
valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(cur_cal_settings.w1_c), math.Float32bits(new_w2_c)) valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(cur_cal_settings.w1_c), math.Float32bits(new_w2_c))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr) s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s) MakePost(s)
SetDownlinkCommand(deveui, "do_nothing") SetDownlinkCommand(deveui, "do_nothing")
} else if (tokens[0] == "tare") { } else if tokens[0] == "tare" {
new_w1_c := float32(w1-cur_cal_settings.w1_0) / float32(calibration_weight_gram) new_w1_c := float32(w1-cur_cal_settings.w1_0) / float32(calibration_weight_gram)
new_w2_c := float32(w2-cur_cal_settings.w2_0) / float32(calibration_weight2_gram) new_w2_c := float32(w2-cur_cal_settings.w2_0) / float32(calibration_weight2_gram)
valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(new_w1_c), math.Float32bits(new_w2_c)) valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(cur_cal_settings.w1_0), uint32(cur_cal_settings.w2_0), math.Float32bits(new_w1_c), math.Float32bits(new_w2_c))
@ -117,10 +117,10 @@ func UpdateCalibrationSettingsFromNode(deveui string, w1_0 int32, w2_0 int32, w1
new_cal_settings.w1_c = w1_c new_cal_settings.w1_c = w1_c
new_cal_settings.w2_c = w2_c new_cal_settings.w2_c = w2_c
if ((new_cal_settings.w1_0 != cur_cal_settings.w1_0) || if (new_cal_settings.w1_0 != cur_cal_settings.w1_0) ||
(new_cal_settings.w2_0 != cur_cal_settings.w2_0) || (new_cal_settings.w2_0 != cur_cal_settings.w2_0) ||
(new_cal_settings.w1_c != cur_cal_settings.w1_c) || (new_cal_settings.w1_c != cur_cal_settings.w1_c) ||
(new_cal_settings.w2_c != cur_cal_settings.w2_c)) { (new_cal_settings.w2_c != cur_cal_settings.w2_c) {
SetCurrentCalibrationSettings(deveui, new_cal_settings) SetCurrentCalibrationSettings(deveui, new_cal_settings)
// since something has changed and is reflected in the local config, we don't do anything any more... // since something has changed and is reflected in the local config, we don't do anything any more...
SetDownlinkCommand(deveui, "do_nothing") SetDownlinkCommand(deveui, "do_nothing")

View File

@ -2,15 +2,15 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json"
"encoding/hex"
"encoding/binary" "encoding/binary"
"encoding/hex"
"encoding/json"
"fmt" "fmt"
"os"
"io/ioutil" "io/ioutil"
"log" "log"
"strings"
"net/http" "net/http"
"os"
"strings"
"time" "time"
) )
@ -19,9 +19,9 @@ const (
username = "beieli" username = "beieli"
password = "beieli4president" password = "beieli4president"
outputfile = "/home/beieli/lorahandler/lorahandler.log" outputfile = "/home/beieli/lorahandler/lorahandler.log"
NOT_ATTACHED = -2147483648
) )
type MessageProperties struct { type MessageProperties struct {
Time string `json:"Time"` Time string `json:"Time"`
Payload_hex string `json:"payload_hex"` Payload_hex string `json:"payload_hex"`
@ -111,10 +111,10 @@ func ProcessInitPacket(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c
fmt.Printf("Processing Init Packet for Deveui %s\n", deveui) fmt.Printf("Processing Init Packet for Deveui %s\n", deveui)
downlink_command = GetDownlinkCommand(deveui) downlink_command = GetDownlinkCommand(deveui)
if (downlink_command == "do_nothing") { if downlink_command == "do_nothing" {
// do nothing // do nothing
fmt.Printf("Init Packet, downlink_command set to do_nothing (or not set at all), nothing to do... Deveui %s\n", deveui) fmt.Printf("Init Packet, downlink_command set to do_nothing (or not set at all), nothing to do... Deveui %s\n", deveui)
} else if (downlink_command == "tare_0") { } else if downlink_command == "tare_0" {
// reset node to 0 // reset node to 0
ResetNodeToZero(deveui, w1, w2) ResetNodeToZero(deveui, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_a ") { } else if strings.HasPrefix(downlink_command, "tare_a ") {
@ -143,17 +143,17 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
pl_128 = payload_128{} pl_128 = payload_128{}
pl_129 = payload_129{} pl_129 = payload_129{}
br := bytes.NewReader(ba) br := bytes.NewReader(ba)
if (s[0:2] == "01") { if s[0:2] == "01" {
err := binary.Read(br, binary.LittleEndian, &pl_1) err := binary.Read(br, binary.LittleEndian, &pl_1)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
} else if (s[0:2] == "80") { } else if s[0:2] == "80" {
err := binary.Read(br, binary.LittleEndian, &pl_128) err := binary.Read(br, binary.LittleEndian, &pl_128)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
} else if (s[0:2] == "81") { } else if s[0:2] == "81" {
err := binary.Read(br, binary.LittleEndian, &pl_129) err := binary.Read(br, binary.LittleEndian, &pl_129)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -161,7 +161,7 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
} else { } else {
fmt.Printf("Payload String is unknown: %s\n", s) fmt.Printf("Payload String is unknown: %s\n", s)
} }
if (s[0:2] == "01") { if s[0:2] == "01" {
fmt.Printf("{\n") fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_1.Version) fmt.Printf(" version: %d,\n", pl_1.Version)
fmt.Printf(" vbat: %d,\n", pl_1.Vbat) fmt.Printf(" vbat: %d,\n", pl_1.Vbat)
@ -197,7 +197,7 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
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, 0, 0, 0, 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(" fw_version: %d,\n", pl_128.Fw_version)
@ -219,10 +219,24 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
// we calculate the weight... // we calculate the weight...
var w32 int32 var w32 int32
var w uint16 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) w1_0_real := pl_128.W1_0
if (w32 < 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 w = 0
} else if (w32 > 65535) { } else if w32 > 65535 {
// this is not realistic (>320 kg), we set this to 0 as well... // this is not realistic (>320 kg), we set this to 0 as well...
w = 0 w = 0
} else { } else {
@ -232,7 +246,7 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
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) 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) 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") { } else if s[0:2] == "81" {
fmt.Printf("{\n") fmt.Printf("{\n")
fmt.Printf(" version: %d,\n", pl_129.Version) fmt.Printf(" version: %d,\n", pl_129.Version)
fmt.Printf(" fw_version: %d,\n", pl_129.Fw_version) fmt.Printf(" fw_version: %d,\n", pl_129.Fw_version)
@ -268,7 +282,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 // 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
@ -279,11 +293,11 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
s := "" s := ""
sv := "" sv := ""
if (v > 0) { if v > 0 {
sv = fmt.Sprintf("v=%di,vp=%di,", int32(v)*7+2510, vp) sv = fmt.Sprintf("v=%di,vp=%di,", int32(v)*7+2510, vp)
} }
sfw := "" sfw := ""
if (fw_version > 0) { 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) 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)
} }
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) 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)
@ -292,8 +306,8 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
w_gram := uint32(w) * 5 w_gram := uint32(w) * 5
addValue(deveui, w_gram) addValue(deveui, w_gram)
w_loss := getMaxValue(deveui) - w_gram; w_loss := getMaxValue(deveui) - w_gram
if (w_loss > 500) { if w_loss > 500 {
// Schwarmalarm! // Schwarmalarm!
alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"swarmalarm\",w=%di,w_loss=%di %d\n", deveui, w_gram, w_loss, mytime*60*1000*1000*1000) alertLogMap[deveui] = fmt.Sprintf("alert,deveui=%s reason=\"swarmalarm\",w=%di,w_loss=%di %d\n", deveui, w_gram, w_loss, mytime*60*1000*1000*1000)
location, _ := time.LoadLocation("Europe/Zurich") location, _ := time.LoadLocation("Europe/Zurich")

View File

@ -2,9 +2,9 @@ package main
import ( import (
"fmt" "fmt"
"time"
"strconv"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"strconv"
"time"
) )
var globalPool *redis.Pool var globalPool *redis.Pool
@ -63,7 +63,7 @@ func initDB() {
// wir machen einen Connection Test // wir machen einen Connection Test
ping(conn) ping(conn)
addValue("0000000000000000",uint32(time.Now().Unix())); addValue("0000000000000000", uint32(time.Now().Unix()))
} }
func closeDB() { func closeDB() {
@ -75,7 +75,7 @@ func checkDevAvailable(deveui string) bool {
defer conn.Close() defer conn.Close()
_, err := redis.String(conn.Do("GET", lastvaluesPrefix+deveui)) _, err := redis.String(conn.Do("GET", lastvaluesPrefix+deveui))
if (err == redis.ErrNil) { if err == redis.ErrNil {
return true return true
} else if err != nil { } else if err != nil {
return false return false
@ -97,7 +97,7 @@ func getMaxValue(deveui string) uint32 {
} }
for _, value := range myvalues { for _, value := range myvalues {
if (uint32(value) > res) { if uint32(value) > res {
res = uint32(value) res = uint32(value)
} }
} }
@ -147,7 +147,6 @@ func getDevAlias(deveui string) string {
return res return res
} }
conn := globalPool.Get() conn := globalPool.Get()
defer conn.Close() defer conn.Close()
@ -298,4 +297,3 @@ func SetCurrentCalibrationSettings(deveui string, cal_settings CalSettings) erro
return nil return nil
} }