CommonService.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import datetime
  4. import os
  5. import time
  6. import hashlib
  7. from pathlib import Path
  8. from random import Random
  9. import ipdb
  10. import requests
  11. import simplejson as json
  12. from boto3 import Session
  13. from django.core import serializers
  14. from django.utils import timezone
  15. from pyipip import IPIPDatabase
  16. import OpenSSL.crypto as ct
  17. from base64 import encodebytes
  18. from AnsjerPush.config import BASE_DIR, ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, PUSH_BUCKET
  19. from Model.models import iotdeviceInfoModel
  20. from Object.enums.EventTypeEnum import EventTypeEnumObj
  21. # 复用性且公用较高封装代码在这
  22. class CommonService:
  23. # 添加模糊搜索
  24. @staticmethod
  25. def get_kwargs(data={}):
  26. kwargs = {}
  27. for (k, v) in data.items():
  28. if v is not None and v != u'':
  29. kwargs[k + '__icontains'] = v
  30. return kwargs
  31. # 定义静态方法
  32. # 格式化query_set转dict
  33. @staticmethod
  34. def qs_to_dict(query_set):
  35. sqlJSON = serializers.serialize('json', query_set)
  36. sqlList = json.loads(sqlJSON)
  37. sqlDict = dict(zip(["datas"], [sqlList]))
  38. return sqlDict
  39. # 获取文件大小
  40. @staticmethod
  41. def get_file_size(file_path='', suffix_type='', decimal_point=0):
  42. # for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
  43. # path = Path() / 'D:/TestServer/123444.mp4'
  44. path = Path() / file_path
  45. size = path.stat().st_size
  46. mb_size = 0.0
  47. if suffix_type == 'MB':
  48. mb_size = size / 1024.0 / 1024.0
  49. if decimal_point != 0:
  50. mb_size = round(mb_size, decimal_point)
  51. return mb_size
  52. @staticmethod
  53. def get_param_flag(data=[]):
  54. # print(data)
  55. flag = True
  56. for v in data:
  57. if v is None:
  58. flag = False
  59. break
  60. return flag
  61. @staticmethod
  62. def get_ip_address(request):
  63. """
  64. 获取ip地址
  65. :param request:
  66. :return:
  67. """
  68. try:
  69. real_ip = request.META['HTTP_X_FORWARDED_FOR']
  70. clientIP = real_ip.split(",")[0]
  71. except:
  72. try:
  73. clientIP = request.META['REMOTE_ADDR']
  74. except Exception as e:
  75. clientIP = ''
  76. return clientIP
  77. # @获取一天每个小时的datetime.datetime
  78. @staticmethod
  79. def getTimeDict(times):
  80. time_dict = {}
  81. t = 0
  82. for x in range(24):
  83. if x < 10:
  84. x = '0' + str(x)
  85. else:
  86. x = str(x)
  87. a = times.strftime("%Y-%m-%d") + " " + x + ":00:00"
  88. time_dict[t] = timezone.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
  89. t += 1
  90. return time_dict
  91. # 根据ip获取地址
  92. @staticmethod
  93. def getAddr(ip):
  94. base_dir = BASE_DIR
  95. # ip数据库
  96. db = IPIPDatabase(base_dir + '/DB/17monipdb.dat')
  97. addr = db.lookup(ip)
  98. ts = addr.split('\t')[0]
  99. return ts
  100. # 通过ip检索ipip指定信息 lang为CN或EN
  101. @staticmethod
  102. def getIpIpInfo(ip, lang, update=False):
  103. ipbd_dir = BASE_DIR + "/DB/mydata4vipday2.ipdb"
  104. db = ipdb.City(ipbd_dir)
  105. if update:
  106. from var_dump import var_dump
  107. var_dump('is_update')
  108. rr = db.reload(ipbd_dir)
  109. var_dump(rr)
  110. info = db.find_map(ip, lang)
  111. return info
  112. @staticmethod
  113. def getUserID(userPhone='13800138000', getUser=True, setOTAID=False, μs=True):
  114. if μs == True:
  115. if getUser == True:
  116. timeID = str(round(time.time() * 1000000))
  117. userID = timeID + userPhone
  118. return userID
  119. else:
  120. if setOTAID == False:
  121. timeID = str(round(time.time() * 1000000))
  122. ID = userPhone + timeID
  123. return ID
  124. else:
  125. timeID = str(round(time.time() * 1000000))
  126. eID = '13800' + timeID + '138000'
  127. return eID
  128. else:
  129. if getUser == True:
  130. timeID = str(round(time.time() * 1000))
  131. userID = timeID + userPhone
  132. return userID
  133. else:
  134. if setOTAID == False:
  135. timeID = str(round(time.time() * 1000))
  136. ID = userPhone + timeID
  137. return ID
  138. else:
  139. timeID = str(round(time.time() * 1000))
  140. eID = '13800' + timeID + '138000'
  141. return eID
  142. # 生成随机数
  143. @staticmethod
  144. def RandomStr(randomlength=8, number=True):
  145. str = ''
  146. if number == False:
  147. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  148. 'tUuVvWwXxYyZz0123456789'
  149. else:
  150. characterSet = '0123456789'
  151. length = len(characterSet) - 1
  152. random = Random()
  153. for index in range(randomlength):
  154. str += characterSet[random.randint(0, length)]
  155. return str
  156. # 生成订单好
  157. @staticmethod
  158. def createOrderID():
  159. random_id = CommonService.RandomStr(6, True)
  160. order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random_id)
  161. print('orderID:')
  162. print(order_id)
  163. return order_id
  164. # qs转换list datetime处理
  165. @staticmethod
  166. def qs_to_list(qs):
  167. res = []
  168. # print(qs)
  169. for ps in qs:
  170. if 'add_time' in ps:
  171. ps['add_time'] = ps['add_time'].strftime("%Y-%m-%d %H:%M:%S")
  172. if 'update_time' in ps:
  173. ps['update_time'] = ps['update_time'].strftime("%Y-%m-%d %H:%M:%S")
  174. if 'end_time' in ps:
  175. ps['end_time'] = ps['end_time'].strftime("%Y-%m-%d %H:%M:%S")
  176. if 'data_joined' in ps:
  177. if ps['data_joined']:
  178. ps['data_joined'] = ps['data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  179. else:
  180. ps['data_joined'] = ''
  181. res.append(ps)
  182. return res
  183. # 获取当前时间
  184. @staticmethod
  185. def get_now_time_str(n_time, tz, lang):
  186. print(n_time)
  187. print(tz)
  188. print(lang)
  189. try:
  190. tz = tz.replace(':', '.')
  191. n_time = int(n_time) + 3600 * float(tz)
  192. except:
  193. n_time = int(n_time)
  194. if lang == 'cn':
  195. return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))
  196. else:
  197. return time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(int(n_time)))
  198. @staticmethod
  199. def app_log_log(uid='None', tz='0'):
  200. file_path = '/'.join((BASE_DIR, 'static/app_log.log'))
  201. file = open(file_path, 'a+')
  202. file.write("uid:" + uid + "; " + "; tz:" + tz)
  203. file.write('\n')
  204. file.flush()
  205. file.close()
  206. @classmethod
  207. def upload_images(cls, file_dict, dir_path):
  208. """
  209. 上传图片到S3,并删除本地图片
  210. @param file_dict: S3图片路径
  211. @param dir_path: 本地图片路径
  212. @return: boolean
  213. """
  214. try:
  215. s3 = Session(
  216. aws_access_key_id=ACCESS_KEY_ID,
  217. aws_secret_access_key=SECRET_ACCESS_KEY,
  218. region_name=REGION_NAME
  219. ).resource('s3')
  220. for file_path, upload_path in file_dict.items():
  221. upload_data = open(file_path, 'rb')
  222. s3.Bucket(PUSH_BUCKET).put_object(Key=upload_path, Body=upload_data)
  223. # 删除图片
  224. cls.del_path(dir_path)
  225. cls.del_path(dir_path + '.jpg')
  226. return True
  227. except Exception as e:
  228. print(repr(e))
  229. return False
  230. @classmethod
  231. def del_path(cls, path):
  232. """
  233. 删除目录文件
  234. @param path: 文件路径
  235. @return: None
  236. """
  237. if not os.path.exists(path):
  238. return
  239. if os.path.isfile(path):
  240. os.remove(path)
  241. else:
  242. items = os.listdir(path)
  243. for f in items:
  244. c_path = os.path.join(path, f)
  245. if os.path.isdir(c_path):
  246. cls.del_path(c_path)
  247. else:
  248. os.remove(c_path)
  249. os.rmdir(path)
  250. @staticmethod
  251. def getMD5Sign(data, key):
  252. '''
  253. 魅族MD5签名
  254. '''
  255. dataList = []
  256. for k in sorted(data):
  257. dataList.append("%s=%s" % (k, data[k]))
  258. data = (''.join(dataList))
  259. data = data + key
  260. sign = hashlib.md5(data.encode(encoding="utf-8")).hexdigest()
  261. return sign
  262. @staticmethod
  263. def check_time_stamp_token(token, time_stamp):
  264. # 时间戳token校验
  265. if not all([token, time_stamp]):
  266. return False
  267. try:
  268. token = int(CommonService.decode_data(token))
  269. time_stamp = int(time_stamp)
  270. now_time = int(time.time())
  271. distance = now_time - time_stamp
  272. if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
  273. return False
  274. return True
  275. except Exception as e:
  276. print(e)
  277. return False
  278. @staticmethod
  279. def decode_data(content, start=1, end=4):
  280. """
  281. 数据解密
  282. @param content: 数据内容
  283. @param start: 起始长度
  284. @param end: 结束长度
  285. @return content: 解密的数据
  286. """
  287. if not content:
  288. return ''
  289. for i in range(start, end):
  290. content = base64.b64decode(content)
  291. content = content.decode('utf-8')
  292. content = content[i:-i]
  293. return content
  294. @staticmethod
  295. def timestamp_to_str(timestamp):
  296. """
  297. 时间戳转时间字符串
  298. @param timestamp: 时间戳
  299. @return time_str: 时间字符串
  300. """
  301. struct_time = time.localtime(timestamp)
  302. time_str = time.strftime("%Y-%m-%d %H:%M:%S", struct_time)
  303. return time_str
  304. @staticmethod
  305. def req_publish_mqtt_msg(identification_code, topic_name, msg, qos=1):
  306. """
  307. 通用发布MQTT消息函数
  308. @param identification_code: 标识码
  309. @param topic_name: 主题名
  310. @param msg: 消息内容
  311. @param qos: mqtt qos等级
  312. @return: boolean
  313. """
  314. if not all([identification_code, topic_name]):
  315. return False
  316. if identification_code.endswith('11L'):
  317. thing_name = 'LC_' + identification_code
  318. else:
  319. thing_name = 'Ansjer_Device_' + identification_code
  320. try:
  321. # 获取数据组织将要请求的url
  322. iot = iotdeviceInfoModel.objects.filter(
  323. thing_name=thing_name).values(
  324. 'endpoint', 'token_iot_number')
  325. if not iot.exists():
  326. return False
  327. endpoint = iot[0]['endpoint']
  328. Token = iot[0]['token_iot_number']
  329. # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
  330. # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  331. # post请求url发布MQTT消息
  332. url = 'https://{}/topics/{}?qos={}'.format(endpoint, topic_name, qos)
  333. authorizer_name = 'Ansjer_Iot_Auth'
  334. signature = CommonService.rsa_sign(Token) # Token签名
  335. headers = {
  336. 'x-amz-customauthorizer-name': authorizer_name,
  337. 'Token': Token,
  338. 'x-amz-customauthorizer-signature': signature}
  339. r = requests.post(url=url, headers=headers, json=msg, timeout=2)
  340. if r.status_code == 200:
  341. res = r.json()
  342. if res['message'] == 'OK':
  343. return True
  344. return False
  345. else:
  346. return False
  347. except Exception as e:
  348. return False
  349. @staticmethod
  350. def rsa_sign(Token):
  351. # 私钥签名Token
  352. if not Token:
  353. return ''
  354. private_key_file = '''-----BEGIN RSA PRIVATE KEY-----
  355. MIIEpQIBAAKCAQEA5iJzEDPqtGmFMggekVro6C0lrjuC2BjunGkrFNJWpDYzxCzE
  356. X5jf4/Fq7hcIaQd5sqHugDxPVollSLPe9zNilbrd0sZfU+Ed8gRVuKW9KwfE9XFr
  357. L0pt6bKRQ0IIRfiZ9TuR0tsQysvcO1GZSXcYfPue3tGM1zOnWFThWDqZ06+sOxzt
  358. RMRl4yNfbpCG4MfxG3itNXOfrjZv2OMLSXrxmzubSvRpUYSvQPs4fm9302SAnySY
  359. 0MKzx6H6528ZQm/IDDSZy6EmNBIyTRDfxC56vnYcXvqedAQh7jJnjdvt6Q4MhASH
  360. eIYi1FBSdu2NT6wgpnrqXzx5pq9kR/lnsLID0wIDAQABAoIBAQCiF4GT1/1oNSpr
  361. ouxk1PNXFPWFUsVGD8mAwVJmx//eiY7MjfuCmdqYYmI+cFqsH2fIOeYSzGfVO9Dq
  362. 9EYHN1oovAWhf7eFDPpajFMUSyiCNmazub8VAAeKowtNpCTPo9pMsDh1m3aoYA4u
  363. ebrN0+Sbo16y8kWRDgDAZoiR7DSMs8lczk16hwfv5mw8XpNDbaL3Coi4Koe2S1Yh
  364. 2SX3vWFlpd7qF1ZYXuZIp+b8JPrV7n9eUKoFgzj0gqgwQK80CoexIjiOrNMPvkQa
  365. q+8kCvFjAzKxOK7e8gjM8lMRiGodb61kmYZkkJzFwWO4EaGbl34lfVECd1Ixp3tF
  366. be0OWAGBAoGBAPSteXDzzToD8ovM7LL11x0jWwI6HOiHu89kZtW566rIezjWBuA2
  367. TxrcYKM3h9jQRXS3CsMdoIv6XGk5lqM8ADtjn23FBWe/THYLh8bm8JOgh5RRWQDg
  368. SvkLfi9Ih2mM4NJfmuuDOh3Nze2efLM7+kOZWUQwF2Zx9mL5jvRBk351AoGBAPDI
  369. sYmT2Li+i5+0vykA2m5uPF8ZOW8BGtAfCZv0suW7BNzSgin78g9WapRd/4p0NNiL
  370. /nVMqPPCpd1akCUpV+GDWQt0hV+HZjxANE0KWhciQRyo2qvo51j8SWILJSgh0tXC
  371. aTF8qt6oGw3VN3m57vKhbrlDaz0J/NDJFci6msAnAoGBAOuG6bXPGijUj+//DYKf
  372. n7jOxdZ49kboEePrtAncdHzri6IEdI3z+WXT6bpzw/LzWUimwldb96WHFNm9s8Hi
  373. Ch8hIODbnP5naUTgiIzw1XhmONyPCewL/F+LrqX5XVA/alNX8JrwsUrrR2WLAGLQ
  374. Q3I69XDsEjptTU2tCO0bCs3ZAoGBAJ2lCHfm0JHET230zONvp5N9oREyVqQSuRdh
  375. +syc3TQDyh85w/bw+X6JOaaCFHj1tFPC9Iqf8k4GNspCLPXnp54CfR4+38O3xnvU
  376. HWoDSRC0YKT++IxtJGriYrlKSr2Hx54kdvLriIPW1D+uRW/xCDza7L9nIKMKEvgv
  377. b4/IfOEpAoGAeKM9Te7T1VzlAkS0CJOwanzwYV/zrex84WuXxlsGgPQ871lTs5AP
  378. H1QLfLfFXH+UVrCEC2yv4eml/cqFkpB3gE5i4MQ8GPVIOSs5tsIyl8YUA03vdNdB
  379. GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
  380. -----END RSA PRIVATE KEY-----'''
  381. # 使用密钥文件方式
  382. # private_key_file_path = os.path.join(BASE_DIR, 'static/iotCore/private.pem')#.replace('\\', '/')
  383. # private_key_file = open(private_key_file_path, 'r')
  384. private_key = ct.load_privatekey(ct.FILETYPE_PEM, private_key_file)
  385. signature = ct.sign(private_key, Token.encode('utf8'), 'sha256')
  386. signature = encodebytes(signature).decode('utf8').replace('\n', '')
  387. # print('signature:', signature)
  388. return signature
  389. @staticmethod
  390. def get_jump_type(event_type):
  391. """
  392. 获取跳转类型
  393. @param event_type: 事件类型
  394. @return event_type: 跳转类型,1:推送消息,2:系统消息,3:音视频通话消息
  395. """
  396. # 跳转类型,1:推送消息,2:系统消息,3:音视频通话消息
  397. jump_type = 1
  398. event_type = int(event_type)
  399. if event_type in EventTypeEnumObj.SYS_MSG_EVENT_TYPE_LIST.value:
  400. jump_type = 2
  401. elif event_type in EventTypeEnumObj.DATA_PUSH_EVENT_TYPE_LIST.value:
  402. jump_type = 3
  403. return jump_type