46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from datetime import datetime
|
|
|
|
from django.db import transaction
|
|
from django.utils import timezone
|
|
|
|
from django_q.tasks import async_task
|
|
|
|
from doorcontrol.models import Door, HIDEvent
|
|
|
|
|
|
@transaction.atomic()
|
|
def getMessages(door: Door):
|
|
last_event = door.hidevent_set.order_by("timestamp").last()
|
|
if last_event is not None:
|
|
last_ts = timezone.make_naive(last_event.timestamp)
|
|
else:
|
|
last_ts = datetime(2010, 1, 1)
|
|
|
|
retrieved_message_count = 0
|
|
created_message_count = 0
|
|
for events_page in door.controller.get_events(last_ts):
|
|
print(f"Importing {len(events_page)} events for {door.name}")
|
|
created_messages = HIDEvent.objects.bulk_create(
|
|
(HIDEvent.from_xml_attributes(door, event.attrib) for event in events_page),
|
|
ignore_conflicts=True,
|
|
)
|
|
|
|
retrieved_message_count += len(events_page)
|
|
created_message_count += len(created_messages)
|
|
|
|
return {
|
|
"retrieved": retrieved_message_count,
|
|
"created": created_message_count,
|
|
}
|
|
|
|
|
|
def q_getMessagesAllDoors():
|
|
# TODO: this should probably use async_iter
|
|
for door in Door.objects.all():
|
|
async_task(
|
|
getMessages,
|
|
door,
|
|
cluster="internal",
|
|
group=f"Fetch HID Events - {door.name}",
|
|
)
|