utils.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. import hashlib
  3. import time
  4. import requests
  5. from requests.auth import HTTPBasicAuth
  6. def get_equip_list(d_id, isfullId=0):
  7. """
  8. 跨平台获取设备信息
  9. :param d_id: 设备号
  10. :param isfullId:0模糊匹配,1表示完整设备号匹配
  11. :return:列表数据,相同设备号放一起,并且最近更新数据靠前,最近更新数据可认为设备最后所在平台
  12. """
  13. if isfullId == 1:
  14. bigdata_res = requests.post("http://8.136.98.49:8002/search/equip", data={"device_id": d_id, "isfullId": "1"},
  15. timeout=5)
  16. siqing_res = requests.post("http://www.yfzhwlw.com/search/equip", data={"device_id": d_id, "isfullId": "1"},
  17. timeout=5)
  18. else:
  19. bigdata_res = requests.post("http://8.136.98.49:8002/search/equip", data={"device_id": d_id}, timeout=5)
  20. siqing_res = requests.post("http://www.yfzhwlw.com/search/equip", data={"device_id": d_id}, timeout=5)
  21. django_data = json.loads(bigdata_res.content.decode()).get("data", [])
  22. siqing_data = json.loads(siqing_res.content.decode()).get("data", [])
  23. data = []
  24. data.extend(django_data)
  25. data.extend(siqing_data)
  26. data = sorted(data, key=lambda e: e.__getitem__('uptime'), reverse=True)
  27. data = sorted(data, key=lambda e: e.__getitem__('device_id'), reverse=True)
  28. return data
  29. class Get_SIM_info(object):
  30. """
  31. 自定义获取SIM卡对应卡商及数据
  32. """
  33. def __init__(self, iccid) -> None:
  34. self.iccid = iccid
  35. self.hz_status = {
  36. 0: "未知",
  37. 1: "测试期",
  38. 2: "沉默期",
  39. 3: "使用中",
  40. 4: "停机",
  41. 5: "停机保号",
  42. 6: "预销号",
  43. 7: "销号"
  44. }
  45. self.qp_status = {
  46. "testing": "测试中",
  47. "inventory": "库存",
  48. "pending-activation": "待激活",
  49. "activation": "已激活",
  50. "deactivation": "已停卡",
  51. "retired": "已销卡"
  52. }
  53. def hz_sim_info(self):
  54. """获取合宙流量卡信息"""
  55. url = "http://api.openluat.com/sim/iotcard/card"
  56. payload = {
  57. 'iccid': self.iccid,
  58. }
  59. appkey = 'KDlhc9VY4rWSXkbM'
  60. appsecret = 'DmwxcSB1b8dx7kLWqWMJNRLgyQhCvPJ7DHIfLdHIz9A18CifSHqJk3nWZ1vHv4cR'
  61. auth = HTTPBasicAuth(appkey, appsecret)
  62. try:
  63. res = requests.post(url, json=payload, auth=auth, timeout=5)
  64. data = json.loads(res.text)
  65. except Exception as e:
  66. data = {'code': 99999, 'msg': '接口调用异常异常'}
  67. return data
  68. def qp_sim_info(self):
  69. """ 获取企鹏(SIMBOSS)流量卡信息 """
  70. url = "https://api.simboss.com/2.0/device/detail"
  71. current_milli_time = lambda: int(round(time.time() * 1000))
  72. data_1 = "appid=%s&iccid=%s&timestamp=%s%s" % (
  73. "102420177762", self.iccid, current_milli_time(), "6397d7e6a56589f1d93284e9800493e1")
  74. sign = hashlib.sha256(data_1.encode('utf-8')).hexdigest()
  75. data = {"appid": "102420177762", "iccid": self.iccid, "timestamp": current_milli_time(), "sign": sign}
  76. try:
  77. res = requests.post(url, data=data, timeout=5)
  78. data = json.loads(res.text)
  79. except Exception as e:
  80. data = {'code': 99999, 'msg': '接口调用异常异常'}
  81. return data
  82. def get_sim_info(self):
  83. """未知卡商情况下获取卡信息"""
  84. hz_data = self.hz_sim_info()
  85. if hz_data["code"] == 0: # 合宙
  86. sim_operators = 1
  87. account_status = self.hz_status[hz_data["data"]["account_status"]]
  88. if hz_data["data"]["active"] == 1:
  89. active_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hz_data["data"]["active_date"]))
  90. data_plan = str(hz_data["data"]["data_plan"]) + "M"
  91. data_usage = str(hz_data["data"]["data_usage"]) + "M"
  92. data_balance = str(hz_data["data"]["data_balance"]) + "M"
  93. expiry_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(hz_data["data"]["expiry_date"]))
  94. else:
  95. active_date = "未激活"
  96. data_plan = "未激活"
  97. data_usage = "未激活"
  98. data_balance = "未激活"
  99. expiry_date = "未激活"
  100. else:
  101. qp_data = self.qp_sim_info()
  102. if qp_data["code"] == "0":
  103. sim_operators = 2
  104. account_status = self.qp_status[qp_data["data"]["status"]]
  105. active_date = qp_data["data"].get("startDate", "")
  106. if active_date:
  107. if qp_data["data"]["useCountAsVolume"] == False:
  108. data_plan = str(qp_data["data"]["totalDataVolume"]) + "M"
  109. data_usage = str(qp_data["data"]["usedDataVolume"]) + "M"
  110. data_balance = str(qp_data["data"]["totalDataVolume"] - qp_data["data"]["usedDataVolume"]) + "M"
  111. else:
  112. data_plan = str(qp_data["data"]["totalDataVolume"] * 1024) + "M"
  113. data_usage = str(qp_data["data"]["usedDataVolume"] * 1024) + "M"
  114. data_balance = str(
  115. (qp_data["data"]["totalDataVolume"] - qp_data["data"]["usedDataVolume"]) * 1024) + "M"
  116. expiry_date = qp_data["data"]["expireDate"]
  117. else:
  118. data_plan = "未激活"
  119. data_usage = "未激活"
  120. data_balance = "未激活"
  121. expiry_date = "未激活"
  122. else:
  123. sim_operators = 3
  124. account_status = "未知"
  125. active_date = "未知"
  126. data_plan = "未知"
  127. data_usage = "未知"
  128. data_balance = "未知"
  129. expiry_date = "未知"
  130. return sim_operators, account_status, active_date, data_plan, data_usage, data_balance, expiry_date