jk_status.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. # File Name:mqtt_chat_client.py
  3. # Python Version:3.5.1
  4. import os
  5. import django
  6. import sys
  7. from qcloudsms_py import SmsSingleSender
  8. from qcloudsms_py.httpclient import HTTPError
  9. from qcloudsms_py import SmsMultiSender
  10. BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录
  11. sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir)))
  12. os.environ.setdefault("DJANGO_SETTINGS_MODULE",
  13. "yfwlw_pro.settings") # project_name 项目名称
  14. django.setup()
  15. import json
  16. from apps.AppInfoManage.models import Equip, Equip_type, MyUser, CBDphoto, CBDstatus, QXZdata, SCDstatus, CBDstatus, Equip_SimInfo, CBDdata, BZYphoto, RecentCBDdata,RecentSCDdata,\
  17. Msg_Conf, Msg_Send, JKphoto, JKdata, JKtoken
  18. import re
  19. import datetime
  20. import time
  21. import random
  22. from apps.SimInfo.views import get_siminfo
  23. from apps.ReportManage.all_dict import insect_dict_new, transpont_equip_scd
  24. import shutil
  25. import requests
  26. import math
  27. # math.ceil 向上取整
  28. # math.floor 向下取整
  29. token = JKtoken.objects.get(account="ysy_haikang").token
  30. url = "https://open.ys7.com/api/lapp/device/list"
  31. live_data = {"accessToken":token}
  32. live_res = requests.post(url,data=live_data)
  33. live_res = eval(live_res.text)
  34. if live_res['code'] == "200":
  35. print("token获取成功")
  36. # 如果返回其他 则重新获取token
  37. else:
  38. print("token过期重新获取")
  39. token_url = 'https://open.ys7.com/api/lapp/token/get'
  40. token_data = {"appKey":"e8ec0d52b4b14628a3daf4b88cbcca2a",
  41. "appSecret":"d4565abea1cd0b90b59917111dcb37bd"}
  42. token_res = requests.post(token_url,data=token_data,timeout=3)
  43. print("token_res1:",token_res.text)
  44. token_res = eval(token_res.text)
  45. if token_res["code"] == "200":
  46. token_obj = JKtoken.objects.get(account="ysy_haikang")
  47. token_obj.token = token_res["data"]["accessToken"]
  48. token_obj.upl_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  49. token_obj.save()
  50. token = token_obj.token
  51. print("-----------------token保存成功")
  52. token = JKtoken.objects.get(account="ysy_haikang").token
  53. url = "https://open.ys7.com/api/lapp/device/list"
  54. live_data = {"accessToken":token}
  55. live_res = requests.post(url,data=live_data)
  56. live_res = eval(live_res.text)
  57. aa = live_res["page"]["total"] / 10
  58. aa = math.ceil(aa)
  59. for i in range(aa):
  60. live_data = {"accessToken":token,"pageStart":i}
  61. live_res = requests.post(url,data=live_data)
  62. live_res = eval(live_res.text)
  63. print("live_res2:",live_res)
  64. for ii in live_res["data"]:
  65. if JKdata.objects.filter(equip_id_id=ii["deviceSerial"]).exists():
  66. print("监控存在")
  67. print(ii["status"])
  68. jk_obj = JKdata.objects.get(equip_id_id=ii["deviceSerial"])
  69. jk_obj.is_online = ii["status"]
  70. jk_obj.save()
  71. print("保存成功")