You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 line
1.0 KiB

  1. package utils
  2. import (
  3. "math"
  4. "strconv"
  5. "github.com/AFASystems/presence/internal/pkg/model"
  6. )
  7. // CalculateDistance calculates the approximate distance from RSSI and TX power
  8. // Uses a hybrid approach combining logarithmic and polynomial models
  9. func CalculateDistance(adv model.BeaconAdvertisement) float64 {
  10. txPower := twosComp(adv.TXPower)
  11. ratio := float64(adv.RSSI) / float64(txPower)
  12. if ratio < 1.0 {
  13. return math.Pow(ratio, 10)
  14. } else {
  15. return (0.89976)*math.Pow(ratio, 7.7095) + 0.111
  16. }
  17. }
  18. // TwosComp converts a two's complement hexadecimal string to int64
  19. func twosComp(inp string) int64 {
  20. i, err := strconv.ParseInt("0x"+inp, 0, 64)
  21. if err != nil {
  22. return 0
  23. }
  24. if i >= 128 {
  25. return i - 256
  26. }
  27. return i
  28. }
  29. // ValidateRSSI validates if RSSI value is within reasonable bounds
  30. func ValidateRSSI(rssi int64) bool {
  31. return rssi >= -120 && rssi <= 0
  32. }
  33. // ValidateTXPower validates if TX power is within reasonable bounds
  34. func ValidateTXPower(txPower string) bool {
  35. power := twosComp(txPower)
  36. return power >= -128 && power <= 127
  37. }