using System; using System.Collections; using System.Collections.Generic; using System.Text; using LitJson; using UnityEngine; using UnityEngine.Networking; using static Manager; using System.Security.Cryptography; using Random = System.Random; using XCharts.Runtime; using System.Linq; public class DeMaDataManager : MonoBehaviour { public static DeMaDataManager Instance; private void Awake() { Instance = this; // StartCoroutine(wx_demo("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9a280bbd81c64f56&secret=aeb6e5818800f8196fcac007e992dae0")); } public static string ip = ""; public IEnumerator Post_Demo(string url,string lstformData,Action callback) { Debug.Log("开始请求" + url + "_" + lstformData); while (ip=="") { Debug.Log("ip未设置" + ip + "_" + lstformData); yield return null; } string m_url = ip + url; Debug.Log("请求地址" + m_url + "_" + lstformData); var request = new UnityWebRequest(m_url, "POST"); byte[] bodyRaw = Encoding.UTF8.GetBytes(lstformData); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.SetRequestHeader("Content-Type", "application/json"); DownloadHandler downloadHandler = new DownloadHandlerBuffer(); request.downloadHandler = downloadHandler; request.SetRequestHeader("AUTHORIZATION", "c9a83db6a916ba530da2df11624c0ae6"); yield return request.SendWebRequest(); if (string.IsNullOrEmpty(request.error)) {//Post的请求成功 //Post请求的返回参数 var data = request.downloadHandler.text; Debug.Log(m_url+"_"+ lstformData + "——成功:" + data); callback.Invoke(data); } else {//Post的请求失败 Debug.Log(m_url + "失败"+ lstformData+"_"+ request.error); callback.Invoke(null); } } public IEnumerator Get_Demo(string url, string lstformData, Action callback) { Debug.Log("开始请求" + url + "_" + lstformData); while (ip == "") { Debug.Log("ip未设置" + ip + "_" + lstformData); yield return null; } JsonData d = JsonMapper.ToObject(lstformData); string m_url = ip + url+"?"; foreach (var item in d.Keys) { m_url+=item+"="+d[item].ToString()+"&"; } Debug.Log("请求地址" + m_url + "_" + lstformData); var request = new UnityWebRequest(m_url, "Get"); byte[] bodyRaw = Encoding.UTF8.GetBytes(lstformData); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.SetRequestHeader("Content-Type", "application/json"); DownloadHandler downloadHandler = new DownloadHandlerBuffer(); request.downloadHandler = downloadHandler; request.SetRequestHeader("AUTHORIZATION", "c9a83db6a916ba530da2df11624c0ae6"); yield return request.SendWebRequest(); if (string.IsNullOrEmpty(request.error)) {//Post的请求成功 //Post请求的返回参数 var data = request.downloadHandler.text; Debug.Log(m_url + "_" + lstformData + "——成功:" + data); callback.Invoke(data); } else {//Post的请求失败 Debug.Log(m_url + "失败" + lstformData + "_" + request.error); callback.Invoke(null); } } public class ShiShiDianList { public string name; public float f; } public IEnumerator GetUseDianLiang(Action> callback) { yield return StartCoroutine(Get_Demo("/digital-twin/v1/electricity/realtime", "", (string msg) => { List lf =new List(); try { JsonData data =JsonMapper.ToObject(msg); for (int i = 0;i(); for (int i = 0;i<10;i++) { ShiShiDianList ssdl =new ShiShiDianList(); ssdl.name = ""; ssdl.f = 0; lf.Add(ssdl); } callback.Invoke(lf); } })); } public class dvtype { public float pre; public int ct; } public IEnumerator GetShishiType(Action> callback) { yield return StartCoroutine(Post_Demo("/digital-twin/v1/subscribe/status", "", (string msg) => { List lf =new List(); for (int i = 0;i<10;i++) { dvtype dt=new dvtype(); dt.ct =1; dt.pre=100; lf.Add(dt); } callback.Invoke(lf); })); } public static string[] GetHoursArray() { return Enumerable.Range(1, 24) .Select(hour => hour.ToString("D2")) .Select(hour => $"{hour}:00") .ToArray(); } public static string[] GetHoursArray2() { return Enumerable.Range(0, 24) .Select(hour => hour.ToString("D2")) .Select(hour => $"{hour}:00") .ToArray(); } public void GetJinRiRanQi(Action callback) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Get_Demo("/digital-twin/v1/gas/realtime", "", (string msg) => { JsonData d =JsonMapper.ToObject(msg); DateTime startTime = new DateTime(DateTime.Now.Date.Ticks); DateTime[] timeStamps = new DateTime[25]; LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); data.xdata.AddRange(GetHoursArray2()); for (int i = 0; i < data.xdata.Count; i++) { timeStamps[i] = startTime.AddHours(i + 1); // 获取当前的秒级时间戳 long timestamp = (long)(timeStamps[i].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds; float ft =0; int ct = 0; long oldtime = i<=0?0:(long)(timeStamps[i-1].ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds; for (int j = 0;j=vtime&&oldtime(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetLeiJiRanqi(Action callback) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Get_Demo("/digital-twin/v1/gas/total/date","", (string msg) => { JsonData d =JsonMapper.ToObject(msg); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); for (int i = 0;i(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetgongyeZaiXian(Action callback,int idx) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 20; i++) { data.xdata.Add("xdata_" + i); } LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据1"; lc.values = new List(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetgongyeLiYong(Action callback,int idx) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 20; i++) { data.xdata.Add("xdata_" + i); } LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据1"; lc.values = new List(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetKeJiZaiXian(Action callback,int idx) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 20; i++) { data.xdata.Add("xdata_" + i); } LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据1"; lc.values = new List(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetGaoJingData(Action callback,int idx) { LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 20; i++) { data.xdata.Add("xdata_" + i); } LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据1"; lc.values = new List(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public void GetZongyongdianData(Action callback,int idx) { JsonData datas =new JsonData(); switch(idx) { case 1: datas["mode"] = "day"; break; case 2: datas["mode"] = "week"; break; case 3: datas["mode"] = "month"; break; } LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Get_Demo("/digital-twin/v1/electricity/total/date", datas.ToJson(), (string msg) => { JsonData d =JsonMapper.ToObject(msg); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); for (int i = 0;i(); for (int i = 0; i < 20; i++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); callback.Invoke(data); } public static string TimeStampToHMS(float timeStampInSeconds) { TimeSpan time = TimeSpan.FromSeconds(timeStampInSeconds); return time.ToString("hh\\:mm"); } public string changedate(string time,int i) { DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(time)).DateTime; switch(i) { case 0: return TimeStampToHMS((long.Parse(time))); break; case 1: return dateTime.ToString("MM/dd"); break; case 2: DateTime dateTime2 = DateTimeOffset.FromUnixTimeSeconds(long.Parse(time)+604800).DateTime; return dateTime.ToString("MM/dd")+"\n"+dateTime2.ToString("MM/dd"); break; case 3: return dateTime.ToString("MM月"); break; } return time; } public class baojing { public string name; public string time; public string dev; } public void GetGaoJingTaiZhang(Action> callback) { List bj = new List(); for (var i=0;i<100;i++) { baojing b = new baojing(); b.name = i.ToString(); b.time = DateTime.Now.ToString(); b.dev = i.ToString(); bj.Add(b); } callback.Invoke(bj); } }