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.
 
 
 
 

39 rivejä
908 B

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