11package com.tencent.iot.video.link.service
22
33import android.util.Log
4- import com.alibaba.fastjson.JSON
5- import com.alibaba.fastjson.JSONObject
6- import com.tencent.iot.video.link.util.JsonManager
74import com.tencent.iot.video.link.callback.VideoCallback
85import com.tencent.iot.video.link.consts.VideoRequestCode
96import com.tencent.iot.video.link.http.HttpCallBack
107import com.tencent.iot.video.link.http.VideoHttpUtil
8+ import com.tencent.iot.video.link.util.JsonManager
9+ import org.json.JSONObject
1110import java.lang.Long
1211import java.nio.charset.Charset
1312import java.security.InvalidKeyException
1413import java.security.MessageDigest
1514import java.security.NoSuchAlgorithmException
1615import java.text.SimpleDateFormat
17- import java.util.*
16+ import java.util.Date
17+ import java.util.TimeZone
18+ import java.util.TreeMap
1819import javax.crypto.Mac
1920import javax.crypto.spec.SecretKeySpec
20- import kotlin.collections.HashMap
21+ import kotlin.Any
22+ import kotlin.Byte
23+ import kotlin.ByteArray
24+ import kotlin.CharArray
25+ import kotlin.Int
26+ import kotlin.String
2127import kotlin.experimental.and
28+ import kotlin.let
2229
2330/* *
2431 * 接口请求文件
@@ -48,10 +55,21 @@ open class VideoBaseService(secretId: String, secretKey: String) {
4855 if (authorization != null ) {
4956 headerParams[" Authorization" ] = authorization
5057 }
51- basePost(VideoHttpUtil .EXPLORER_SERVICE + VideoHttpUtil .REST_HOST_URL , param, headerParams, callback, VideoRequestCode .video_describe_devices)
58+ basePost(
59+ VideoHttpUtil .EXPLORER_SERVICE + VideoHttpUtil .REST_HOST_URL ,
60+ param,
61+ headerParams,
62+ callback,
63+ VideoRequestCode .video_describe_devices
64+ )
5265 }
5366
54- fun getSnapshotUrl (productId : String , devName : String , thumbnail : String , callback : VideoCallback ) {
67+ fun getSnapshotUrl (
68+ productId : String ,
69+ devName : String ,
70+ thumbnail : String ,
71+ callback : VideoCallback
72+ ) {
5573 var headerParams = videoCommonHeaderParams(" DescribeCloudStorageThumbnail" , " 2020-12-15" )
5674 val param = TreeMap <String , Any >()
5775 param[" ProductId" ] = productId
@@ -61,8 +79,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
6179 if (authorization != null ) {
6280 headerParams[" Authorization" ] = authorization
6381 }
64- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
65- param, headerParams, callback, VideoRequestCode .video_describe_snapshot)
82+ basePost(
83+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
84+ param, headerParams, callback, VideoRequestCode .video_describe_snapshot
85+ )
6686 }
6787
6888 fun getVideoUrl (videoUrl : String , expireTime : kotlin.Long , callback : VideoCallback ) {
@@ -74,11 +94,18 @@ open class VideoBaseService(secretId: String, secretKey: String) {
7494 if (authorization != null ) {
7595 headerParams[" Authorization" ] = authorization
7696 }
77- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
78- param, headerParams, callback, VideoRequestCode .video_describe_video_url)
97+ basePost(
98+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
99+ param, headerParams, callback, VideoRequestCode .video_describe_video_url
100+ )
79101 }
80102
81- fun getMJPEGVideoUrl (productId : String , devName : String , startTime : kotlin.Long , callback : VideoCallback ) {
103+ fun getMJPEGVideoUrl (
104+ productId : String ,
105+ devName : String ,
106+ startTime : kotlin.Long ,
107+ callback : VideoCallback
108+ ) {
82109 var headerParams = videoCommonHeaderParams(" DescribeCloudStorageStreamData" , " 2021-11-25" )
83110 val param = TreeMap <String , Any >()
84111 param[" ProductId" ] = productId
@@ -88,11 +115,19 @@ open class VideoBaseService(secretId: String, secretKey: String) {
88115 if (authorization != null ) {
89116 headerParams[" Authorization" ] = authorization
90117 }
91- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
92- param, headerParams, callback, VideoRequestCode .video_describe_video_url)
118+ basePost(
119+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
120+ param, headerParams, callback, VideoRequestCode .video_describe_video_url
121+ )
93122 }
94123
95- fun getIPCRecordData (productId : String , devName : String , startDate : Date , endDate : Date , callback : VideoCallback ) {
124+ fun getIPCRecordData (
125+ productId : String ,
126+ devName : String ,
127+ startDate : Date ,
128+ endDate : Date ,
129+ callback : VideoCallback
130+ ) {
96131 var headerParams = videoCommonHeaderParams(" DescribeCloudStorageEvents" , " 2020-12-15" )
97132 val param = TreeMap <String , Any >()
98133 param[" ProductId" ] = productId
@@ -104,8 +139,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
104139 if (authorization != null ) {
105140 headerParams[" Authorization" ] = authorization
106141 }
107- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
108- param, headerParams, callback, VideoRequestCode .video_describe_record_date)
142+ basePost(
143+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
144+ param, headerParams, callback, VideoRequestCode .video_describe_record_date
145+ )
109146 }
110147
111148 fun getIPCCurrentDayRecordData (productId : String , devName : String , callback : VideoCallback ) {
@@ -128,8 +165,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
128165 if (authorization != null ) {
129166 headerParams[" Authorization" ] = authorization
130167 }
131- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
132- param, headerParams, callback, VideoRequestCode .video_describe_date)
168+ basePost(
169+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
170+ param, headerParams, callback, VideoRequestCode .video_describe_date
171+ )
133172 }
134173
135174 fun getIPCTimeData (
@@ -144,8 +183,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
144183 if (authorization != null ) {
145184 headerParams[" Authorization" ] = authorization
146185 }
147- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
148- param, headerParams, callback, VideoRequestCode .video_describe_date_time)
186+ basePost(
187+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
188+ param, headerParams, callback, VideoRequestCode .video_describe_date_time
189+ )
149190 }
150191
151192 fun getProductInfo (productId : String , callback : VideoCallback ) {
@@ -156,11 +197,13 @@ open class VideoBaseService(secretId: String, secretKey: String) {
156197 if (authorization != null ) {
157198 headerParams[" Authorization" ] = authorization
158199 }
159- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
160- param, headerParams, callback, VideoRequestCode .video_describe_product)
200+ basePost(
201+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
202+ param, headerParams, callback, VideoRequestCode .video_describe_product
203+ )
161204 }
162205
163- fun getDeviceXp2pInfo (productId : String ,deviceName : String , callback : VideoCallback ) {
206+ fun getDeviceXp2pInfo (productId : String , deviceName : String , callback : VideoCallback ) {
164207 var headerParams = videoCommonHeaderParams(" DescribeP2PInfo" , " 2021-11-25" )
165208 val param = TreeMap <String , Any >()
166209 param[" ProductId" ] = productId
@@ -170,8 +213,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
170213 if (authorization != null ) {
171214 headerParams[" Authorization" ] = authorization
172215 }
173- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
174- param, headerParams, callback, VideoRequestCode .video_describe_product)
216+ basePost(
217+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
218+ param, headerParams, callback, VideoRequestCode .video_describe_product
219+ )
175220 }
176221
177222
@@ -184,8 +229,10 @@ open class VideoBaseService(secretId: String, secretKey: String) {
184229 if (authorization != null ) {
185230 headerParams[" Authorization" ] = authorization
186231 }
187- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
188- param, headerParams, callback, VideoRequestCode .video_describe_url)
232+ basePost(
233+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
234+ param, headerParams, callback, VideoRequestCode .video_describe_url
235+ )
189236 }
190237
191238 /* *
@@ -203,7 +250,13 @@ open class VideoBaseService(secretId: String, secretKey: String) {
203250 if (authorization != null ) {
204251 headerParams[" Authorization" ] = authorization
205252 }
206- basePost(VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL , param, headerParams, callback, VideoRequestCode .video_describe_devices)
253+ basePost(
254+ VideoHttpUtil .VIDEO_SERVICE + VideoHttpUtil .REST_HOST_URL ,
255+ param,
256+ headerParams,
257+ callback,
258+ VideoRequestCode .video_describe_devices
259+ )
207260 }
208261
209262 /* *
@@ -256,15 +309,17 @@ open class VideoBaseService(secretId: String, secretKey: String) {
256309 val httpRequestMethod = " POST"
257310 val canonicalUri = " /"
258311 val canonicalQueryString = " "
259- val canonicalHeadersBuilder = " content-type:application/json; charset=utf-8\n host:${service}${VideoHttpUtil .REST_HOST_URL } \n "
312+ val canonicalHeadersBuilder =
313+ " content-type:application/json; charset=utf-8\n host:${service}${VideoHttpUtil .REST_HOST_URL } \n "
260314 val signedHeadersBuilder = " content-type;host"
261315 val canonicalHeaders = canonicalHeadersBuilder
262316 val signedHeaders = signedHeadersBuilder.toLowerCase()
263317
264318 // 将Extra参数加到待签名字符串中,否则会签名失败
265319 var payload: String? = JsonManager .toJson(param)
266320 val hashedRequestPayload: String? = payload?.let { sha256Hex(it) }
267- val canonicalRequest = " ${httpRequestMethod} \n ${canonicalUri} \n ${canonicalQueryString} \n ${canonicalHeaders} \n ${signedHeaders} \n ${hashedRequestPayload} "
321+ val canonicalRequest =
322+ " ${httpRequestMethod} \n ${canonicalUri} \n ${canonicalQueryString} \n ${canonicalHeaders} \n ${signedHeaders} \n ${hashedRequestPayload} "
268323 println (canonicalRequest)
269324
270325 // ************* 步骤 2:拼接待签名字符串 *************
@@ -281,14 +336,14 @@ open class VideoBaseService(secretId: String, secretKey: String) {
281336
282337 // ************* 步骤 3:计算签名 ************* SecretKey
283338 val secretDate: ByteArray = hmac256(
284- (" TC3" + secretKey).toByteArray(UTF8 ),
285- date
286- )
339+ (" TC3" + secretKey).toByteArray(UTF8 ),
340+ date
341+ )
287342 val secretService: ByteArray = hmac256(secretDate, service)
288343 val secretSigning: ByteArray = hmac256(
289- secretService,
290- " tc3_request"
291- )
344+ secretService,
345+ " tc3_request"
346+ )
292347 val byteArray: ByteArray = hmac256(secretSigning, stringToSign)
293348 val signature: String? = encodeHexString(byteArray)
294349
@@ -345,14 +400,20 @@ open class VideoBaseService(secretId: String, secretKey: String) {
345400 /* *
346401 * base请求
347402 */
348- fun basePost (url : String , param : TreeMap <String , Any >, headerParams : Map <String , Any >, callback : VideoCallback , reqCode : Int ) {
403+ fun basePost (
404+ url : String ,
405+ param : TreeMap <String , Any >,
406+ headerParams : Map <String , Any >,
407+ callback : VideoCallback ,
408+ reqCode : Int
409+ ) {
349410 VideoHttpUtil .post(url, param, headerParams, object :
350411 HttpCallBack {
351412 override fun onSuccess (response : String ) {
352413 Log .d(" 响应${headerParams[" X-TC-Action" ]} " , response)
353- val jsonObject = JSON .parse (response) as JSONObject
354- val jsonResponset = jsonObject.getJSONObject(" Response" ) as JSONObject
355- if (! jsonResponset.containsKey (" ERROR" )) {
414+ val jsonObject = JSONObject (response)
415+ val jsonResponset = jsonObject.getJSONObject(" Response" )
416+ if (! jsonResponset.has (" ERROR" )) {
356417 callback.success(response, reqCode)
357418 } else {
358419 callback.fail(" error" , reqCode)
@@ -364,5 +425,4 @@ open class VideoBaseService(secretId: String, secretKey: String) {
364425 }
365426 })
366427 }
367-
368428}
0 commit comments