FanyiRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using Ropin.Inspection.Common.Helper;
  4. using Ropin.Inspection.Model;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.NodeServices;
  12. using System.Net.Http.Headers;
  13. using System.Net;
  14. namespace Ropin.Inspection.Repository
  15. {
  16. public class FanyiRepository : IFanyiRepository
  17. {
  18. private readonly INodeServices _nodeServices;
  19. public FanyiRepository(INodeServices nodeServices)
  20. {
  21. _nodeServices = nodeServices;
  22. }
  23. public async Task<string> GetWebScadaDevSpotHisData(string devId, List<string> names, List<string> groupnames)
  24. {
  25. string jResult = String.Empty;
  26. if (DateTime.Now >= FanyiHelper.tokenTime)
  27. {
  28. FanyiHelper.tokenResult = FanyiHelper.GetToken(new AccessUser { client_id = "b19d14eeacb74522bd29627b79c18ab8", client_secret = "7b89e021586c43d3b79440ba6eea0b67" });
  29. FanyiHelper.tokenTime = DateTime.Now.AddSeconds(7200);
  30. using (HttpClient client = new HttpClient())
  31. {
  32. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token);
  33. var requestt = new HttpRequestMessage(HttpMethod.Get, "https://fbox360.com/api/client/box/grouped");
  34. requestt.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  35. var response = await client.SendAsync(requestt);
  36. string result = await response.Content.ReadAsStringAsync();
  37. if (response.StatusCode != HttpStatusCode.OK)
  38. {
  39. }
  40. else
  41. {
  42. //todo
  43. }
  44. }
  45. }
  46. using (HttpClient client = new HttpClient())
  47. {
  48. string[] idarr = { "224872398136431165" };
  49. string postData = Newtonsoft.Json.JsonConvert.SerializeObject(new
  50. {
  51. type = 0,
  52. ids = idarr,
  53. g = 0,
  54. begin = DateTime.Now.AddDays(-5).Ticks,
  55. end = DateTime.Now.Ticks,
  56. tr = 3,
  57. limit = -500,
  58. tz = "Asia/Shanghai"
  59. });
  60. using (var httpClient = new HttpClient())
  61. {
  62. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token);
  63. //httpClient.BaseAddress = new Uri("https://fbox360.com/api/v2/hdata/get");
  64. httpClient.BaseAddress = new Uri("http://fbhs1.fbox360.com/api/v2/hdata/get");
  65. //httpClient.BaseAddress = new Uri("http://fbcs101.fbox360.com/api/v2/hdata/get");
  66. httpClient.DefaultRequestHeaders.Accept.Clear();
  67. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  68. var jsonString = postData;
  69. HttpResponseMessage esponse = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(jsonString, Encoding.UTF8, "application/json"));
  70. if (esponse.IsSuccessStatusCode)
  71. {
  72. string json = await esponse.Content.ReadAsStringAsync();
  73. List<DeviceVlue> deviceVueList = JsonConvert.DeserializeObject<List<DeviceVlue>>(json);
  74. }
  75. }
  76. return await Task.FromResult(jResult);
  77. }
  78. }
  79. Dictionary<string, DeviceVlue> alarmDic = new Dictionary<string, DeviceVlue>();
  80. public Dictionary<string, DeviceVlue> msgAlarmDic = new Dictionary<string, DeviceVlue>();
  81. public async Task<string> GetWebScadaDevSpotValue(string devId, string boxNo, string storeCode, List<string> names, List<string> groupnames, List<string> calFormula, INodeServices services, List<List<CalFormula>> calFormulaList)
  82. {
  83. //Dictionary<string, DeviceVlue> msgAlarmDic = new Dictionary<string, DeviceVlue>();
  84. string jResult = String.Empty;
  85. if (DateTime.Now >= FanyiHelper.tokenTime)
  86. {
  87. FanyiHelper.tokenResult = FanyiHelper.GetToken(new AccessUser { client_id = "b19d14eeacb74522bd29627b79c18ab8", client_secret = "7b89e021586c43d3b79440ba6eea0b67" });
  88. FanyiHelper.tokenTime = DateTime.Now.AddSeconds(7200);
  89. using (HttpClient client = new HttpClient())
  90. {
  91. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token);
  92. var requestt = new HttpRequestMessage(HttpMethod.Get, "https://fbox360.com/api/client/box/grouped");
  93. requestt.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  94. var response = await client.SendAsync(requestt);
  95. string result = await response.Content.ReadAsStringAsync();
  96. if (response.StatusCode != HttpStatusCode.OK)
  97. {
  98. }
  99. else
  100. {
  101. //todo
  102. }
  103. }
  104. }
  105. using (HttpClient client = new HttpClient())
  106. {
  107. string postData = Newtonsoft.Json.JsonConvert.SerializeObject(new
  108. {
  109. names = names,
  110. groupnames = groupnames,
  111. timeOut = 6000
  112. });
  113. using (var httpClient = new HttpClient())
  114. {
  115. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token);
  116. httpClient.BaseAddress = new Uri("http://fbcs101.fbox360.com/api/v2/dmon/value/get?boxNo=" + boxNo);
  117. httpClient.DefaultRequestHeaders.Accept.Clear();
  118. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  119. var jsonString = postData;
  120. HttpResponseMessage esponse = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(jsonString, Encoding.UTF8, "application/json"));
  121. if (esponse.IsSuccessStatusCode)
  122. {
  123. string json = await esponse.Content.ReadAsStringAsync();
  124. List<DeviceVlue> deviceVueList = JsonConvert.DeserializeObject<List<DeviceVlue>>(json);
  125. List<decimal> li1 = new List<decimal>();
  126. var jarray = new JArray();
  127. var aljarray = new JArray();
  128. //aljarray.Add(new JObject { { "colour", "red" } });
  129. //aljarray.Add(new JObject { { "colour", "green" } });
  130. //aljarray.Add(new JObject { { "colour", "black" } });
  131. //aljarray.Add(new JValue("red") );
  132. //aljarray.Add(new JValue("green"));
  133. //aljarray.Add(new JValue("black"));
  134. for (int i = 0; i < deviceVueList.Count; i++)
  135. {
  136. if (null == deviceVueList[i])
  137. continue;
  138. deviceVueList[i].storeCode = storeCode;
  139. //JObject HS = new JObject { { "name" + i, deviceVueList[0]?.name }, { "value" + i, deviceVueList[i]?.value.ToString().Split('.')[0]} };
  140. //JObject HS = new JObject { { "name", deviceVueList[0]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] }, { "mix", calFormula?[i].Split(',')?[1] }, { "max", calFormula?[i].Split(',')?[2] } };
  141. //List<string> args = new List<string>();
  142. //args.Add(calFormula?[i]?.Split(',')?[1]);
  143. //args.Add(calFormula?[i]?.Split(',')?[2]);
  144. //args.Add(deviceVueList[i]?.value.ToString());
  145. //JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } };
  146. //JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] }, { "colour", await services.InvokeAsync<string>(calFormula?[i].Split(',')?[0], args)} };
  147. if (null == calFormulaList)
  148. {
  149. JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } };
  150. //HS.Add(jo);
  151. jarray.Add(HS);
  152. }
  153. if (null != calFormulaList)
  154. if (i < calFormulaList.Count && null != calFormulaList.ElementAt(i))
  155. {
  156. foreach (var item in calFormulaList[i])
  157. {
  158. List<string> calargs = item.Value.ToList();
  159. calargs.Add(deviceVueList[i]?.value.ToString());
  160. //JObject jo = new JObject { item.Name, await services.InvokeAsync<string>("./wwwroot/uploads/scripts/" + item.Name, calargs) };
  161. bool bAlarm = false;
  162. if (item.Name == "changecolour")
  163. {
  164. string alarmColour = await services.InvokeAsync<string>("./wwwroot/uploads/scripts/" + item.Name, calargs);
  165. bAlarm = FanyiHelper.AlarmByColour(alarmColour, deviceVueList[i]);
  166. JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] }, { item.Name, alarmColour } };
  167. //HS.Add(jo);
  168. jarray.Add(HS);
  169. }
  170. else if (item.Name == "alarmlight")
  171. {
  172. JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } };
  173. //HS.Add(jo);
  174. jarray.Add(HS);
  175. string alarmColour = await services.InvokeAsync<string>("./wwwroot/uploads/scripts/" + item.Name, calargs);
  176. bAlarm = FanyiHelper.AlarmByColour(alarmColour, deviceVueList[i]);
  177. JObject jo = new JObject { { "alarmcolour", alarmColour } };
  178. aljarray.Add(jo);
  179. }
  180. else if (item.Name == "noformula")
  181. {
  182. JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } };
  183. //HS.Add(jo);
  184. jarray.Add(HS);
  185. }
  186. }
  187. }
  188. //jarray.Add(HS);
  189. }
  190. var deviceJObject = new JObject();
  191. deviceJObject.Add("device", jarray);
  192. deviceJObject.Add("alarmlight", aljarray);
  193. jResult = JsonConvert.SerializeObject(deviceJObject);
  194. }
  195. }
  196. }
  197. return jResult;
  198. }
  199. }
  200. }