k5sat/app.py

78 lines
2.7 KiB
Python
Raw Normal View History

2024-02-04 14:30:25 +00:00
import head
from datetime import datetime, timedelta
2024-02-25 06:47:44 +00:00
from flask import Flask, request, jsonify
2024-02-04 14:30:25 +00:00
from flask_cors import cross_origin
import ephem
2024-02-25 06:47:44 +00:00
import redis
import uuid
2024-02-04 14:30:25 +00:00
app = Flask(__name__)
2024-02-25 06:47:44 +00:00
r = redis.Redis(host='127.0.0.1', port=6379, db=0)
2024-02-04 14:30:25 +00:00
def local2utc(local_dtm):
# 本地时间转 UTC 时间( -8:00
return datetime.utcfromtimestamp(local_dtm.timestamp())
2024-02-25 08:08:17 +00:00
@app.route("/lol", methods=['POST'])
2024-02-12 06:47:01 +00:00
@cross_origin()
2024-02-25 06:47:44 +00:00
def lol():
func = request.json.get('func', 0)
_uuid = request.json.get('uuid', str(uuid.uuid4()))
_cache = request.json.get('cache', '')
if func == 0:
r.set(_uuid, _cache, 600)
else:
2024-02-25 08:12:24 +00:00
_cache = r.get(_uuid)
2024-02-25 06:47:44 +00:00
return jsonify({'code': 200, 'uuid': _uuid, 'cache': _cache})
2024-02-04 14:30:25 +00:00
@app.route("/pass", methods=['POST'])
@cross_origin()
def satpass():
sat = request.json.get('sat', '')
sat_line_1 = request.json.get('sat_line_1', '')
sat_line_2 = request.json.get('sat_line_2', '')
lat = request.json.get('lat', 0)
lng = request.json.get('lng', 0)
alt = request.json.get('alt', 0)
target_satellite, find_flag = head.FIND_SATE(sat_line_1, sat_line_2, sat)
pass_times, departure_times = head.CAL_PASS_TIME(target_satellite,
float(lat), float(lng),
float(alt))
return jsonify({
'code': 200,
'pass_times': pass_times,
'departure_times': departure_times
})
@app.route("/doppler", methods=['POST'])
@cross_origin()
def doppler():
sat = request.json.get('sat', '')
sat_line_1 = request.json.get('sat_line_1', '')
sat_line_2 = request.json.get('sat_line_2', '')
lat = request.json.get('lat', 0)
lng = request.json.get('lng', 0)
alt = request.json.get('alt', 0)
tx = request.json.get('tx', 0)
rx = request.json.get('rx', 0)
format = "%Y-%m-%d %H:%M:%S"
pass_time = datetime.strptime(request.json.get('pass_time', ''), format)
departure_time = datetime.strptime(request.json.get('departure_time', ''),
format)
satellite = ephem.readtle(sat, sat_line_1, sat_line_2)
2024-02-04 16:19:49 +00:00
# print(str(pass_time) + " " + str(local2utc(pass_time)))
2024-02-04 14:30:25 +00:00
shift_array = []
2024-02-04 16:22:06 +00:00
while pass_time < departure_time + timedelta(seconds=1):
2024-02-04 14:30:25 +00:00
AZ, EI, SHITF_UP, SHIFT_DOWN, DIS = head.CAL_DATA(
satellite, sat_line_1, sat_line_2, float(lng), float(lat),
float(alt), local2utc(pass_time),
float(tx) * 1000000,
float(rx) * 1000000)
shift_array.append([SHITF_UP, SHIFT_DOWN])
2024-02-04 16:22:06 +00:00
pass_time = pass_time + timedelta(seconds=1)
2024-02-04 14:30:25 +00:00
return jsonify({'code': 200, 'shift_array': shift_array})