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

@ -1,17 +1,17 @@
package main
import (
"fmt"
"strings"
"io/ioutil"
"log"
"time"
"net/http"
"net/url"
"fmt"
"io/ioutil"
"log"
"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 {
@ -37,13 +37,13 @@ func sendSMS(phonenumber string, alertMessage string) {
}
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")) {
// we strip of the leading +
smsnumber = strings.Replace(smsnumber, "+", "", -1)
sendSMS(smsnumber,alertMessage)
}
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") {
// we strip of the leading +
smsnumber = strings.Replace(smsnumber, "+", "", -1)
sendSMS(smsnumber, alertMessage)
}
}

View File

@ -1,17 +1,17 @@
package main
import (
"fmt"
"log"
"time"
"math"
"strings"
"strconv"
"io/ioutil"
"net/http"
"fmt"
"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)
@ -44,87 +44,87 @@ 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)
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=00%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
fmt.Printf("Called ResetNodeToZero for deveui %s\n", deveui)
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")
}
func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32) {
fmt.Printf("Called CalibrateScale for deveui %s, downlink_command: %s\n", deveui, downlink_command)
var cur_cal_settings CalSettings
cur_cal_settings = GetCurrentCalibrationSettings(deveui)
var calibration_weight_gram int32
var calibration_weight2_gram int32
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
}
n, err := strconv.ParseInt(tokens[1], 10, 32)
if err == nil {
calibration_weight_gram = int32(n)
} else {
fmt.Printf("Error: cannot get tare weight in gram: %s, deveui: %s\n", downlink_command, deveui)
return
}
fmt.Printf("Called CalibrateScale for deveui %s, downlink_command: %s\n", deveui, downlink_command)
var cur_cal_settings CalSettings
cur_cal_settings = GetCurrentCalibrationSettings(deveui)
var calibration_weight_gram int32
var calibration_weight2_gram int32
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
}
n, err := strconv.ParseInt(tokens[1], 10, 32)
if err == nil {
calibration_weight_gram = int32(n)
} else {
fmt.Printf("Error: cannot get tare weight in gram: %s, deveui: %s\n", downlink_command, deveui)
return
}
if (len(tokens) > 2) {
n, err := strconv.ParseInt(tokens[2], 10, 32)
if err == nil {
calibration_weight2_gram = int32(n)
} else {
fmt.Printf("Error: cannot get tare weight2 in gram: %s, deveui: %s\n", downlink_command, deveui)
return
}
}
if len(tokens) > 2 {
n, err := strconv.ParseInt(tokens[2], 10, 32)
if err == nil {
calibration_weight2_gram = int32(n)
} else {
fmt.Printf("Error: cannot get tare weight2 in gram: %s, deveui: %s\n", downlink_command, deveui)
return
}
}
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))
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))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui,"do_nothing")
} else {
fmt.Printf("Error: either use tare_a or tare_b or tare; deveui %s\n", deveui)
}
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))
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))
s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr)
MakePost(s)
SetDownlinkCommand(deveui, "do_nothing")
} else {
fmt.Printf("Error: either use tare_a or tare_b or tare; deveui %s\n", deveui)
}
}
func UpdateCalibrationSettingsFromNode(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c float32) {
fmt.Printf("Called UpdateCalibrationSettingsFromNode for deveui %s\n", deveui)
var cur_cal_settings CalSettings
var new_cal_settings CalSettings
fmt.Printf("Called UpdateCalibrationSettingsFromNode for deveui %s\n", deveui)
var cur_cal_settings CalSettings
var new_cal_settings CalSettings
cur_cal_settings = GetCurrentCalibrationSettings(deveui)
cur_cal_settings = GetCurrentCalibrationSettings(deveui)
new_cal_settings.w1_0 = w1_0
new_cal_settings.w2_0 = w2_0
new_cal_settings.w1_c = w1_c
new_cal_settings.w2_c = w2_c
new_cal_settings.w1_0 = w1_0
new_cal_settings.w2_0 = w2_0
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) ||
(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)
// since something has changed and is reflected in the local config, we don't do anything any more...
SetDownlinkCommand(deveui,"do_nothing")
} else {
fmt.Printf("Info: no change in calibration settings for deveui %s\n", deveui)
}
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)
// since something has changed and is reflected in the local config, we don't do anything any more...
SetDownlinkCommand(deveui, "do_nothing")
} else {
fmt.Printf("Info: no change in calibration settings for deveui %s\n", deveui)
}
}

View File

@ -2,33 +2,33 @@ package main
import (
"bytes"
"encoding/json"
"encoding/hex"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"io/ioutil"
"log"
"strings"
"net/http"
"time"
"os"
"strings"
"time"
)
const (
MyDB = "beieliscaledb"
username = "beieli"
password = "beieli4president"
outputfile = "/home/beieli/lorahandler/lorahandler.log"
MyDB = "beieliscaledb"
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"`
DevEUI string `json:"DevEUI"`
DevAddr string `json:"DevAddr"`
LrrLAT float32 `json:"LrrLAT"`
LrrLON float32 `json:"LrrLON"`
Time string `json:"Time"`
Payload_hex string `json:"payload_hex"`
DevEUI string `json:"DevEUI"`
DevAddr string `json:"DevAddr"`
LrrLAT float32 `json:"LrrLAT"`
LrrLON float32 `json:"LrrLON"`
}
type Message struct {
@ -36,68 +36,68 @@ type Message struct {
}
type payload_128 struct {
Version uint8
Version uint8
Fw_version int32
Vbat uint8
H uint8
T int16
P uint8
W1 int32
W2 int32
W1_0 int32
W2_0 int32
W1_C float32
W2_C float32
Vbat uint8
H uint8
T int16
P uint8
W1 int32
W2 int32
W1_0 int32
W2_0 int32
W1_C float32
W2_C float32
}
type payload_129 struct {
Version uint8
Version uint8
Fw_version int32
Vbat uint8
H uint8
T int16
P uint8
W1 int32
W2 int32
W uint16
Vbat uint8
H uint8
T int16
P uint8
W1 int32
W2 int32
W uint16
}
type payload_1 struct {
Version uint8
Vbat uint8
H1 uint8
H2 uint8
H3 uint8
H4 uint8
H5 uint8
H6 uint8
H7 uint8
H8 uint8
T int16
TC1 int8
TC2 int8
TC3 int8
TC4 int8
TC5 int8
TC6 int8
TC7 int8
P1 uint8
P2 uint8
P3 uint8
P4 uint8
P5 uint8
P6 uint8
P7 uint8
P8 uint8
W1 uint16
W2 uint16
W3 uint16
W4 uint16
W5 uint16
W6 uint16
W7 uint16
W8 uint16
O uint8
Vbat uint8
H1 uint8
H2 uint8
H3 uint8
H4 uint8
H5 uint8
H6 uint8
H7 uint8
H8 uint8
T int16
TC1 int8
TC2 int8
TC3 int8
TC4 int8
TC5 int8
TC6 int8
TC7 int8
P1 uint8
P2 uint8
P3 uint8
P4 uint8
P5 uint8
P6 uint8
P7 uint8
P8 uint8
W1 uint16
W2 uint16
W3 uint16
W4 uint16
W5 uint16
W6 uint16
W7 uint16
W8 uint16
O uint8
}
// Global variables
@ -107,199 +107,213 @@ var alertMap map[string]string
var alertLogMap map[string]string
func ProcessInitPacket(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c float32, w1 int32, w2 int32) {
var downlink_command string
var downlink_command string
fmt.Printf("Processing Init Packet for Deveui %s\n", deveui)
downlink_command = GetDownlinkCommand(deveui)
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") {
// reset node to 0
ResetNodeToZero(deveui, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_a ") {
// calibrate Scale A : tare_a <gram_a>
CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_b ") {
// calibrate Scale B : tare_b <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare ") {
// calibrate both Scale A and Scale B in on step : tare <gram_a> <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2)
} else {
fmt.Printf("Error: Unknown downlink_command: %s (DevEUI: %s)\n", downlink_command, deveui)
}
// We update the local settings on each init package (if something changed...)
UpdateCalibrationSettingsFromNode(deveui, w1_0, w2_0, w1_c, w2_c)
fmt.Printf("Processing Init Packet for Deveui %s\n", deveui)
downlink_command = GetDownlinkCommand(deveui)
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" {
// reset node to 0
ResetNodeToZero(deveui, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_a ") {
// calibrate Scale A : tare_a <gram_a>
CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_b ") {
// calibrate Scale B : tare_b <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare ") {
// calibrate both Scale A and Scale B in on step : tare <gram_a> <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2)
} else {
fmt.Printf("Error: Unknown downlink_command: %s (DevEUI: %s)\n", downlink_command, deveui)
}
// We update the local settings on each init package (if something changed...)
UpdateCalibrationSettingsFromNode(deveui, w1_0, w2_0, w1_c, 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_128 payload_128
var pl_129 payload_129
ba, _ = hex.DecodeString(s)
pl_1 = payload_1{}
pl_128 = payload_128{}
pl_129 = payload_129{}
br := bytes.NewReader(ba)
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") {
err := binary.Read(br, binary.LittleEndian, &pl_128)
if err != nil {
fmt.Println(err)
}
} 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)
}
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("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_1.O)
var step = int64(pl_1.O / 7)
var ba []byte
var pl_1 payload_1
var pl_128 payload_128
var pl_129 payload_129
ba, _ = hex.DecodeString(s)
pl_1 = payload_1{}
pl_128 = payload_128{}
pl_129 = payload_129{}
br := bytes.NewReader(ba)
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" {
err := binary.Read(br, binary.LittleEndian, &pl_128)
if err != nil {
fmt.Println(err)
}
} 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)
}
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("}\n")
if write2file {
// Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_1.O)
var step = int64(pl_1.O / 7)
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// 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)
//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)
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)
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)
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)
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)
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)
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)
}
} 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("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// the first temperature is usually too high (maybe becaus of lorawan send, so we take
// 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)
//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)
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)
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)
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)
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)
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)
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)
}
} 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("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
// 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) {
// this is not realistic (>320 kg), we set this to 0 as well...
w = 0
} else {
w = uint16(w32)
}
// we calculate the weight...
var w32 int32
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 {
// 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)
}
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("}\n")
if write2file {
// Time of Packet received
var tfp = (time.Now().Unix() / 60)
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" {
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("}\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)
}
}
// Send alert if necessary
if val, ok := alertMap[deveui]; ok {
sendAlert(deveui,val)
delete(alertMap,deveui)
// alte Werte loeschen
deleteValues(deveui)
}
if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2)
delete(alertLogMap,deveui)
}
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)
// alte Werte loeschen
deleteValues(deveui)
}
if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2)
delete(alertLogMap, deveui)
}
}
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
// eine gueltige Messung
var vp int16 // Voltage in %
if (h > 0) {
vp = int16(v) - 70
if vp < 0 {
vp = 0
} else if vp > 100 {
vp = 100
}
// wir nehmen humidity als Referenz, wenn diese > 0 ist, dann ist es
// eine gueltige Messung
var vp int16 // Voltage in %
if h > 0 {
vp = int16(v) - 70
if vp < 0 {
vp = 0
} else if vp > 100 {
vp = 100
}
s := ""
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)
}
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)
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)
}
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)
WriteStringToFile(s)
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)
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)
}
}
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)
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)
}
}
}
func WriteStringToFile(s string) {
@ -315,16 +329,16 @@ func WriteStringToFile(s string) {
}
func main() {
// Init Redis
initDB()
// Init Redis
initDB()
// Init alertMap
alertMap = make(map[string]string)
alertLogMap = make(map[string]string)
// Init alertMap
alertMap = make(map[string]string)
alertLogMap = make(map[string]string)
// Open Output File
f, err := os.OpenFile(outputfile,os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
file = f
// Open Output File
f, err := os.OpenFile(outputfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
file = f
if err != nil {
fmt.Println(err)
os.Exit(1)
@ -359,9 +373,9 @@ 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)
DecodePayload(u.Prop.Payload_hex, u.Prop.DevEUI, u.Prop.DevAddr, u.Prop.LrrLAT, u.Prop.LrrLON, true)
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

@ -1,10 +1,10 @@
package main
import (
"fmt"
"time"
"strconv"
"github.com/gomodule/redigo/redis"
"fmt"
"github.com/gomodule/redigo/redis"
"strconv"
"time"
)
var globalPool *redis.Pool
@ -13,10 +13,10 @@ const lastvaluesPrefix string = "lastvalues:"
const devPrefix string = "dev:"
type CalSettings struct {
w1_0 int32
w2_0 int32
w1_c float32
w2_c float32
w1_0 int32
w2_0 int32
w1_c float32
w2_c float32
}
func newPool() *redis.Pool {
@ -56,14 +56,14 @@ func initDB() {
pool := newPool()
// get a connection from the globalPool (redis.Conn)
conn := pool.Get()
defer conn.Close()
defer conn.Close()
globalPool = pool
globalPool = pool
// wir machen einen Connection Test
ping(conn)
// wir machen einen Connection Test
ping(conn)
addValue("0000000000000000",uint32(time.Now().Unix()));
addValue("0000000000000000", uint32(time.Now().Unix()))
}
func closeDB() {
@ -71,231 +71,229 @@ func closeDB() {
}
func checkDevAvailable(deveui string) bool {
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
_, err := redis.String(conn.Do("GET", lastvaluesPrefix + deveui))
if (err == redis.ErrNil) {
return true
} else if err != nil {
return false
_, err := redis.String(conn.Do("GET", lastvaluesPrefix+deveui))
if err == redis.ErrNil {
return true
} else if err != nil {
return false
}
return false
}
func getMaxValue(deveui string) uint32 {
var res uint32 = 0
var myvalues []uint32
conn := globalPool.Get()
defer conn.Close()
var res uint32 = 0
var myvalues []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)
return 0
}
if err := redis.ScanSlice(values, &myvalues); err != nil {
fmt.Println(err)
return 0
}
for _, value := range myvalues {
if (uint32(value) > res) {
res = uint32(value)
}
}
for _, value := range myvalues {
if uint32(value) > res {
res = uint32(value)
}
}
return res
return res
}
func addValue(deveui string, value uint32) {
conn := globalPool.Get()
defer conn.Close()
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)
// we set an expiration time of one hour
_, err3 := conn.Do("EXPIRE", lastvaluesPrefix+deveui, 3600)
if err3 != nil {
return
}
}
}
func deleteValues(deveui string) {
conn := globalPool.Get()
defer conn.Close()
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
}
}
}
func getDevAlias(deveui string) string {
res := deveui
res := deveui
if deveui == "" {
return res
}
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
alias, err := redis.String(conn.Do("HGET", devPrefix+deveui, "alias"))
if err == nil {
res = alias
} else {
res = deveui
}
if err == nil {
res = alias
} else {
res = deveui
}
return res
return res
}
func getDevAlarmactive(deveui string) string {
res := "0"
res := "0"
if deveui == "" {
return res
}
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
alarmactive, err := redis.String(conn.Do("HGET", devPrefix+deveui, "alarmactive"))
if err == nil {
res = alarmactive
}
if err == nil {
res = alarmactive
}
return res
return res
}
func getSmsnumber(deveui string) string {
res := ""
if deveui == "" {
return res
}
res := ""
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
smsnumber, err := redis.String(conn.Do("HGET", devPrefix+deveui, "smsnumber"))
if err == nil {
res = smsnumber
}
if err == nil {
res = smsnumber
}
return res
return res
}
func GetDownlinkCommand(deveui string) string {
// 0: do nothing...
res := "do_nothing"
// 0: do nothing...
res := "do_nothing"
if deveui == "" {
return res
}
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
downlink_command, err := redis.String(conn.Do("HGET", devPrefix+deveui, "downlink_command"))
if err == nil {
res = downlink_command
} else {
fmt.Println(err)
res = "do_nothing"
}
if err == nil {
res = downlink_command
} else {
fmt.Println(err)
res = "do_nothing"
}
return res
return res
}
func SetDownlinkCommand(deveui string, new_command string) error {
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
// SET object
_, err := conn.Do("HMSET", devPrefix+deveui, "downlink_command", new_command)
if err != nil {
return err
}
// SET object
_, err := conn.Do("HMSET", devPrefix+deveui, "downlink_command", new_command)
if err != nil {
return err
}
return nil
return nil
}
func GetCurrentCalibrationSettings(deveui string) CalSettings {
var res CalSettings
if deveui == "" {
return res
}
var res CalSettings
if deveui == "" {
return res
}
conn := globalPool.Get()
defer conn.Close()
conn := globalPool.Get()
defer conn.Close()
s_w1_0, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w1_0"))
if err == nil {
n, err := strconv.ParseInt(s_w1_0, 10, 32)
if err == nil {
res.w1_0 = int32(n)
}
}
if err == nil {
n, err := strconv.ParseInt(s_w1_0, 10, 32)
if err == nil {
res.w1_0 = int32(n)
}
}
s_w2_0, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w2_0"))
if err == nil {
n, err := strconv.ParseInt(s_w2_0, 10, 32)
if err == nil {
res.w2_0 = int32(n)
}
}
if err == nil {
n, err := strconv.ParseInt(s_w2_0, 10, 32)
if err == nil {
res.w2_0 = int32(n)
}
}
s_w1_c, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w1_c"))
if err == nil {
f, err := strconv.ParseFloat(s_w1_c, 32)
if err == nil {
res.w1_c = float32(f)
}
}
if err == nil {
f, err := strconv.ParseFloat(s_w1_c, 32)
if err == nil {
res.w1_c = float32(f)
}
}
s_w2_c, err := redis.String(conn.Do("HGET", devPrefix+deveui, "w2_c"))
if err == nil {
f, err := strconv.ParseFloat(s_w2_c, 32)
if err == nil {
res.w2_c = float32(f)
}
}
if err == nil {
f, err := strconv.ParseFloat(s_w2_c, 32)
if err == nil {
res.w2_c = float32(f)
}
}
return res
return res
}
func SetCurrentCalibrationSettings(deveui string, cal_settings CalSettings) error {
if deveui == "" {
return nil
}
if deveui == "" {
return nil
}
conn := globalPool.Get()
defer conn.Close()
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)
if err != nil {
return err
}
// SET object
_, 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
return nil
}