CommonService.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. @staticmethod
  310. def get_date_from_timestamp(timestamp, timezone_offset):
  311. # 创建时区对象
  312. tz = datetime.timezone(datetime.timedelta(hours=timezone_offset))
  313. # 使用时间戳创建 datetime 对象
  314. dt = datetime.datetime.fromtimestamp(timestamp, tz)
  315. # 格式化成 '%Y-%m-%d'
  316. formatted_date = dt.strftime('%Y-%m-%d %H:%M:%S')
  317. return formatted_date
  318. # 计算N个月后的时间戳
  319. @staticmethod
  320. def calcMonthLater(addMonth, unix_timestamp=None):
  321. if unix_timestamp:
  322. now_year = time.localtime(unix_timestamp).tm_year
  323. now_month = time.localtime(unix_timestamp).tm_mon
  324. now_day = time.localtime(unix_timestamp).tm_mday
  325. now_hour = time.localtime(unix_timestamp).tm_hour
  326. now_min = time.localtime(unix_timestamp).tm_min
  327. now_second = time.localtime(unix_timestamp).tm_sec
  328. else:
  329. now_year = datetime.datetime.now().year
  330. now_month = datetime.datetime.now().month
  331. now_day = datetime.datetime.now().day
  332. now_hour = datetime.datetime.now().hour
  333. now_min = datetime.datetime.now().minute
  334. now_second = datetime.datetime.now().second
  335. for add in range(addMonth):
  336. if now_month == 12:
  337. now_year += 1
  338. now_month = 1
  339. else:
  340. now_month += 1
  341. timestamps = 0
  342. for is_format in range(4):
  343. try:
  344. date_format = '{now_year}-{now_month}-{now_day} {now_hour}:{now_min}:{now_second}' \
  345. .format(now_year=now_year, now_month=now_month, now_day=now_day, now_hour=now_hour,
  346. now_min=now_min, now_second=now_second)
  347. timestamps = CommonService.str_to_timestamp(date_format)
  348. except Exception as e:
  349. if str(e) == 'day is out of range for month':
  350. now_day = now_day - 1
  351. return timestamps
  352. @staticmethod
  353. def updateMac(mac: str):
  354. macArray = mac.split(':')
  355. macArray[0] = int(macArray[0], 16)
  356. macArray[1] = int(macArray[1], 16)
  357. macArray[2] = int(macArray[2], 16)
  358. first = int(macArray[5], 16)
  359. second = int(macArray[4], 16)
  360. three = int(macArray[3], 16)
  361. if first == 255 and second == 255 and three == 255:
  362. return None
  363. first += 1
  364. if first / 256 == 1:
  365. second += 1
  366. first = first % 256
  367. if second / 256 == 1:
  368. three += 1
  369. second = second % 256
  370. macArray[3] = three
  371. macArray[4] = second
  372. macArray[5] = first
  373. tmp = ':'.join(map(lambda x: "%02x" % x, macArray))
  374. return tmp.upper()
  375. @staticmethod
  376. def encode_data_without_salt(content):
  377. return base64.b64encode(str(content).encode("utf-8")).decode('utf8')
  378. @staticmethod
  379. def check_time_stamp_token(token, time_stamp):
  380. # 时间戳token校验
  381. if not all([token, time_stamp]):
  382. return False
  383. try:
  384. token = int(CommonService.decode_data(token))
  385. time_stamp = int(time_stamp)
  386. now_time = int(time.time())
  387. distance = now_time - time_stamp
  388. if token != time_stamp or distance > 60000 or distance < -60000: # 为了全球化时间控制在一天内
  389. return False
  390. return True
  391. except Exception as e:
  392. print(e)
  393. return False
  394. @staticmethod
  395. def check_time_stamp_token_without_distance(time_stamp_token, time_stamp):
  396. """
  397. 用于没有RTC设备的时间戳token校验
  398. @param time_stamp: 时间戳
  399. @param time_stamp_token: 时间戳token
  400. @return: boolean True/False
  401. """
  402. if not all([time_stamp_token, time_stamp]):
  403. return False
  404. try:
  405. token = CommonService.decode_data(time_stamp_token)
  406. if token != time_stamp:
  407. return False
  408. return True
  409. except Exception as e:
  410. print(e)
  411. return False
  412. @staticmethod
  413. def req_publish_mqtt_msg(identification_code, topic_name, msg, qos=1):
  414. """
  415. 通用发布MQTT消息函数
  416. @param identification_code: 标识码
  417. @param topic_name: 主题名
  418. @param msg: 消息内容
  419. @param qos: mqtt qos等级
  420. @return: boolean
  421. """
  422. if not all([identification_code, topic_name]):
  423. return False
  424. if identification_code.endswith('11L'):
  425. thing_name = 'LC_' + identification_code
  426. else:
  427. thing_name = 'Ansjer_Device_' + identification_code
  428. try:
  429. # 获取数据组织将要请求的url
  430. iot = iotdeviceInfoModel.objects.filter(
  431. thing_name=thing_name).values(
  432. 'endpoint', 'token_iot_number')
  433. if not iot.exists():
  434. return False
  435. endpoint = iot[0]['endpoint']
  436. Token = iot[0]['token_iot_number']
  437. # api doc: https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/http.html
  438. # url: https://IoT_data_endpoint/topics/url_encoded_topic_name?qos=1
  439. # post请求url发布MQTT消息
  440. url = 'https://{}/topics/{}?qos={}'.format(endpoint, topic_name, qos)
  441. authorizer_name = 'Ansjer_Iot_Auth'
  442. signature = CommonService.rsa_sign(Token) # Token签名
  443. headers = {
  444. 'x-amz-customauthorizer-name': authorizer_name,
  445. 'Token': Token,
  446. 'x-amz-customauthorizer-signature': signature}
  447. r = requests.post(url=url, headers=headers, json=msg, timeout=2)
  448. if r.status_code == 200:
  449. res = r.json()
  450. if res['message'] == 'OK':
  451. return True
  452. return False
  453. else:
  454. return False
  455. except Exception as e:
  456. return False
  457. @staticmethod
  458. def rsa_sign(Token):
  459. # 私钥签名Token
  460. if not Token:
  461. return ''
  462. private_key_file = '''-----BEGIN RSA PRIVATE KEY-----
  463. MIIEpQIBAAKCAQEA5iJzEDPqtGmFMggekVro6C0lrjuC2BjunGkrFNJWpDYzxCzE
  464. X5jf4/Fq7hcIaQd5sqHugDxPVollSLPe9zNilbrd0sZfU+Ed8gRVuKW9KwfE9XFr
  465. L0pt6bKRQ0IIRfiZ9TuR0tsQysvcO1GZSXcYfPue3tGM1zOnWFThWDqZ06+sOxzt
  466. RMRl4yNfbpCG4MfxG3itNXOfrjZv2OMLSXrxmzubSvRpUYSvQPs4fm9302SAnySY
  467. 0MKzx6H6528ZQm/IDDSZy6EmNBIyTRDfxC56vnYcXvqedAQh7jJnjdvt6Q4MhASH
  468. eIYi1FBSdu2NT6wgpnrqXzx5pq9kR/lnsLID0wIDAQABAoIBAQCiF4GT1/1oNSpr
  469. ouxk1PNXFPWFUsVGD8mAwVJmx//eiY7MjfuCmdqYYmI+cFqsH2fIOeYSzGfVO9Dq
  470. 9EYHN1oovAWhf7eFDPpajFMUSyiCNmazub8VAAeKowtNpCTPo9pMsDh1m3aoYA4u
  471. ebrN0+Sbo16y8kWRDgDAZoiR7DSMs8lczk16hwfv5mw8XpNDbaL3Coi4Koe2S1Yh
  472. 2SX3vWFlpd7qF1ZYXuZIp+b8JPrV7n9eUKoFgzj0gqgwQK80CoexIjiOrNMPvkQa
  473. q+8kCvFjAzKxOK7e8gjM8lMRiGodb61kmYZkkJzFwWO4EaGbl34lfVECd1Ixp3tF
  474. be0OWAGBAoGBAPSteXDzzToD8ovM7LL11x0jWwI6HOiHu89kZtW566rIezjWBuA2
  475. TxrcYKM3h9jQRXS3CsMdoIv6XGk5lqM8ADtjn23FBWe/THYLh8bm8JOgh5RRWQDg
  476. SvkLfi9Ih2mM4NJfmuuDOh3Nze2efLM7+kOZWUQwF2Zx9mL5jvRBk351AoGBAPDI
  477. sYmT2Li+i5+0vykA2m5uPF8ZOW8BGtAfCZv0suW7BNzSgin78g9WapRd/4p0NNiL
  478. /nVMqPPCpd1akCUpV+GDWQt0hV+HZjxANE0KWhciQRyo2qvo51j8SWILJSgh0tXC
  479. aTF8qt6oGw3VN3m57vKhbrlDaz0J/NDJFci6msAnAoGBAOuG6bXPGijUj+//DYKf
  480. n7jOxdZ49kboEePrtAncdHzri6IEdI3z+WXT6bpzw/LzWUimwldb96WHFNm9s8Hi
  481. Ch8hIODbnP5naUTgiIzw1XhmONyPCewL/F+LrqX5XVA/alNX8JrwsUrrR2WLAGLQ
  482. Q3I69XDsEjptTU2tCO0bCs3ZAoGBAJ2lCHfm0JHET230zONvp5N9oREyVqQSuRdh
  483. +syc3TQDyh85w/bw+X6JOaaCFHj1tFPC9Iqf8k4GNspCLPXnp54CfR4+38O3xnvU
  484. HWoDSRC0YKT++IxtJGriYrlKSr2Hx54kdvLriIPW1D+uRW/xCDza7L9nIKMKEvgv
  485. b4/IfOEpAoGAeKM9Te7T1VzlAkS0CJOwanzwYV/zrex84WuXxlsGgPQ871lTs5AP
  486. H1QLfLfFXH+UVrCEC2yv4eml/cqFkpB3gE5i4MQ8GPVIOSs5tsIyl8YUA03vdNdB
  487. GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
  488. -----END RSA PRIVATE KEY-----'''
  489. # 使用密钥文件方式
  490. # private_key_file_path = os.path.join(BASE_DIR, 'static/iotCore/private.pem')#.replace('\\', '/')
  491. # private_key_file = open(private_key_file_path, 'r')
  492. private_key = ct.load_privatekey(ct.FILETYPE_PEM, private_key_file)
  493. signature = ct.sign(private_key, Token.encode('utf8'), 'sha256')
  494. signature = encodebytes(signature).decode('utf8').replace('\n', '')
  495. # print('signature:', signature)
  496. return signature
  497. @staticmethod
  498. def get_payment_status_url(lang, payment_status):
  499. # 返回相应的支付状态url
  500. if lang == 'cn':
  501. file_name = 'success.html' if payment_status == 'success' else 'fail.html'
  502. else:
  503. file_name = 'en_success.html' if payment_status == 'success' else 'en_fail.html'
  504. pay_failed_url = "{}web/paid2/{}".format(SERVER_DOMAIN_SSL, file_name)
  505. return pay_failed_url
  506. # 根据uid查询序列号,存在则返回序列号,否则返uid
  507. @staticmethod
  508. def query_serial_with_uid(uid):
  509. device_info_qs = Device_Info.objects.filter(UID=uid).values('serial_number')
  510. if device_info_qs.exists():
  511. serial_number = device_info_qs[0]['serial_number']
  512. if serial_number:
  513. return serial_number
  514. return uid
  515. # 根据序列号查询uid,存在则返回uid,否则返回序列号
  516. @staticmethod
  517. def query_uid_with_serial(serial_number):
  518. device_info_qs = Device_Info.objects.filter(serial_number=serial_number).values('UID')
  519. if device_info_qs.exists():
  520. uid = device_info_qs[0]['UID']
  521. if uid:
  522. return uid
  523. return serial_number
  524. @staticmethod
  525. def get_full_serial_number(uid, serial_number, device_type):
  526. """
  527. 根据uid查询返回完整序列号
  528. @param uid: uid
  529. @param serial_number: 9位序列号
  530. @param device_type: 设备类型
  531. @return: full_serial_number
  532. """
  533. p2p_type = str(UIDModel.objects.filter(uid=uid).values('p2p_type')[0]['p2p_type'])
  534. # 设备类型转为16进制并补齐4位
  535. device_type = hex(device_type)[2:]
  536. device_type = (4 - len(device_type)) * '0' + device_type
  537. full_serial_number = serial_number + p2p_type + device_type
  538. return full_serial_number
  539. # 根据企业标识返回物品名
  540. @staticmethod
  541. def get_thing_name(company_mark, thing_name_suffix):
  542. if company_mark == '11A':
  543. return 'Ansjer_Device_' + thing_name_suffix
  544. elif company_mark == '11L':
  545. return 'LC_' + thing_name_suffix
  546. else:
  547. return thing_name_suffix
  548. @staticmethod
  549. def confirm_region_id():
  550. """
  551. 根据配置信息确定region_id
  552. @return: region_id
  553. """
  554. region_id = 3
  555. if CONFIG_INFO == CONFIG_US: # 美洲
  556. region_id = 3
  557. elif CONFIG_INFO == CONFIG_EUR: # 欧洲
  558. region_id = 4
  559. elif CONFIG_INFO == CONFIG_CN: # 中国
  560. region_id = 1
  561. elif CONFIG_INFO == CONFIG_TEST: # 测试
  562. region_id = 5
  563. return region_id
  564. @staticmethod
  565. def verify_token_get_user_id(request_dict, request):
  566. """
  567. 认证token,获取user id
  568. @param request_dict: 请求参数
  569. @param request: 请求体
  570. @return: token_obj.code, token_obj.userID, response
  571. """
  572. try:
  573. token_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  574. lang = request_dict.get('lang', None)
  575. response = ResponseObject(lang if lang else token_obj.lang)
  576. return token_obj.code, token_obj.userID, response
  577. except Exception as e:
  578. print(e)
  579. return 309, None, None
  580. @staticmethod
  581. def cutting_time(start_time, end_time, time_unit):
  582. """
  583. 按时间单位切割时间段
  584. @param start_time: 开始时间
  585. @param end_time: 结束时间
  586. @param time_unit: 时间单位
  587. @return: time_list 切割后的时间列表
  588. """
  589. time_list = []
  590. while True:
  591. if time_unit == 'day':
  592. temp_time = start_time + relativedelta(days=1)
  593. elif time_unit == 'week':
  594. temp_time = start_time + relativedelta(days=7)
  595. elif time_unit == 'month':
  596. temp_time = start_time + relativedelta(months=1)
  597. elif time_unit == 'quarter':
  598. temp_time = start_time + relativedelta(months=3)
  599. elif time_unit == 'year':
  600. temp_time = start_time + relativedelta(years=1)
  601. else:
  602. break
  603. if temp_time < end_time:
  604. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  605. CommonService.str_to_timestamp(temp_time.strftime('%Y-%m-%d %H:%M:%S')))
  606. time_list.append(time_tuple)
  607. start_time = temp_time
  608. else:
  609. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  610. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  611. if time_tuple not in time_list:
  612. time_list.append(time_tuple)
  613. break
  614. if not time_list:
  615. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  616. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  617. time_list = [time_tuple]
  618. return time_list
  619. @staticmethod
  620. def cutting_time_stamp(start_time, end_time):
  621. """
  622. 按天切割时间段
  623. @param start_time: 开始时间
  624. @param end_time: 结束时间
  625. @return: time_list 切割后的时间列表
  626. """
  627. time_list = []
  628. while True:
  629. mid_time = datetime.datetime(start_time.year, start_time.month, start_time.day) + relativedelta(days=1)
  630. if mid_time < end_time:
  631. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  632. CommonService.str_to_timestamp(mid_time.strftime('%Y-%m-%d %H:%M:%S')))
  633. time_list.append(time_tuple)
  634. start_time = mid_time
  635. else:
  636. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  637. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  638. if time_tuple not in time_list:
  639. time_list.append(time_tuple)
  640. break
  641. if not time_list:
  642. time_tuple = (CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S')),
  643. CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
  644. time_list = [time_tuple]
  645. return time_list
  646. @staticmethod
  647. def get_domain_name():
  648. """
  649. 获取域名
  650. @return: domain_name_list 域名列表
  651. """
  652. if CONFIG_INFO == CONFIG_TEST:
  653. domain_name_list = [SERVER_DOMAIN_TEST[:-1]]
  654. elif CONFIG_INFO == CONFIG_CN or CONFIG_INFO == CONFIG_US or CONFIG_INFO == CONFIG_EUR:
  655. domain_name_list = [SERVER_DOMAIN_US[:-1], SERVER_DOMAIN_CN[:-1], SERVER_DOMAIN_EUR[:-1]]
  656. else:
  657. domain_name_list = []
  658. return domain_name_list
  659. @staticmethod
  660. def get_orders_domain_name_list():
  661. """
  662. 获取其他服务器域名列表
  663. @return: orders_domain_name_list 其他服务器域名列表
  664. """
  665. orders_domain_name_list = SERVER_DOMAIN_LIST
  666. if CONFIG_INFO == CONFIG_TEST:
  667. orders_domain_name_list = [SERVER_DOMAIN_CN, SERVER_DOMAIN_US, SERVER_DOMAIN_EUR]
  668. elif CONFIG_INFO == CONFIG_CN:
  669. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_US, SERVER_DOMAIN_EUR]
  670. elif CONFIG_INFO == CONFIG_US:
  671. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_EUR]
  672. elif CONFIG_INFO == CONFIG_EUR:
  673. orders_domain_name_list = [SERVER_DOMAIN_TEST, SERVER_DOMAIN_CN, SERVER_DOMAIN_US]
  674. return orders_domain_name_list
  675. @staticmethod
  676. def list_sort(e):
  677. """
  678. 列表排序
  679. @param e: 列表元素
  680. """
  681. return sorted(e, key=lambda item: -item['count'])
  682. @staticmethod
  683. def list_sort_v2(e, order_by):
  684. """
  685. 列表排序
  686. @param e: 列表元素
  687. @param order_by: 排序对象
  688. """
  689. return sorted(e, key=lambda item: item[order_by], reverse=True)
  690. @staticmethod
  691. def Package_Type(order_type, content):
  692. """
  693. 套餐类型
  694. """
  695. if order_type == 0:
  696. content = content + '(' + '云存' + ')'
  697. return content
  698. elif order_type == 1:
  699. content = content + '(' + 'AI' + ')'
  700. return content
  701. elif order_type == 2:
  702. pass
  703. elif order_type == 4:
  704. content = content + '(' + '云盘' + ')'
  705. return content
  706. @staticmethod
  707. def is_cloud_device(ucode, device_type):
  708. """
  709. 设备是否支持云存
  710. @param ucode: 设备版本
  711. @param device_type: 设备类型
  712. """
  713. if len(ucode) > 4:
  714. number = ucode[-4]
  715. else:
  716. return False
  717. device_type_qs = AppDeviceType.objects.filter(type=device_type).values('model')
  718. model = device_type_qs[0]['model'] if device_type_qs.exists() else ''
  719. # 判断设备是否为ipc设备和是否支持云存
  720. if model == 2 and number in ['4', '5']:
  721. return True
  722. return False
  723. @staticmethod
  724. def negative_number_judgment(number_list):
  725. """
  726. 判断正负数
  727. @param number_list: float或int类型列表
  728. """
  729. if any(i < 0 for i in number_list):
  730. return False
  731. else:
  732. return True
  733. @staticmethod
  734. def check_password(password1, password2):
  735. """
  736. 比较密码
  737. @param 返回True or False
  738. """
  739. return constant_time_compare(password1, password2)
  740. @staticmethod
  741. def compare_version_number(version_number, version_number_list):
  742. """
  743. 比对版本号大小
  744. @param version_number: 版本号
  745. @param version_number_list: 版本号列表
  746. """
  747. version_list = []
  748. input_version = LooseVersion(version_number)
  749. for version in version_number_list:
  750. version = LooseVersion(version)
  751. if input_version >= version:
  752. version_list.append(version)
  753. else:
  754. continue
  755. return version_list
  756. @staticmethod
  757. def convert_to_timestamp(timezone_offset, time_string):
  758. """
  759. 时间字符串转为时间戳
  760. @param timezone_offset: 时区
  761. @param time_string: 时间字符串
  762. @return: timestamp
  763. """
  764. datetime_obj = datetime.datetime.strptime(time_string, '%Y-%m-%d %H:%M:%S')
  765. # 创建一个表示指定时区的timedelta对象
  766. utc_offset = datetime.timedelta(hours=timezone_offset)
  767. # 调整时区
  768. datetime_obj = datetime_obj - utc_offset
  769. # datetime.datetime对象 -> str
  770. time_str_utc = datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
  771. timestamp = calendar.timegm(time.strptime(time_str_utc, '%Y-%m-%d %H:%M:%S'))
  772. return timestamp
  773. @staticmethod
  774. def get_uid_by_serial_number(serial_number):
  775. """
  776. 根据序列号获取绑定uid
  777. @param serial_number: 9位序列号
  778. @return: uid信息
  779. """
  780. c_serial_qs = UIDCompanySerialModel.objects.filter(company_serial__serial_number=serial_number[0:6])
  781. if not c_serial_qs.exists():
  782. return serial_number
  783. c_serial_info = c_serial_qs.values('uid__uid')
  784. return c_serial_info[0]['uid__uid']
  785. @staticmethod
  786. def get_serial_number_by_uid(uid):
  787. """
  788. 根据序列号获取绑定uid
  789. @param uid: uid
  790. @return: uid信息
  791. """
  792. c_serial_qs = UIDCompanySerialModel.objects.filter(uid__uid=uid)
  793. if not c_serial_qs.exists():
  794. return uid
  795. c_serial_qs = c_serial_qs.annotate(mark=F('company_serial__company__mark'),
  796. serial_number=F('company_serial__serial_number'))
  797. c_serial_info = c_serial_qs.values('mark', 'serial_number')
  798. return c_serial_info[0]['serial_number'] + c_serial_info[0]['mark']
  799. @staticmethod
  800. def get_user_tz(user_id):
  801. """
  802. 获取用户时区
  803. @param user_id: 用户id
  804. @return: tz
  805. """
  806. # 从gateway_push表查询时区
  807. gateway_push_qs = GatewayPush.objects.filter(user_id=user_id).order_by('-id').first()
  808. if gateway_push_qs is None:
  809. tz = 0.00
  810. else:
  811. # 截掉.00然后转为浮点型
  812. tz = float(gateway_push_qs.tz[:-3])
  813. return tz
  814. @staticmethod
  815. def update_alexa_events(data_list):
  816. """
  817. 请求Alexa服务器更新事件网关
  818. 邮件提醒捕获的异常
  819. @param data_list: 数据列表
  820. @return:
  821. """
  822. try:
  823. data_list = json.dumps(data_list)
  824. data = {'data_list': data_list}
  825. url = ALEXA_DOMAIN + 'deviceStatus/addOrUpdateV2'
  826. requests.post(url, data=data, timeout=30)
  827. except Exception as e:
  828. S3Email().faEmail(
  829. '请求Alexa服务器更新事件网关异常:error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)),
  830. 'servers@ansjer.com')
  831. pass