Skip to content

Commit

Permalink
fix: clamp distance to max theoretical value (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 23, 2022
1 parent ad00304 commit ae71fbd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/ibeacon_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

UNPACK_IBEACON = struct.Struct(">HHb").unpack

MAX_THEORETICAL_DISTANCE = 400.0

APPLE_MFR_ID = 76
IBEACON_FIRST_BYTE = 0x02
Expand Down Expand Up @@ -86,4 +87,4 @@ def calculate_distance_meters(power: int, rssi: int) -> float | None:
distance = pow(ratio, 10)
else:
distance = cast(float, 0.89976 * pow(ratio, 7.7095) + 0.111)
return distance if distance < 1000 else None
return min(distance, MAX_THEORETICAL_DISTANCE)
5 changes: 3 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ def test_not_parse_short_service_info():
def tests_calculate_distance_meters():
assert calculate_distance_meters(-59, -60) == 1.1352362990362899
assert calculate_distance_meters(59, -60) == 1.183020818815412
assert calculate_distance_meters(12, -80) is None
assert calculate_distance_meters(12, -80) == 400.0
assert calculate_distance_meters(59, 0) is None
assert calculate_distance_meters(-3, -100) is None
assert calculate_distance_meters(-3, -100) == 400.0
assert calculate_distance_meters(-3, -96) == 400.0
assert calculate_distance_meters(-3, -3) == 1.01076
assert calculate_distance_meters(-4, -3) == 0.056313514709472656

0 comments on commit ae71fbd

Please sign in to comment.