ota_client.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 urllib3
  8. import json
  9. import requests
  10. import os
  11. import httplib
  12. httplib.HTTPConnection._http_vsn = 10
  13. httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
  14. # HTTP协议传参时的自动化测试标识,string型,每次请求必带的参数,KEY为type
  15. SAT_TYPE = "1"
  16. class Ota_Client():
  17. def __init__(self):
  18. # 各服务器getParam接口
  19. # self.getParamDict = getOtaParamUrl_dict()
  20. self.getParamDict = {
  21. "CN": "https://cn.ota.qhmoka.com/ota-services/strategy/getParam",
  22. "NA": "https://na.ota.qhmoka.com/ota-services/strategy/getParam",
  23. "LA": "https://la.ota.qhmoka.com/ota-services/strategy/getParam",
  24. "ME": "https://me.ota.qhmoka.com/ota-services/strategy/getParam",
  25. "EU": "https://eu.ota.qhmoka.com/ota-services/strategy/getParam",
  26. "AP": "https://ap.ota.qhmoka.com/ota-services/strategy/getParam",
  27. "TEST": "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  28. }
  29. # http request中统一使用的headers
  30. self.headers = {
  31. "Content-Type": "application/json"
  32. }
  33. def postRequest(self, url, post_data, headers):
  34. response = requests.post(url, data=post_data, headers=headers)
  35. printLog("response:%s" % response)
  36. printLog("response.status_code:%s" % response.status_code)
  37. printLog("response.text:%s" % response.text)
  38. if response.status_code != 200:
  39. printLog("OtaClient.postRequest()", u"Response结果码异常。")
  40. return False, response
  41. else:
  42. return True, response
  43. def getAppKey(self, area):
  44. appKeys = []
  45. url = ""
  46. if 1:
  47. try:
  48. url = self.getParamDict[area]
  49. except Exception, e:
  50. printLog("OtaClient.addUserId()", u"无法通过传入的area获取url地址。%s"%e)
  51. return False
  52. # 测试服务器地址
  53. if 0:
  54. url = "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  55. post_dict = {
  56. "type": SAT_TYPE,
  57. "area": area
  58. }
  59. post_data = json.dumps(post_dict)
  60. result, response = self.postRequest(url, post_data, self.headers)
  61. if result is False:
  62. return False, appKeys
  63. resDict = json.loads(response.text)
  64. if int(resDict['code']) != 1000:
  65. result = False
  66. else:
  67. result = True
  68. appKeys = resDict['data']['appKeys']
  69. printLog("OtaClient.getAppKey()", u"请求返回信息:%s" % resDict['msg'])
  70. return result, appKeys
  71. def httpAddUserId(self, area, appKey, clientType, projectId, checkVersion, version, userId):
  72. url = ""
  73. if 1:
  74. try:
  75. url = self.getParamDict[area]
  76. except Exception, e:
  77. printLog("OtaClient.addUserId()", u"无法通过传入的area获取url地址。%s"%e)
  78. return False
  79. # 测试服务器地址
  80. if 0:
  81. url = "https://test.uc.qhmoka.com/ota-services/strategy/getParam"
  82. post_dict = {
  83. "type": SAT_TYPE,
  84. "area": area,
  85. "appKey": appKey,
  86. "clientType": clientType,
  87. "projectId": projectId,
  88. "checkVersion": checkVersion,
  89. "version": version,
  90. "userId": userId
  91. }
  92. post_data = json.dumps(post_dict)
  93. result, response = self.postRequest(url, post_data, self.headers)
  94. if result is False:
  95. return False
  96. resDict = json.loads(response.text)
  97. if int(resDict['code']) != 1000:
  98. result = False
  99. else:
  100. result = True
  101. printLog("OtaClient.addUserId()", u"请求返回信息:%s" % resDict['msg'])
  102. return result
  103. def httpGetUserId(self, deviceId, clientType, mac, forFormal=True):
  104. # 测试环境:
  105. testRoot = "http://test.dsp.server.qhmoka.com"
  106. # 正式环境:
  107. formalRoot = "https://dsp.server.qhmoka.com"
  108. # 登录的账号密码
  109. userName = "auto"
  110. password = "123456"
  111. if forFormal is True:
  112. loginUrl = formalRoot + "/login"
  113. else:
  114. loginUrl = testRoot + "/login"
  115. token = ScbcCopyKey.HTTPLogin(loginUrl, userName, password, 1)
  116. # token长度不为0, 表示成功获取token值;
  117. if token.__len__():
  118. print u"login token = ", token
  119. userId = ScbcCopyKey.HTTPGetUserId("http://test.dsp.server.qhmoka.com/api/automation/getUserId", token,
  120. clientType, deviceId, mac.replace('-', ':'))
  121. # userId == -1表示获取失败;
  122. return userId
  123. def getDeviceId(self):
  124. return ScbcCopyKey.GetDeviceId()
  125. def getClientType(self):
  126. return ScbcCopyKey.GetClientType()
  127. def getMAC(self):
  128. return ScbcCopyKey.GetMAC()
  129. '''
  130. 从云测试系统随机取一个deviceId,返回
  131. :param forFormal:是否为正式服务器,若为正式,则为True
  132. :return deviceId
  133. '''
  134. def httpGetDeviceId(self,forFormal=True):
  135. # urlTest = "http://10.201.40.65:8088/otaad/deviceId/getDeviceId.do" # 云测试系统地址
  136. # urlFormal = "http://10.201.40.65:8088/otaad/deviceId/getDeviceId.do"
  137. urlTest = "http://10.126.16.60:8680/otaad/deviceId/getDeviceId.do"
  138. urlFormal = "http://10.126.16.60:8380/otaad/deviceId/getDeviceId.do"
  139. post_data = None
  140. if forFormal is True:
  141. result, response = self.postRequest(urlFormal,post_data,self.headers)
  142. else:
  143. result, response = self.postRequest(urlTest, post_data,self.headers)
  144. # print "result",result
  145. # print "response",response
  146. resDict = json.loads(response.text)
  147. if str(resDict['code']) == "00":
  148. return resDict['deviceId'] # 获取成功则返回deviceId
  149. else:
  150. return None # 获取失败则返回None
  151. '''
  152. 返回clientType,deviceId,userId 给云测试平台,将这三个数据绑定在一起
  153. :param clientType,deviceId,userId
  154. :return 若删除成功,返回True,否则返回False
  155. '''
  156. def httpGetDIdRespond(self,clientType,deviceId,userId=None,forFormal=True):
  157. urlTest = "http://10.126.16.60:8680/otaad/deviceId/deleteDeviceId.do" # 云测试系统地址
  158. urlFormal = "http://10.126.16.60:8380/otaad/deviceId/deleteDeviceId.do"
  159. supportData = {
  160. "deviceId": deviceId,
  161. "clientType": clientType,
  162. "userId": userId
  163. }
  164. post_data = json.dumps(supportData)
  165. if forFormal is True:
  166. result, response = self.postRequest(urlFormal, post_data, self.headers)
  167. else:
  168. result, response = self.postRequest(urlTest, post_data, self.headers)
  169. resDict = json.loads(response.text)
  170. if str(resDict['code']) == "00":
  171. return True # 删除成功则返回True
  172. else:
  173. return False # 删除失败则返回False
  174. if __name__ == '__main__':
  175. otaClient = OtaClient()
  176. clientType = "123456"
  177. deviceId = "ed5011314ad738e3abaf05868ef168dcae00e094"
  178. userId = "987654"
  179. forFormal = True
  180. # print otaClient.httpGetDeviceId(False)
  181. print otaClient.httpGetDIdRespond(clientType,deviceId,userId,False)