Merge pull request #210 from jennypaxian/main

Add last seen to ibeacon fakemobileclient
This commit is contained in:
Jenny Paxian 2024-12-29 18:24:34 +01:00 committed by GitHub
commit e58a64f57d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,6 +22,8 @@ from bleak.backends.scanner import AdvertisementData
PORT = int(sys.argv[1]) if sys.argv[1:] else 8042 PORT = int(sys.argv[1]) if sys.argv[1:] else 8042
last_bt_time = {}
ibeacon_format = Struct( ibeacon_format = Struct(
"type_length" / Const(b"\x02\x15"), "type_length" / Const(b"\x02\x15"),
"uuid" / Array(16, Byte), "uuid" / Array(16, Byte),
@ -63,7 +65,15 @@ async def ble_scan():
apple_data = advertisement_data.manufacturer_data[0x004C] apple_data = advertisement_data.manufacturer_data[0x004C]
ibeacon = ibeacon_format.parse(apple_data) ibeacon = ibeacon_format.parse(apple_data)
uuid = UUID(bytes=bytes(ibeacon.uuid)) uuid = UUID(bytes=bytes(ibeacon.uuid))
beacons.append({'uuid': str(uuid), 'major': ibeacon.major, 'minor': ibeacon.minor, 'distance': calc_distance(ibeacon.power, advertisement_data.rssi), 'last_seen_ago': 0 })
now = time.time()
if str(uuid) in last_bt_time:
lastTime = last_bt_time[str(uuid)]
else:
lastTime = now
last_bt_time[str(uuid)] = now
beacons.append({'uuid': str(uuid), 'major': ibeacon.major, 'minor': ibeacon.minor, 'distance': calc_distance(ibeacon.power, advertisement_data.rssi), 'last_seen_ago': lastTime })
except KeyError: except KeyError:
# Apple company ID (0x004c) not found # Apple company ID (0x004c) not found
pass pass