123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- 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<string> 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<string> 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<List<ShiShiDianList>> callback)
- {
- yield return StartCoroutine(Get_Demo("/digital-twin/v1/electricity/realtime", "", (string msg) => {
- List<ShiShiDianList> lf =new List<ShiShiDianList>();
- try
- {
- JsonData data =JsonMapper.ToObject(msg);
- for (int i = 0;i<data["data"]["workshops"].Count;i++)
- {
- ShiShiDianList ssdl =new ShiShiDianList();
- ssdl.name = data["data"]["workshops"][i]["name"].ToString();
- for (int j = 0;j< data["data"]["electricity"].Count;j++)
- {
- if( data["data"]["workshops"][i]["id"].ToString()==data["data"]["electricity"][j]["workshopsID"].ToString())
- {
- ssdl.f +=float.Parse(data["data"]["electricity"][j]["value"].ToString()) ;
- // break;
- }
- }
-
- lf.Add(ssdl);
- }
- callback.Invoke(lf);
- }
- catch{
- lf =new List<ShiShiDianList>();
- 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<List<dvtype>> callback)
- {
- yield return StartCoroutine(Post_Demo("/digital-twin/v1/subscribe/status", "", (string msg) => {
- List<dvtype> lf =new List<dvtype>();
- 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<JsonData> 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<LineChartData> callback)
- {
- LineChartData data = new LineChartData();
- data.xdata = new List<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<float>();
- 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<d["data"]["list"].Count;j++)
- {
- long vtime =long.Parse(d["data"]["list"][j]["date"].ToString());
- if(timestamp>vtime&&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<float>();
- 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<LineChartData> callback)
- {
- LineChartData data = new LineChartData();
- data.xdata = new List<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<float>();
- for (int i = 0;i<d["data"]["list"].Count;i++)
- {
- data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),1));
- lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
- }
- data.sdata.Add(lc);
- callback.Invoke(data);
-
- }));
- return;
- for (int i = 0; i < 20; i++)
- {
- data.xdata.Add("xdata_" + i);
- }
- LineChartSeriesData lc = new LineChartSeriesData();
- lc.name = "数据1";
- lc.values = new List<float>();
- 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<JsonData> callback)
- {
- JsonData dataj=new JsonData();
- dataj["buID"] =int.Parse(id) ;
- LineChartData data = new LineChartData();
- data.xdata = new List<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<LineChartData> callback,int idx)
- {
- LineChartData data = new LineChartData();
- data.xdata = new List<string>();
- data.sdata = new List<LineChartSeriesData>();
- for (int i = 0; i < 20; i++)
- {
- data.xdata.Add("xdata_" + i);
- }
- LineChartSeriesData lc = new LineChartSeriesData();
- lc.name = "数据1";
- lc.values = new List<float>();
- 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<LineChartData> callback,int idx)
- {
- LineChartData data = new LineChartData();
- data.xdata = new List<string>();
- data.sdata = new List<LineChartSeriesData>();
- for (int i = 0; i < 20; i++)
- {
- data.xdata.Add("xdata_" + i);
- }
- LineChartSeriesData lc = new LineChartSeriesData();
- lc.name = "数据1";
- lc.values = new List<float>();
- 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<LineChartData> 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<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<float>();
- for (int i = 0;i<d["data"]["list"].Count;i++)
- {
- data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
- lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
- }
- data.sdata.Add(lc);
- callback.Invoke(data);
- }
- catch
- {
- Debug.Log("msg error===>"+msg);
- }
-
- }));
- return;
- }
-
- public void GetGaoJingData(Action<LineChartData> 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<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<float>();
- for (int i = 0;i<d["data"]["list"].Count;i++)
- {
- data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
- lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
- Debug.Log(" data.xdata=1=>"+ 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<LineChartData> 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<string>();
- data.sdata = new List<LineChartSeriesData>();
- 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<float>();
- for (int i = 0;i<d["data"]["list"].Count;i++)
- {
- data.xdata.Add(changedate(d["data"]["list"][i]["date"].ToString(),idx));
- lc.values.Add(float.Parse(d["data"]["list"][i]["value"].ToString()));
- }
- data.sdata.Add(lc);
- callback.Invoke(data);
- }
- catch
- {
- Debug.Log("msg 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<float>();
- 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<List<baojing>> callback)
- {
- List<baojing> bj = new List<baojing>();
- StartCoroutine(getAlarmledger(1,bj,callback));
- }
- IEnumerator getAlarmledger(int page, List<baojing> bj,Action<List<baojing>> 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;i<d["data"]["list"].Count;i++)
- {
- for (var j=0;j< d["data"]["list"][i]["alarmLocation"].Count;j++)
- {
- baojing b = new baojing();
- b.name = d["data"]["list"][i]["deviceName"].ToString();
- b.time = changedate( d["data"]["list"][i]["date"].ToString(),4);
- b.dev = d["data"]["list"][i]["alarmLocation"][j].ToString();
- bj.Add(b);
- }
- }
- }));
- while(d==null)
- {
- yield return null;
- }
- if((int.Parse(d["data"]["total"].ToString())/10+1)>page)
- {
- page++;
- yield return StartCoroutine(getAlarmledger(page,bj,callback));
- }
- else
- {
- callback.Invoke(bj);
- }
-
- }
- }
|