123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- 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<string, TsData> findLatestByTagNames(string[] tagNames)
- {
- Dictionary<string, TsData> result = new Dictionary<string, TsData>();
- 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<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(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<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(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<Dictionary<string, TsData>> lastResponse = client.Execute<Dictionary<string, TsData>>(lastRequest);
- if (lastResponse.Data != null)
- {
- foreach (var item in lastResponse.Data)
- {
- result.Add(item.Key, item.Value);
- }
- }
- }
- return result;
- }
- public static Dictionary<string, TsData> 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<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
- return response.Data;
- }
- public static List<TsData> 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<List<TsData>> response = client.Execute<List<TsData>>(request);
- return response.Data;
- }
- public static List<TsData> 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<List<TsData>> response = client.Execute<List<TsData>>(request);
- return response.Data;
- }
- public static List<TsData> 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<List<TsData>> response = client.Execute<List<TsData>>(request);
- return response.Data;
- }
- public static List<TsData> 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<List<TsData>> response = client.Execute<List<TsData>>(request);
- return response.Data;
- }
- public static List<DoubleStatData> 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<List<DoubleStatData>> response = client.Execute<List<DoubleStatData>>(request);
- return response.Data;
- }
- public static List<DoubleStatData> 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<List<DoubleStatData>> response = client.Execute<List<DoubleStatData>>(request);
- return response.Data;
- }
- public static Dictionary<string, TsData> 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<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
- return response.Data;
- }
- public static Dictionary<string, TsData> 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<Dictionary<string, TsData>> response = client.Execute<Dictionary<string, TsData>>(request);
- return response.Data;
- }
- public static void writeHistoryBatch(List<TsPointData> 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<Object>(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<Object>(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<Object>(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<Object>(request);
- }
- catch (Exception)
- {
- throw;
- }
-
-
- }
- }
- }
|