89 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			89 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Python
		
	
	
	
| from .models import Equipment
 | |
| from apps.system.models import Dept
 | |
| from apps.mtm.models import Mgroup
 | |
| import datetime
 | |
| from .models import RuningState
 | |
| 
 | |
| 
 | |
| def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs):
 | |
|     """
 | |
|     last_mrs 设备运行状态值
 | |
|     """
 | |
|     from apps.wpm.models import StLog
 | |
|     from apps.wpm.tasks import cal_exp_duration_hour
 | |
|     from apps.wpm.services import get_sflog
 | |
| 
 | |
|     equip = Equipment.objects.get(id=equipId)
 | |
|     equip.running_state = last_mrs
 | |
|     equip.save(update_fields=["running_state"])
 | |
| 
 | |
|     mgroup: Mgroup = equip.mgroup
 | |
|     indicate = equip.indicate_mgroup_running
 | |
|     
 | |
|     if mgroup and indicate:
 | |
|         mrun = None
 | |
|         if last_mrs in [RuningState.STOP, RuningState.FAILURE, RuningState.STANDBY]:  # 设备停止信号
 | |
|             if indicate in [Equipment.IM_DANDAN, Equipment.IM_DOUDAN]:
 | |
|                 mrun= False
 | |
|             else:
 | |
|                 if not Equipment.objects.filter(mgroup=mgroup, indicate_mgroup_running=indicate, running_state=RuningState.RUNING).exists():
 | |
|                     mrun = False
 | |
|         elif last_mrs == RuningState.RUNING:  # 设备启动信号
 | |
|             if indicate in [Equipment.IM_DANDAN, Equipment.IM_DANDOU]:
 | |
|                 mrun= True
 | |
|             else:
 | |
|                 if not Equipment.objects.filter(mgroup=mgroup, indicate_mgroup_running=indicate, running_state__in=[RuningState.STOP, RuningState.FAILURE, RuningState.STANDBY]).exists():
 | |
|                     mrun = True
 | |
|                     
 | |
|         if mrun is not None:
 | |
|             mgroup.is_runing = mrun
 | |
|             mgroup.save(update_fields=["is_runing"])
 | |
| 
 | |
|             last_stlog = StLog.objects.filter(mgroup=mgroup, is_shutdown=True).order_by("-start_time").first()  # 找到最后一次停机记录
 | |
| 
 | |
|             if last_stlog:
 | |
|                 if last_timex >= last_stlog.start_time:  # 认为是有效信号
 | |
|                     if last_stlog.end_time is None and mrun:  # 从停到开
 | |
|                         last_stlog.end_time = last_timex
 | |
|                         last_stlog.duration = (last_stlog.end_time - last_stlog.start_time).total_seconds() / 3600
 | |
|                         last_stlog.save()
 | |
|                         cal_exp_duration_hour(last_stlog.id)  # 触发时间分配
 | |
|                     elif last_stlog.end_time and mrun is False and last_timex > last_stlog.end_time:  # 从开到停
 | |
|                         StLog.objects.create(title="停机", is_shutdown=True, mgroup=mgroup, end_time=None, start_time=last_timex, sflog=get_sflog(mgroup, last_timex))
 | |
|             elif mrun is False:
 | |
|                 StLog.objects.create(title="停机", is_shutdown=True, mgroup=mgroup, end_time=None, start_time=last_timex, sflog=get_sflog(mgroup, last_timex))
 | |
|                 mgroup.is_runing = False
 | |
|                 mgroup.save()
 | |
| 
 | |
| 
 | |
| def daoru_equipment(path: str):
 | |
|     from apps.utils.snowflake import idWorker
 | |
|     from openpyxl import load_workbook
 | |
| 
 | |
|     wb = load_workbook(path)
 | |
|     sheet = wb["em_equipment"]
 | |
|     i = 2
 | |
|     while sheet[f"a{i}"].value:
 | |
|         name = sheet[f"a{i}"].value
 | |
|         number = sheet[f"b{i}"].value
 | |
|         model = sheet[f"c{i}"].value
 | |
|         parameter = sheet[f"d{i}"].value
 | |
|         place = sheet[f"e{i}"].value
 | |
|         power_kw = sheet[f"f{i}"].value
 | |
|         belong_dept_name = sheet[f"g{i}"].value
 | |
|         mgroup_name = sheet[f"h{i}"].value
 | |
|         Equipment.objects.get_or_create(
 | |
|             number=number,
 | |
|             defaults={
 | |
|                 "id": idWorker.get_id(),
 | |
|                 "name": name,
 | |
|                 "model": model,
 | |
|                 "parameter": parameter,
 | |
|                 "place": place,
 | |
|                 "power_kw": power_kw,
 | |
|                 "belong_dept": Dept.objects.get(name=belong_dept_name) if belong_dept_name else None,
 | |
|                 "mgroup": Mgroup.objects.get(name=mgroup_name) if mgroup_name else None,
 | |
|             },
 | |
|         )
 | |
|         i = i + 1
 |