Тестовый контур
This commit is contained in:
parent
fd3625d2bd
commit
14621327f9
@ -10,7 +10,10 @@
|
||||
- POSTGRES_DB - имя базы данных
|
||||
- POSTGRES_USER - пользователь базы данных
|
||||
- POSTGRES_PASSWORD - пароль пользователя базы данных
|
||||
- POSTGRES_PORT - порт базы данных
|
||||
- POSTGRES_PORT - порт базы данных
|
||||
- TEST_CIRCUIT - Тестовый контур:
|
||||
- 1: True
|
||||
- 0: False
|
||||
## Использование
|
||||
|
||||
После проведения миграций создать суперпользователя из стандартной команды джанго:
|
||||
|
||||
@ -3,13 +3,16 @@ import datetime
|
||||
import io
|
||||
import json
|
||||
import math
|
||||
import random
|
||||
import traceback
|
||||
import uuid
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
import requests
|
||||
import requests.exceptions as rex
|
||||
from bs4 import BeautifulSoup
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Min, Q, Sum
|
||||
from requests.auth import HTTPBasicAuth
|
||||
@ -24,6 +27,8 @@ from apps.gto.models.model_tracking import EquipmentTracking
|
||||
from apps.gto.models.model_type_sensor import TypeSensor
|
||||
from apps.seismicproc.models.model_earthquake import Earthquake
|
||||
from apps.util import mssql
|
||||
from apps.util.random_coords import get_realistic_coords
|
||||
from apps.workerslocation.models.workers_locations import WorkersLocation
|
||||
from mfssogr.celery import app
|
||||
from sabron.util import logging
|
||||
|
||||
@ -887,3 +892,39 @@ def load_navigation(): #
|
||||
load_asd_polygon()
|
||||
except Exception as err:
|
||||
logging.error(traceback.format_exc())
|
||||
|
||||
|
||||
@app.task()
|
||||
def test_circuit():
|
||||
MAX_SPEED_KPH = 50
|
||||
try:
|
||||
equipment_count = getattr(settings, 'TEST_CIRCUIT_EQUIPMENT_COUNT', 5)
|
||||
worker_count = getattr(settings, 'TEST_CIRCUIT_WORKER_COUNT', 5)
|
||||
center_coords = (
|
||||
float(settingsmodule.get_settings("latitude", "55.563254")),
|
||||
float(settingsmodule.get_settings("longitude", "86.104064"))
|
||||
)
|
||||
|
||||
for i in range(equipment_count):
|
||||
obj, created = Equipment.objects.get_or_create(
|
||||
uid=f"test_object_{i}", defaults={"time": timezone.now()}
|
||||
)
|
||||
lat, lon = get_realistic_coords(center_coords, (obj.lat, obj.lon) if not created else None, obj.speed)
|
||||
obj.lat = lat
|
||||
obj.lon = lon
|
||||
obj.speed = random.uniform(10, MAX_SPEED_KPH)
|
||||
obj.time = timezone.now()
|
||||
obj.save()
|
||||
|
||||
for i in range(worker_count):
|
||||
obj, created = WorkersLocation.objects.get_or_create(
|
||||
uid=f"test_object_{i}", defaults={"update_time": timezone.now()}
|
||||
)
|
||||
lat, lon = get_realistic_coords(center_coords, (obj.latitude, obj.longitude) if not created else None, obj.velocity)
|
||||
obj.latitude = lat
|
||||
obj.longitude = lon
|
||||
obj.velocity = random.uniform(10, MAX_SPEED_KPH)
|
||||
obj.update_time = timezone.now()
|
||||
obj.save()
|
||||
except Exception:
|
||||
logging.error(traceback.format_exc())
|
||||
|
||||
32
apps/util/random_coords.py
Normal file
32
apps/util/random_coords.py
Normal file
@ -0,0 +1,32 @@
|
||||
import math
|
||||
import random
|
||||
|
||||
MAX_SPEED_KPH = 50
|
||||
DEGREES_PER_KM = 1 / 111
|
||||
MAX_SPEED_DEGREES = MAX_SPEED_KPH * DEGREES_PER_KM * (30 / 3600)
|
||||
|
||||
|
||||
def get_realistic_coords(center_coords: tuple, current_coords: tuple = None,
|
||||
current_speed: float = 0):
|
||||
center_lat, center_lon = center_coords
|
||||
if current_coords:
|
||||
current_lat, current_lon = current_coords
|
||||
else:
|
||||
current_lat, current_lon = center_lat, center_lon
|
||||
|
||||
direction = random.uniform(0, 2 * 3.14159)
|
||||
|
||||
speed_degrees = current_speed * DEGREES_PER_KM * (
|
||||
30 / 3600)
|
||||
lat_offset = speed_degrees * random.uniform(0.8, 1.2) * math.cos(
|
||||
direction)
|
||||
lon_offset = speed_degrees * random.uniform(0.8, 1.2) * math.sin(
|
||||
direction)
|
||||
|
||||
new_lat = current_lat + lat_offset
|
||||
new_lon = current_lon + lon_offset
|
||||
|
||||
new_lat = max(center_lat - 1, min(center_lat + 1, new_lat))
|
||||
new_lon = max(center_lon - 1, min(center_lon + 1, new_lon))
|
||||
|
||||
return new_lat, new_lon
|
||||
@ -3,6 +3,7 @@ import os
|
||||
from datetime import timedelta
|
||||
|
||||
from celery import Celery
|
||||
from django.conf import settings
|
||||
|
||||
# set the default Django settings module for the 'celery' program.
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mfssogr.settings")
|
||||
@ -25,37 +26,29 @@ def debug_task(self):
|
||||
|
||||
|
||||
app.conf.enable_utc = False
|
||||
app.conf.beat_schedule = {
|
||||
"apps.schedulers.tasks.load_earthquake": { # Получение данных по землятресениям
|
||||
schedule_dict = {
|
||||
"apps.schedulers.tasks.load_earthquake": {
|
||||
# Получение данных по землятресениям
|
||||
"task": "apps.schedulers.tasks.load_earthquake",
|
||||
"schedule": timedelta(
|
||||
minutes=30
|
||||
), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
),
|
||||
# change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
},
|
||||
"apps.schedulers.tasks.load_navigation": { # Получение данных по навигации
|
||||
"task": "apps.schedulers.tasks.load_navigation",
|
||||
"schedule": timedelta(
|
||||
seconds=60
|
||||
), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
),
|
||||
# change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
},
|
||||
#'apps.schedulers.tasks.update_block': {# Получение данных по блокировкам
|
||||
# 'task': 'apps.schedulers.tasks.update_block',
|
||||
# 'schedule': timedelta(seconds=10), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
# },
|
||||
#'apps.schedulers.tasks.update_ops_skpv_date': { # Получение данных со водоснабжение
|
||||
# 'task': 'apps.schedulers.tasks.update_ops_skpv_date',
|
||||
# 'schedule': timedelta(seconds=10), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
# },
|
||||
#'apps.schedulers.tasks.update_ops_ppz_date': { # Получение данных противопожарная защита
|
||||
# 'task': 'apps.schedulers.tasks.update_ops_ppz_date',
|
||||
# 'schedule': timedelta(seconds=10), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
# },
|
||||
#'apps.schedulers.tasks.update_ops_scada_date': { # Получение данных Системы контроля работы оборудования
|
||||
# 'task': 'apps.schedulers.tasks.update_ops_scada_date',
|
||||
# 'schedule': timedelta(seconds=10), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
# },
|
||||
#'apps.schedulers.tasks.update_eps': {
|
||||
# 'task': 'apps.schedulers.tasks.update_eps',
|
||||
# 'schedule': timedelta(minutes=5), # change to `crontab(minute=0, hour=0)` if you want it to run daily at midnight
|
||||
# },
|
||||
}
|
||||
if settings.TEST_CIRCUIT:
|
||||
schedule_dict["apps.schedulers.tasks.test_circuit"] = {
|
||||
"task": "apps.schedulers.tasks.test_circuit",
|
||||
"schedule": timedelta(
|
||||
seconds=30
|
||||
),
|
||||
}
|
||||
|
||||
app.conf.beat_schedule = schedule_dict
|
||||
|
||||
@ -22,10 +22,12 @@ STATIC_URL = "/static/"
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware", # Управление сессиями между запросами
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
# Управление сессиями между запросами
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware", # Связывает пользователей, использующих сессии, запросами.
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
# Связывает пользователей, использующих сессии, запросами.
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
@ -61,7 +63,6 @@ TEMPLATES = [
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
WSGI_APPLICATION = "mfssogr.wsgi.application"
|
||||
ASGI_APPLICATION = "mfssogr.wsgi.application"
|
||||
|
||||
@ -80,7 +81,6 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
|
||||
@ -97,7 +97,6 @@ LOGIN_URL = "login"
|
||||
LOGOUT_URL = "logout"
|
||||
LOGIN_REDIRECT_URL = "/"
|
||||
|
||||
|
||||
CELERY_RESULT_BACKEND = "django-db"
|
||||
CELERY_CACHE_BACKEND = "django-cache"
|
||||
|
||||
@ -109,6 +108,18 @@ ON_UPDATE_FUNCTION = "apps.util.version.print_update"
|
||||
|
||||
URL_BAUI = os.getenv("URL_BAUI", "/")
|
||||
|
||||
# Настройки тестового контур
|
||||
|
||||
TEST_CIRCUIT = bool(int(os.getenv("TEST_CIRCUIT", "0")))
|
||||
|
||||
if TEST_CIRCUIT:
|
||||
TEST_CIRCUIT_EQUIPMENT_COUNT = int(os.getenv(
|
||||
"TEST_CIRCUIT_EQUIPMENT_COUNT", "5"
|
||||
))
|
||||
TEST_CIRCUIT_WORKER_COUNT = int(os.getenv(
|
||||
"TEST_CIRCUIT_WORKER_COUNT", "5"
|
||||
))
|
||||
|
||||
from .app_settings import *
|
||||
|
||||
try:
|
||||
|
||||
@ -44,7 +44,7 @@ if settings.DEBUG:
|
||||
|
||||
settingsmodule.settings_install()
|
||||
|
||||
tasks.load_earthquake()
|
||||
tasks.load_navigation()
|
||||
# tasks.load_earthquake()
|
||||
# tasks.load_navigation()
|
||||
|
||||
Update().ready()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user