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"); 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(request.error); } } 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, 25) .Select(hour => hour.ToString("D2")) .Select(hour => $"{hour}:00") .ToArray(); } public void GetDeviceType(string id,Action callback) { JsonData jsonData =new JsonData(); jsonData["deviceCode"]=id; StartCoroutine(Post_Demo("/digital-twin/v1/device/runtime", jsonData.ToJson(), (string msg) => { JsonData d =JsonMapper.ToObject(msg); callback.Invoke(d); })); } 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) => { try { 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;jvtime&&oldtime<=vtime) { ft += float.Parse(d["data"]["list"][j]["value"].ToString()); ct++; } else { } } if(ct==0) { Debug.Log(oldtime+"_"+timestamp+"_"+data.xdata[i]); ct=1; } float value =ft/ct; lc.values.Add(value); } data.sdata.Add(lc); callback.Invoke(data); }catch { Debug.Log("error==>"+msg); } })); return; 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 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 GetDeviceInfo(string id,Action callback) { JsonData dataj=new JsonData(); dataj["buID"] =int.Parse(id) ; LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Post_Demo("/digital-twin/v1/device/realtime",dataj.ToJson(), (string msg) => { JsonData d =JsonMapper.ToObject(msg); callback.Invoke(d); })); return; } 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(string url,Action callback,int idx,int type) { JsonData datas =new JsonData(); switch(idx) { case 1: datas["mode"] = "day"; break; case 2: datas["mode"] = "week"; break; case 3: datas["mode"] = "month"; break; } datas["buID"] = type; LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Post_Demo(url, datas.ToJson(), (string msg) => { try { JsonData d =JsonMapper.ToObject(msg); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); for (int i = 0;i"+msg); } })); return; } public void GetGaoJingData(Action callback,int idx,int type) { JsonData datas =new JsonData(); switch(idx) { case 1: datas["mode"] = "day"; break; case 2: datas["mode"] = "week"; break; case 3: datas["mode"] = "month"; break; } datas["buID"] = type; LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); StartCoroutine(Post_Demo("/digital-twin/v1/device/ratio/alarm", datas.ToJson(), (string msg) => { try { JsonData d =JsonMapper.ToObject(msg); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); for (int i = 0;i"+ d["data"]["list"][i]["date"].ToString()); Debug.Log(" data.xdata=2=>"+ data.xdata[ data.xdata.Count-1]); } data.sdata.Add(lc); callback.Invoke(data); } catch { Debug.Log("msg error===>"+msg); } })); return; } 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(Post_Demo("/digital-twin/v1/electricity/total/date", datas.ToJson(), (string msg) => { try { JsonData d =JsonMapper.ToObject(msg); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "kWh"; lc.values = new List(); for (int i = 0;i"+msg); } })); return; 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 static string TimeStampToHMS(float timeStampInSeconds) { TimeSpan time = TimeSpan.FromSeconds(timeStampInSeconds); return time.ToString("hh\\:mm"); } public static DateTime UnixTimeStampToDateTime(double timestamp) { // Unix时间戳基准时间1970年1月1日对应的DateTime DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // 将时间戳转换为秒 long unixTimeStampInTicks = (long)(timestamp * TimeSpan.TicksPerSecond); // 在UTC时间基础上增加转换后的秒数得到中国区的时间 DateTime dateTimeCn = new DateTime(unixStartTime.Ticks + unixTimeStampInTicks, DateTimeKind.Utc).ToLocalTime(); return dateTimeCn; } public static string changedate(string time,int i) { DateTime dateTime = UnixTimeStampToDateTime(long.Parse(time)); switch(i) { case 0: return TimeStampToHMS((long.Parse(time))); break; case 1: return dateTime.ToString("MM/dd"); break; case 2: DateTime dateTime2 =UnixTimeStampToDateTime(long.Parse(time)+604800); return dateTime.ToString("MM/dd")+"\n"+dateTime2.ToString("MM/dd"); break; case 3: return dateTime.ToString("MM月"); break; case 4: return dateTime.ToString("MM-dd"); break; } return time; } public class baojing { public string name; public string time; public string dev; } public void GetGaoJingTaiZhang(Action> callback) { List bj = new List(); StartCoroutine(getAlarmledger(1,bj,callback)); } IEnumerator getAlarmledger(int page, List bj,Action> callback) { JsonData datas =new JsonData(); datas["page"] = page; datas["pageSize"] = 10; JsonData d =new JsonData(); yield return StartCoroutine(Post_Demo("/digital-twin/v1/device/alarmLedger",datas.ToJson(), (string msg) => { d =JsonMapper.ToObject(msg); for (var i=0;ipage) { page++; yield return StartCoroutine(getAlarmledger(page,bj,callback)); } else { callback.Invoke(bj); } } }