1 usingNewtonsoft.Json;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Net.Http;6 usingSystem.Net.Http.Headers;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingYJT.Common;10
11 /*20190819 by suzong*/
12 namespaceYJT.Wisdom.Api.lib13{14 ///
15 ///萤石云请求封装
16 ///
17 public classYsClient18{19 private static readonly string requestUrl = "https://open.ys7.com/";20 private static readonly string appKey = "";//官网注册获得
21 private static readonly string appSecret = "";//官网注册获得
22
23 ///
24 ///获得token
25 ///
26 /// {code:200,data:{accessToken:"",expireTime:精确到毫秒}}
27 public static async TaskGetToken()28{29 string key = ConfigHelper.GetSetting("CacheKey:YsToken") ?? "YsAccessToken";30 string tokenStr = MemoryCacheHelper.Get(key)?.ToString();31 if (string.IsNullOrEmpty(tokenStr))32{33 string str = await HttpHelper.HttpPostAsync($"{requestUrl}api/lapp/token/get?appKey={appKey}&appSecret={appSecret}");34 YsResult result = JsonConvert.DeserializeObject(str);35 //缓存token 缓存时间为5天
36 tokenStr = result?.data?.accessToken;37 MemoryCacheHelper.Set(key, tokenStr, TimeSpan.FromDays(5));38}39 returntokenStr;40}41
42 ///
43 ///添加设备
44 ///
45 /// 设备序列号
46 /// 设备验证码
47 ///
48 public static async Task SaveDevice(string deviceSerial, stringvalidateCode)49{50 if (string.IsNullOrEmpty(deviceSerial) || string.IsNullOrEmpty(validateCode))51 return new YsResult() { code = "-1", msg = "缺少验证码或序列号"};52
53 string str = await HttpHelper.HttpPostAsync($"{requestUrl}api/lapp/device/add?accessToken={GetToken().Result}&deviceSerial={deviceSerial.ToUpper()}&validateCode={validateCode.ToUpper()}");54 return JsonConvert.DeserializeObject(str);55}56
57 /