From 55759ec705eba278dd865bd87124c2589d315c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=88=D0=BA=D0=B8=D0=BD=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9?= Date: Tue, 18 Nov 2025 10:06:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=B2=D1=8F=D0=B7=D0=BA?= =?UTF-8?q?=D0=B0=20LyngSat=20=D1=81=D1=80=D0=B0=D0=B7=D1=83=20=D0=B2=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20=D0=B8=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D1=80=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbapp/mainapp/utils.py | 51 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/dbapp/mainapp/utils.py b/dbapp/mainapp/utils.py index d62c41d..a36c1ed 100644 --- a/dbapp/mainapp/utils.py +++ b/dbapp/mainapp/utils.py @@ -69,6 +69,45 @@ def find_matching_transponder(satellite, frequency, polarization): # Возвращаем самый свежий транспондер return transponders.first() + +def find_matching_lyngsat(satellite, frequency, polarization, tolerance_mhz=0.1): + """ + Находит подходящий источник LyngSat для заданных параметров. + + Алгоритм: + 1. Фильтрует источники LyngSat по спутнику и поляризации + 2. Проверяет, совпадает ли частота с заданной точностью (по умолчанию ±0.1 МГц) + 3. Возвращает самый свежий источник (по last_update) + + Args: + satellite: объект Satellite + frequency: частота в МГц + polarization: объект Polarization + tolerance_mhz: допуск по частоте в МГц (по умолчанию 0.1) + + Returns: + LyngSat или None: найденный источник LyngSat или None + """ + # Импортируем здесь, чтобы избежать циклических импортов + from lyngsatapp.models import LyngSat + + if not satellite or not polarization or frequency == -1.0: + return None + + # Фильтруем источники LyngSat по спутнику и поляризации + lyngsat_sources = LyngSat.objects.filter( + id_satellite=satellite, + polarization=polarization, + frequency__isnull=False + ).filter( + # Проверяем, входит ли частота в допуск + frequency__gte=frequency - tolerance_mhz, + frequency__lte=frequency + tolerance_mhz + ).order_by('-last_update') # Сортируем по дате обновления (самые свежие первыми) + + # Возвращаем самый свежий источник + return lyngsat_sources.first() + # ============================================================================ # Константы # ============================================================================ @@ -433,11 +472,15 @@ def _create_objitem_from_row(row, sat, source, user_to_use, consts): # Находим подходящий транспондер transponder = find_matching_transponder(sat, freq, polarization_obj) - # Создаем новый ObjItem и связываем с Source и Transponder + # Находим подходящий источник LyngSat (точность 0.1 МГц) + lyngsat_source = find_matching_lyngsat(sat, freq, polarization_obj, tolerance_mhz=0.1) + + # Создаем новый ObjItem и связываем с Source, Transponder и LyngSat obj_item = ObjItem.objects.create( name=source_name, source=source, transponder=transponder, + lyngsat_source=lyngsat_source, created_by=user_to_use ) @@ -767,11 +810,15 @@ def _create_objitem_from_csv_row(row, source, user_to_use): # Находим подходящий транспондер transponder = find_matching_transponder(sat_obj, row["freq"], pol_obj) - # Создаем новый ObjItem и связываем с Source и Transponder + # Находим подходящий источник LyngSat (точность 0.1 МГц) + lyngsat_source = find_matching_lyngsat(sat_obj, row["freq"], pol_obj, tolerance_mhz=0.1) + + # Создаем новый ObjItem и связываем с Source, Transponder и LyngSat obj_item = ObjItem.objects.create( name=row["obj"], source=source, transponder=transponder, + lyngsat_source=lyngsat_source, created_by=user_to_use )