ota_client.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #-*- coding:utf-8 -*-
  2. from ssat_sdk.utils.LoggingUtil import printLog
  3. from ssat_sdk.device_manage import ScbcCopyKey
  4. from ssat_sdk.sat_environment import getOtaParamUrl_dict
  5. import urllib
  6. import urllib2
  7. import json
  8. import requests
  9. # HTTP协议传参时的自动化测试标识,string型,每次请求必带的参数,KEY为type
  10. SAT_TYPE = "1"
  11. class OtaClient():
  12. def __init__(self):
  13. # 各服务器getParam接口
  14. self.getParamDict = getOtaParamUrl_dict()
  15. # self.getParamDict = {
  16. # "CN": "https://cn.ota.qhmoka.com/ota-services/strategy/getParam",
  17. # "NA": "https://na.ota.qhmoka.com/ota-services/strategy/getParam",
  18. # "LA": "https://la.ota.qhmoka.com/ota-services/strategy/getParam",
  19. # "ME": "https://me.ota.qhmoka.com/ota-services/strategy/getParam",
  20. # "EU": "https://eu.ota.qhmoka.com/ota-services/strategy/getParam",
  21. # "AP": "https://ap.ota.qhmoka.com/ota-services/strategy/getParam",
  22. # "TEST": "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  23. # }
  24. # http request中统一使用的headers
  25. self.headers = {
  26. "Content-Type": "application/json"
  27. }
  28. def postRequest(self, url, post_data, headers):
  29. response = requests.post(url, data=post_data, headers=headers)
  30. printLog("response:%s" % response)
  31. printLog("response.status_code:%s" % response.status_code)
  32. printLog("response.text:%s" % response.text)
  33. if response.status_code != 200:
  34. printLog("OtaClient.postRequest()", u"Response结果码异常。")
  35. return False, response
  36. else:
  37. return True, response
  38. def getAppKey(self, area):
  39. appKeys = []
  40. url = ""
  41. if 1:
  42. try:
  43. url = self.getParamDict[area]
  44. except Exception, e:
  45. printLog("OtaClient.addUserId()", u"无法通过传入的area获取url地址。%s"%e)
  46. return False
  47. # 测试服务器地址
  48. if 0:
  49. url = "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  50. post_dict = {
  51. "type": SAT_TYPE,
  52. "area": area
  53. }
  54. post_data = json.dumps(post_dict)
  55. result, response = self.postRequest(url, post_data, self.headers)
  56. if result is False:
  57. return False, appKeys
  58. resDict = json.loads(response.text)
  59. if int(resDict['code']) != 1000:
  60. result = False
  61. else:
  62. result = True
  63. appKeys = resDict['data']['appKeys']
  64. printLog("OtaClient.getAppKey()", u"请求返回信息:%s" % resDict['msg'])
  65. return result, appKeys
  66. def httpAddUserId(self, area, appKey, clientType, projectId, checkVersion, version, userId):
  67. url = ""
  68. if 1:
  69. try:
  70. url = self.getParamDict[area]
  71. except Exception, e:
  72. printLog("OtaClient.addUserId()", u"无法通过传入的area获取url地址。%s"%e)
  73. return False
  74. # 测试服务器地址
  75. if 0:
  76. url = "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  77. post_dict = {
  78. "type": SAT_TYPE,
  79. "area": area,
  80. "appKey": appKey,
  81. "clientType": clientType,
  82. "projectId": projectId,
  83. "checkVersion": checkVersion,
  84. "version": version,
  85. "userId": userId
  86. }
  87. post_data = json.dumps(post_dict)
  88. result, response = self.postRequest(url, post_data, self.headers)
  89. if result is False:
  90. return False
  91. resDict = json.loads(response.text)
  92. if int(resDict['code']) != 1000:
  93. result = False
  94. else:
  95. result = True
  96. printLog("OtaClient.addUserId()", u"请求返回信息:%s" % resDict['msg'])
  97. return result
  98. def httpGetUserId(self, deviceId, clientType, mac, forFormal=True):
  99. # 测试环境:
  100. testRoot = "http://test.dsp.server.qhmoka.com"
  101. # 正式环境:
  102. formalRoot = "https://dsp.server.qhmoka.com"
  103. # 登录的账号密码
  104. userName = "auto"
  105. password = "123456"
  106. if forFormal is True:
  107. loginUrl = formalRoot + "/login"
  108. else:
  109. loginUrl = testRoot + "/login"
  110. token = ScbcCopyKey.HTTPLogin(loginUrl, userName, password, 1)
  111. # token长度不为0, 表示成功获取token值;
  112. if token.__len__():
  113. print u"login token = ", token
  114. userId = ScbcCopyKey.HTTPGetUserId("http://test.dsp.server.qhmoka.com/api/automation/getUserId", token,
  115. clientType, deviceId, mac.replace('-', ':'))
  116. # userId == -1表示获取失败;
  117. return userId
  118. def getDeviceId(self):
  119. return ScbcCopyKey.GetDeviceId()
  120. def getClientType(self):
  121. return ScbcCopyKey.GetClientType()
  122. def getMAC(self):
  123. return ScbcCopyKey.GetMAC()
  124. if __name__ == '__main__':
  125. pass