151 lines
4.5 KiB
Python
151 lines
4.5 KiB
Python
|
|
import time
|
|
from apps.ecm.service import check_not_in_place
|
|
from apps.ecm.tasks import opl_task
|
|
from apps.opm.models import Operation, Opl, OplWorker
|
|
from apps.third.models import TDevice
|
|
from apps.utils.sms import send_sms
|
|
from apps.wf.models import Ticket, Transition
|
|
from django_celery_results.models import TaskResult
|
|
|
|
|
|
def get_op_charger(state, ticket, new_ticket_data, handler):
|
|
"""_summary_
|
|
|
|
Args:
|
|
state (_type_): 工作流节点实例
|
|
ticket (_type_): 工单实例
|
|
new_ticket_data (_type_): 提交的工单数据
|
|
handler (_type_): 处理人实例
|
|
"""
|
|
opl = Opl.objects.filter(ticket=ticket).first()
|
|
if opl:
|
|
return [opl.charger.id]
|
|
|
|
|
|
def get_op_workers(state, ticket, new_ticket_data, handler):
|
|
opl = Opl.objects.filter(ticket=ticket).first()
|
|
if opl:
|
|
return list(OplWorker.objects.filter(opl=opl).values_list('worker__id', flat=True))
|
|
|
|
|
|
def get_op_monitor(state, ticket, new_ticket_data, handler):
|
|
opl = Opl.objects.filter(ticket=ticket).first()
|
|
if opl:
|
|
return [opl.monitor.id]
|
|
|
|
|
|
def bind_opl(ticket: Ticket, transition: Transition, new_ticket_data: dict):
|
|
opl = Opl.objects.get(id=new_ticket_data['opl'])
|
|
ticket_data = ticket.ticket_data
|
|
ticket_data.update({
|
|
'operation': opl.operation.id, # operation id值
|
|
'level': opl.level,
|
|
'power_days': opl.power_days,
|
|
'work_scope': opl.work_scope,
|
|
'monitor': opl.monitor.id,
|
|
'workers': list(OplWorker.objects.filter(opl=opl).values_list('worker__id', flat=True)),
|
|
'charger': opl.charger.id,
|
|
'dept_ter': opl.operation.dept_ter.id,
|
|
'dept_bus': opl.operation.dept_bus.id
|
|
})
|
|
ticket.ticket_data = ticket_data
|
|
ticket.create_by = opl.create_by
|
|
ticket.save()
|
|
opl.ticket = ticket
|
|
opl.number = ticket.sn
|
|
opl.save()
|
|
op = opl.operation
|
|
if op.state == Operation.OP_CREATE:
|
|
op.state = Operation.OP_AUDIT
|
|
op.save()
|
|
|
|
|
|
def t_submit_close_mtask(ticket: Ticket, transition: Transition, new_ticket_data: dict):
|
|
# 提交作业关闭时关闭作业监控
|
|
opl = Opl.objects.filter(ticket=ticket).first()
|
|
if opl and opl.mtask_uid:
|
|
close_mtask(opl.mtask_uid)
|
|
# 防止没变化,手动处理一下
|
|
|
|
|
|
def opl_audit_end(ticket: Ticket):
|
|
# 任务执行1
|
|
opl = Opl.objects.get(ticket=ticket)
|
|
|
|
op = opl.operation
|
|
if op.state == Operation.OP_AUDIT:
|
|
op.state = Operation.OP_WAIT
|
|
op.save()
|
|
|
|
# 授予相关工作人员区域进入权限
|
|
# worker_ep_ids = list(OplWorker.objects.filter(opl=opl).values_list('worker__id', flat=True))
|
|
# 发送通知
|
|
phone = opl.create_by.phone if opl.create_by else None
|
|
if phone:
|
|
send_sms(phone=phone, template_code=1005, template_param={'name': ticket.workflow.name})
|
|
|
|
|
|
def opl_start(ticket: Ticket):
|
|
# 任务执行2 开始许可证作业
|
|
opl = Opl.objects.get(ticket=ticket)
|
|
|
|
op = opl.operation
|
|
if op.state == Operation.OP_WAIT:
|
|
op.state = Operation.OP_WORK
|
|
op.save()
|
|
|
|
# 检查作业人员是否就位
|
|
check_not_in_place(opl)
|
|
|
|
# 给摄像头加载循环拍照算法
|
|
start_mtask(opl)
|
|
|
|
|
|
def start_mtask(opl: Opl):
|
|
if opl.mtask_uid:
|
|
# 先关闭
|
|
close_mtask(opl.mtask_uid)
|
|
opl.mtask_uid = None
|
|
opl.save()
|
|
op = opl.operation
|
|
mtask_uid = None
|
|
vc_codes = []
|
|
# 找到作业点的摄像头, 如果指定摄像头就用指定的摄像头
|
|
if op.vchannels:
|
|
vc_codes = list(op.vchannels.all().values_list('code', flat=True))
|
|
opl_id = opl.id
|
|
task = opl_task.delay(vc_codes, opl_id)
|
|
mtask_uid = task.id
|
|
opl.mtask_uid = mtask_uid
|
|
opl.save()
|
|
return dict(vc_codes=vc_codes, mtask_uid=mtask_uid)
|
|
|
|
|
|
def close_mtask(task_id: str):
|
|
"""关闭celery任务
|
|
"""
|
|
from celery.app.control import Control
|
|
from server.celery import app
|
|
celery_control = Control(app=app)
|
|
celery_control.revoke(task_id, terminate=True)
|
|
|
|
|
|
def opl_end(ticket: Ticket):
|
|
"""
|
|
作业许可证关闭时执行
|
|
"""
|
|
opl = ticket.opl
|
|
if opl.mtask_uid:
|
|
# 关闭作业视频监控任务
|
|
close_mtask(opl.mtask_uid)
|
|
operation = opl.operation
|
|
opls = Opl.objects.filter(operation=operation)
|
|
opls.filter(ticket=None).delete() # 删除无用许可证
|
|
states = opls.values_list('ticket__state__type', flat=True)
|
|
if 0 in states or 1 in states: # 查看工单状态
|
|
pass
|
|
else:
|
|
operation.state = Operation.OP_DONE
|
|
operation.save()
|