CommonService.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. # -*- coding: utf-8 -*-
  2. # 高复用性函数封装到CommonService类
  3. import base64
  4. import datetime
  5. import time
  6. from pathlib import Path
  7. from random import Random
  8. import ipdb
  9. import requests
  10. import simplejson as json
  11. from django.core import serializers
  12. from django.http import HttpResponseRedirect
  13. from django.utils import timezone
  14. from pyipip import IPIPDatabase
  15. from Ansjer.config import BASE_DIR, UNICODE_ASCII_CHARACTER_SET, SERVER_DOMAIN_SSL
  16. import OpenSSL.crypto as ct
  17. from base64 import encodebytes
  18. from Controller.CheckUserData import RandomStr
  19. from Model.models import iotdeviceInfoModel, Device_Info
  20. from Service.ModelService import ModelService
  21. class CommonService:
  22. # 添加模糊搜索
  23. @staticmethod
  24. def get_kwargs(data={}):
  25. kwargs = {}
  26. for (k, v) in data.items():
  27. if v is not None and v != u'':
  28. kwargs[k + '__icontains'] = v
  29. return kwargs
  30. # 定义静态方法
  31. # 格式化query_set转dict
  32. @staticmethod
  33. def qs_to_dict(query_set):
  34. sqlJSON = serializers.serialize('json', query_set)
  35. sqlList = json.loads(sqlJSON)
  36. sqlDict = dict(zip(["datas"], [sqlList]))
  37. return sqlDict
  38. # 格式化query_set转dict
  39. @staticmethod
  40. def request_dict_to_dict(request_dict):
  41. # 传参格式转换,键包含meta获取meta[]中的值,值'true'/'false'转为True,False
  42. key_list = []
  43. value_list = []
  44. for k, v in request_dict.items():
  45. key_list.append(k[k.index('[')+1:k.index(']')] if 'meta' in k else k)
  46. if v == 'true':
  47. v = True
  48. elif v == 'false':
  49. v = False
  50. value_list.append(v)
  51. data_dict = dict(zip(key_list, value_list))
  52. print(data_dict)
  53. return data_dict
  54. # 获取文件大小
  55. @staticmethod
  56. def get_file_size(file_path='', suffix_type='', decimal_point=0):
  57. # for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
  58. # path = Path() / 'D:/TestServer/123444.mp4'
  59. path = Path() / file_path
  60. size = path.stat().st_size
  61. mb_size = 0.0
  62. if suffix_type == 'MB':
  63. mb_size = size / 1024.0 / 1024.0
  64. if decimal_point != 0:
  65. mb_size = round(mb_size, decimal_point)
  66. return mb_size
  67. @staticmethod
  68. def get_param_flag(data=[]):
  69. # print(data)
  70. flag = True
  71. for v in data:
  72. if v is None:
  73. flag = False
  74. break
  75. return flag
  76. @staticmethod
  77. def get_ip_address(request):
  78. """
  79. 获取ip地址
  80. :param request:
  81. :return:
  82. """
  83. try:
  84. real_ip = request.META['HTTP_X_FORWARDED_FOR']
  85. clientIP = real_ip.split(",")[0]
  86. except:
  87. try:
  88. clientIP = request.META['REMOTE_ADDR']
  89. except Exception as e:
  90. clientIP = ''
  91. return clientIP
  92. # @获取一天每个小时的datetime.datetime
  93. @staticmethod
  94. def getTimeDict(times):
  95. time_dict = {}
  96. t = 0
  97. for x in range(24):
  98. if x < 10:
  99. x = '0' + str(x)
  100. else:
  101. x = str(x)
  102. a = times.strftime("%Y-%m-%d") + " " + x + ":00:00"
  103. time_dict[t] = timezone.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
  104. t += 1
  105. return time_dict
  106. # 根据ip获取地址
  107. @staticmethod
  108. def getAddr(ip):
  109. print('start_time=' + str(time.time()))
  110. base_dir = BASE_DIR
  111. # ip数据库
  112. db = IPIPDatabase(base_dir + '/DB/17monipdb.dat')
  113. addr = db.lookup(ip)
  114. # ModelService.add_tmp_log(addr)
  115. ts = addr.split('\t')[0]
  116. print('end_time=' + str(time.time()))
  117. return ts
  118. # 通过ip检索ipip指定信息 lang为CN或EN
  119. @staticmethod
  120. def getIpIpInfo(ip, lang, update=False):
  121. ipbd_dir = BASE_DIR + "/DB/mydata4vipday2.ipdb"
  122. db = ipdb.City(ipbd_dir)
  123. if update:
  124. rr = db.reload(ipbd_dir)
  125. info = db.find_map(ip, lang)
  126. return info
  127. @staticmethod
  128. def getUserID(userPhone='13800138000', getUser=True, setOTAID=False, μs=True):
  129. if μs == True:
  130. if getUser == True:
  131. timeID = str(round(time.time() * 1000000))
  132. userID = timeID + userPhone
  133. return userID
  134. else:
  135. if setOTAID == False:
  136. timeID = str(round(time.time() * 1000000))
  137. ID = userPhone + timeID
  138. return ID
  139. else:
  140. timeID = str(round(time.time() * 1000000))
  141. eID = '13800' + timeID + '138000'
  142. return eID
  143. else:
  144. if getUser == True:
  145. timeID = str(round(time.time() * 1000))
  146. userID = timeID + userPhone
  147. return userID
  148. else:
  149. if setOTAID == False:
  150. timeID = str(round(time.time() * 1000))
  151. ID = userPhone + timeID
  152. return ID
  153. else:
  154. timeID = str(round(time.time() * 1000))
  155. eID = '13800' + timeID + '138000'
  156. return eID
  157. # 生成随机数
  158. @staticmethod
  159. def RandomStr(randomlength=8, number=True):
  160. str = ''
  161. if number == False:
  162. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  163. 'tUuVvWwXxYyZz0123456789'
  164. else:
  165. characterSet = '0123456789'
  166. length = len(characterSet) - 1
  167. random = Random()
  168. for index in range(randomlength):
  169. str += characterSet[random.randint(0, length)]
  170. return str
  171. # 生成订单好
  172. @staticmethod
  173. def createOrderID():
  174. random_id = CommonService.RandomStr(6, True)
  175. order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random_id)
  176. print('orderID:')
  177. print(order_id)
  178. return order_id
  179. # qs转换list datetime处理
  180. @staticmethod
  181. def qs_to_list(qs):
  182. res = []
  183. # print(qs)
  184. for ps in qs:
  185. try:
  186. if 'time' in ps:
  187. ps['time'] = ps['time'].strftime("%Y-%m-%d %H:%M:%S")
  188. if 'add_time' in ps:
  189. ps['add_time'] = ps['add_time'].strftime("%Y-%m-%d %H:%M:%S")
  190. if 'update_time' in ps:
  191. ps['update_time'] = ps['update_time'].strftime("%Y-%m-%d %H:%M:%S")
  192. if 'end_time' in ps:
  193. ps['end_time'] = ps['end_time'].strftime("%Y-%m-%d %H:%M:%S")
  194. if 'data_joined' in ps:
  195. if ps['data_joined']:
  196. ps['data_joined'] = ps['data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  197. else:
  198. ps['data_joined'] = ''
  199. if 'userID__data_joined' in ps:
  200. if ps['userID__data_joined']:
  201. ps['userID__data_joined'] = ps['userID__data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  202. else:
  203. ps['userID__data_joined'] = ''
  204. except Exception as e:
  205. pass
  206. res.append(ps)
  207. return res
  208. # 获取当前时间
  209. @staticmethod
  210. def get_now_time_str(n_time, tz, lang):
  211. print(n_time)
  212. print(tz)
  213. print(lang)
  214. n_time = int(n_time) + 3600 * float(tz)
  215. if lang == 'cn':
  216. return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))
  217. else:
  218. return time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(int(n_time)))
  219. # 生成随机数
  220. @staticmethod
  221. def encrypt_data(randomlength=8, number=False):
  222. str = ''
  223. if number == False:
  224. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  225. 'tUuVvWwXxYyZz0123456789'
  226. else:
  227. characterSet = '0123456789'
  228. length = len(characterSet) - 1
  229. random = Random()
  230. for index in range(randomlength):
  231. str += characterSet[random.randint(0, length)]
  232. return str
  233. @staticmethod
  234. def decode_data(content, start=1, end=4):
  235. if not content:
  236. return ''
  237. try:
  238. for i in range(start, end):
  239. if i == 1:
  240. content = base64.b64decode(content)
  241. content = content.decode('utf-8')
  242. content = content[1:-1]
  243. if i == 2:
  244. content = base64.b64decode(content)
  245. content = content.decode('utf-8')
  246. content = content[2:-2]
  247. if i == 3:
  248. content = base64.b64decode(content)
  249. content = content.decode('utf-8')
  250. content = content[3:-3]
  251. return content
  252. except Exception as e:
  253. print(e)
  254. return None
  255. @staticmethod
  256. def encode_data(content, start=1, end=4):
  257. if not content:
  258. return ''
  259. for i in range(start, end):
  260. if i == 1:
  261. content = RandomStr(3, False)+content+RandomStr(3, False)
  262. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  263. if i == 2:
  264. content = RandomStr(2, False)+str(content)+RandomStr(2, False)
  265. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  266. if i == 3:
  267. content = RandomStr(1, False)+str(content)+RandomStr(1, False)
  268. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  269. return content
  270. #把格式化时间转换成时间戳
  271. @staticmethod
  272. def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):
  273. if str_time:
  274. time_tuple = time.strptime(str_time, format) # 把格式化好的时间转换成元祖
  275. result = time.mktime(time_tuple) # 把时间元祖转换成时间戳
  276. return int(result)
  277. return int(time.time())
  278. # 把时间戳转换成格式化
  279. @staticmethod
  280. def timestamp_to_str(timestamp=None, format='%Y-%m-%d %H:%M:%S'):
  281. if timestamp:
  282. time_tuple = time.localtime(timestamp) # 把时间戳转换成时间元祖
  283. result = time.strftime(format, time_tuple) # 把时间元祖转换成格式化好的时间
  284. return result
  285. else:
  286. return time.strptime(format)
  287. #计算N个月后的时间戳
  288. @staticmethod
  289. def calcMonthLater(addMonth, unix_timestamp=None):
  290. if unix_timestamp:
  291. now_year = time.localtime(unix_timestamp).tm_year
  292. now_month = time.localtime(unix_timestamp).tm_mon
  293. now_day = time.localtime(unix_timestamp).tm_mday
  294. now_hour = time.localtime(unix_timestamp).tm_hour
  295. now_min = time.localtime(unix_timestamp).tm_min
  296. now_second = time.localtime(unix_timestamp).tm_sec
  297. else:
  298. now_year = datetime.datetime.now().year
  299. now_month = datetime.datetime.now().month
  300. now_day = datetime.datetime.now().day
  301. now_hour = datetime.datetime.now().hour
  302. now_min = datetime.datetime.now().minute
  303. now_second = datetime.datetime.now().second
  304. for add in range(addMonth):
  305. if now_month == 12:
  306. now_year += 1
  307. now_month = 1
  308. else:
  309. now_month += 1
  310. for is_format in range(4):
  311. try:
  312. date_format = '{now_year}-{now_month}-{now_day} {now_hour}:{now_min}:{now_second}' \
  313. .format(now_year=now_year,now_month=now_month,now_day=now_day,now_hour=now_hour,
  314. now_min=now_min,now_second=now_second)
  315. timestamps = CommonService.str_to_timestamp(date_format)
  316. except Exception as e:
  317. if str(e) == 'day is out of range for month':
  318. now_day = now_day - 1
  319. return timestamps
  320. @staticmethod
  321. def updateMac(mac: str):
  322. macArray = mac.split(':')
  323. macArray[0] = int(macArray[0], 16)
  324. macArray[1] = int(macArray[1], 16)
  325. macArray[2] = int(macArray[2], 16)
  326. first = int(macArray[5], 16)
  327. second = int(macArray[4], 16)
  328. three = int(macArray[3], 16)
  329. # print(macArray)
  330. # print(first)
  331. # print(second)
  332. # print(three)
  333. if first == 255 and second == 255 and three == 255:
  334. return None
  335. first += 1
  336. if first / 256 == 1:
  337. second += 1
  338. first = first % 256
  339. if second / 256 == 1:
  340. three += 1
  341. second = second % 256
  342. macArray[3] = three
  343. macArray[4] = second
  344. macArray[5] = first
  345. # print(macArray)
  346. tmp = ':'.join(map(lambda x: "%02x" % x, macArray))
  347. # print(tmp)
  348. return tmp.upper()
  349. @staticmethod
  350. def decode_data(content, start=1, end=4):
  351. if not content:
  352. return ''
  353. try:
  354. for i in range(start, end):
  355. if i == 1:
  356. content = base64.b64decode(content)
  357. content = content.decode('utf-8')
  358. content = content[1:-1]
  359. if i == 2:
  360. content = base64.b64decode(content)
  361. content = content.decode('utf-8')
  362. content = content[2:-2]
  363. if i == 3:
  364. content = base64.b64decode(content)
  365. content = content.decode('utf-8')
  366. content = content[3:-3]
  367. print(content)
  368. return content
  369. except Exception as e:
  370. print(e)
  371. return None
  372. @staticmethod
  373. def encode_data(content, start=1, end=4):
  374. if not content:
  375. return ''
  376. for i in range(start, end):
  377. if i == 1:
  378. content = CommonService.RandomStr(3, False) + content + CommonService.RandomStr(3, False)
  379. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  380. if i == 2:
  381. content = CommonService.RandomStr(2, False) + str(content) + CommonService.RandomStr(2, False)
  382. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  383. if i == 3:
  384. content = CommonService.RandomStr(1, False) + str(content) + CommonService.RandomStr(1, False)
  385. content = base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  386. return content
  387. @staticmethod
  388. def encode_data_without_salt(content):
  389. return base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  390. @staticmethod
  391. def check_time_stamp_token(token, time_stamp):
  392. # 时间戳token校验
  393. if not all([token, time_stamp]):
  394. return False
  395. try:
  396. token = int(CommonService.decode_data(token))
  397. time_stamp = int(time_stamp)
  398. now_time = int(time.time())
  399. distance = now_time - time_stamp
  400. if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
  401. return False
  402. return True
  403. except Exception as e:
  404. print(e)
  405. return False
  406. @staticmethod
  407. def check_time_stamp_token_without_distance(time_stamp_token, time_stamp):
  408. """
  409. 用于没有RTC设备的时间戳token校验
  410. @param time_stamp: 时间戳
  411. @param time_stamp_token: 时间戳token
  412. @return: boolean True/False
  413. """
  414. if not all([time_stamp_token, time_stamp]):
  415. return False
  416. try:
  417. token = CommonService.decode_data(time_stamp_token)
  418. if token != time_stamp:
  419. return False
  420. return True
  421. except Exception as e:
  422. print(e)
  423. return False
  424. @staticmethod
  425. def req_publish_mqtt_msg(thing_name, topic_name, msg):
  426. # 通用发布MQTT消息函数
  427. if not all([thing_name, topic_name, msg]):
  428. return False
  429. try:
  430. # 获取数据组织将要请求的url
  431. iot = iotdeviceInfoModel.objects.filter(
  432. thing_name__icontains=thing_name).values(
  433. 'endpoint', 'token_iot_number')
  434. if not iot.exists():
  435. return False
  436. endpoint = iot[0]['endpoint']
  437. Token = iot[0]['token_iot_number']
  438. # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
  439. # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  440. # post请求url发布MQTT消息
  441. url = 'https://{}/topics/{}'.format(endpoint, topic_name)
  442. authorizer_name = 'Ansjer_Iot_Auth'
  443. signature = CommonService.rsa_sign(Token) # Token签名
  444. headers = {
  445. 'x-amz-customauthorizer-name': authorizer_name,
  446. 'Token': Token,
  447. 'x-amz-customauthorizer-signature': signature}
  448. r = requests.post(url=url, headers=headers, json=msg, timeout=2)
  449. if r.status_code == 200:
  450. res = r.json()
  451. if res['message'] == 'OK':
  452. return True
  453. return False
  454. else:
  455. return False
  456. except Exception as e:
  457. return False
  458. @staticmethod
  459. def rsa_sign(Token):
  460. # 私钥签名Token
  461. if not Token:
  462. return ''
  463. private_key_file = '''-----BEGIN RSA PRIVATE KEY-----
  464. MIIEpQIBAAKCAQEA5iJzEDPqtGmFMggekVro6C0lrjuC2BjunGkrFNJWpDYzxCzE
  465. X5jf4/Fq7hcIaQd5sqHugDxPVollSLPe9zNilbrd0sZfU+Ed8gRVuKW9KwfE9XFr
  466. L0pt6bKRQ0IIRfiZ9TuR0tsQysvcO1GZSXcYfPue3tGM1zOnWFThWDqZ06+sOxzt
  467. RMRl4yNfbpCG4MfxG3itNXOfrjZv2OMLSXrxmzubSvRpUYSvQPs4fm9302SAnySY
  468. 0MKzx6H6528ZQm/IDDSZy6EmNBIyTRDfxC56vnYcXvqedAQh7jJnjdvt6Q4MhASH
  469. eIYi1FBSdu2NT6wgpnrqXzx5pq9kR/lnsLID0wIDAQABAoIBAQCiF4GT1/1oNSpr
  470. ouxk1PNXFPWFUsVGD8mAwVJmx//eiY7MjfuCmdqYYmI+cFqsH2fIOeYSzGfVO9Dq
  471. 9EYHN1oovAWhf7eFDPpajFMUSyiCNmazub8VAAeKowtNpCTPo9pMsDh1m3aoYA4u
  472. ebrN0+Sbo16y8kWRDgDAZoiR7DSMs8lczk16hwfv5mw8XpNDbaL3Coi4Koe2S1Yh
  473. 2SX3vWFlpd7qF1ZYXuZIp+b8JPrV7n9eUKoFgzj0gqgwQK80CoexIjiOrNMPvkQa
  474. q+8kCvFjAzKxOK7e8gjM8lMRiGodb61kmYZkkJzFwWO4EaGbl34lfVECd1Ixp3tF
  475. be0OWAGBAoGBAPSteXDzzToD8ovM7LL11x0jWwI6HOiHu89kZtW566rIezjWBuA2
  476. TxrcYKM3h9jQRXS3CsMdoIv6XGk5lqM8ADtjn23FBWe/THYLh8bm8JOgh5RRWQDg
  477. SvkLfi9Ih2mM4NJfmuuDOh3Nze2efLM7+kOZWUQwF2Zx9mL5jvRBk351AoGBAPDI
  478. sYmT2Li+i5+0vykA2m5uPF8ZOW8BGtAfCZv0suW7BNzSgin78g9WapRd/4p0NNiL
  479. /nVMqPPCpd1akCUpV+GDWQt0hV+HZjxANE0KWhciQRyo2qvo51j8SWILJSgh0tXC
  480. aTF8qt6oGw3VN3m57vKhbrlDaz0J/NDJFci6msAnAoGBAOuG6bXPGijUj+//DYKf
  481. n7jOxdZ49kboEePrtAncdHzri6IEdI3z+WXT6bpzw/LzWUimwldb96WHFNm9s8Hi
  482. Ch8hIODbnP5naUTgiIzw1XhmONyPCewL/F+LrqX5XVA/alNX8JrwsUrrR2WLAGLQ
  483. Q3I69XDsEjptTU2tCO0bCs3ZAoGBAJ2lCHfm0JHET230zONvp5N9oREyVqQSuRdh
  484. +syc3TQDyh85w/bw+X6JOaaCFHj1tFPC9Iqf8k4GNspCLPXnp54CfR4+38O3xnvU
  485. HWoDSRC0YKT++IxtJGriYrlKSr2Hx54kdvLriIPW1D+uRW/xCDza7L9nIKMKEvgv
  486. b4/IfOEpAoGAeKM9Te7T1VzlAkS0CJOwanzwYV/zrex84WuXxlsGgPQ871lTs5AP
  487. H1QLfLfFXH+UVrCEC2yv4eml/cqFkpB3gE5i4MQ8GPVIOSs5tsIyl8YUA03vdNdB
  488. GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
  489. -----END RSA PRIVATE KEY-----'''
  490. # 使用密钥文件方式
  491. # private_key_file_path = os.path.join(BASE_DIR, 'static/iotCore/private.pem')#.replace('\\', '/')
  492. # private_key_file = open(private_key_file_path, 'r')
  493. private_key = ct.load_privatekey(ct.FILETYPE_PEM, private_key_file)
  494. signature = ct.sign(private_key, Token.encode('utf8'), 'sha256')
  495. signature = encodebytes(signature).decode('utf8').replace('\n', '')
  496. # print('signature:', signature)
  497. return signature
  498. @staticmethod
  499. def get_payment_status_url(lang, payment_status):
  500. # 返回相应的支付状态url
  501. if lang == 'cn':
  502. file_name = 'success.html' if payment_status == 'success' else 'fail.html'
  503. else:
  504. file_name = 'en_success.html' if payment_status == 'success' else 'en_fail.html'
  505. pay_failed_url = "{}web/paid2/{}".format(SERVER_DOMAIN_SSL, file_name)
  506. return pay_failed_url
  507. # 根据uid查询序列号,存在则返回序列号,否则返uid
  508. @staticmethod
  509. def query_serial_with_uid(uid):
  510. device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number')
  511. if device_info_qs.exists():
  512. serial_number = device_info_qs[0]['serial_number']
  513. if serial_number:
  514. return serial_number
  515. return uid