227 lines
9.0 KiB
Python
227 lines
9.0 KiB
Python
# coding: gb2312
|
|
# 代码编写人:曾忠和
|
|
import threading
|
|
import time
|
|
import traceback
|
|
from copy import deepcopy
|
|
|
|
from LogRecord.log_recorder import GLOBAL_LOG
|
|
from PureBackend.base_driver import MODEL_MEDIAPIPE
|
|
from PureBackend.healthy_backend import HealthyBackend
|
|
from PureBackend.network_driver import NetworkDriver
|
|
from PureBackend.synchronization import Synchronization
|
|
from PureBackend.general import *
|
|
from AcrossPlatform.get_platform import *
|
|
|
|
|
|
class ExamDriver(NetworkDriver, Synchronization, HealthyBackend):
|
|
|
|
def __init__(self, master_mode=True, positioning=True, camera=True, model=MODEL_MEDIAPIPE, speaker=True,
|
|
multi_positioning_mode=True, device_type=UNKNOWN, pure_mode=False):
|
|
super().__init__(master_mode, positioning, camera, model, speaker, multi_positioning_mode, device_type, pure_mode)
|
|
# 当前作训项目
|
|
self.project = None
|
|
self.exercise_tag = ""
|
|
self.running = None
|
|
self.running_mes = {}
|
|
# 实时展示
|
|
# 初始化传送管理端的成绩
|
|
self.synchronization_info = {}
|
|
self.statistics_score = {SITUP: [], RUNAROUND: [], PUSHUP: [], PULLUP: [], RUNNING: [], 'total': [],
|
|
'top': {'total': {}, SITUP: {}, RUNAROUND: {}, PULLUP: {}, PUSHUP: {}, RUNNING: {}}}
|
|
self._statistics_score = {}
|
|
self._statistics_sql_score = {}
|
|
self._sql_score = {}
|
|
self.hrbo_info = {}
|
|
self._hrbo_info = {}
|
|
self.closed_chamber = {}
|
|
self.tag_mes = {HR: None, BO: None}
|
|
|
|
self.send_score_signal = threading.Event()
|
|
self.send_score_signal.set()
|
|
self.send_sql_score_signal = threading.Event()
|
|
|
|
threading.Thread(target=self.send_score).start()
|
|
threading.Thread(target=self.send_sql_score).start()
|
|
|
|
# 开始进行作训
|
|
def start_exercise(self):
|
|
try:
|
|
self.reset_hr_bo_record()
|
|
self.camera.start_record()
|
|
video_name = self.project.info[ID] + '_' + self.project.info[
|
|
"name"] + '_' + self.project.exercise_type + '_' + 'unknow'
|
|
# 开始屏幕录制
|
|
if SYS_PLATFORM == WINDOWS:
|
|
self.video_recorder.write_start(video_name)
|
|
self.project.start()
|
|
# self.project.waiting_for_start()
|
|
# 清空传送管理端的成绩
|
|
self.synchronization_info = {}
|
|
self.tag_mes = {HR: None, BO: None}
|
|
return True
|
|
except Exception as e:
|
|
GLOBAL_LOG.write(f"开始进行作训发生错误,{str(e)},错误来源:{traceback.format_exc()}",need_print=True)
|
|
return None
|
|
|
|
# 强制结束作训测试
|
|
def stop_exercise(self):
|
|
try:
|
|
if self.project:
|
|
info = self.project.get_info()
|
|
else:
|
|
info = None
|
|
self.camera.stop_record()
|
|
self.camera.clear_cache()
|
|
self.project.kill()
|
|
result = self.project.get_result()
|
|
score = result["score"]
|
|
if score == '不合格':
|
|
real_score = 0
|
|
else:
|
|
real_score = float(score)
|
|
video_name = self.project.info[ID] + '_' + self.project.info[
|
|
"name"] + '_' + self.project.exercise_type + '_' + str(real_score)
|
|
if SYS_PLATFORM == WINDOWS:
|
|
# 结束屏幕录制
|
|
self.video_recorder.write_stop(video_name)
|
|
self.synchronization_info[self.exercise_tag] = {}
|
|
self.send_score_signal.set()
|
|
return info
|
|
except Exception as e:
|
|
GLOBAL_LOG.write(f"强制结束作训测试发生错误,{str(e)},错误来源:{traceback.format_exc()}")
|
|
return None
|
|
|
|
# 获取作训成绩
|
|
def get_score(self):
|
|
try:
|
|
score_result = self.project.get_result()
|
|
band_id = self.project.info.get("band_id")
|
|
tag_mes = self.positioning.get_tag_mes(band_id)
|
|
temp = {}
|
|
if tag_mes:
|
|
self.update_hr_bo_record(tag_mes)
|
|
temp['hr'] = tag_mes[HR]
|
|
temp['bo'] = "{:.2%}".format(tag_mes[BO] / 100)
|
|
if tag_mes[HR] >= 190 or tag_mes[BO] <= 80:
|
|
temp['normal'] = False
|
|
else:
|
|
temp['normal'] = True
|
|
self.tag_mes = {HR: temp['hr'], BO: temp['bo']}
|
|
else:
|
|
if self.tag_mes[HR] and self.tag_mes[BO]:
|
|
tag_mes = self.tag_mes
|
|
else:
|
|
tag_mes = {HR: '-', BO: '-'}
|
|
temp['hr'] = tag_mes[HR]
|
|
temp['bo'] = tag_mes[BO]
|
|
temp['normal'] = True
|
|
# # 测试数据
|
|
# tag_mes = {'hr': 90, 'bo': 88}
|
|
# self.update_hr_bo_record(tag_mes)
|
|
# 更新hrbo
|
|
score_result.update(self.hr_bo_record)
|
|
|
|
temp['name'] = self.project.info[NAME]
|
|
temp['id'] = self.project.info[ID]
|
|
temp['count'] = score_result['count']
|
|
self.synchronization_info[self.exercise_tag] = temp
|
|
self.send_score_signal.set()
|
|
return score_result
|
|
except Exception as e:
|
|
GLOBAL_LOG.write(f"获取作训成绩出现错误,错误原因:{e.args}, "f"{traceback.format_exc()}", need_print=True)
|
|
return None
|
|
|
|
# 获取跑步成绩
|
|
def running_score(self):
|
|
try:
|
|
raw_score = self.running.get_score()
|
|
final_score = []
|
|
# 对初始成绩进行排序
|
|
if raw_score:
|
|
raw_score = sorted(raw_score,
|
|
key=lambda x: (-x["score"], -x["round"], x["band_id"], ["total_time"]))
|
|
rank = 0
|
|
last_total_time = -1
|
|
for score in raw_score:
|
|
if score["total_time"] != last_total_time:
|
|
rank += 1
|
|
last_total_time = score["total_time"]
|
|
full_mes = deepcopy(score)
|
|
band_id = score["band_id"]
|
|
tag_mes = self.positioning.get_tag_status(band_id)
|
|
person_mes = self.running_mes[band_id]
|
|
full_mes.update(person_mes)
|
|
full_mes.update({
|
|
"rank": rank
|
|
})
|
|
full_mes.update(tag_mes)
|
|
final_score.append(full_mes)
|
|
temp = []
|
|
for row in final_score:
|
|
if row[HR] != 0 and row[BO] != 0:
|
|
hr = row[HR]
|
|
bo = "{:.2%}".format(row[BO] / 100)
|
|
if row[HR] >= 220 or row[BO] < 80:
|
|
normal = False
|
|
else:
|
|
normal = True
|
|
else:
|
|
hr = '-'
|
|
bo = '-'
|
|
normal = True
|
|
temp.append(
|
|
{'id': row['id'], 'name': row['name'], 'hr': hr, 'bo': bo,
|
|
'rank': row['rank'], 'finish': row['finish'], 'round': row['round'],
|
|
'count': row['total_time'], 'normal': normal})
|
|
self.synchronization_info[RUNNING] = temp
|
|
self.send_score_signal.set()
|
|
return final_score
|
|
except Exception as e:
|
|
GLOBAL_LOG.write(f"获取跑步成绩出现错误,错误原因:{e.args}, "f"{traceback.format_exc()}", need_print=True)
|
|
return None
|
|
|
|
# 实时大屏数据
|
|
# 向管理端发送成绩
|
|
def send_score(self):
|
|
while True:
|
|
try:
|
|
self.send_score_signal.wait()
|
|
self.send_score_signal.clear()
|
|
manager_ip_list = self.connection.get_manager()
|
|
ip = self.connection.get_self_ip()
|
|
if manager_ip_list:
|
|
self.synchronization_info['time'] = time.time()
|
|
address_list = []
|
|
pkg_list = []
|
|
for manage_ip in manager_ip_list:
|
|
address_list.append((manage_ip, server_port))
|
|
pkg_list.append(
|
|
[{'data': {DATA: self.synchronization_info, 'ip': ip}, 'data_type': 'totals_data'}])
|
|
self.client.distributed_send(address_list, pkg_list)
|
|
except Exception:
|
|
continue
|
|
|
|
# 向管理端发送数据库成绩
|
|
def send_sql_score(self):
|
|
while True:
|
|
try:
|
|
self.send_sql_score_signal.wait()
|
|
self.send_sql_score_signal.clear()
|
|
manager_ip_list = self.connection.get_manager()
|
|
ip = self.connection.get_self_ip()
|
|
_ip = ip.split('.')[2]
|
|
if manager_ip_list:
|
|
address_list = []
|
|
pkg_list = []
|
|
for manage_ip in manager_ip_list:
|
|
address_list.append((manage_ip, server_port))
|
|
pkg_list.append(
|
|
[{'data': {DATA: self._statistics_sql_score, 'ip': ip},
|
|
'data_type': 'totals_sql_data'}])
|
|
self.client.distributed_send(address_list, pkg_list)
|
|
self._statistics_sql_score = {}
|
|
except Exception as e:
|
|
# print(traceback.format_exc())
|
|
continue
|