using RestSharp; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using WisdomClient.data; namespace WisdomClient { public class RestfulClient { private static int QUERY_COUNT = 75; private static string serverUrl; public static string ServerUrl { get { if (string.IsNullOrWhiteSpace(serverUrl)) serverUrl = ConfigurationManager.AppSettings["RestServerUrl"]; return serverUrl; } set { serverUrl = value; } } public static Dictionary findLatestByTagNames(string[] tagNames) { Dictionary result = new Dictionary(); var client = new RestClient(ServerUrl); int selectCount = tagNames.Length / QUERY_COUNT; int remainder = tagNames.Length % QUERY_COUNT; if (remainder == 0)//如果被整除 { for (int i = 0; i < selectCount; i++) { string[] subTagName = tagNames.Skip(QUERY_COUNT * i).Take(QUERY_COUNT).ToArray(); var request = new RestRequest("latest", Method.GET); request.AddParameter("keys", String.Join(",", subTagName)); IRestResponse> response = client.Execute>(request); if (response.Data != null) { foreach (var item in response.Data) { result.Add(item.Key, item.Value); } } } } else//tagNames长度无法被1000整除 { for (int i = 0; i < selectCount; i++) { string[] subTagName = tagNames.Skip(QUERY_COUNT * i).Take(QUERY_COUNT).ToArray(); var request = new RestRequest("latest", Method.GET); request.AddParameter("keys", String.Join(",", subTagName)); IRestResponse> response = client.Execute>(request); if (response.Data != null) { foreach (var item in response.Data) { if (result.ContainsKey(item.Key)) Console.WriteLine(item.Key); result.Add(item.Key, item.Value); } } } //将数组剩余元素进行查询 String[] lastTagName = tagNames.Skip(QUERY_COUNT * selectCount).Take(remainder).ToArray(); var lastRequest = new RestRequest("latest", Method.GET); lastRequest.AddParameter("keys", String.Join(",", lastTagName)); IRestResponse> lastResponse = client.Execute>(lastRequest); if (lastResponse.Data != null) { foreach (var item in lastResponse.Data) { result.Add(item.Key, item.Value); } } } return result; } public static Dictionary findLatestByThingCodes(string thingType, string thingId, string[] uCodes) { var client = new RestClient(ServerUrl); var request = new RestRequest("latest", Method.GET); request.AddParameter("thingType", thingType); request.AddParameter("thingId", thingId); request.AddParameter("uniformCodes", String.Join(",", uCodes)); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistoryRawByTagName(string tagName, long startTs, long endTs) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/raw", Method.GET); request.AddParameter("tagName", tagName); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistoryRawByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/raw", Method.GET); request.AddParameter("thingType", thingType); request.AddParameter("thingId", thingId); request.AddParameter("uniformCode", uniformCode); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistorySnapByTagName(string tagName, long startTs, long endTs, int interval) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/snap", Method.GET); request.AddParameter("tagName", tagName); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); request.AddParameter("interval", interval); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistorySnapByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs, int interval) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/snap", Method.GET); request.AddParameter("thingType", thingType); request.AddParameter("thingId", thingId); request.AddParameter("uniformCode", uniformCode); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); request.AddParameter("interval", interval); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistoryStatByTagName(string tagName, long startTs, long endTs, int interval) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/stat", Method.GET); request.AddParameter("tagName", tagName); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); request.AddParameter("interval", interval); IRestResponse> response = client.Execute>(request); return response.Data; } public static List findHistoryStatByThingCodes(string thingType, string thingId, string uniformCode, long startTs, long endTs, int interval) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/stat", Method.GET); request.AddParameter("thingType", thingType); request.AddParameter("thingId", thingId); request.AddParameter("uniformCode", uniformCode); request.AddParameter("startTs", startTs); request.AddParameter("endTs", endTs); request.AddParameter("interval", interval); IRestResponse> response = client.Execute>(request); return response.Data; } public static Dictionary findHistorySectionByTagNames(string[] tagNames, long ts) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/section", Method.GET); request.AddParameter("tagName", String.Join(",", tagNames)); request.AddParameter("ts", ts); IRestResponse> response = client.Execute>(request); return response.Data; } public static Dictionary findHistorySectionByThingCodes(string thingType, string thingId, string[] uCodes, long ts) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/section", Method.GET); request.AddParameter("thingType", thingType); request.AddParameter("thingId", thingId); request.AddParameter("uniformCodes", String.Join(",", uCodes)); request.AddParameter("ts", ts); IRestResponse> response = client.Execute>(request); return response.Data; } public static void writeHistoryBatch(List tsPointDataList) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/batch", Method.POST); request.RequestFormat = DataFormat.Json; //request.AddParameter("tagName", String.Join(",", tagNames)); request.AddBody(tsPointDataList); //string tmp = request.JsonSerializer.Serialize(tsPointDataList); //request.AddBody(tmp); client.Execute(request); //return false; } public static void writeHistory(TsPointData tsPointData) { var client = new RestClient(ServerUrl); var request = new RestRequest("history", Method.POST); request.RequestFormat = DataFormat.Json; //request.AddParameter("tagName", String.Join(",", tagNames)); request.AddBody(tsPointData); //string tmp = request.JsonSerializer.Serialize(tsPointDataList); //request.AddBody(tmp); client.Execute(request); //return false; } public static void writeHistoryList(TsPointDataList tsPointDataList) { var client = new RestClient(ServerUrl); var request = new RestRequest("history/list", Method.POST); request.RequestFormat = DataFormat.Json; request.AddBody(tsPointDataList); client.Execute(request); //return false; } public static void writeLatest(TsPointData tsPointData) { try { var client = new RestClient(ServerUrl); var request = new RestRequest("latest", Method.POST); request.RequestFormat = DataFormat.Json; request.AddBody(tsPointData); var result = client.Execute(request); } catch (Exception) { throw; } } } }