2025-07-07 14:41:17 +07:00

68 lines
2.0 KiB
Python

import datetime
from django.contrib.gis.geos import Point
from apps.eps.models.tags.model_tagdates import TagDate
from apps.eps.models.tags.model_tags import Tag
from apps.eps.repositories.tag_repository import TagRepository
class ChirpstackTagsRepository(TagRepository):
def update_tag(self, tag: Tag, rx_info: dict) -> None:
"""
Обновляет свойства тегу.
:param tag:
:param rx_info:
:return:
"""
point = Point(
x=rx_info["location"]["longitude"],
y=rx_info["location"]["latitude"],
srid=4326,
)
tag.geo_point = point
tag.date_time_street = datetime.datetime.fromisoformat(rx_info["gwTime"])
tag.save()
self.create_tag_history(tag, rx_info)
def create_tag(self, tag_dict: dict) -> None:
"""
Создает тэг.
:param tag_dict:
:return:
"""
date_time = datetime.datetime.fromisoformat(tag_dict["rxInfo"][0]["gwTime"])
rx_info = tag_dict["rxInfo"][0]
point = Point(
x=rx_info["location"]["longitude"],
y=rx_info["location"]["latitude"],
srid=4326,
)
tag = Tag.objects.create(
name=tag_dict["deviceInfo"]["deviceName"],
sn=tag_dict["deviceInfo"]["deviceName"],
geo_point=point,
date_time_street=date_time,
is_street=True,
)
self.create_tag_history(tag, rx_info)
def create_tag_history(self, tag: Tag, rx_info: dict) -> None:
"""
Создает историю перемещений тэга.
:param tag:
:param rx_info:
:return:
"""
date_time = datetime.datetime.fromisoformat(rx_info["gwTime"])
TagDate.objects.create(
tag=tag,
x=rx_info["location"]["latitude"],
y=rx_info["location"]["longitude"],
time=date_time,
street=True,
)