diff options
| author | Smoke <[email protected]> | 2024-01-22 15:56:16 -1000 |
|---|---|---|
| committer | Smoke <[email protected]> | 2024-01-22 15:56:16 -1000 |
| commit | 5fa965d66db226df3b94131c9b0933822d55c985 (patch) | |
| tree | 432f82aec644c24c6d325d4407801d37e9b2a75e /lora | |
| parent | 70bb2c77356d349165ba46ea98f8346284c2e44e (diff) | |
updates
Diffstat (limited to 'lora')
| -rw-r--r-- | lora/helpers.go | 54 | ||||
| -rw-r--r-- | lora/lora_test.go | 43 |
2 files changed, 77 insertions, 20 deletions
diff --git a/lora/helpers.go b/lora/helpers.go index c843cc3..e3e7750 100644 --- a/lora/helpers.go +++ b/lora/helpers.go @@ -1,35 +1,41 @@ +// Package lora provides utilities to assess the signal quality of LoRa (Long Range) communication +// based on RSSI (Received Signal Strength Indicator) and SNR (Signal-to-Noise Ratio) values. package lora import "fmt" -// translated from https://sensing-labs.com/f-a-q/a-good-radio-level/ - -// Define signal quality and diagnostic notes. -type signalQuality string -type diagnosticNote string +// SignalQuality defines a type for representing the quality of a signal. +type SignalQuality string const ( - Good signalQuality = "GOOD" - Fair signalQuality = "FAIR" - Bad signalQuality = "BAD" + // SignalQualityGood indicates a good signal quality. + SignalQualityGood SignalQuality = "GOOD" + // SignalQualityFair indicates a fair signal quality. + SignalQualityFair SignalQuality = "FAIR" + // SignalQualityBad indicates a bad signal quality. + SignalQualityBad SignalQuality = "BAD" ) -// getSignalQuality determines the signal quality based on RSSI and SNR. -func getSignalQuality(rssi, snr float64) signalQuality { +// GetSignalQuality determines the signal quality based on RSSI and SNR. +// A GOOD signal is determined by SNR >= -7 and RSSI >= -115. +// A FAIR signal is determined by SNR >= -15 and RSSI >= -126. +// Any signal that does not meet the GOOD or FAIR criteria is considered BAD. +func GetSignalQuality(rssi, snr float64) SignalQuality { // Define the boundaries for GOOD signal quality if snr >= -7 && rssi >= -115 { - return Good + return SignalQualityGood } // Define the boundaries for FAIR signal quality if snr >= -15 && rssi >= -126 { - return Fair + return SignalQualityFair } // If none of the above conditions are met, signal is BAD - return Bad + return SignalQualityBad } -// getDiagnosticNotes provides recommendations based on RSSI and SNR values. -func getDiagnosticNotes(rssi, snr float64) diagnosticNote { +// GetDiagnosticNotes provides recommendations based on RSSI and SNR values. +// It returns different notes depending on the quality of the RF level. +func GetDiagnosticNotes(rssi, snr float64) string { if rssi >= -115 && snr >= -7 { return "RF level is optimal to get a good reception reliability." } else if rssi >= -126 && snr >= -15 { @@ -39,14 +45,22 @@ func getDiagnosticNotes(rssi, snr float64) diagnosticNote { } } -func Demo() { - // Example usage +// ExampleGetSignalQuality demonstrates the usage of the GetSignalQuality function. +func ExampleGetSignalQuality() { rssi := -120.0 // RSSI value snr := -10.0 // SNR value - quality := getSignalQuality(rssi, snr) - notes := getDiagnosticNotes(rssi, snr) - + quality := GetSignalQuality(rssi, snr) fmt.Printf("The signal quality is %s.\n", quality) + // Output: The signal quality is FAIR. +} + +// ExampleGetDiagnosticNotes demonstrates the usage of the GetDiagnosticNotes function. +func ExampleGetDiagnosticNotes() { + rssi := -130.0 // RSSI value + snr := -20.0 // SNR value + + notes := GetDiagnosticNotes(rssi, snr) fmt.Printf("Diagnostic Notes: %s\n", notes) + // Output: Diagnostic Notes: NOISY environment. Try to put device out of electromagnetic sources. } diff --git a/lora/lora_test.go b/lora/lora_test.go new file mode 100644 index 0000000..43d0b7b --- /dev/null +++ b/lora/lora_test.go @@ -0,0 +1,43 @@ +package lora + +import "testing" + +// TestGetSignalQuality tests the GetSignalQuality function with different RSSI and SNR values. +func TestGetSignalQuality(t *testing.T) { + tests := []struct { + rssi float64 + snr float64 + expected SignalQuality + }{ + {-115, -7, SignalQualityGood}, + {-120, -10, SignalQualityFair}, + {-130, -20, SignalQualityBad}, + } + + for _, test := range tests { + actual := GetSignalQuality(test.rssi, test.snr) + if actual != test.expected { + t.Errorf("GetSignalQuality(%v, %v) = %v; want %v", test.rssi, test.snr, actual, test.expected) + } + } +} + +// TestGetDiagnosticNotes tests the GetDiagnosticNotes function with different RSSI and SNR values. +func TestGetDiagnosticNotes(t *testing.T) { + tests := []struct { + rssi float64 + snr float64 + expected string + }{ + {-115, -7, "RF level is optimal to get a good reception reliability."}, + {-126, -15, "RF level is not optimal but must be sufficient. Try to improve your device position if possible. You will have to monitor the stability of the RF level."}, + {-130, -20, "NOISY environment. Try to put device out of electromagnetic sources."}, + } + + for _, test := range tests { + actual := GetDiagnosticNotes(test.rssi, test.snr) + if actual != test.expected { + t.Errorf("GetDiagnosticNotes(%v, %v) = %v; want %v", test.rssi, test.snr, actual, test.expected) + } + } +} |
