RestfulClient.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using RestSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using WisdomClient.data;
  8. namespace WisdomClient
  9. {
  10. public class RestfulClient
  11. {
  12. private static int QUERY_COUNT = 75;
  13. private static string serverUrl;
  14. public static string ServerUrl
  15. {
  16. get
  17. {
  18. if (string.IsNullOrWhiteSpace(serverUrl))
  19. serverUrl = ConfigurationManager.AppSettings["RestServerUrl"];
  20. return serverUrl;
  21. }
  22. set { serverUrl = value; }
  23. }
  24. public static Dictionary<string, TsData> findLatestByTagNames(string[] tagNames)
  25. {
  26. Dictionary<string, TsData> result = new Dictionary<string, TsData>();
  27. var client = new RestClient(ServerUrl);
  28. int selectCount = tagNames.Length / QUERY_COUNT;
  29. int remainder = tagNames.Length % QUERY_COUNT;
  30. if (remainder == 0)//如果被整除
  31. {
  32. for (int i = 0; i < selectCount; i++)
  33. {
  34. string[] subTagName = tagNames.Skip(QUERY_COUNT * i).Take(QUERY_COUNT).ToArray();
  35. var request = new RestRequest("latest", Method.GET);
  36. request.AddParameter("keys", String.Join(",", subTagName));
  37. IRestResponse<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
  38. if (response.Data != null)
  39. {
  40. foreach (var item in response.Data)
  41. {
  42. result.Add(item.Key, item.Value);
  43. }
  44. }
  45. }
  46. }
  47. else//tagNames长度无法被1000整除
  48. {
  49. for (int i = 0; i < selectCount; i++)
  50. {
  51. string[] subTagName = tagNames.Skip(QUERY_COUNT * i).Take(QUERY_COUNT).ToArray();
  52. var request = new RestRequest("latest", Method.GET);
  53. request.AddParameter("keys", String.Join(",", subTagName));
  54. IRestResponse<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
  55. if (response.Data != null)
  56. {
  57. foreach (var item in response.Data)
  58. {
  59. if (result.ContainsKey(item.Key))
  60. Console.WriteLine(item.Key);
  61. result.Add(item.Key, item.Value);
  62. }
  63. }
  64. }
  65. //将数组剩余元素进行查询
  66. String[] lastTagName = tagNames.Skip(QUERY_COUNT * selectCount).Take(remainder).ToArray();
  67. var lastRequest = new RestRequest("latest", Method.GET);
  68. lastRequest.AddParameter("keys", String.Join(",", lastTagName));
  69. IRestResponse<Dictionary<string, TsData>> lastResponse = client.Execute<Dictionary<string, TsData>>(lastRequest);
  70. if (lastResponse.Data != null)
  71. {
  72. foreach (var item in lastResponse.Data)
  73. {
  74. result.Add(item.Key, item.Value);
  75. }
  76. }
  77. }
  78. return result;
  79. }
  80. public static Dictionary<string, TsData> findLatestByThingCodes(string thingType, string thingId, string[] uCodes)
  81. {
  82. var client = new RestClient(ServerUrl);
  83. var request = new RestRequest("latest", Method.GET);
  84. request.AddParameter("thingType", thingType);
  85. request.AddParameter("thingId", thingId);
  86. request.AddParameter("uniformCodes", String.Join(",", uCodes));
  87. IRestResponse<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
  88. return response.Data;
  89. }
  90. public static List<TsData> findHistoryRawByTagName(string tagName, long startTs, long endTs)
  91. {
  92. var client = new RestClient(ServerUrl);
  93. var request = new RestRequest("history/raw", Method.GET);
  94. request.AddParameter("tagName", tagName);
  95. request.AddParameter("startTs", startTs);
  96. request.AddParameter("endTs", endTs);
  97. IRestResponse<List<TsData>> response = client.Execute<List<TsData>>(request);
  98. return response.Data;
  99. }
  100. public static List<TsData> findHistoryRawByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs)
  101. {
  102. var client = new RestClient(ServerUrl);
  103. var request = new RestRequest("history/raw", Method.GET);
  104. request.AddParameter("thingType", thingType);
  105. request.AddParameter("thingId", thingId);
  106. request.AddParameter("uniformCode", uniformCode);
  107. request.AddParameter("startTs", startTs);
  108. request.AddParameter("endTs", endTs);
  109. IRestResponse<List<TsData>> response = client.Execute<List<TsData>>(request);
  110. return response.Data;
  111. }
  112. public static List<TsData> findHistorySnapByTagName(string tagName, long startTs, long endTs, int interval)
  113. {
  114. var client = new RestClient(ServerUrl);
  115. var request = new RestRequest("history/snap", Method.GET);
  116. request.AddParameter("tagName", tagName);
  117. request.AddParameter("startTs", startTs);
  118. request.AddParameter("endTs", endTs);
  119. request.AddParameter("interval", interval);
  120. IRestResponse<List<TsData>> response = client.Execute<List<TsData>>(request);
  121. return response.Data;
  122. }
  123. public static List<TsData> findHistorySnapByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs, int interval)
  124. {
  125. var client = new RestClient(ServerUrl);
  126. var request = new RestRequest("history/snap", Method.GET);
  127. request.AddParameter("thingType", thingType);
  128. request.AddParameter("thingId", thingId);
  129. request.AddParameter("uniformCode", uniformCode);
  130. request.AddParameter("startTs", startTs);
  131. request.AddParameter("endTs", endTs);
  132. request.AddParameter("interval", interval);
  133. IRestResponse<List<TsData>> response = client.Execute<List<TsData>>(request);
  134. return response.Data;
  135. }
  136. public static List<DoubleStatData> findHistoryStatByTagName(string tagName, long startTs, long endTs, int interval)
  137. {
  138. var client = new RestClient(ServerUrl);
  139. var request = new RestRequest("history/stat", Method.GET);
  140. request.AddParameter("tagName", tagName);
  141. request.AddParameter("startTs", startTs);
  142. request.AddParameter("endTs", endTs);
  143. request.AddParameter("interval", interval);
  144. IRestResponse<List<DoubleStatData>> response = client.Execute<List<DoubleStatData>>(request);
  145. return response.Data;
  146. }
  147. public static List<DoubleStatData> findHistoryStatByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs, int interval)
  148. {
  149. var client = new RestClient(ServerUrl);
  150. var request = new RestRequest("history/stat", Method.GET);
  151. request.AddParameter("thingType", thingType);
  152. request.AddParameter("thingId", thingId);
  153. request.AddParameter("uniformCode", uniformCode);
  154. request.AddParameter("startTs", startTs);
  155. request.AddParameter("endTs", endTs);
  156. request.AddParameter("interval", interval);
  157. IRestResponse<List<DoubleStatData>> response = client.Execute<List<DoubleStatData>>(request);
  158. return response.Data;
  159. }
  160. public static Dictionary<string, TsData> findHistorySectionByTagNames(string[] tagNames, long ts)
  161. {
  162. var client = new RestClient(ServerUrl);
  163. var request = new RestRequest("history/section", Method.GET);
  164. request.AddParameter("tagName", String.Join(",", tagNames));
  165. request.AddParameter("ts", ts);
  166. IRestResponse<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
  167. return response.Data;
  168. }
  169. public static Dictionary<string, TsData> findHistorySectionByThingCodes(string thingType, string thingId, string[] uCodes, long ts)
  170. {
  171. var client = new RestClient(ServerUrl);
  172. var request = new RestRequest("history/section", Method.GET);
  173. request.AddParameter("thingType", thingType);
  174. request.AddParameter("thingId", thingId);
  175. request.AddParameter("uniformCodes", String.Join(",", uCodes));
  176. request.AddParameter("ts", ts);
  177. IRestResponse<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
  178. return response.Data;
  179. }
  180. public static void writeHistoryBatch(List<TsPointData> tsPointDataList)
  181. {
  182. var client = new RestClient(ServerUrl);
  183. var request = new RestRequest("history/batch", Method.POST);
  184. request.RequestFormat = DataFormat.Json;
  185. //request.AddParameter("tagName", String.Join(",", tagNames));
  186. request.AddBody(tsPointDataList);
  187. //string tmp = request.JsonSerializer.Serialize(tsPointDataList);
  188. //request.AddBody(tmp);
  189. client.Execute<Object>(request);
  190. //return false;
  191. }
  192. public static void writeHistory(TsPointData tsPointData)
  193. {
  194. var client = new RestClient(ServerUrl);
  195. var request = new RestRequest("history", Method.POST);
  196. request.RequestFormat = DataFormat.Json;
  197. //request.AddParameter("tagName", String.Join(",", tagNames));
  198. request.AddBody(tsPointData);
  199. //string tmp = request.JsonSerializer.Serialize(tsPointDataList);
  200. //request.AddBody(tmp);
  201. client.Execute<Object>(request);
  202. //return false;
  203. }
  204. public static void writeHistoryList(TsPointDataList tsPointDataList)
  205. {
  206. var client = new RestClient(ServerUrl);
  207. var request = new RestRequest("history/list", Method.POST);
  208. request.RequestFormat = DataFormat.Json;
  209. request.AddBody(tsPointDataList);
  210. client.Execute<Object>(request);
  211. //return false;
  212. }
  213. public static void writeLatest(TsPointData tsPointData)
  214. {
  215. try
  216. {
  217. var client = new RestClient(ServerUrl);
  218. var request = new RestRequest("latest", Method.POST);
  219. request.RequestFormat = DataFormat.Json;
  220. request.AddBody(tsPointData);
  221. var result = client.Execute<Object>(request);
  222. }
  223. catch (Exception)
  224. {
  225. throw;
  226. }
  227. }
  228. }
  229. }