ningshuxia
2022-12-12 e81ca048ef4e9345e904b74ffffd3e8413d18a7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Application
{
    /// <summary>
    /// 萤石云辅助类
    /// </summary>
    internal class SzjtYs7Helper 
    {
        // https://open.ys7.com/ 账号:15751003281 密码:hq199411
        //API  https://open.ys7.com/doc/zh/book/index/live_proto.html
        //AppKey   4dd1ebea34544429b55d0e12fef86156
        //Secret   1c6b63694f25c9ccf52920a6ab52cde7
 
        //缓存
        private static Dictionary<string, SzjtYs7TokenInfo> _dict = new Dictionary<string, SzjtYs7TokenInfo>();
 
        /// <summary>
        /// 获取许可
        /// </summary>
        public static string GetAccessToken(string appKey, string appSecret)
        {
            var key = $"{appKey}_{appSecret}";
            if (_dict.ContainsKey(key))
            {
                var tokenInfo = _dict[key];
                if (tokenInfo.ExpireTime != null && DateTime.Now > tokenInfo.ExpireTime && !string.IsNullOrEmpty(tokenInfo.Value))
                {
                    return tokenInfo.Value;
                }
            }
            var url = $"https://open.ys7.com/api/lapp/token/get?appKey={appKey}&appSecret={appSecret}";
            var responsetext = string.Empty;
            using (var httpClient = new HttpClient())
            using (var request = new HttpRequestMessage(HttpMethod.Post, url))
            {
                var response = httpClient.SendAsync(request).Result;
                response.EnsureSuccessStatusCode();
                responsetext = response.Content.ReadAsStringAsync().Result;
            }
            JObject dict = JObject.Parse(responsetext);
            JObject data = dict["data"] as Newtonsoft.Json.Linq.JObject;
            var token = new SzjtYs7TokenInfo();
            token.Value = data["accessToken"].ToString();
            long ticks = long.Parse(data["expireTime"].ToString());
            token.ExpireTime = new DateTime(ticks).AddMinutes(-10);
            _dict[key] = token;
            return token.Value;
        }
 
        /// <summary>
        /// 获取播放地址
        /// </summary>
        public static string GetLiveUrl(string appKey, string appSecret, string verificationCode, string seriesNO, int channelNO)
        {
            var viewToken = GetAccessToken(appKey, appSecret);
            var url = $"https://open.ys7.com/ezopen/h5/iframe?url=ezopen://{verificationCode}@open.ys7.com/{seriesNO}/1.live&autoplay=1&accessToken={viewToken}&channelNo={channelNO}";
            return url;
        }
 
 
 
    }
}