diff --git a/calibration.go b/calibration.go index d56e683..427de02 100644 --- a/calibration.go +++ b/calibration.go @@ -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) - s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=00", api_url, 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) - // values should be taken from next packet... - SetDownlinkCommand(deveui,"update_cal_from_node") + SetDownlinkCommand(deveui,"do_nothing") } func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32) { @@ -86,40 +86,45 @@ func CalibrateScale(deveui string, downlink_command string, w1 int32, w2 int32) 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.w2_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) 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) } - SetDownlinkCommand(deveui,"update_cal_from_node") -} - -func UpdateNodeCalibrationSettings(deveui string) { - fmt.Printf("Called UpdateNodeCalibrationSettings for deveui %s\n", deveui) - var cur_cal_settings CalSettings - cur_cal_settings = GetCurrentCalibrationSettings(deveui) - - 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(cur_cal_settings.w2_c)) - s := fmt.Sprintf("%s/downlink?DevEUI=%s&FPort=1&Payload=01%s", api_url, deveui, valstr) - MakePost(s) } 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 + + 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 - SetCurrentCalibrationSettings(deveui,new_cal_settings) - SetDownlinkCommand(deveui,"do_nothing") + + 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) + } } diff --git a/lorahandler.go b/lorahandler.go index 09d56eb..dcb0d33 100644 --- a/lorahandler.go +++ b/lorahandler.go @@ -116,7 +116,7 @@ func ProcessInitPacket(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c 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_0, w2_0) + ResetNodeToZero(deveui, w1, w2) } else if strings.HasPrefix(downlink_command, "tare_a ") { // calibrate Scale A : tare_a CalibrateScale(deveui, downlink_command, w1, w2) @@ -126,15 +126,11 @@ func ProcessInitPacket(deveui string, w1_0 int32, w2_0 int32, w1_c float32, w2_c } else if strings.HasPrefix(downlink_command, "tare ") { // calibrate both Scale A and Scale B in on step : tare CalibrateScale(deveui, downlink_command, w1, w2) - } else if (downlink_command == "update_cal_on_node") { - // update calibration settings on node - UpdateNodeCalibrationSettings(deveui) - } else if (downlink_command == "update_cal_from_node") { - // update calibration settings from node - UpdateCalibrationSettingsFromNode(deveui, w1_0, w2_0, w1_c, w2_c) } 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) { diff --git a/persistence.go b/persistence.go index e493f17..b221681 100644 --- a/persistence.go +++ b/persistence.go @@ -288,6 +288,8 @@ func SetCurrentCalibrationSettings(deveui string, cal_settings CalSettings) erro 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) + // 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 {