using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.NodeServices; using System.Net.Http.Headers; using System.Net; namespace Ropin.Inspection.Repository { public class FanyiRepository : IFanyiRepository { private readonly INodeServices _nodeServices; public FanyiRepository(INodeServices nodeServices) { _nodeServices = nodeServices; } public async Task GetWebScadaDevSpotHisData(string devId, List names, List groupnames) { string jResult = String.Empty; if (DateTime.Now >= FanyiHelper.tokenTime) { FanyiHelper.tokenResult = FanyiHelper.GetToken(new AccessUser { client_id = "b19d14eeacb74522bd29627b79c18ab8", client_secret = "7b89e021586c43d3b79440ba6eea0b67" }); FanyiHelper.tokenTime = DateTime.Now.AddSeconds(7200); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token); var requestt = new HttpRequestMessage(HttpMethod.Get, "https://fbox360.com/api/client/box/grouped"); requestt.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.SendAsync(requestt); string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { } else { //todo } } } using (HttpClient client = new HttpClient()) { string[] idarr = { "224872398136431165" }; string postData = Newtonsoft.Json.JsonConvert.SerializeObject(new { type = 0, ids = idarr, g = 0, begin = DateTime.Now.AddDays(-5).Ticks, end = DateTime.Now.Ticks, tr = 3, limit = -500, tz = "Asia/Shanghai" }); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token); //httpClient.BaseAddress = new Uri("https://fbox360.com/api/v2/hdata/get"); httpClient.BaseAddress = new Uri("http://fbhs1.fbox360.com/api/v2/hdata/get"); //httpClient.BaseAddress = new Uri("http://fbcs101.fbox360.com/api/v2/hdata/get"); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var jsonString = postData; HttpResponseMessage esponse = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(jsonString, Encoding.UTF8, "application/json")); if (esponse.IsSuccessStatusCode) { string json = await esponse.Content.ReadAsStringAsync(); List deviceVueList = JsonConvert.DeserializeObject>(json); } } return await Task.FromResult(jResult); } } Dictionary alarmDic = new Dictionary(); public Dictionary msgAlarmDic = new Dictionary(); public async Task GetWebScadaDevSpotValue(string devId, string boxNo, string storeCode, List names, List groupnames, List calFormula, INodeServices services, List> calFormulaList) { //Dictionary msgAlarmDic = new Dictionary(); string jResult = String.Empty; if (DateTime.Now >= FanyiHelper.tokenTime) { FanyiHelper.tokenResult = FanyiHelper.GetToken(new AccessUser { client_id = "b19d14eeacb74522bd29627b79c18ab8", client_secret = "7b89e021586c43d3b79440ba6eea0b67" }); FanyiHelper.tokenTime = DateTime.Now.AddSeconds(7200); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token); var requestt = new HttpRequestMessage(HttpMethod.Get, "https://fbox360.com/api/client/box/grouped"); requestt.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.SendAsync(requestt); string result = await response.Content.ReadAsStringAsync(); if (response.StatusCode != HttpStatusCode.OK) { } else { //todo } } } using (HttpClient client = new HttpClient()) { string postData = Newtonsoft.Json.JsonConvert.SerializeObject(new { names = names, groupnames = groupnames, timeOut = 6000 }); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", FanyiHelper.tokenResult.access_token); httpClient.BaseAddress = new Uri("http://fbcs101.fbox360.com/api/v2/dmon/value/get?boxNo=" + boxNo); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var jsonString = postData; HttpResponseMessage esponse = await httpClient.PostAsync(httpClient.BaseAddress, new StringContent(jsonString, Encoding.UTF8, "application/json")); if (esponse.IsSuccessStatusCode) { string json = await esponse.Content.ReadAsStringAsync(); List deviceVueList = JsonConvert.DeserializeObject>(json); List li1 = new List(); var jarray = new JArray(); var aljarray = new JArray(); //aljarray.Add(new JObject { { "colour", "red" } }); //aljarray.Add(new JObject { { "colour", "green" } }); //aljarray.Add(new JObject { { "colour", "black" } }); //aljarray.Add(new JValue("red") ); //aljarray.Add(new JValue("green")); //aljarray.Add(new JValue("black")); for (int i = 0; i < deviceVueList.Count; i++) { if (null == deviceVueList[i]) continue; deviceVueList[i].storeCode = storeCode; //JObject HS = new JObject { { "name" + i, deviceVueList[0]?.name }, { "value" + i, deviceVueList[i]?.value.ToString().Split('.')[0]} }; //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] } }; //List args = new List(); //args.Add(calFormula?[i]?.Split(',')?[1]); //args.Add(calFormula?[i]?.Split(',')?[2]); //args.Add(deviceVueList[i]?.value.ToString()); //JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } }; //JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] }, { "colour", await services.InvokeAsync(calFormula?[i].Split(',')?[0], args)} }; if (null == calFormulaList) { JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } }; //HS.Add(jo); jarray.Add(HS); } if (null != calFormulaList) if (i < calFormulaList.Count && null != calFormulaList.ElementAt(i)) { foreach (var item in calFormulaList[i]) { List calargs = item.Value.ToList(); calargs.Add(deviceVueList[i]?.value.ToString()); //JObject jo = new JObject { item.Name, await services.InvokeAsync("./wwwroot/uploads/scripts/" + item.Name, calargs) }; bool bAlarm = false; if (item.Name == "changecolour") { string alarmColour = await services.InvokeAsync("./wwwroot/uploads/scripts/" + item.Name, calargs); bAlarm = FanyiHelper.AlarmByColour(alarmColour, deviceVueList[i]); JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] }, { item.Name, alarmColour } }; //HS.Add(jo); jarray.Add(HS); } else if (item.Name == "alarmlight") { JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } }; //HS.Add(jo); jarray.Add(HS); string alarmColour = await services.InvokeAsync("./wwwroot/uploads/scripts/" + item.Name, calargs); bAlarm = FanyiHelper.AlarmByColour(alarmColour, deviceVueList[i]); JObject jo = new JObject { { "alarmcolour", alarmColour } }; aljarray.Add(jo); } else if (item.Name == "noformula") { JObject HS = new JObject { { "name", deviceVueList[i]?.name }, { "value", deviceVueList[i]?.value.ToString().Split('.')[0] } }; //HS.Add(jo); jarray.Add(HS); } } } //jarray.Add(HS); } var deviceJObject = new JObject(); deviceJObject.Add("device", jarray); deviceJObject.Add("alarmlight", aljarray); jResult = JsonConvert.SerializeObject(deviceJObject); } } } return jResult; } } }