using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.Application
{
///
/// 萤石云辅助类
///
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 _dict = new Dictionary();
///
/// 获取许可
///
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;
}
///
/// 获取播放地址
///
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;
}
}
}