Merge branch 'master' of https://e.coding.net/ctcdevteam/ehs/ehs_server
This commit is contained in:
commit
40c6479cf3
|
@ -159,8 +159,9 @@ def save_voice_and_speak(event: Event):
|
|||
if event.voice: # 如果已经生成了报警声音不用再请求
|
||||
pass
|
||||
else:
|
||||
_, event.voice, _ = generate_voice(event.voice_msg, v_p)
|
||||
event.save()
|
||||
if getattr(settings, 'BD_SP_ENABLED', False):
|
||||
_, event.voice, _ = generate_voice(event.voice_msg, v_p)
|
||||
event.save()
|
||||
if main_cate.speaker_on:
|
||||
sps = []
|
||||
if event.area: # 如果事件存在发生区域
|
||||
|
|
|
@ -8,6 +8,9 @@ from django.core.cache import cache
|
|||
from apps.utils.tasks import ctask_run
|
||||
import logging
|
||||
from apps.mtm.services import mgroup_run_change
|
||||
import random
|
||||
import time
|
||||
|
||||
myLogger = logging.getLogger("log")
|
||||
baseTime = datetime.datetime(year=1990, month=4, day=4, hour=0, minute=0, second=0, tzinfo=tz.gettz(settings.TIME_ZONE))
|
||||
|
||||
|
@ -27,20 +30,22 @@ def set_eq_rs(equipId, last_timex: datetime, last_mrs: int):
|
|||
eq_rs_change = False
|
||||
ten_minutes = datetime.timedelta(minutes=10)
|
||||
now = datetime.datetime.now()
|
||||
mgroup_sort_trigger = False
|
||||
if eq_rs_cache["running_state"] != last_mrs: # 如果状态变动了要调用方法否则只需更新缓存
|
||||
eq_rs_change = True
|
||||
update_time = now
|
||||
elif now > eq_rs_cache.get('update_time', baseTime) + ten_minutes: # 如果当前时间大于缓存时间10分钟则更新
|
||||
eq_rs_change = True
|
||||
update_time = now
|
||||
mgroup_sort_trigger = True
|
||||
else:
|
||||
update_time = eq_rs_cache.get('update_time', baseTime)
|
||||
cache.set(f"equipment_{equipId}", {"running_state": last_mrs, "running_state_timex": last_timex, "update_time":update_time}, timeout=None) # 更新缓存
|
||||
if eq_rs_change:
|
||||
ctask_run.delay("apps.em.services.shutdown_or_startup", equipId, last_timex, last_mrs)
|
||||
ctask_run.delay("apps.em.services.shutdown_or_startup", equipId, last_timex, last_mrs, '', mgroup_sort_trigger)
|
||||
|
||||
|
||||
def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str = ""):
|
||||
def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str = "", mgroup_sort_trigger: bool = False):
|
||||
"""
|
||||
last_mrs 设备运行状态值
|
||||
"""
|
||||
|
@ -70,6 +75,8 @@ def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str
|
|||
new_run = True
|
||||
|
||||
if new_run is not None:
|
||||
if mgroup_sort_trigger:
|
||||
time.sleep(mgroup.sort)
|
||||
mgroup_run_change(mgroup, new_run, last_timex, note)
|
||||
|
||||
|
||||
|
|
|
@ -82,8 +82,8 @@ class MpointStatSerializer(CustomModelSerializer):
|
|||
ep_belong_name = serializers.CharField(source="mpoint.ep_belong.name", read_only=True)
|
||||
mgroup_name = serializers.CharField(source="mgroup.name", read_only=True)
|
||||
belong_dept_name = serializers.CharField(source="mgroup.belong_dept.name", read_only=True)
|
||||
sflog_start_time = serializers.CharField(source="sflog.start_time", read_only=True)
|
||||
sflog_end_time = serializers.CharField(source="sflog.end_time", read_only=True)
|
||||
sflog_start_time = serializers.DateTimeField(source="sflog.start_time", read_only=True)
|
||||
sflog_end_time = serializers.DateTimeField(source="sflog.end_time", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = MpointStat
|
||||
|
@ -162,7 +162,7 @@ class EnStatSerializer(CustomModelSerializer):
|
|||
ret[f'{item["material_name"]}_{item["testitem_name"].replace("-", "")}_rate_pass'] = 0
|
||||
else:
|
||||
value = item["rate_pass"]
|
||||
rounded_value = round(value) if value is not None else 0
|
||||
rounded_value = round(value, 2) if value is not None else 0
|
||||
formatted_value = "{:.2f}".format(rounded_value)
|
||||
ret[f'{item["material_name"]}_{item["testitem_name"].replace("-", "")}_rate_pass'] = formatted_value
|
||||
if equip_elec_data:
|
||||
|
|
|
@ -185,7 +185,14 @@ class MpointCache:
|
|||
last_data = current_cache_val["last_data"]
|
||||
if isinstance(last_val, (int, float)):
|
||||
last_val = last_val*current_cache_val.get('coefficient', 1.0)
|
||||
last_data["last_val"] = last_val
|
||||
if (last_val == 0 and
|
||||
current_cache_val.get("type", 0) == 10 and
|
||||
current_cache_val.get('is_rep_ep_running_state', None) is False and
|
||||
get_sysconfig('enm.use_cache_when_w_el_0', False) is True and
|
||||
current_cache_val.get("material_name", "") in ["动力电", "工业水"]): # 如果电表断电
|
||||
pass
|
||||
else:
|
||||
last_data["last_val"] = last_val
|
||||
last_data["last_timex"] = last_timex
|
||||
last_mrs = None # 设备状态信号
|
||||
mpoint_is_rep_ep_running_state = current_cache_val["is_rep_ep_running_state"]
|
||||
|
|
|
@ -116,12 +116,13 @@ def mgroup_run_change(mgroup: Mgroup, new_run: bool, last_timex: datetime, note:
|
|||
"""
|
||||
from apps.wpm.services import get_sflog
|
||||
from apps.wpm.tasks import cal_exp_duration_sec
|
||||
from apps.wpm.models import StLog
|
||||
from apps.wpm.models import StLog, SfLogExp
|
||||
mgroup.is_running = new_run
|
||||
mgroup.save(update_fields=["is_running"])
|
||||
|
||||
last_stlog = StLog.objects.filter(mgroup=mgroup, is_shutdown=True).order_by("-start_time").first() # 找到最后一次停机记录
|
||||
|
||||
need_cal_exp = True
|
||||
if last_stlog:
|
||||
if last_timex >= last_stlog.start_time: # 认为是有效信号
|
||||
if last_stlog.end_time is None and new_run: # 从停到开
|
||||
|
@ -129,22 +130,29 @@ def mgroup_run_change(mgroup: Mgroup, new_run: bool, last_timex: datetime, note:
|
|||
last_stlog.duration_sec = (last_stlog.end_time - last_stlog.start_time).total_seconds()
|
||||
last_stlog.save()
|
||||
elif last_stlog.end_time and new_run is False and last_timex > last_stlog.end_time: # 从开到停
|
||||
last_stlog, _ = StLog.objects.get_or_create(
|
||||
cur_sflog = get_sflog(mgroup, last_timex)
|
||||
last_stlog, is_created = StLog.objects.get_or_create(
|
||||
mgroup=mgroup,
|
||||
is_shutdown=True,
|
||||
start_time=last_timex,
|
||||
defaults={
|
||||
'title': '停机',
|
||||
'end_time': None,
|
||||
'sflog': get_sflog(mgroup, last_timex),
|
||||
'sflog': cur_sflog,
|
||||
'reason':note,
|
||||
}
|
||||
)
|
||||
if is_created:
|
||||
SfLogExp.objects.get_or_create(stlog=last_stlog, sflog=cur_sflog)
|
||||
need_cal_exp = False
|
||||
elif new_run is False:
|
||||
last_stlog = StLog.objects.create(title="停机", is_shutdown=True, mgroup=mgroup, end_time=None, start_time=last_timex, sflog=get_sflog(mgroup, last_timex), reason = note)
|
||||
cur_sflog = get_sflog(mgroup, last_timex)
|
||||
last_stlog = StLog.objects.create(title="停机", is_shutdown=True, mgroup=mgroup, end_time=None, start_time=last_timex, sflog=cur_sflog, reason = note)
|
||||
mgroup.is_running = False
|
||||
mgroup.save()
|
||||
if last_stlog:
|
||||
SfLogExp.objects.get_or_create(stlog=last_stlog, sflog=cur_sflog)
|
||||
need_cal_exp = False
|
||||
if last_stlog and need_cal_exp:
|
||||
cal_exp_duration_sec(last_stlog.id) # 触发时间分配
|
||||
|
||||
def bind_routepack(ticket: Ticket, transition, new_ticket_data: dict):
|
||||
|
|
|
@ -19,6 +19,8 @@ from django.core.cache import cache
|
|||
from django.utils.timezone import localdate
|
||||
from apps.qm.models import NotOkOption
|
||||
from apps.wf.serializers import TicketSimpleSerializer
|
||||
import logging
|
||||
mylogger = logging.getLogger("log")
|
||||
|
||||
class OtherLogSerializer(CustomModelSerializer):
|
||||
class Meta:
|
||||
|
@ -57,8 +59,8 @@ class StLogSerializer(CustomModelSerializer):
|
|||
def update(self, instance, validated_data):
|
||||
if instance.is_shutdown: # 停机不可编辑end_time
|
||||
validated_data.pop('end_time', None)
|
||||
if instance.end_time:
|
||||
raise ParseError('该异常已结束无需编辑')
|
||||
# if instance.end_time:
|
||||
# raise ParseError('该异常已结束无需编辑')
|
||||
with transaction.atomic():
|
||||
validated_data.pop('mgroup', None)
|
||||
validated_data.pop('start_time', None)
|
||||
|
@ -70,7 +72,7 @@ class StLogSerializer(CustomModelSerializer):
|
|||
cal_exp_duration_sec(instance.id)
|
||||
try:
|
||||
sflogexp = SfLogExp.objects.get(
|
||||
stlog=instance.stlog, sflog=current_sflog)
|
||||
stlog=instance, sflog=current_sflog)
|
||||
sflogexp.note = current_note
|
||||
sflogexp.save()
|
||||
except SfLogExp.DoesNotExist:
|
||||
|
@ -111,36 +113,36 @@ class SfLogSerializer(CustomModelSerializer):
|
|||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
with transaction.atomic():
|
||||
old_pcoal_heat = instance.pcoal_heat
|
||||
old_team = instance.team
|
||||
instance: SfLog = super().update(instance, validated_data)
|
||||
new_pcoal_heat = instance.pcoal_heat
|
||||
new_team = instance.team
|
||||
mgroup: Mgroup = instance.mgroup
|
||||
old_pcoal_heat = instance.pcoal_heat
|
||||
old_team = instance.team
|
||||
instance: SfLog = super().update(instance, validated_data)
|
||||
new_pcoal_heat = instance.pcoal_heat
|
||||
new_team = instance.team
|
||||
mgroup: Mgroup = instance.mgroup
|
||||
|
||||
# 更新煤粉热值触发计算
|
||||
if instance.mgroup == '回转窑' and instance.shift.name in ['早班', '白班']:
|
||||
year_s, month_s, day_s = instance.get_ymd
|
||||
if new_pcoal_heat: # 更新一下缓存
|
||||
cache.set(f'pcoal_heat_{year_s}_{month_s}_{day_s}', new_pcoal_heat)
|
||||
if new_pcoal_heat != old_pcoal_heat:
|
||||
cal_enstat_when_pcoal_heat_change.delay(year_s, month_s, day_s)
|
||||
# 更新煤粉热值触发计算
|
||||
if instance.mgroup.name == '回转窑' and instance.shift.name in ['早班', '白班']:
|
||||
mylogger.info(f'回转窑白班/早班{instance.get_ymd}更新{new_pcoal_heat}-{old_pcoal_heat}')
|
||||
year_s, month_s, day_s = instance.get_ymd
|
||||
if new_pcoal_heat: # 更新一下缓存
|
||||
cache.set(f'pcoal_heat_{year_s}_{month_s}_{day_s}', new_pcoal_heat, timeout=60 * 60 * 48)
|
||||
if new_pcoal_heat != old_pcoal_heat:
|
||||
cal_enstat_when_pcoal_heat_change.delay(year_s, month_s, day_s)
|
||||
|
||||
# 更新班组触发计算
|
||||
if new_team != old_team:
|
||||
default_state = 'pending'
|
||||
if timezone.now() > instance.end_time:
|
||||
default_state = 'normal'
|
||||
# 分配班组时创建人员到岗情况
|
||||
for item in TeamMember.objects.filter(team=new_team, mgroup=instance.mgroup):
|
||||
AttLog.objects.get_or_create(sflog=instance, user=item.user, defaults={
|
||||
'sflog': instance, 'user': item.user, 'post': item.post, 'state': default_state, 'create_by': self.context['request'].user})
|
||||
if mgroup.need_enm:
|
||||
from apps.qm.tasks import cal_quastat_sflog
|
||||
cal_enstat_when_team_change.delay(instance.id)
|
||||
cal_quastat_sflog.delay(instance.id)
|
||||
return instance
|
||||
# 更新班组触发计算
|
||||
if new_team != old_team:
|
||||
default_state = 'pending'
|
||||
if timezone.now() > instance.end_time:
|
||||
default_state = 'normal'
|
||||
# 分配班组时创建人员到岗情况
|
||||
for item in TeamMember.objects.filter(team=new_team, mgroup=instance.mgroup):
|
||||
AttLog.objects.get_or_create(sflog=instance, user=item.user, defaults={
|
||||
'sflog': instance, 'user': item.user, 'post': item.post, 'state': default_state, 'create_by': self.context['request'].user})
|
||||
if mgroup.need_enm:
|
||||
from apps.qm.tasks import cal_quastat_sflog
|
||||
cal_enstat_when_team_change.delay(instance.id)
|
||||
cal_quastat_sflog.delay(instance.id)
|
||||
return instance
|
||||
|
||||
def to_representation(self, instance):
|
||||
ret = super().to_representation(instance)
|
||||
|
|
|
@ -17,6 +17,8 @@ from apps.mtm.services import cal_material_count
|
|||
from apps.wf.models import Ticket
|
||||
from django.db import transaction
|
||||
from apps.utils.thread import MyThread
|
||||
import logging
|
||||
myLogger = logging.getLogger('log')
|
||||
|
||||
def generate_new_batch(old_batch: str, mlog: Mlog):
|
||||
new_batch = old_batch
|
||||
|
@ -97,11 +99,16 @@ def get_pcoal_heat(year_s: int, month_s: int, day_s: int):
|
|||
qs = SfLog.objects.get(work_date__year=year_s, work_date__month=month_s, work_date__day=day_s,
|
||||
mgroup__name='回转窑', shift__name__in=['白班', '早班']) # hardcode
|
||||
if qs.pcoal_heat is None or qs.pcoal_heat <=0:
|
||||
qs.pcoal_heat = 6000
|
||||
s = SfLog.objects.filter(pcoal_heat__isnull=False).order_by('-start_time').first()
|
||||
if s:
|
||||
qs.pcoal_heat = s.pcoal_heat
|
||||
else:
|
||||
qs.pcoal_heat = 6000
|
||||
qs.save(update_fields=['pcoal_heat'])
|
||||
cache.set(f'pcoal_heat_{year_s}_{month_s}_{day_s}', qs.pcoal_heat)
|
||||
cache.set(key, qs.pcoal_heat, timeout=60 * 60 * 48)
|
||||
return qs.pcoal_heat
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
myLogger.error(f'获取煤粉热值失败,{e}', exc_info=True)
|
||||
return 6000
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@ from apps.wpm.models import SfLog, StLog, SfLogExp
|
|||
from django.utils import timezone
|
||||
from django.db.models import F
|
||||
from apps.wpm.services import get_pcoal_heat
|
||||
from django.core.cache import cache
|
||||
import time
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
|
@ -101,16 +103,15 @@ def cal_exp_duration_sec(stlogId: str='', all=False, now: datetime=None):
|
|||
ret = SfLogExp.objects.filter(
|
||||
sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration_sec'))
|
||||
if ret.get('sum', 0):
|
||||
get_total_sec_now(sflog.id)
|
||||
sflog.shut_sec = ret['sum']
|
||||
sflog.save()
|
||||
# 更新sflog总时长
|
||||
if sflog.end_time > now:
|
||||
get_total_sec_now(sflog.id)
|
||||
|
||||
# if stlogId:
|
||||
# cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None,
|
||||
# None, None, None, None, cascade=True, cal_attrs=['run_hour'])
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def cal_enstat_when_pcoal_heat_change(year_s, month_s, day_s):
|
||||
from apps.enm.models import EnStat
|
||||
|
|
Loading…
Reference in New Issue