first commit
This commit is contained in:
5
code/system/apis/vdes/__init__.py
Normal file
5
code/system/apis/vdes/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/5/9 23:26
|
||||
# @Author : 臧成龙
|
||||
# @FileName: __init__.py.py
|
||||
# @Software: PyCharm
|
||||
5
code/system/apis/vdes/chat/__init__.py
Normal file
5
code/system/apis/vdes/chat/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time : 2022/5/9 23:26
|
||||
# @Author : 臧成龙
|
||||
# @FileName: __init__.py.py
|
||||
# @Software: PyCharm
|
||||
25
code/system/apis/vdes/chat/common.py
Normal file
25
code/system/apis/vdes/chat/common.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from ninja import Router
|
||||
from ninja import Router, UploadedFile, File
|
||||
import os
|
||||
from fuadmin import settings
|
||||
|
||||
router = Router()
|
||||
|
||||
# @router.get("/uploadFile")
|
||||
# def upload_file(request, files: list[UploadedFile] = File(...)):
|
||||
# file_list = []
|
||||
# upload_dir = os.path.join(settings.BASE_DIR, 'upload')
|
||||
# os.makedirs(upload_dir, exist_ok=True)
|
||||
|
||||
# for file in files:
|
||||
# new_path = os.path.join(upload_dir, file.name)
|
||||
# with open(new_path, 'wb+') as destination:
|
||||
# for chunk in file.chunks():
|
||||
# destination.write(chunk)
|
||||
# file_list.append({
|
||||
# 'name': file.name,
|
||||
# 'path': new_path,
|
||||
# 'size': file.size
|
||||
# })
|
||||
|
||||
# return file_list
|
||||
102
code/system/apis/vdes/chat/contacts.py
Normal file
102
code/system/apis/vdes/chat/contacts.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from system.models import Contacts, HistorySession
|
||||
from ninja import Router, Schema, Field
|
||||
|
||||
router = Router()
|
||||
|
||||
class addContactSchema(Schema):
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
name: str = Field(None, alias="name")
|
||||
phone: str = Field(None, alias="phone")
|
||||
region: str = Field(None, alias="region")
|
||||
|
||||
class deleteContactSchema(Schema):
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
|
||||
class editContactSchema(Schema):
|
||||
oldMMSI: str = Field(None, alias="old_mmsi")
|
||||
newMMSI: str = Field(None, alias="new_mmsi")
|
||||
name: str = Field(None, alias="name")
|
||||
phone: str = Field(None, alias="phone")
|
||||
region: str = Field(None, alias="region")
|
||||
|
||||
|
||||
@router.get('/contactsList')
|
||||
def get_contacts_list(request):
|
||||
try:
|
||||
contacts = Contacts.objects.all().values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/deleteContact')
|
||||
def delete_contact(request, data: deleteContactSchema):
|
||||
try:
|
||||
if not data.MMSI:
|
||||
return {'code': 400, 'message': 'MMSI不能为空'}
|
||||
contact = Contacts.objects.filter(MMSI=data.MMSI).first()
|
||||
if not contact:
|
||||
return {'code': 404, 'message': '未找到对应联系人'}
|
||||
# 删除关联的历史会话记录
|
||||
HistorySession.objects.filter(FormId=contact.Id).delete()
|
||||
contact.delete()
|
||||
return {'code': 200, 'message': '删除成功'}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
@router.post('/editContact')
|
||||
def edit_contact(request, data: editContactSchema):
|
||||
try:
|
||||
if not data.oldMMSI:
|
||||
return {'code': 400, 'message': '原MMSI不能为空'}
|
||||
|
||||
contact = Contacts.objects.filter(MMSI=data.oldMMSI).first()
|
||||
if not contact:
|
||||
return {'code': 404, 'message': '未找到对应联系人'}
|
||||
# 更新联系人信息
|
||||
contact.MMSI = data.newMMSI if data.newMMSI else contact.MMSI
|
||||
contact.Name = data.name if data.name else contact.Name
|
||||
contact.Mobile = data.phone if data.phone else contact.Mobile
|
||||
contact.Region = data.region if data.region else contact.Region
|
||||
contact.save()
|
||||
|
||||
# 更新关联的历史会话记录
|
||||
if data.newMMSI or data.name:
|
||||
HistorySession.objects.filter(FormId=contact.Id).update(
|
||||
MMSI=data.newMMSI if data.newMMSI else contact.MMSI,
|
||||
Name=data.name if data.name else contact.Name
|
||||
)
|
||||
|
||||
return {
|
||||
'code': 200,
|
||||
'message': '修改成功',
|
||||
'data': {
|
||||
'id': contact.Id,
|
||||
'MMSI': contact.MMSI
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/addContact')
|
||||
def add_contact(request, data: addContactSchema):
|
||||
try:
|
||||
if not data.MMSI:
|
||||
return {'code': 400, 'message': 'MMSI不能为空'}
|
||||
name = data.name if data.name else data.MMSI
|
||||
contact = Contacts.objects.create(
|
||||
MMSI=data.MMSI,
|
||||
Name=name,
|
||||
Mobile=data.phone,
|
||||
Region=data.region
|
||||
)
|
||||
return {
|
||||
'code': 200,
|
||||
'message': '成功',
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
53
code/system/apis/vdes/chat/content.py
Normal file
53
code/system/apis/vdes/chat/content.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from system.models import Content
|
||||
from ninja import Router, Schema, Field
|
||||
from datetime import datetime
|
||||
from vdes_utils.vdes_message_packaged import VdesMessagePackaged
|
||||
|
||||
router = Router()
|
||||
|
||||
@router.get('/getContent')
|
||||
def get_content(request):
|
||||
try:
|
||||
contacts = Content.objects.all().order_by('Id').values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
@router.post('/sendMessage')
|
||||
def send_message(request):
|
||||
# try:
|
||||
# 解析JSON数据
|
||||
import json
|
||||
data = json.loads(request.body)
|
||||
MMSI = data['mmsi']
|
||||
# 创建并保存Content记录
|
||||
content = Content.objects.create(
|
||||
SendId=data['SendId'],
|
||||
ReciverId=data['ReciverId'],
|
||||
Content=data['Content'],
|
||||
Type=data['Type'],
|
||||
# State=data['State'],
|
||||
State=1, # 设置为已发送状态
|
||||
NoCode=data['NoCode'],
|
||||
CreateDateUtc=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
||||
Title=data['Title'],
|
||||
Description=data['Description'],
|
||||
Label=data['Label'],
|
||||
Thumbnail=data['Thumbnail'],
|
||||
ReadFlag=data['ReadFlag'],
|
||||
Avatar=data['Avatar'],
|
||||
SoundStatus=0,
|
||||
|
||||
)
|
||||
message = data['Content'].replace('<p>', '').replace('</p>', '')
|
||||
# message = "这是一条长消息测试消息1111111111111111111111111111111111111111111111111111111"
|
||||
vdes_message_packaged = VdesMessagePackaged(message, MMSI)
|
||||
vdes_message_packaged.send_message()
|
||||
|
||||
return { "code": 200, "message": "消息保存成功" }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
50
code/system/apis/vdes/chat/historySession.py
Normal file
50
code/system/apis/vdes/chat/historySession.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from system.models import HistorySession
|
||||
from ninja import Router, Schema, Field
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
class HistorySessionSchema(Schema):
|
||||
FormId: str = Field(None, alias="formId")
|
||||
Name: str = Field(None, alias="name")
|
||||
MMSI: str = Field(None, alias="mmsi")
|
||||
Mobile: str = Field(None, alias="mobile")
|
||||
Region: str = Field(None, alias="region")
|
||||
ById: str = Field(None, alias="byId")
|
||||
|
||||
|
||||
@router.get('/getHistorySession')
|
||||
def get_history_session(request):
|
||||
try:
|
||||
contacts = HistorySession.objects.all().values()
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
"data": list(contacts)
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
@router.post('/insertHistorySession')
|
||||
def insert_history_session(request, data: HistorySessionSchema):
|
||||
try:
|
||||
existing_record = HistorySession.objects.filter(
|
||||
FormId=data.FormId,
|
||||
ById=data.ById
|
||||
).first()
|
||||
if not existing_record:
|
||||
history_session = HistorySession.objects.create(
|
||||
FormId=data.FormId,
|
||||
Name=data.Name,
|
||||
MMSI=data.MMSI,
|
||||
Mobile=data.Mobile,
|
||||
Region=data.Region,
|
||||
ById=data.ById
|
||||
)
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "成功",
|
||||
}
|
||||
except Exception as e:
|
||||
return {'code': 500, 'message': str(e)}
|
||||
50
code/system/apis/vdes/chat/historysession_bk.py
Normal file
50
code/system/apis/vdes/chat/historysession_bk.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# from system.models import HistorySession
|
||||
# from ninja import Router, Schema, Field
|
||||
|
||||
# router = Router()
|
||||
|
||||
|
||||
# class HistorySessionSchema(Schema):
|
||||
# FormId: str = Field(None, alias="formId")
|
||||
# Name: str = Field(None, alias="name")
|
||||
# MMSI: str = Field(None, alias="mmsi")
|
||||
# Mobile: str = Field(None, alias="mobile")
|
||||
# Region: str = Field(None, alias="region")
|
||||
# ById: str = Field(None, alias="byId")
|
||||
|
||||
|
||||
# @router.get('/getHistorySession')
|
||||
# def get_history_session(request):
|
||||
# try:
|
||||
# contacts = HistorySession.objects.all().values()
|
||||
# return {
|
||||
# "code": 200,
|
||||
# "message": "成功",
|
||||
# "data": list(contacts)
|
||||
# }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
|
||||
|
||||
# @router.post('/insertHistorySession')
|
||||
# def insert_history_session(request, data: HistorySessionSchema):
|
||||
# try:
|
||||
# existing_record = HistorySession.objects.filter(
|
||||
# FormId=data.FormId,
|
||||
# ById=data.ById
|
||||
# ).first()
|
||||
# if not existing_record:
|
||||
# history_session = HistorySession.objects.create(
|
||||
# FormId=data.FormId,
|
||||
# Name=data.Name,
|
||||
# MMSI=data.MMSI,
|
||||
# Mobile=data.Mobile,
|
||||
# Region=data.Region,
|
||||
# ById=data.ById
|
||||
# )
|
||||
# return {
|
||||
# "code": 200,
|
||||
# "message": "成功",
|
||||
# }
|
||||
# except Exception as e:
|
||||
# return {'code': 500, 'message': str(e)}
|
||||
151
code/system/apis/vdes/header.py
Normal file
151
code/system/apis/vdes/header.py
Normal file
@@ -0,0 +1,151 @@
|
||||
import random
|
||||
import math
|
||||
from ninja import Router
|
||||
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
from system.models import GpsStatus, ShipLatestStatus, MessageFlag
|
||||
from fuadmin.settings import OWN_SHIP_MMSI
|
||||
|
||||
|
||||
router = Router()
|
||||
|
||||
def haversine(lat1, lon1, lat2, lon2):
|
||||
"""计算两点间的大圆距离(公里)"""
|
||||
R = 6371 # 地球半径(公里)
|
||||
|
||||
# 将角度转换为弧度
|
||||
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
|
||||
|
||||
# 差值
|
||||
dlat = lat2 - lat1
|
||||
dlon = lon2 - lon1
|
||||
|
||||
# Haversine公式
|
||||
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
|
||||
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
|
||||
|
||||
return R * c
|
||||
|
||||
|
||||
def calculate_relative_bearing(own_lat, own_lon, own_course, target_lat, target_lon):
|
||||
"""
|
||||
计算目标点相对本船航向的方向角(相对方位,单位°,0~360,顺时针)
|
||||
"""
|
||||
d_lon = math.radians(target_lon - own_lon)
|
||||
lat1 = math.radians(own_lat)
|
||||
lat2 = math.radians(target_lat)
|
||||
|
||||
x = math.sin(d_lon) * math.cos(lat2)
|
||||
y = math.cos(lat1)*math.sin(lat2) - math.sin(lat1)*math.cos(lat2)*math.cos(d_lon)
|
||||
initial_bearing = math.atan2(x, y)
|
||||
initial_bearing_deg = (math.degrees(initial_bearing) + 360) % 360 # 转换为 0~360 度
|
||||
|
||||
# 相对航向(目标相对于本船正前的角度)
|
||||
relative_bearing = (initial_bearing_deg - own_course + 360) % 360
|
||||
return relative_bearing
|
||||
|
||||
|
||||
@router.get("/header")
|
||||
def get_header_info(request):
|
||||
# 固定本船MMSI
|
||||
ownship_mmsi = OWN_SHIP_MMSI
|
||||
# current_time = datetime.now()
|
||||
# current_date = current_time.strftime('%Y-%m-%d')
|
||||
# current_time_str = current_time.strftime('%H:%M')
|
||||
|
||||
# # 检查是否是第一次调用,如果是则初始化位置
|
||||
# if not hasattr(get_header_info, 'ship_data'):
|
||||
# # 初始位置 (30.9918, 122.2788)
|
||||
# get_header_info.ship_data = {
|
||||
# 'mmsi': ownship_mmsi,
|
||||
# 'latitude': 30.9918,
|
||||
# 'longitude': 122.2788,
|
||||
# 'speed': 12.0,
|
||||
# 'course': 45,
|
||||
# 'date': current_date,
|
||||
# 'time': current_time_str
|
||||
# }
|
||||
# else:
|
||||
# # 更新位置和航向
|
||||
# get_header_info.ship_data['latitude'] = round(get_header_info.ship_data['latitude'] + 0.001, 4)
|
||||
# get_header_info.ship_data['longitude'] = round(get_header_info.ship_data['longitude'] + 0.001, 4)
|
||||
# get_header_info.ship_data['course'] = round((get_header_info.ship_data['course'] + 5) % 360, 1)
|
||||
# get_header_info.ship_data['date'] = current_date
|
||||
# get_header_info.ship_data['time'] = current_time_str
|
||||
|
||||
# # 将数据保存到GpsStatus模型
|
||||
# GpsStatus.objects.create(
|
||||
# utc_time=current_time,
|
||||
# longitude=get_header_info.ship_data['longitude'],
|
||||
# latitude=get_header_info.ship_data['latitude'],
|
||||
# altitude=0.0, # 固定值
|
||||
# used_satellites=10, # 固定值
|
||||
# gps_satellites=6, # 固定值
|
||||
# beidou_satellites=4, # 固定值
|
||||
# gps_status=1, # 固定值,1表示正常
|
||||
# fix_mode=3, # 固定值,3表示3D定位
|
||||
# hdop=1.2, # 固定值
|
||||
# speed=get_header_info.ship_data['speed'],
|
||||
# direction=get_header_info.ship_data['course']
|
||||
# )
|
||||
|
||||
#查询最新经纬度数据
|
||||
latest_gps = GpsStatus.objects.order_by('-utc_time').first()
|
||||
|
||||
# 计算 10 分钟前的时间,基于 GpsStatus 中的时间
|
||||
ten_minutes_ago = latest_gps.utc_time - timedelta(minutes=10)
|
||||
# 筛选出 10 分钟内的船只记录
|
||||
recent_ships = ShipLatestStatus.objects.filter(timestamp__gte=ten_minutes_ago)
|
||||
closest_ship = None
|
||||
min_distance_km = float('inf')
|
||||
for ship in recent_ships:
|
||||
# 计算本船与当前船只的距离(公里)
|
||||
distance_km = haversine(latest_gps.latitude, latest_gps.longitude, ship.latitude, ship.longitude)
|
||||
if distance_km < min_distance_km:
|
||||
min_distance_km = distance_km
|
||||
closest_ship = ship
|
||||
# 判断是否小于 5 海里(1 海里 = 1.852 公里)
|
||||
warning_message = ''
|
||||
# min_distance_km = random.randint(1, 20)
|
||||
print(min_distance_km)
|
||||
if 3 * 1.852 < min_distance_km < 5 * 1.852:
|
||||
warning_message = '注意,5海里范围内有其他船只'
|
||||
print(warning_message)
|
||||
elif min_distance_km <= 3 * 1.852:
|
||||
relative_bearing = calculate_relative_bearing(
|
||||
latest_gps.latitude,
|
||||
latest_gps.longitude,
|
||||
latest_gps.direction, # 本船航向
|
||||
closest_ship.latitude,
|
||||
closest_ship.longitude
|
||||
)
|
||||
warning_message = f'警告:有船靠近,相对方位 {relative_bearing:.1f}°,相距 {min_distance_km / 1.852:.2f} 海里, 速度{closest_ship.speed}节'
|
||||
else:
|
||||
warning_message = '保持正常航行'
|
||||
flag = MessageFlag.objects.first()
|
||||
has_message = flag.has_new_message
|
||||
if has_message:
|
||||
flag.has_new_message = False
|
||||
flag.save()
|
||||
return {
|
||||
'mmsi': ownship_mmsi, # 固定本船MMSI
|
||||
'latitude': latest_gps.latitude,
|
||||
'longitude': latest_gps.longitude,
|
||||
'speed': latest_gps.speed,
|
||||
'course': latest_gps.direction,
|
||||
'date': latest_gps.utc_time.strftime('%Y-%m-%d'),
|
||||
'time': latest_gps.utc_time.strftime('%H:%M'),
|
||||
"satelliteCount": random.randint(0, 12),
|
||||
"messageCount": 12,
|
||||
"reminderCount": random.randint(0, 20),
|
||||
"alertCount": random.randint(0, 15),
|
||||
"approachingCount": random.randint(0, 5),
|
||||
"trackStatus": False,
|
||||
"gpsSignal": round(random.uniform(0, 5), 1),
|
||||
"beidouSignal": round(random.uniform(0, 5), 1),
|
||||
"vdesSignal": round(random.uniform(0, 5), 1),
|
||||
"warning_message": warning_message,
|
||||
"is_receive_vdes_data": has_message
|
||||
}
|
||||
# return get_header_info.ship_data
|
||||
Reference in New Issue
Block a user