CommonService.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. import base64
  2. import calendar
  3. import datetime
  4. import time
  5. from base64 import encodebytes
  6. from distutils.version import LooseVersion
  7. from pathlib import Path
  8. from random import Random
  9. import OpenSSL.crypto as ct
  10. import ipdb
  11. import requests
  12. import simplejson as json
  13. from dateutil.relativedelta import relativedelta
  14. from django.core import serializers
  15. from django.db.models import F
  16. from django.utils import timezone
  17. from django.utils.crypto import constant_time_compare
  18. from pyipip import IPIPDatabase
  19. from Ansjer.config import BASE_DIR, SERVER_DOMAIN_SSL, CONFIG_INFO, CONFIG_TEST, CONFIG_CN, SERVER_DOMAIN_TEST, \
  20. SERVER_DOMAIN_CN, SERVER_DOMAIN_US, CONFIG_US, CONFIG_EUR, SERVER_DOMAIN_LIST, SERVER_DOMAIN_EUR, ALEXA_DOMAIN
  21. from Controller.CheckUserData import RandomStr
  22. from Model.models import iotdeviceInfoModel, Device_Info, UIDModel, AppDeviceType, UIDCompanySerialModel, GatewayPush, \
  23. Device_User
  24. from Object.AWS.S3Email import S3Email
  25. from Object.ResponseObject import ResponseObject
  26. from Object.TokenObject import TokenObject
  27. class CommonService:
  28. # 高复用性函数类
  29. @staticmethod
  30. def get_kwargs(data=None):
  31. # 添加模糊搜索
  32. if data is None:
  33. data = {}
  34. kwargs = {}
  35. for (k, v) in data.items():
  36. if v is not None and v != u'':
  37. kwargs[k + '__icontains'] = v
  38. return kwargs
  39. @staticmethod
  40. def qs_to_dict(query_set):
  41. # 格式化query_set转dict
  42. sqlJSON = serializers.serialize('json', query_set)
  43. sqlList = json.loads(sqlJSON)
  44. sqlDict = dict(zip(["datas"], [sqlList]))
  45. return sqlDict
  46. # 格式化query_set转dict
  47. @staticmethod
  48. def request_dict_to_dict(request_dict):
  49. # 传参格式转换,键包含meta获取meta[]中的值,值'true'/'false'转为True,False
  50. key_list = []
  51. value_list = []
  52. for k, v in request_dict.items():
  53. key_list.append(k[k.index('[') + 1:k.index(']')] if 'meta' in k else k)
  54. if v == 'true':
  55. v = True
  56. elif v == 'false':
  57. v = False
  58. value_list.append(v)
  59. data_dict = dict(zip(key_list, value_list))
  60. print(data_dict)
  61. return data_dict
  62. # 获取文件大小
  63. @staticmethod
  64. def get_file_size(file_path='', suffix_type='', decimal_point=0):
  65. # for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
  66. # path = Path() / 'D:/TestServer/123444.mp4'
  67. path = Path() / file_path
  68. size = path.stat().st_size
  69. mb_size = 0.0
  70. if suffix_type == 'MB':
  71. mb_size = size / 1024.0 / 1024.0
  72. if decimal_point != 0:
  73. mb_size = round(mb_size, decimal_point)
  74. return mb_size
  75. @staticmethod
  76. def get_param_flag(data=None):
  77. # print(data)
  78. if data is None:
  79. data = []
  80. flag = True
  81. for v in data:
  82. if v is None:
  83. flag = False
  84. break
  85. return flag
  86. @staticmethod
  87. def get_ip_address(request):
  88. """
  89. 获取ip地址
  90. :param request:
  91. :return:
  92. """
  93. try:
  94. real_ip = request.META['HTTP_X_FORWARDED_FOR']
  95. clientIP = real_ip.split(",")[0]
  96. except:
  97. try:
  98. clientIP = request.META['REMOTE_ADDR']
  99. except Exception as e:
  100. clientIP = ''
  101. return clientIP
  102. # @获取一天每个小时的datetime.datetime
  103. @staticmethod
  104. def getTimeDict(times):
  105. time_dict = {}
  106. t = 0
  107. for x in range(24):
  108. if x < 10:
  109. x = '0' + str(x)
  110. else:
  111. x = str(x)
  112. a = times.strftime("%Y-%m-%d") + " " + x + ":00:00"
  113. time_dict[t] = timezone.datetime.strptime(a, '%Y-%m-%d %H:%M:%S')
  114. t += 1
  115. return time_dict
  116. # 根据ip获取地址
  117. @staticmethod
  118. def getAddr(ip):
  119. print('start_time=' + str(time.time()))
  120. base_dir = BASE_DIR
  121. # ip数据库
  122. db = IPIPDatabase(base_dir + '/DB/17monipdb.dat')
  123. addr = db.lookup(ip)
  124. # ModelService.add_tmp_log(addr)
  125. ts = addr.split('\t')[0]
  126. print('end_time=' + str(time.time()))
  127. return ts
  128. # 通过ip检索ipip指定信息 lang为CN或EN
  129. @staticmethod
  130. def getIpIpInfo(ip, lang, update=False):
  131. ipbd_dir = BASE_DIR + "/DB/mydata4vipday2.ipdb"
  132. db = ipdb.City(ipbd_dir)
  133. if update:
  134. rr = db.reload(ipbd_dir)
  135. info = db.find_map(ip, lang)
  136. return info
  137. @staticmethod
  138. def getUserID(userPhone='13800138000', getUser=True, setOTAID=False, μs=True):
  139. if μs == True:
  140. if getUser == True:
  141. timeID = str(round(time.time() * 1000000))
  142. userID = timeID + userPhone
  143. return userID
  144. else:
  145. if setOTAID == False:
  146. timeID = str(round(time.time() * 1000000))
  147. ID = userPhone + timeID
  148. return ID
  149. else:
  150. timeID = str(round(time.time() * 1000000))
  151. eID = '13800' + timeID + '138000'
  152. return eID
  153. else:
  154. if getUser == True:
  155. timeID = str(round(time.time() * 1000))
  156. userID = timeID + userPhone
  157. return userID
  158. else:
  159. if setOTAID == False:
  160. timeID = str(round(time.time() * 1000))
  161. ID = userPhone + timeID
  162. return ID
  163. else:
  164. timeID = str(round(time.time() * 1000))
  165. eID = '13800' + timeID + '138000'
  166. return eID
  167. @staticmethod
  168. def get_username(userID):
  169. """
  170. 根据用户id获取用户名/邮箱/电话
  171. @param userID: 用户id
  172. @return:
  173. """
  174. if userID:
  175. device_user_qs = Device_User.objects.filter(userID=userID).values('username', 'userEmail', 'phone')
  176. if device_user_qs.exists():
  177. if device_user_qs[0]['username']:
  178. return device_user_qs[0]['username']
  179. elif device_user_qs[0]['userEmail']:
  180. return device_user_qs[0]['userEmail']
  181. elif device_user_qs[0]['phone']:
  182. return device_user_qs[0]['phone']
  183. return ''
  184. # 生成随机数
  185. @staticmethod
  186. def RandomStr(randomlength=8, number=True):
  187. str = ''
  188. if number == False:
  189. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  190. 'tUuVvWwXxYyZz0123456789'
  191. else:
  192. characterSet = '0123456789'
  193. length = len(characterSet) - 1
  194. random = Random()
  195. for index in range(randomlength):
  196. str += characterSet[random.randint(0, length)]
  197. return str
  198. # 生成订单好
  199. @staticmethod
  200. def createOrderID():
  201. random_id = CommonService.RandomStr(6, True)
  202. order_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + str(random_id)
  203. print('orderID:')
  204. print(order_id)
  205. return order_id
  206. # qs转换list datetime处理
  207. @staticmethod
  208. def qs_to_list(qs):
  209. res = []
  210. # print(qs)
  211. for ps in qs:
  212. try:
  213. if 'time' in ps:
  214. ps['time'] = ps['time'].strftime("%Y-%m-%d %H:%M:%S")
  215. if 'add_time' in ps:
  216. ps['add_time'] = ps['add_time'].strftime("%Y-%m-%d %H:%M:%S")
  217. if 'update_time' in ps:
  218. ps['update_time'] = ps['update_time'].strftime("%Y-%m-%d %H:%M:%S")
  219. if 'end_time' in ps:
  220. ps['end_time'] = ps['end_time'].strftime("%Y-%m-%d %H:%M:%S")
  221. if 'data_joined' in ps:
  222. if ps['data_joined']:
  223. ps['data_joined'] = ps['data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  224. else:
  225. ps['data_joined'] = ''
  226. if 'userID__data_joined' in ps:
  227. if ps['userID__data_joined']:
  228. ps['userID__data_joined'] = ps['userID__data_joined'].strftime("%Y-%m-%d %H:%M:%S")
  229. else:
  230. ps['userID__data_joined'] = ''
  231. except Exception as e:
  232. pass
  233. res.append(ps)
  234. return res
  235. # 获取当前时间
  236. @staticmethod
  237. def get_now_time_str(n_time, tz, lang):
  238. print(n_time)
  239. print(tz)
  240. print(lang)
  241. n_time = int(n_time) + 3600 * float(tz)
  242. if lang == 'cn':
  243. return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(n_time)))
  244. else:
  245. return time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(int(n_time)))
  246. # 生成随机数
  247. @staticmethod
  248. def encrypt_data(randomlength=8, number=False):
  249. str = ''
  250. if number == False:
  251. characterSet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsT' \
  252. 'tUuVvWwXxYyZz0123456789'
  253. else:
  254. characterSet = '0123456789'
  255. length = len(characterSet) - 1
  256. random = Random()
  257. for index in range(randomlength):
  258. str += characterSet[random.randint(0, length)]
  259. return str
  260. @staticmethod
  261. def encode_data(content, start=1, end=4):
  262. """
  263. 数据加密
  264. @param content: 数据内容
  265. @param start: 起始长度
  266. @param end: 结束长度
  267. @return content: 加密的数据
  268. """
  269. if not content:
  270. return ''
  271. for i in range(start, end):
  272. length = end - i
  273. content = RandomStr(length, False) + content + RandomStr(length, False)
  274. content = base64.b64encode(str(content).encode('utf-8')).decode('utf8')
  275. return content
  276. @staticmethod
  277. def decode_data(content, start=1, end=4):
  278. """
  279. 数据解密
  280. @param content: 数据内容
  281. @param start: 起始长度
  282. @param end: 结束长度
  283. @return content: 解密的数据
  284. """
  285. if not content:
  286. return ''
  287. for i in range(start, end):
  288. content = base64.b64decode(content)
  289. content = content.decode('utf-8')
  290. content = content[i:-i]
  291. return content
  292. # 把格式化时间转换成时间戳
  293. @staticmethod
  294. def str_to_timestamp(str_time=None, format='%Y-%m-%d %H:%M:%S'):
  295. if str_time:
  296. time_tuple = time.strptime(str_time, format) # 把格式化好的时间转换成元祖
  297. result = time.mktime(time_tuple) # 把时间元祖转换成时间戳
  298. return int(result)
  299. return int(time.time())
  300. # 把时间戳转换成格式化
  301. @staticmethod
  302. def timestamp_to_str(timestamp=None, format='%Y-%m-%d %H:%M:%S'):
  303. if timestamp:
  304. time_tuple = time.localtime(timestamp) # 把时间戳转换成时间元祖
  305. result = time.strftime(format, time_tuple) # 把时间元祖转换成格式化好的时间
  306. return result
  307. else:
  308. return time.strptime(format)
  309. # 计算N个月后的时间戳
  310. @staticmethod
  311. def calcMonthLater(addMonth, unix_timestamp=None):
  312. if unix_timestamp:
  313. now_year = time.localtime(unix_timestamp).tm_year
  314. now_month = time.localtime(unix_timestamp).tm_mon
  315. now_day = time.localtime(unix_timestamp).tm_mday
  316. now_hour = time.localtime(unix_timestamp).tm_hour
  317. now_min = time.localtime(unix_timestamp).tm_min
  318. now_second = time.localtime(unix_timestamp).tm_sec
  319. else:
  320. now_year = datetime.datetime.now().year
  321. now_month = datetime.datetime.now().month
  322. now_day = datetime.datetime.now().day
  323. now_hour = datetime.datetime.now().hour
  324. now_min = datetime.datetime.now().minute
  325. now_second = datetime.datetime.now().second
  326. for add in range(addMonth):
  327. if now_month == 12:
  328. now_year += 1
  329. now_month = 1
  330. else:
  331. now_month += 1
  332. timestamps = 0
  333. for is_format in range(4):
  334. try:
  335. date_format = '{now_year}-{now_month}-{now_day} {now_hour}:{now_min}:{now_second}' \
  336. .format(now_year=now_year, now_month=now_month, now_day=now_day, now_hour=now_hour,
  337. now_min=now_min, now_second=now_second)
  338. timestamps = CommonService.str_to_timestamp(date_format)
  339. except Exception as e:
  340. if str(e) == 'day is out of range for month':
  341. now_day = now_day - 1
  342. return timestamps
  343. @staticmethod
  344. def updateMac(mac: str):
  345. macArray = mac.split(':')
  346. macArray[0] = int(macArray[0], 16)
  347. macArray[1] = int(macArray[1], 16)
  348. macArray[2] = int(macArray[2], 16)
  349. first = int(macArray[5], 16)
  350. second = int(macArray[4], 16)
  351. three = int(macArray[3], 16)
  352. if first == 255 and second == 255 and three == 255:
  353. return None
  354. first += 1
  355. if first / 256 == 1:
  356. second += 1
  357. first = first % 256
  358. if second / 256 == 1:
  359. three += 1
  360. second = second % 256
  361. macArray[3] = three
  362. macArray[4] = second
  363. macArray[5] = first
  364. tmp = ':'.join(map(lambda x: "%02x" % x, macArray))
  365. return tmp.upper()
  366. @staticmethod
  367. def encode_data_without_salt(content):
  368. return base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  369. @staticmethod
  370. def check_time_stamp_token(token, time_stamp):
  371. # 时间戳token校验
  372. if not all([token, time_stamp]):
  373. return False
  374. try:
  375. token = int(CommonService.decode_data(token))
  376. time_stamp = int(time_stamp)
  377. now_time = int(time.time())
  378. distance = now_time - time_stamp
  379. if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
  380. return False
  381. return True
  382. except Exception as e:
  383. print(e)
  384. return False
  385. @staticmethod
  386. def check_time_stamp_token_without_distance(time_stamp_token, time_stamp):
  387. """
  388. 用于没有RTC设备的时间戳token校验
  389. @param time_stamp: 时间戳
  390. @param time_stamp_token: 时间戳token
  391. @return: boolean True/False
  392. """
  393. if not all([time_stamp_token, time_stamp]):
  394. return False
  395. try:
  396. token = CommonService.decode_data(time_stamp_token)
  397. if token != time_stamp:
  398. return False
  399. return True
  400. except Exception as e:
  401. print(e)
  402. return False
  403. @staticmethod
  404. def req_publish_mqtt_msg(identification_code, topic_name, msg, qos=1):
  405. """
  406. 通用发布MQTT消息函数
  407. @param identification_code: 标识码
  408. @param topic_name: 主题名
  409. @param msg: 消息内容
  410. @param qos: mqtt qos等级
  411. @return: boolean
  412. """
  413. if not all([identification_code, topic_name]):
  414. return False
  415. if identification_code.endswith('11L'):
  416. thing_name = 'LC_' + identification_code
  417. else:
  418. thing_name = 'Ansjer_Device_' + identification_code
  419. try:
  420. # 获取数据组织将要请求的url
  421. iot = iotdeviceInfoModel.objects.filter(
  422. thing_name=thing_name).values(
  423. 'endpoint', 'token_iot_number')
  424. if not iot.exists():
  425. return False
  426. endpoint = iot[0]['endpoint']
  427. Token = iot[0]['token_iot_number']
  428. # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
  429. # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  430. # post请求url发布MQTT消息
  431. url = 'https://{}/topics/{}?qos={}'.format(endpoint, topic_name, qos)
  432. authorizer_name = 'Ansjer_Iot_Auth'
  433. signature = CommonService.rsa_sign(Token) # Token签名
  434. headers = {
  435. 'x-amz-customauthorizer-name': authorizer_name,
  436. 'Token': Token,
  437. 'x-amz-customauthorizer-signature': signature}
  438. r = requests.post(url=url, headers=headers, json=msg, timeout=2)
  439. if r.status_code == 200:
  440. res = r.json()
  441. if res['message'] == 'OK':
  442. return True
  443. return False
  444. else:
  445. return False
  446. except Exception as e:
  447. return False
  448. @staticmethod
  449. def rsa_sign(Token):
  450. # 私钥签名Token
  451. if not Token:
  452. return ''
  453. private_key_file = '''-----BEGIN RSA PRIVATE KEY-----
  454. MIIEpQIBAAKCAQEA5iJzEDPqtGmFMggekVro6C0lrjuC2BjunGkrFNJWpDYzxCzE
  455. X5jf4/Fq7hcIaQd5sqHugDxPVollSLPe9zNilbrd0sZfU+Ed8gRVuKW9KwfE9XFr
  456. L0pt6bKRQ0IIRfiZ9TuR0tsQysvcO1GZSXcYfPue3tGM1zOnWFThWDqZ06+sOxzt
  457. RMRl4yNfbpCG4MfxG3itNXOfrjZv2OMLSXrxmzubSvRpUYSvQPs4fm9302SAnySY
  458. 0MKzx6H6528ZQm/IDDSZy6EmNBIyTRDfxC56vnYcXvqedAQh7jJnjdvt6Q4MhASH
  459. eIYi1FBSdu2NT6wgpnrqXzx5pq9kR/lnsLID0wIDAQABAoIBAQCiF4GT1/1oNSpr
  460. ouxk1PNXFPWFUsVGD8mAwVJmx//eiY7MjfuCmdqYYmI+cFqsH2fIOeYSzGfVO9Dq
  461. 9EYHN1oovAWhf7eFDPpajFMUSyiCNmazub8VAAeKowtNpCTPo9pMsDh1m3aoYA4u
  462. ebrN0+Sbo16y8kWRDgDAZoiR7DSMs8lczk16hwfv5mw8XpNDbaL3Coi4Koe2S1Yh
  463. 2SX3vWFlpd7qF1ZYXuZIp+b8JPrV7n9eUKoFgzj0gqgwQK80CoexIjiOrNMPvkQa
  464. q+8kCvFjAzKxOK7e8gjM8lMRiGodb61kmYZkkJzFwWO4EaGbl34lfVECd1Ixp3tF
  465. be0OWAGBAoGBAPSteXDzzToD8ovM7LL11x0jWwI6HOiHu89kZtW566rIezjWBuA2
  466. TxrcYKM3h9jQRXS3CsMdoIv6XGk5lqM8ADtjn23FBWe/THYLh8bm8JOgh5RRWQDg
  467. SvkLfi9Ih2mM4NJfmuuDOh3Nze2efLM7+kOZWUQwF2Zx9mL5jvRBk351AoGBAPDI
  468. sYmT2Li+i5+0vykA2m5uPF8ZOW8BGtAfCZv0suW7BNzSgin78g9WapRd/4p0NNiL
  469. /nVMqPPCpd1akCUpV+GDWQt0hV+HZjxANE0KWhciQRyo2qvo51j8SWILJSgh0tXC
  470. aTF8qt6oGw3VN3m57vKhbrlDaz0J/NDJFci6msAnAoGBAOuG6bXPGijUj+//DYKf
  471. n7jOxdZ49kboEePrtAncdHzri6IEdI3z+WXT6bpzw/LzWUimwldb96WHFNm9s8Hi
  472. Ch8hIODbnP5naUTgiIzw1XhmONyPCewL/F+LrqX5XVA/alNX8JrwsUrrR2WLAGLQ
  473. Q3I69XDsEjptTU2tCO0bCs3ZAoGBAJ2lCHfm0JHET230zONvp5N9oREyVqQSuRdh
  474. +syc3TQDyh85w/bw+X6JOaaCFHj1tFPC9Iqf8k4GNspCLPXnp54CfR4+38O3xnvU
  475. HWoDSRC0YKT++IxtJGriYrlKSr2Hx54kdvLriIPW1D+uRW/xCDza7L9nIKMKEvgv
  476. b4/IfOEpAoGAeKM9Te7T1VzlAkS0CJOwanzwYV/zrex84WuXxlsGgPQ871lTs5AP
  477. H1QLfLfFXH+UVrCEC2yv4eml/cqFkpB3gE5i4MQ8GPVIOSs5tsIyl8YUA03vdNdB
  478. GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
  479. -----END RSA PRIVATE KEY-----'''
  480. # 使用密钥文件方式
  481. # private_key_file_path = os.path.join(BASE_DIR, 'static/iotCore/private.pem')#.replace('\\', '/')
  482. # private_key_file = open(private_key_file_path, 'r')
  483. private_key = ct.load_privatekey(ct.FILETYPE_PEM, private_key_file)
  484. signature = ct.sign(private_key, Token.encode('utf8'), 'sha256')
  485. signature = encodebytes(signature).decode('utf8').replace('\n', '')
  486. # print('signature:', signature)
  487. return signature
  488. @staticmethod
  489. def get_payment_status_url(lang, payment_status):
  490. # 返回相应的支付状态url
  491. if lang == 'cn':
  492. file_name = 'success.html' if payment_status == 'success' else 'fail.html'
  493. else:
  494. file_name = 'en_success.html' if payment_status == 'success' else 'en_fail.html'
  495. pay_failed_url = "{}web/paid2/{}".format(SERVER_DOMAIN_SSL, file_name)
  496. return pay_failed_url
  497. # 根据uid查询序列号,存在则返回序列号,否则返uid
  498. @staticmethod
  499. def query_serial_with_uid(uid):
  500. device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number')
  501. if device_info_qs.exists():
  502. serial_number = device_info_qs[0]['serial_number']
  503. if serial_number:
  504. return serial_number
  505. return uid
  506. # 根据序列号查询uid,存在则返回uid,否则返回序列号
  507. @staticmethod
  508. def query_uid_with_serial(serial_number):
  509. device_info_qs = Device_Info.objects.filter(serial_number=serial_number).values('UID')
  510. if device_info_qs.exists():
  511. uid = device_info_qs[0]['UID']
  512. if uid:
  513. return uid
  514. return serial_number
  515. @staticmethod
  516. def get_full_serial_number(uid, serial_number, device_type):
  517. """
  518. 根据uid查询返回完整序列号
  519. @param uid: uid
  520. @param serial_number: 9位序列号
  521. @param device_type: 设备类型
  522. @return: full_serial_number
  523. """
  524. p2p_type = str(UIDModel.objects.filter(uid=uid).values('p2p_type')[0]['p2p_type'])
  525. # 设备类型转为16进制并补齐4位
  526. device_type = hex(device_type)[2:]
  527. device_type = (4 - len(device_type)) * '0' + device_type
  528. full_serial_number = serial_number + p2p_type + device_type
  529. return full_serial_number
  530. # 根据企业标识返回物品名
  531. @staticmethod
  532. def get_thing_name(company_mark, thing_name_suffix):
  533. if company_mark == '11A':
  534. return 'Ansjer_Device_' + thing_name_suffix
  535. elif company_mark == '11L':
  536. return 'LC_' + thing_name_suffix
  537. else:
  538. return thing_name_suffix
  539. @staticmethod
  540. def confirm_region_id():
  541. """
  542. 根据配置信息确定region_id
  543. @return: region_id
  544. """
  545. region_id = 3
  546. if CONFIG_INFO == CONFIG_US: # 美洲
  547. region_id = 3
  548. elif CONFIG_INFO == CONFIG_EUR: # 欧洲
  549. region_id = 4
  550. elif CONFIG_INFO == CONFIG_CN: # 中国
  551. region_id = 1
  552. elif CONFIG_INFO == CONFIG_TEST: # 测试
  553. region_id = 5
  554. return region_id
  555. @staticmethod
  556. def verify_token_get_user_id(request_dict, request):
  557. """
  558. 认证token,获取user id
  559. @param request_dict: 请求参数
  560. @param request: 请求体
  561. @return: token_obj.code, token_obj.userID, response
  562. """
  563. try:
  564. token_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  565. lang = request_dict.get('lang', None)
  566. response = ResponseObject(lang if lang else token_obj.lang)
  567. return token_obj.code, token_obj.userID, response
  568. except Exception as e:
  569. print(e)
  570. return 309, None, None
  571. @staticmethod
  572. def cutting_time(start_time, end_time, time_unit):
  573. """
  574. 按时间单位切割时间段
  575. @param start_time: 开始时间
  576. @param end_time: 结束时间
  577. @param time_unit: 时间单位
  578. @return: time_list 切割后的时间列表
  579. """
  580. time_list = []
  581. while True:
  582. if time_unit == 'day':
  583. temp_time = start_time + relativedelta(days=1)
  584. elif time_unit == 'week':
  585. temp_time = start_time + relativedelta(days=7)
  586. elif time_unit == 'month':
  587. temp_time = start_time + relativedelta(months=1)
  588. elif time_unit == 'quarter':
  589. temp_time = start_time + relativedelta(months=3)
  590. elif time_unit == 'year':
  591. temp_time = start_time + relativedelta(years=1)
  592. else:
  593. break
  594. if temp_time < end_time:
  595. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  596. CommonService.str_to_timestamp(temp_time.strftime('%Y-%m-%d %H:%M:%S')))
  597. time_list.append(time_tuple)
  598. start_time = temp_time
  599. else:
  600. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  601. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  602. if time_tuple not in time_list:
  603. time_list.append(time_tuple)
  604. break
  605. if not time_list:
  606. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  607. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  608. time_list = [time_tuple]
  609. return time_list
  610. @staticmethod
  611. def cutting_time_stamp(start_time, end_time):
  612. """
  613. 按天切割时间段
  614. @param start_time: 开始时间
  615. @param end_time: 结束时间
  616. @return: time_list 切割后的时间列表
  617. """
  618. time_list = []
  619. while True:
  620. mid_time = datetime.datetime(start_time.year, start_time.month, start_time.day) + relativedelta(days=1)
  621. if mid_time < end_time:
  622. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  623. CommonService.str_to_timestamp(mid_time.strftime('%Y-%m-%d %H:%M:%S')))
  624. time_list.append(time_tuple)
  625. start_time = mid_time
  626. else:
  627. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  628. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  629. if time_tuple not in time_list:
  630. time_list.append(time_tuple)
  631. break
  632. if not time_list:
  633. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  634. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  635. time_list = [time_tuple]
  636. return time_list
  637. @staticmethod
  638. def get_domain_name():
  639. """
  640. 获取域名
  641. @return: domain_name_list 域名列表
  642. """
  643. if CONFIG_INFO == CONFIG_TEST:
  644. domain_name_list = [SERVER_DOMAIN_TEST[:-1]]
  645. elif CONFIG_INFO == CONFIG_CN or CONFIG_INFO == CONFIG_US or CONFIG_INFO == CONFIG_EUR:
  646. domain_name_list = [SERVER_DOMAIN_US[:-1], SERVER_DOMAIN_CN[:-1], SERVER_DOMAIN_EUR[:-1]]
  647. else:
  648. domain_name_list = []
  649. return domain_name_list
  650. @staticmethod
  651. def get_orders_domain_name_list():
  652. """
  653. 获取其他服务器域名列表
  654. @return: orders_domain_name_list 其他服务器域名列表
  655. """
  656. orders_domain_name_list = SERVER_DOMAIN_LIST
  657. if CONFIG_INFO == CONFIG_TEST:
  658. orders_domain_name_list = [SERVER_DOMAIN_CN, SERVER_DOMAIN_US, SERVER_DOMAIN_EUR]
  659. elif CONFIG_INFO == CONFIG_CN:
  660. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_US, SERVER_DOMAIN_EUR]
  661. elif CONFIG_INFO == CONFIG_US:
  662. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_EUR]
  663. elif CONFIG_INFO == CONFIG_EUR:
  664. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US]
  665. return orders_domain_name_list
  666. @staticmethod
  667. def list_sort(e):
  668. """
  669. 列表排序
  670. @param e: 列表元素
  671. """
  672. return sorted(e, key=lambda item: -item['count'])
  673. @staticmethod
  674. def list_sort_v2(e, order_by):
  675. """
  676. 列表排序
  677. @param e: 列表元素
  678. @param order_by: 排序对象
  679. """
  680. return sorted(e, key=lambda item: item[order_by], reverse=True)
  681. @staticmethod
  682. def Package_Type(order_type, content):
  683. """
  684. 套餐类型
  685. """
  686. if order_type == 0:
  687. content = content + '(' + '云存' + ')'
  688. return content
  689. elif order_type == 1:
  690. content = content + '(' + 'AI' + ')'
  691. return content
  692. elif order_type == 2:
  693. pass
  694. @staticmethod
  695. def is_cloud_device(ucode, device_type):
  696. """
  697. 设备是否支持云存
  698. @param ucode: 设备版本
  699. @param device_type: 设备类型
  700. """
  701. if len(ucode) > 4:
  702. number = ucode[-4]
  703. else:
  704. return False
  705. device_type_qs = AppDeviceType.objects.filter(type=device_type).values('model')
  706. model = device_type_qs[0]['model'] if device_type_qs.exists() else ''
  707. # 判断设备是否为ipc设备和是否支持云存
  708. if model == 2 and number in ['4', '5']:
  709. return True
  710. return False
  711. @staticmethod
  712. def negative_number_judgment(number_list):
  713. """
  714. 判断正负数
  715. @param number_list: float或int类型列表
  716. """
  717. if any(i < 0 for i in number_list):
  718. return False
  719. else:
  720. return True
  721. @staticmethod
  722. def check_password(password1, password2):
  723. """
  724. 比较密码
  725. @param 返回True or False
  726. """
  727. return constant_time_compare(password1, password2)
  728. @staticmethod
  729. def compare_version_number(version_number, version_number_list):
  730. """
  731. 比对版本号大小
  732. @param version_number: 版本号
  733. @param version_number_list: 版本号列表
  734. """
  735. version_list = []
  736. input_version = LooseVersion(version_number)
  737. for version in version_number_list:
  738. version = LooseVersion(version)
  739. if input_version >= version:
  740. version_list.append(version)
  741. else:
  742. continue
  743. return version_list
  744. @staticmethod
  745. def convert_to_timestamp(timezone_offset, time_string):
  746. """
  747. 时间字符串转为时间戳
  748. @param timezone_offset: 时区
  749. @param time_string: 时间字符串
  750. @return: timestamp
  751. """
  752. datetime_obj = datetime.datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S')
  753. # 创建一个表示指定时区的timedelta对象
  754. utc_offset = datetime.timedelta(hours=timezone_offset)
  755. # 调整时区
  756. datetime_obj = datetime_obj - utc_offset
  757. # datetime.datetime对象 -> str
  758. time_str_utc = datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
  759. timestamp = calendar.timegm(time.strptime(time_str_utc, '%Y-%m-%d %H:%M:%S'))
  760. return timestamp
  761. @staticmethod
  762. def get_uid_by_serial_number(serial_number):
  763. """
  764. 根据序列号获取绑定uid
  765. @param serial_number: 9位序列号
  766. @return: uid信息
  767. """
  768. c_serial_qs = UIDCompanySerialModel.objects.filter(company_serial__serial_number=serial_number[0:6])
  769. if not c_serial_qs.exists():
  770. return serial_number
  771. c_serial_info = c_serial_qs.values('uid__uid')
  772. return c_serial_info[0]['uid__uid']
  773. @staticmethod
  774. def get_serial_number_by_uid(uid):
  775. """
  776. 根据序列号获取绑定uid
  777. @param uid: uid
  778. @return: uid信息
  779. """
  780. c_serial_qs = UIDCompanySerialModel.objects.filter(uid__uid=uid)
  781. if not c_serial_qs.exists():
  782. return uid
  783. c_serial_qs = c_serial_qs.annotate(mark=F('company_serial__company__mark'),
  784. serial_number=F('company_serial__serial_number'))
  785. c_serial_info = c_serial_qs.values('mark', 'serial_number')
  786. return c_serial_info[0]['serial_number'] + c_serial_info[0]['mark']
  787. @staticmethod
  788. def update_alexa_events(data_list):
  789. """
  790. 请求Alexa服务器更新事件网关
  791. 邮件提醒捕获的异常
  792. @param data_list: 数据列表
  793. @return:
  794. """
  795. try:
  796. data_list = json.dumps(data_list)
  797. data = {'data_list': data_list}
  798. url = ALEXA_DOMAIN + 'deviceStatus/addOrUpdateV2'
  799. requests.post(url, data=data, timeout=30)
  800. except Exception as e:
  801. S3Email().faEmail(
  802. '请求Alexa服务器更新事件网关异常:error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)),
  803. 'servers@ansjer.com')
  804. pass