# Create your tasks here
from __future__ import absolute_import, unicode_literals
from apps.utils.tasks import CustomTask
from celery import shared_task
from django.utils.timezone import localtime
from apps.em.models import Equipment
from apps.em.services import set_eq_rs, get_eq_rs, shutdown_or_startup
from apps.mtm.models import Mgroup
from django.utils import timezone

@shared_task(base=CustomTask)
def check_equipment_offline(seconds=30):
    """监测设备是否掉线

    监测设备是否掉线
    """
    # equips = Equipment.objects.filter(mp_ep_monitored__is_rep_ep_running_state=True, mp_ep_monitored__enabled=True)
    equips = Equipment.objects.all()
    now = localtime()
    for equip in equips:
        rs = get_eq_rs(equip.id)
        if (now - rs['running_state_timex']).total_seconds() > seconds:
                set_eq_rs(equip.id, now, Equipment.OFFLINE)


@shared_task(base=CustomTask)
def check_mgroup_running():
    """监测工段是否运行
    """
    # 没有标志设备的工段直接干停
    Mgroup.objects.filter(is_running=True).exclude(id__in=Equipment.objects.filter(indicate_mgroup_running__isnull=False, mgroup__isnull=False).values_list('mgroup', flat=True)).update(is_running=True)
    # equips = Equipment.objects.filter(indicate_mgroup_running__isnull=False, mgroup__isnull=False)
    # for equip in equips:
    #     rs = get_eq_rs(equip.id)
    #     if rs['running_state'] == Equipment.OFFLINE: # 如果掉线了不关心
    #         pass
    #     else:
    #         shutdown_or_startup(equip.id, rs["running_state_timex"], rs["running_state"])

@shared_task(base=CustomTask)
def check_equipment_measure_time():
    """
    监测设备检定状态
    """
    all_equips = Equipment.objects.values('id', 'check_days_number', 'next_check_date', 'state_measure')
    for equip in all_equips:
        if equip['check_days_number'] and equip['next_check_date']:
            # 更新检定状态
            now = localtime().date()
            if equip['next_check_date'] <= now :
                    Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_EXPIRED)
            elif equip['next_check_date'] - now <= timezone.timedelta(days=equip['check_days_number']):
                Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_WARNING)
            else:
                Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_NORMAL)
        else:
            Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_WAIT)


    # if instance.check_days_number and instance.next_check_date:
    #     # 更新检定状态
    #     now = timezone.localtime().date()
    #     if instance.next_check_date <= now :
    #         instance.state_measure = Equipment.EQ_EXPIRED
    #     elif instance.next_check_date - now <= timezone.timedelta(days=instance.check_days_number):
    #         instance.state_measure = Equipment.EQ_WARNING
    #     else:
    #         instance.state_measure = Equipment.EQ_NORMAL
    # else:
    #     instance.state_measure = Equipment.EQ_WAIT
    # instance.save()