@@ -374,14 +374,14 @@ func editDevicesHandler(w http.ResponseWriter, r *http.Request) {
374374// device voltages.
375375//
376376// Query params:
377- // - ma: MAC address
378- // - vb: Battery Voltage
377+ // - ma: MAC address
378+ // - vb: Battery Voltage
379379// - vnw: Network Voltage
380380// - vp1: Power 1 Voltage
381381// - vp2: Power 2 Voltage
382382// - vp3: Power 3 Voltage
383- // - va: Alarm Voltage
384- // - vr: Alarm Recovery Voltage
383+ // - va: Alarm Voltage
384+ // - vr: Alarm Recovery Voltage
385385//
386386// NOTE: All voltages are parsed in Volts.
387387func calibrateDevicesHandler (w http.ResponseWriter , r * http.Request ) {
@@ -441,12 +441,16 @@ func calibrateDevicesHandler(w http.ResponseWriter, r *http.Request) {
441441 device , err := model .GetDevice (ctx , settingsStore , model .MacEncode (mac ))
442442 if err != nil {
443443 writeDevices (w , r , "unable to get device to calibrate (%s): %v" , mac , err )
444+ return
444445 }
445446
446447 // Names of the voltage sensors to calibrate.
447448 var voltageSensors = []string {
448- model .NameBatterySensor , model .NameNWVoltage , model .NameP1Voltage ,
449- model .NameP2Voltage , model .NameP3Voltage ,
449+ model .NameBatterySensor ,
450+ model .NameNWVoltage ,
451+ model .NameP1Voltage ,
452+ model .NameP2Voltage ,
453+ model .NameP3Voltage ,
450454 }
451455
452456 // Load the most recent sensor values.
@@ -459,50 +463,56 @@ func calibrateDevicesHandler(w http.ResponseWriter, r *http.Request) {
459463 // Calibrate each of the voltage sensors.
460464 var msgs []string
461465 for _ , sensor := range sensors {
462- if sliceutils .ContainsString (voltageSensors , sensor .Name ) {
463- scalar , err := model .GetLatestScalar (ctx , mediaStore , model .ToSID (mac , sensor .Pin ))
464- if err != nil {
465- msgs = append (msgs , fmt .Sprintf ("unable to get latest scalar for %s: %v" , sensor .Name , err ))
466- continue
467- }
468- reportedTime := time .Unix (scalar .Timestamp , 0 )
469-
470- // Check if the scalar was recently reported (last 2 monitor periods).
471- if reportedTime .Before (time .Now ().Add (- 2 * time .Duration (device .MonitorPeriod ) * time .Second )) {
472- msgs = append (msgs , fmt .Sprintf ("scalar (%s) is out of date (timestamp: %s)(current time: %s)" ,
473- sensor .Name , reportedTime .Format (time .ANSIC ), time .Now ().Format (time .ANSIC )))
474- continue
475- }
476-
477- var actual float64
478- switch sensor .Name {
479- case model .NameBatterySensor :
480- actual = vb
481- case model .NameNWVoltage :
482- actual = vnw
483- case model .NameP1Voltage :
484- actual = vp1
485- case model .NameP2Voltage :
486- actual = vp2
487- case model .NameP3Voltage :
488- actual = vp3
489- default :
490- // This shouldn't be possible with the ContainsString check.
491- }
492-
493- // This most likely means the field was left blank. This is not a
494- // meaningful way of calibrating the system.
495- if actual == 0 {
496- continue
497- }
498-
499- // Calculate the new scale value.
500- sensor .Args = strconv .FormatFloat (actual / scalar .Value , 'f' , - 1 , 64 )
501- log .Printf ("calibrated sensor value for %s: %s" , sensor .Name , sensor .Args )
502-
503- // Save the sensor with the new scale factor.
504- model .PutSensorV2 (ctx , settingsStore , & sensor )
466+ if ! sliceutils .ContainsString (voltageSensors , sensor .Name ) {
467+ continue
468+ }
469+
470+ scalar , err := model .GetLatestScalar (ctx , mediaStore , model .ToSID (mac , sensor .Pin ))
471+ if err != nil {
472+ msgs = append (msgs , fmt .Sprintf ("unable to get latest scalar for %s: %v" , sensor .Name , err ))
473+ continue
474+ }
475+ reportedTime := time .Unix (scalar .Timestamp , 0 )
476+
477+ // Check if the scalar was recently reported (last 2 monitor periods).
478+ if reportedTime .Before (time .Now ().Add (- 2 * time .Duration (device .MonitorPeriod ) * time .Second )) {
479+ msgs = append (msgs , fmt .Sprintf ("scalar (%s) is out of date (timestamp: %s)(current time: %s)" ,
480+ sensor .Name , reportedTime .Format (time .ANSIC ), time .Now ().Format (time .ANSIC )))
481+
482+ // Continue to calibrate other sensors that are still reporting.
483+ continue
505484 }
485+
486+ var actual float64
487+ switch sensor .Name {
488+ case model .NameBatterySensor :
489+ actual = vb
490+ case model .NameNWVoltage :
491+ actual = vnw
492+ case model .NameP1Voltage :
493+ actual = vp1
494+ case model .NameP2Voltage :
495+ actual = vp2
496+ case model .NameP3Voltage :
497+ actual = vp3
498+ default :
499+ // This shouldn't be possible with the ContainsString check.
500+ log .Panicln ("cannot handle unexpected sensor name" )
501+ }
502+
503+ // This most likely means the field was left blank. This is not a
504+ // meaningful way of calibrating the system.
505+ if actual == 0 {
506+ continue
507+ }
508+
509+ // Calculate the new scale value.
510+ sensor .Args = strconv .FormatFloat (actual / scalar .Value , 'f' , - 1 , 64 )
511+ log .Printf ("calibrated sensor value for %s: %s" , sensor .Name , sensor .Args )
512+
513+ // Save the sensor with the new scale factor.
514+ model .PutSensorV2 (ctx , settingsStore , & sensor )
515+
506516 }
507517
508518 msg := strings .Join (msgs , "," )
0 commit comments