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 package main
import ( import (
"fmt" "fmt"
"strings" "io/ioutil"
"io/ioutil" "log"
"log" "net/http"
"time" "net/url"
"net/http" "strings"
"net/url" "time"
) )
func sendSMS(phonenumber string, alertMessage string) { 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) req, err := http.NewRequest("GET", myurl, nil)
if err != nil { if err != nil {
@ -37,13 +37,13 @@ func sendSMS(phonenumber string, alertMessage string) {
} }
func sendAlert(deveui string, alertMessage string) { func sendAlert(deveui string, alertMessage string) {
fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage) fmt.Printf("sendAlert: deveui=%s, message=%s\n", deveui, alertMessage)
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

@ -1,17 +1,17 @@
package main package main
import ( import (
"fmt" "fmt"
"log" "io/ioutil"
"time" "log"
"math" "math"
"strings" "net/http"
"strconv" "strconv"
"io/ioutil" "strings"
"net/http" "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) { func MakePost(url string) {
req, err := http.NewRequest("POST", url, nil) 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) { func ResetNodeToZero(deveui string, w1_0 int32, w2_0 int32) {
fmt.Printf("Called ResetNodeToZero for deveui %s\n", deveui) 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) s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=00%s", api_url, deveui, valstr)
MakePost(s) MakePost(s)
SetDownlinkCommand(deveui,"do_nothing") SetDownlinkCommand(deveui, "do_nothing")
} }
func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32) { 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) fmt.Printf("Called CalibrateScale for deveui %s, downlink_command: %s\n", deveui, downlink_command)
var cur_cal_settings CalSettings var cur_cal_settings CalSettings
cur_cal_settings = GetCurrentCalibrationSettings(deveui) cur_cal_settings = GetCurrentCalibrationSettings(deveui)
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
} }
n, err := strconv.ParseInt(tokens[1], 10, 32) n, err := strconv.ParseInt(tokens[1], 10, 32)
if err == nil { if err == nil {
calibration_weight_gram = int32(n) calibration_weight_gram = int32(n)
} else { } else {
fmt.Printf("Error: cannot get tare weight in gram: %s, deveui: %s\n", downlink_command, deveui) fmt.Printf("Error: cannot get tare weight in gram: %s, deveui: %s\n", downlink_command, deveui)
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)
} else { } else {
fmt.Printf("Error: cannot get tare weight2 in gram: %s, deveui: %s\n", downlink_command, deveui) fmt.Printf("Error: cannot get tare weight2 in gram: %s, deveui: %s\n", downlink_command, deveui)
return return
} }
} }
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))
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 { } else {
fmt.Printf("Error: either use tare_a or tare_b or tare; deveui %s\n", deveui) 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) { 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) fmt.Printf("Called UpdateCalibrationSettingsFromNode for deveui %s\n", deveui)
var cur_cal_settings CalSettings var cur_cal_settings CalSettings
var new_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.w1_0 = w1_0
new_cal_settings.w2_0 = w2_0 new_cal_settings.w2_0 = w2_0
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")
} else { } else {
fmt.Printf("Info: no change in calibration settings for deveui %s\n", deveui) fmt.Printf("Info: no change in calibration settings for deveui %s\n", deveui)
} }
} }

View File

@ -2,33 +2,33 @@ 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"
"time" "os"
"strings"
"time"
) )
const ( const (
MyDB = "beieliscaledb" MyDB = "beieliscaledb"
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"`
DevEUI string `json:"DevEUI"` DevEUI string `json:"DevEUI"`
DevAddr string `json:"DevAddr"` DevAddr string `json:"DevAddr"`
LrrLAT float32 `json:"LrrLAT"` LrrLAT float32 `json:"LrrLAT"`
LrrLON float32 `json:"LrrLON"` LrrLON float32 `json:"LrrLON"`
} }
type Message struct { type Message struct {
@ -36,68 +36,68 @@ type Message struct {
} }
type payload_128 struct { type payload_128 struct {
Version uint8 Version uint8
Fw_version int32 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
W1_0 int32 W1_0 int32
W2_0 int32 W2_0 int32
W1_C float32 W1_C float32
W2_C float32 W2_C float32
} }
type payload_129 struct { type payload_129 struct {
Version uint8 Version uint8
Fw_version int32 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 W uint16
} }
type payload_1 struct { type payload_1 struct {
Version uint8 Version uint8
Vbat uint8 Vbat uint8
H1 uint8 H1 uint8
H2 uint8 H2 uint8
H3 uint8 H3 uint8
H4 uint8 H4 uint8
H5 uint8 H5 uint8
H6 uint8 H6 uint8
H7 uint8 H7 uint8
H8 uint8 H8 uint8
T int16 T int16
TC1 int8 TC1 int8
TC2 int8 TC2 int8
TC3 int8 TC3 int8
TC4 int8 TC4 int8
TC5 int8 TC5 int8
TC6 int8 TC6 int8
TC7 int8 TC7 int8
P1 uint8 P1 uint8
P2 uint8 P2 uint8
P3 uint8 P3 uint8
P4 uint8 P4 uint8
P5 uint8 P5 uint8
P6 uint8 P6 uint8
P7 uint8 P7 uint8
P8 uint8 P8 uint8
W1 uint16 W1 uint16
W2 uint16 W2 uint16
W3 uint16 W3 uint16
W4 uint16 W4 uint16
W5 uint16 W5 uint16
W6 uint16 W6 uint16
W7 uint16 W7 uint16
W8 uint16 W8 uint16
O uint8 O uint8
} }
// Global variables // Global variables
@ -107,199 +107,213 @@ var alertMap map[string]string
var alertLogMap 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) { 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) 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 ") {
// calibrate Scale A : tare_a <gram_a> // calibrate Scale A : tare_a <gram_a>
CalibrateScale(deveui, downlink_command, w1, w2) CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare_b ") { } else if strings.HasPrefix(downlink_command, "tare_b ") {
// calibrate Scale B : tare_b <gram_b> // calibrate Scale B : tare_b <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2) CalibrateScale(deveui, downlink_command, w1, w2)
} else if strings.HasPrefix(downlink_command, "tare ") { } else if strings.HasPrefix(downlink_command, "tare ") {
// calibrate both Scale A and Scale B in on step : tare <gram_a> <gram_b> // calibrate both Scale A and Scale B in on step : tare <gram_a> <gram_b>
CalibrateScale(deveui, downlink_command, w1, w2) CalibrateScale(deveui, downlink_command, w1, w2)
} else { } else {
fmt.Printf("Error: Unknown downlink_command: %s (DevEUI: %s)\n", downlink_command, deveui) 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...) // We update the local settings on each init package (if something changed...)
UpdateCalibrationSettingsFromNode(deveui, w1_0, w2_0, w1_c, w2_c) UpdateCalibrationSettingsFromNode(deveui, w1_0, w2_0, w1_c, w2_c)
} }
func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrlon float32, write2file bool) { func DecodePayload(s string, deveui string, devaddr string, lrrlat float32, lrrlon float32, write2file bool) {
var ba []byte var ba []byte
var pl_1 payload_1 var pl_1 payload_1
var pl_128 payload_128 var pl_128 payload_128
var pl_129 payload_129 var pl_129 payload_129
ba, _ = hex.DecodeString(s) ba, _ = hex.DecodeString(s)
pl_1 = payload_1{} pl_1 = payload_1{}
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)
} }
} 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)
fmt.Printf(" offset: %d\n",pl_1.O) 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(" 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(" 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(" 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: %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(" 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") fmt.Printf("}\n")
if write2file { if write2file {
// Time of first Packet // Time of first Packet
var tfp = (time.Now().Unix() / 60) - int64(pl_1.O) var tfp = (time.Now().Unix() / 60) - int64(pl_1.O)
var step = int64(pl_1.O / 7) var step = int64(pl_1.O / 7)
// the first temperature is usually too high (maybe becaus of lorawan send, so we take // 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... // 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,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) //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) 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) 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) 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) 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) 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) 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)
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(" cal_1_0: %d\n",pl_128.W1_0) 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_2_0: %d\n", pl_128.W2_0)
fmt.Printf(" cal_1_C: %f\n",pl_128.W1_C) fmt.Printf(" cal_1_C: %f\n", pl_128.W1_C)
fmt.Printf(" cal_2_C: %f\n",pl_128.W2_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)
// 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
w = 0 multiply_by_2 := false
} else if (w32 > 65535) { if pl_128.W1_0 == NOT_ATTACHED {
// this is not realistic (>320 kg), we set this to 0 as well... w1_0_real = pl_128.W1
w = 0 multiply_by_2 = true
} else { }
w = uint16(w32) 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) 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)
fmt.Printf(" vbat: %d,\n", pl_129.Vbat) fmt.Printf(" vbat: %d,\n", pl_129.Vbat)
fmt.Printf(" humidity: %d\n",pl_129.H) fmt.Printf(" humidity: %d\n", pl_129.H)
fmt.Printf(" pressure: %d\n",pl_129.P) fmt.Printf(" pressure: %d\n", pl_129.P)
fmt.Printf(" weight1: %d\n",pl_129.W1) fmt.Printf(" weight1: %d\n", pl_129.W1)
fmt.Printf(" weight2: %d\n",pl_129.W2) fmt.Printf(" weight2: %d\n", pl_129.W2)
fmt.Printf(" weight: %d\n",pl_129.W) fmt.Printf(" weight: %d\n", pl_129.W)
fmt.Printf(" temp: %d\n",pl_129.T) fmt.Printf(" temp: %d\n", pl_129.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_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 // Send alert if necessary
if val, ok := alertMap[deveui]; ok { if val, ok := alertMap[deveui]; ok {
sendAlert(deveui,val) sendAlert(deveui, val)
delete(alertMap,deveui) delete(alertMap, deveui)
// alte Werte loeschen // alte Werte loeschen
deleteValues(deveui) deleteValues(deveui)
} }
if val2, ok2 := alertLogMap[deveui]; ok2 { if val2, ok2 := alertLogMap[deveui]; ok2 {
WriteStringToFile(val2) WriteStringToFile(val2)
delete(alertLogMap,deveui) 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) { 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 {
vp = 100 vp = 100
} }
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)
WriteStringToFile(s) WriteStringToFile(s)
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")
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)
} }
} }
} }
func WriteStringToFile(s string) { func WriteStringToFile(s string) {
@ -315,16 +329,16 @@ func WriteStringToFile(s string) {
} }
func main() { func main() {
// Init Redis // Init Redis
initDB() initDB()
// Init alertMap // Init alertMap
alertMap = make(map[string]string) alertMap = make(map[string]string)
alertLogMap = make(map[string]string) alertLogMap = make(map[string]string)
// Open Output File // 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 file = f
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -359,9 +373,9 @@ func main() {
fmt.Println("Payload Hex: " + u.Prop.Payload_hex) fmt.Println("Payload Hex: " + u.Prop.Payload_hex)
fmt.Println("DevEUI: " + u.Prop.DevEUI) fmt.Println("DevEUI: " + u.Prop.DevEUI)
fmt.Println("DevAddr: " + u.Prop.DevAddr) fmt.Println("DevAddr: " + u.Prop.DevAddr)
fmt.Printf("LrrLAT: %f\n",u.Prop.LrrLAT) fmt.Printf("LrrLAT: %f\n", u.Prop.LrrLAT)
fmt.Printf("LrrLON: %f\n",u.Prop.LrrLON) 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) DecodePayload(u.Prop.Payload_hex, u.Prop.DevEUI, u.Prop.DevAddr, u.Prop.LrrLAT, u.Prop.LrrLON, true)
}) })
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View File

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