CommonService.py 32 KB

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