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,16 +2,16 @@ package main
import (
"fmt"
"strings"
"io/ioutil"
"log"
"time"
"net/http"
"net/url"
"strings"
"time"
)
func sendSMS(phonenumber string, alertMessage string) {
myurl := fmt.Sprintf("https://api.smsapi.com/sms.do?to=%s&message=%s&format=json",phonenumber,url.QueryEscape(alertMessage))
myurl := fmt.Sprintf("https://api.smsapi.com/sms.do?to=%s&message=%s&format=json", phonenumber, url.QueryEscape(alertMessage))
req, err := http.NewRequest("GET", myurl, nil)
if err != nil {
@ -40,10 +40,10 @@ func sendAlert(deveui string, alertMessage string) {
fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage)
smsnumber := getSmsnumber(deveui)
alarmactive := getDevAlarmactive(deveui)
fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber,alarmactive)
if ((smsnumber != "") && (alarmactive == "1")) {
fmt.Printf("sendAlert: deveui=%s, smsnumber=%s, alarmactive=%s\n", deveui, smsnumber, alarmactive)
if (smsnumber != "") && (alarmactive == "1") {
// we strip of the leading +
smsnumber = strings.Replace(smsnumber, "+", "", -1)
sendSMS(smsnumber,alertMessage)
sendSMS(smsnumber, alertMessage)
}
}

View File

@ -2,16 +2,16 @@ package main
import (
"fmt"
"log"
"time"
"math"
"strings"
"strconv"
"io/ioutil"
"log"
"math"
"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"
func MakePost(url string) {
req, err := http.NewRequest("POST", url, nil)
@ -45,10 +45,10 @@ func MakePost(url string) {
func ResetNodeToZero(deveui string, w1_0 int32, w2_0 int32) {
fmt.Printf("Called ResetNodeToZero for deveui %s\n", deveui)
valstr := fmt.Sprintf("%08X%08X%08X%08X",uint32(w1_0),uint32(w2_0),0,0)
valstr := fmt.Sprintf("%08X%08X%08X%08X", uint32(w1_0), uint32(w2_0), 0, 0)
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=00%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
SetDownlinkCommand(deveui, "do_nothing")
}
func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32) {
@ -57,8 +57,8 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
cur_cal_settings = GetCurrentCalibrationSettings(deveui)
var calibration_weight_gram int32
var calibration_weight2_gram int32
tokens := strings.Split(downlink_command," ")
if (len(tokens) < 2) {
tokens := strings.Split(downlink_command, " ")
if len(tokens) < 2 {
// 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)
return
@ -71,7 +71,7 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
return
}
if (len(tokens) > 2) {
if len(tokens) > 2 {
n, err := strconv.ParseInt(tokens[2], 10, 32)
if err == nil {
calibration_weight2_gram = int32(n)
@ -81,25 +81,25 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32)
}
}
if (tokens[0] == "tare_a") {
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))
if tokens[0] == "tare_a" {
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))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
} else if (tokens[0] == "tare_b") {
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))
SetDownlinkCommand(deveui, "do_nothing")
} else if tokens[0] == "tare_b" {
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))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
} else if (tokens[0] == "tare") {
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)
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))
SetDownlinkCommand(deveui, "do_nothing")
} else if tokens[0] == "tare" {
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)
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))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
SetDownlinkCommand(deveui, "do_nothing")
} else {
fmt.Printf("Error: either use tare_a or tare_b or tare; deveui %s\n", deveui)
}
@ -117,13 +117,13 @@ func UpdateCalibrationSettingsFromNode(deveui string, w1_0 int32, w2_0 int32, w1
new_cal_settings.w1_c = w1_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.w1_c != cur_cal_settings.w1_c) ||
(new_cal_settings.w2_c != cur_cal_settings.w2_c)) {
SetCurrentCalibrationSettings(deveui,new_cal_settings)
(new_cal_settings.w2_c != cur_cal_settings.w2_c) {
SetCurrentCalibrationSettings(deveui, new_cal_settings)
// 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")
} else {
fmt.Printf("Info: no change in calibration settings for deveui %s\n", deveui)
}

View File

@ -2,15 +2,15 @@ package main
import (
"bytes"
"encoding/json"
"encoding/hex"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"io/ioutil"
"log"
"strings"
"net/http"
"os"
"strings"
"time"
)
@ -19,9 +19,9 @@ const (
username = "beieli"
password = "beieli4president"
outputfile = "/home/beieli/lorahandler/lorahandler.log"
NOT_ATTACHED = -2147483648
)
type MessageProperties struct {
Time string `json:"Time"`
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)
downlink_command = GetDownlinkCommand(deveui)
if (downlink_command == "do_nothing") {
if downlink_command == "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)
} else if (downlink_command == "tare_0") {
} else if downlink_command == "tare_0" {
// reset node to 0
ResetNodeToZero(deveui, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_a ") {
@ -143,34 +143,34 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
pl_128 = payload_128{}
pl_129 = payload_129{}
br := bytes.NewReader(ba)
if (s[0:2] == "01") {
if s[0:2] == "01" {
err := binary.Read(br, binary.LittleEndian, &pl_1)
if err != nil {
fmt.Println(err)
}
} else if (s[0:2] == "80") {
} 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") {
} else if s[0:2] == "81" {
err := binary.Read(br, binary.LittleEndian, &pl_129)
if err != nil {
fmt.Println(err)
}
} 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(" 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(" 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
@ -181,36 +181,36 @@ 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...
t := pl_1.T
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,0,0,0,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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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(" 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(" 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
@ -219,48 +219,62 @@ func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrl
// 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) {
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) {
} 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)
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") {
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(" 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)
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
if val, ok := alertMap[deveui]; ok {
sendAlert(deveui,val)
delete(alertMap,deveui)
sendAlert(deveui, val)
delete(alertMap, deveui)
// alte Werte loeschen
deleteValues(deveui)
}
if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2)
delete(alertLogMap,deveui)
delete(alertLogMap, deveui)
}
}
@ -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
// eine gueltige Messung
var vp int16 // Voltage in %
if (h > 0) {
if h > 0 {
vp = int16(v) - 70
if vp < 0 {
vp = 0
@ -279,25 +293,25 @@ func WriteDatapoint(mytime int64, deveui string, devaddr string, v uint8, h uint
s := ""
sv := ""
if (v > 0) {
sv = fmt.Sprintf("v=%di,vp=%di,",int32(v)*7+2510,vp)
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)
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)
}
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)
WriteStringToFile(s)
w_gram := uint32(w)*5
addValue(deveui,w_gram)
w_loss := getMaxValue(deveui) - w_gram;
if (w_loss > 500) {
w_gram := uint32(w) * 5
addValue(deveui, w_gram)
w_loss := getMaxValue(deveui) - w_gram
if w_loss > 500 {
// 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")
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)
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)
}
}
}
@ -323,7 +337,7 @@ func main() {
alertLogMap = make(map[string]string)
// Open Output File
f, err := os.OpenFile(outputfile,os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
f, err := os.OpenFile(outputfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
file = f
if err != nil {
fmt.Println(err)
@ -359,8 +373,8 @@ func main() {
fmt.Println("Payload Hex: " + u.Prop.Payload_hex)
fmt.Println("DevEUI: " + u.Prop.DevEUI)
fmt.Println("DevAddr: " + u.Prop.DevAddr)
fmt.Printf("LrrLAT: %f\n",u.Prop.LrrLAT)
fmt.Printf("LrrLON: %f\n",u.Prop.LrrLON)
fmt.Printf("LrrLAT: %f\n", u.Prop.LrrLAT)
fmt.Printf("LrrLON: %f\n", u.Prop.LrrLON)
DecodePayload(u.Prop.Payload_hex, u.Prop.DevEUI, u.Prop.DevAddr, u.Prop.LrrLAT, u.Prop.LrrLON, true)
})
log.Fatal(http.ListenAndServe(":8080", nil))

View File

@ -2,9 +2,9 @@ package main
import (
"fmt"
"time"
"strconv"
"github.com/gomodule/redigo/redis"
"strconv"
"time"
)
var globalPool *redis.Pool
@ -63,7 +63,7 @@ func initDB() {
// wir machen einen Connection Test
ping(conn)
addValue("0000000000000000",uint32(time.Now().Unix()));
addValue("0000000000000000", uint32(time.Now().Unix()))
}
func closeDB() {
@ -74,8 +74,8 @@ func checkDevAvailable(deveui string) bool {
conn := globalPool.Get()
defer conn.Close()
_, err := redis.String(conn.Do("GET", lastvaluesPrefix + deveui))
if (err == redis.ErrNil) {
_, err := redis.String(conn.Do("GET", lastvaluesPrefix+deveui))
if err == redis.ErrNil {
return true
} else if err != nil {
return false
@ -89,7 +89,7 @@ func getMaxValue(deveui string) uint32 {
conn := globalPool.Get()
defer conn.Close()
values, _ := redis.Values(conn.Do("LRANGE", lastvaluesPrefix + deveui, 0, -1))
values, _ := redis.Values(conn.Do("LRANGE", lastvaluesPrefix+deveui, 0, -1))
if err := redis.ScanSlice(values, &myvalues); err != nil {
fmt.Println(err)
@ -97,7 +97,7 @@ func getMaxValue(deveui string) uint32 {
}
for _, value := range myvalues {
if (uint32(value) > res) {
if uint32(value) > res {
res = uint32(value)
}
}
@ -109,20 +109,20 @@ func addValue(deveui string, value uint32) {
conn := globalPool.Get()
defer conn.Close()
_, err := conn.Do("LPUSH", lastvaluesPrefix + deveui, value)
_, err := conn.Do("LPUSH", lastvaluesPrefix+deveui, value)
if err != nil {
return
}
_, err2 := conn.Do("LTRIM", lastvaluesPrefix + deveui, 0, 4)
_, err2 := conn.Do("LTRIM", lastvaluesPrefix+deveui, 0, 4)
if err2 != nil {
return
}
// we set an expiration time of one hour
_, err3 := conn.Do("EXPIRE", lastvaluesPrefix + deveui, 3600)
_, err3 := conn.Do("EXPIRE", lastvaluesPrefix+deveui, 3600)
if err3 != nil {
return
@ -133,7 +133,7 @@ func deleteValues(deveui string) {
conn := globalPool.Get()
defer conn.Close()
_, err := conn.Do("LTRIM", lastvaluesPrefix + deveui, 1, 0)
_, err := conn.Do("LTRIM", lastvaluesPrefix+deveui, 1, 0)
if err != nil {
return
@ -147,7 +147,6 @@ func getDevAlias(deveui string) string {
return res
}
conn := globalPool.Get()
defer conn.Close()
@ -283,19 +282,18 @@ func SetCurrentCalibrationSettings(deveui string, cal_settings CalSettings) erro
conn := globalPool.Get()
defer conn.Close()
w1_0 := fmt.Sprintf("%d",cal_settings.w1_0)
w2_0 := fmt.Sprintf("%d",cal_settings.w2_0)
w1_c := fmt.Sprintf("%f",cal_settings.w1_c)
w2_c := fmt.Sprintf("%f",cal_settings.w2_c)
w1_0 := fmt.Sprintf("%d", cal_settings.w1_0)
w2_0 := fmt.Sprintf("%d", cal_settings.w2_0)
w1_c := fmt.Sprintf("%f", cal_settings.w1_c)
w2_c := fmt.Sprintf("%f", cal_settings.w2_c)
fmt.Printf("DEBUG SetCurrentCalibrationSettings: w1_0: %s, w2_0: %s, w1_c: %s, w2_c: %s\n",w1_0, w2_0, w1_c, w2_c)
fmt.Printf("DEBUG SetCurrentCalibrationSettings: w1_0: %s, w2_0: %s, w1_c: %s, w2_c: %s\n", w1_0, w2_0, w1_c, w2_c)
// SET object
_, err := conn.Do("HMSET", devPrefix+deveui, "w1_0",w1_0,"w2_0",w2_0,"w1_c",w1_c,"w2_c",w2_c)
_, err := conn.Do("HMSET", devPrefix+deveui, "w1_0", w1_0, "w2_0", w2_0, "w1_c", w1_c, "w2_c", w2_c)
if err != nil {
return err
}
return nil
}