39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import os
|
|
import sys
|
|
import django
|
|
import logging
|
|
from django.core.cache import cache
|
|
from django.utils import timezone
|
|
import time
|
|
|
|
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
BASE_DIR = os.path.dirname(CUR_DIR)
|
|
sys.path.insert(0, BASE_DIR)
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
|
|
django.setup()
|
|
|
|
from apps.em.models import Equipment, RuningState
|
|
|
|
|
|
def run():
|
|
while True:
|
|
now = timezone.now()
|
|
equips = Equipment.objects.all()
|
|
for equip in equips:
|
|
is_offline = True
|
|
cache_key = f"equipment_{equip.id}"
|
|
cache_value = cache.get(cache_key, None)
|
|
if cache_value:
|
|
last_timex = cache_value.get("running_state_timex", None)
|
|
if last_timex and (now - last_timex).total_seconds() <= 20:
|
|
is_offline = False
|
|
if is_offline:
|
|
equip.running_state = RuningState.OFFLINE
|
|
equip.save(update_fields=["running_state"])
|
|
time.sleep(5)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run()
|