using System; using System.Collections; using System.Collections.Generic; using System.Text; using LitJson; using UnityEngine; using UnityEngine.Networking; using static Manager; public class DataManager : MonoBehaviour { public static DataManager Instance; private void Awake() { Instance = this; } public void GetJiFang(Action callback) { ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < 8; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = i; srd.name = i + "_机房"; sd.serverList.Add(srd); } callback.Invoke(sd); } IEnumerator getUsageInfo(ServerData data, Action callback, int type) { string typestr = "last_month_usage"; if (type == 0) { typestr = "last_week_usage"; } LineChartData data2 = new LineChartData(); data2.xdata = new List(); data2.sdata = new List(); yield return StartCoroutine(Post_Demo("/detail/electricUsageInfo", "", (string msg) => { JsonData datamsg = JsonMapper.ToObject(msg); for (int j = 0; j < datamsg["data"][typestr].Count; j++) { if(!data2.xdata.Contains(datamsg["data"][typestr][j]["date"].ToString())) data2.xdata.Add(datamsg["data"][typestr][j]["date"].ToString()); } for (int i = 0; i < data.serverList.Count; i++) { LineChartSeriesData lc = new LineChartSeriesData(); lc.name = data.serverList[i].name; lc.values = new List(); for (int z = 0; z < datamsg["data"][typestr].Count; z++) { if(data.serverList[i].name == datamsg["data"][typestr][z]["server_room_name"].ToString()) { lc.values.Add(float.Parse(datamsg["data"][typestr][z]["usage"].ToString())); } } data2.sdata.Add(lc); } })); callback.Invoke(data2); } public void GetDLBiao(Action callback,int type) { GetWSD((data) => { StartCoroutine(getUsageInfo(data, callback, type)); }); return; LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 20; i++) { data.xdata.Add("xdata_" + i); } for (int i = 0; i < 8; i++) { LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据_"+ i; lc.values = new List(); for (int j = 0; j < 20; j++) { lc.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc); } callback.Invoke(data); } public void GetJL(int id,Action> callback) { List list = new List(); JsonData data = new JsonData(); data["server_room_id"] = id; StartCoroutine(Post_Demo("/detail/rackElectricInfo", data.ToJson(), (string msg) => { JsonData data = JsonMapper.ToObject(msg); for (int i = 0; i < data["data"]["groups"].Count; i++) { ServerJiLongData jilong = new ServerJiLongData(); jilong.id = i; jilong.name = data["data"]["groups"][i]["name"].ToString(); jilong.jiguilist = new List(); for (int z = 0; z < data["data"]["groups"][i]["racks"].Count; z++) { ServerjiguiData jg = new ServerjiguiData(); jg.name = data["data"]["groups"][i]["racks"][z]["name"].ToString(); jg.srz = float.Parse(data["data"]["groups"][i]["racks"][z]["last_day_usage"].ToString()); jg.syz = float.Parse(data["data"]["groups"][i]["racks"][z]["last_month_usage"].ToString()); jg.szz = float.Parse(data["data"]["groups"][i]["racks"][z]["last_week_usage"].ToString()); jg.zgl = float.Parse(data["data"]["groups"][i]["racks"][z]["power"].ToString()); jilong.jiguilist.Add(jg); } list.Add(jilong); } callback.Invoke(list); })); } public void GetDL(Action callback) { GetWSD((data) => { for (int j = 0; j < data.serverList.Count; j++) { if(data.serverList[j].jilonglist==null) { data.serverList[j].jilonglist = new List(); } } callback.Invoke(data); }); return; ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < 8; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = i; srd.name = i + "_机房"; srd.zonggonglv = UnityEngine.Random.Range(30.0f, 40.0f); srd.shangridl = UnityEngine.Random.Range(30.0f, 40.0f); srd.jilonglist = new List(); for (int j = 0; j < i; j++) { ServerJiLongData jilong = new ServerJiLongData(); jilong.id = j; jilong.name = i + "_" + j; jilong.jiguilist = new List(); int rand = UnityEngine.Random.Range(2,4)>2?4:2; for (int z = 0; z < rand; z++) { ServerjiguiData jg = new ServerjiguiData(); jg.name = i + "_" + j + "_" + z; jg.srz = 123; jg.syz = 123; jg.szz = 123; jg.zgl = 123; jilong.jiguilist.Add(jg); } srd.jilonglist.Add(jilong); } sd.serverList.Add(srd); } callback.Invoke(sd); } public static string ip = ""; public IEnumerator Post_Demo(string url,string lstformData,Action callback) { while(ip=="") { yield return null; } string m_url = ip + url; 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; 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); } } public void GetTwoWsdTb(int id,int rid, Action> callback) { JsonData data = new JsonData(); data["server_room_id"] = id; data["rack_id"] = rid; StartCoroutine(Post_Demo("/detail/tempHumiInfo", data.ToJson(), (string msg) => { List sjds = new List(); JsonData datamsg = JsonMapper.ToObject(msg); LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); //24小时last_24h_temp 温度 LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "温度"; lc.values = new List(); for (int i = 0; i < datamsg["data"]["last_24h_temp"].Count; i++) { data.xdata.Add(datamsg["data"]["last_24h_temp"][i]["date"].ToString().Split(' ')[1]); lc.values.Add(float.Parse(datamsg["data"]["last_24h_temp"][i]["value"].ToString())); } data.sdata.Add(lc); //24小时last_24h_humi 温度 LineChartSeriesData lc2 = new LineChartSeriesData(); lc2.name = "湿度"; lc2.values = new List(); for (int i = 0; i < datamsg["data"]["last_24h_humi"].Count; i++) { lc2.values.Add(float.Parse(datamsg["data"]["last_24h_humi"][i]["value"].ToString())); } data.sdata.Add(lc2); sjds.Add(data); data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); //24小时last_day_temp 温度 lc = new LineChartSeriesData(); lc.name = "温度"; lc.values = new List(); for (int i = 0; i < datamsg["data"]["last_day_temp"].Count; i++) { data.xdata.Add(datamsg["data"]["last_day_temp"][i]["date"].ToString().Split(' ')[1]); lc.values.Add(float.Parse(datamsg["data"]["last_day_temp"][i]["value"].ToString())); } data.sdata.Add(lc); //24小时last_day_humi 温度 lc2 = new LineChartSeriesData(); lc2.name = "湿度"; lc2.values = new List(); for (int i = 0; i < datamsg["data"]["last_day_humi"].Count; i++) { lc2.values.Add(float.Parse(datamsg["data"]["last_day_humi"][i]["value"].ToString())); } data.sdata.Add(lc2); sjds.Add(data); data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); //24小时last_weed_temp 温度 lc = new LineChartSeriesData(); lc.name = "温度"; lc.values = new List(); for (int i = 0; i < datamsg["data"]["last_week_temp"].Count; i++) { data.xdata.Add(datamsg["data"]["last_week_temp"][i]["date"].ToString().Split(' ')[0]); lc.values.Add(float.Parse(datamsg["data"]["last_week_temp"][i]["value"].ToString())); } data.sdata.Add(lc); //24小时last_week_humi 温度 lc2 = new LineChartSeriesData(); lc2.name = "湿度"; lc2.values = new List(); for (int i = 0; i < datamsg["data"]["last_week_humi"].Count; i++) { lc2.values.Add(float.Parse(datamsg["data"]["last_week_humi"][i]["value"].ToString())); } data.sdata.Add(lc2); sjds.Add(data); data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); //24小时last_weed_temp 温度 lc = new LineChartSeriesData(); lc.name = "温度"; lc.values = new List(); for (int i = 0; i < datamsg["data"]["last_month_temp"].Count; i++) { data.xdata.Add(datamsg["data"]["last_month_temp"][i]["date"].ToString().Split(' ')[0]); lc.values.Add(float.Parse(datamsg["data"]["last_month_temp"][i]["value"].ToString())); } data.sdata.Add(lc); //24小时last_week_humi 温度 lc2 = new LineChartSeriesData(); lc2.name = "湿度"; lc2.values = new List(); for (int i = 0; i < datamsg["data"]["last_month_humi"].Count; i++) { lc2.values.Add(float.Parse(datamsg["data"]["last_month_humi"][i]["value"].ToString())); } data.sdata.Add(lc2); sjds.Add(data); callback.Invoke(sjds); })); } public void GetTwoWsd(int id, Action> callback) { JsonData data = new JsonData(); data["server_room_id"] = id; StartCoroutine(Post_Demo("/detail/rackTempHumi", data.ToJson(), (string msg) => { List sjds = new List(); JsonData data = JsonMapper.ToObject(msg); for (int i = 0; i < data["data"]["racks"].Count; i++) { ServerjiguiData sjd = new ServerjiguiData(); sjd.id =int.Parse( data["data"]["racks"][i]["id"].ToString()); sjd.name = data["data"]["racks"][i]["name"].ToString(); sjd.shidu = float.Parse(data["data"]["racks"][i]["temp"].ToString()); sjd.wendu = float.Parse(data["data"]["racks"][i]["humi"].ToString()); sjds.Add(sjd); } callback.Invoke(sjds); })); } public void GetWSD(Action callback) { Debug.Log("获取温湿度===》"); StartCoroutine(Post_Demo("/overview/serverroom","",(string msg)=> { JsonData data = JsonMapper.ToObject(msg); ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < data["data"]["item_info"].Count; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = int.Parse(data["data"]["item_info"][i]["server_room_id"].ToString()); srd.name = data["data"]["item_info"][i]["item_name"].ToString(); srd.wendu = float.Parse(data["data"]["item_info"][i]["item_temp"].ToString()); srd.shidu = float.Parse(data["data"]["item_info"][i]["item_humi"].ToString()); try { srd.shangridl = float.Parse(data["data"]["item_info"][i]["item_lastday_usage"].ToString()); srd.zonggonglv = float.Parse(data["data"]["item_info"][i]["item_total_power"].ToString()); } catch { } sd.serverList.Add(srd); } callback.Invoke(sd); })); return; ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < 8; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = i; srd.name = i + "_机房"; srd.wendu = UnityEngine.Random.Range(30.0f, 40.0f); srd.shidu = UnityEngine.Random.Range(30.0f, 40.0f); srd.jilonglist = new List(); for (int j = 0; j < i; j++) { ServerJiLongData jilong = new ServerJiLongData(); jilong.id = j; jilong.name = i + "_" + j; jilong.jiguilist = new List(); int rand = UnityEngine.Random.Range(2, 4) > 2 ? 4 : 2; for (int z = 0; z < rand; z++) { ServerjiguiData jg = new ServerjiguiData(); jg.name = i + "_" + j + "_" + z; jg.wendu = 123; jg.shidu = 123; jilong.jiguilist.Add(jg); } srd.jilonglist.Add(jilong); } sd.serverList.Add(srd); } callback.Invoke(sd); } ServerRoomData checkroom(int id , List roomdata ) { for (int i = 0; i < roomdata.Count; i++) { if(roomdata[i].id == id) { return roomdata[i]; } } return null; } public void GetVideo(Action callback) { Debug.Log("获取video===》"); StartCoroutine(Post_Demo("/overview/serverLiveOverview", "", (string msg) => { JsonData data = JsonMapper.ToObject(msg); ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < data["data"]["items"].Count; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = int.Parse(data["data"]["items"][i]["server_room_id"].ToString()); srd.name = data["data"]["items"][i]["server_name"].ToString(); srd.videolist = new List(); ServerVideoData svd = new ServerVideoData(); svd.id = 1; svd.type = 0; svd.name = "机房摄像头"; svd.url = new List(); srd.videolist.Add(svd); for (int j = 0; j < data["data"]["items"][i]["video_list"]["room"].Count; j++) { svd.url.Add(data["data"]["items"][i]["video_list"]["room"][j].ToString()); } ServerVideoData svd2 = new ServerVideoData(); svd2.id = 1; svd.type = 1; svd2.name = "机笼摄像头"; svd2.url = new List(); srd.videolist.Add(svd2); for (int j = 0; j < data["data"]["items"][i]["video_list"]["rack"].Count; j++) { svd2.url.Add(data["data"]["items"][i]["video_list"]["rack"][j].ToString()); } sd.serverList.Add(srd); } callback.Invoke(sd); })); return; ServerData sd = new ServerData(); sd.serverList = new List(); for (int i = 0; i < 8; i++) { ServerRoomData srd = new ServerRoomData(); srd.id = i; srd.videolist = new List(); ServerVideoData svd = new ServerVideoData(); svd.id = 1; svd.name = "机房摄像头"; svd.url = new List(); for (int j = 0; j < 8; j++) { svd.url.Add("https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8"); } srd.videolist.Add(svd); ServerVideoData svd2 = new ServerVideoData(); svd2.id = 1; svd2.name = "机笼摄像头"; svd2.url = new List(); for (int j = 0; j < 8; j++) { svd2.url.Add("https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8"); } srd.videolist.Add(svd2); sd.serverList.Add(srd); } callback.Invoke(sd); } public void GetTongSuan(Action callback) { StartCoroutine(Post_Demo("/overview/generalResourceOverview", "", (string msg) => { JsonData data = JsonMapper.ToObject(msg)["data"]; TongSuanData jdata = new TongSuanData(); jdata.devct = float.Parse(data["host_number"].ToString()); jdata.serverct = float.Parse(data["server_number"].ToString()); jdata.zhiji = float.Parse(data["network_device_number"].ToString()); callback.Invoke(jdata); })); return; TongSuanData jdata = new TongSuanData(); jdata.devct = 123; jdata.serverct = 123; jdata.zhiji = 123; callback.Invoke(jdata); } public void GetJianKongPt(Action callback) { StartCoroutine(Post_Demo("/overview/monitor", "", (string msg) => { JsonData data = JsonMapper.ToObject(msg)["data"]; JianKongPtData jdata = new JianKongPtData(); jdata.shijianct =float.Parse( data["strategy_number"].ToString()) ; jdata.zhixingct = float.Parse(data["plugin_number"].ToString()); jdata.gaojingct = float.Parse(data["warn_number"].ToString()); callback.Invoke(jdata); })); return; JianKongPtData jdata = new JianKongPtData(); jdata.shijianct = 123; jdata.zhixingct = 123; jdata.gaojingct = 123; callback.Invoke(jdata); } public void GetSixServer(Action> callback) { Debug.Log("6个服务器===》"); StartCoroutine(Post_Demo("/overview/intelligentResourceOverview", "", (string msg) => { JsonData data = JsonMapper.ToObject(msg); List lp = new List(); for (int i = 0; i < data["data"]["items"].Count; i++) { PieChartData p = new PieChartData(); p.title = data["data"]["items"][i]["name"].ToString(); p.value =float.Parse( data["data"]["items"][i]["count"].ToString()); p.items = new List(); foreach(var k in data["data"]["items"][i].Keys) { if(k!="name"&&k!="count") { PieChartItemData idata1 = new PieChartItemData(); idata1.name = k; idata1.value = float.Parse(data["data"]["items"][i][k].ToString().Split('%')[0]); p.items.Add(idata1); } } lp.Add(p); } PieChartData p2 = new PieChartData(); p2.title = data["data"]["flops"]["name"].ToString(); p2.value = float.Parse(data["data"]["flops"]["value"].ToString().Split(' ')[0]); lp.Add(p2); callback.Invoke(lp); })); return; List lp = new List(); for (int i = 0; i < 6; i++) { PieChartData p = new PieChartData(); p.title = i.ToString(); p.value = 1000000; p.items = new List(); PieChartItemData idata1 = new PieChartItemData(); idata1.name = "RUN"; idata1.value = 27; p.items.Add(idata1); PieChartItemData idata2 = new PieChartItemData(); idata2.name = "IDLE"; idata2.value = 27; p.items.Add(idata2); lp.Add(p); } callback.Invoke(lp); } public void GetK8s(Action> callback) { Debug.Log("获取k8s===》"); List rvs = new List(); StartCoroutine(Post_Demo("/overview/kubernetsCluster", "", (string msg) => { JsonData datamsg = JsonMapper.ToObject(msg); RingValueData d1 = new RingValueData(); d1.value = datamsg["data"]["cluster_name"].ToString(); RingValueData d2 = new RingValueData(); d2.value = datamsg["data"]["cluster_status"].ToString(); datamsg["data"]["cluster_status"].ToString(); RingValueData d3 = new RingValueData(); d3.value = datamsg["data"]["cluster_node_count"][0].ToString(); d3.max = datamsg["data"]["cluster_node_count"][1].ToString(); rvs.Add(d1); rvs.Add(d2); rvs.Add(d3); callback.Invoke(rvs); })); return; } public void GetGPUData(Action callback) { Debug.Log("获取gpu===》"); StartCoroutine(Post_Demo("/overview/gpupower", "", (string msg) => { JsonData datamsg = JsonMapper.ToObject(msg); LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "今日"; lc.values = new List(); for (int i = 0; i < datamsg["data"][ "today_info"].Count; i++) { data.xdata.Add(datamsg["data"]["today_info"][i]["time"].ToString().Split(' ')[1]); lc.values.Add(float.Parse(datamsg["data"]["today_info"][i]["value"].ToString())); } data.sdata.Add(lc); LineChartSeriesData lc2 = new LineChartSeriesData(); lc2.name = "昨日"; lc2.values = new List(); for (int i = 0; i < datamsg["data"]["yesterday_info"].Count; i++) { lc2.values.Add(float.Parse( datamsg["data"]["yesterday_info"][i]["value"].ToString())); } data.sdata.Add(lc2); callback.Invoke(data); })); return; LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < 10; i++) { data.xdata.Add("xdata_" + i); } LineChartSeriesData lc = new LineChartSeriesData(); lc.name = "数据1"; lc.values = new List(); for (int i = 0; i < 10; i++) { lc.values.Add(UnityEngine.Random.Range(1, 800)); } data.sdata.Add(lc); LineChartSeriesData lc2 = new LineChartSeriesData(); lc2.name = "数据2"; lc2.values = new List(); for (int i = 0; i < 10; i++) { lc2.values.Add(UnityEngine.Random.Range(1, 800)); } data.sdata.Add(lc2); callback.Invoke(data); } public void GetGaoJingData(Action callback) { StartCoroutine(Post_Demo("/overview/warnData", "", (string msg) => { JsonData datamsg = JsonMapper.ToObject(msg); LineChartData data = new LineChartData(); data.xdata = new List(); data.sdata = new List(); for (int i = 0; i < datamsg["data"]["prop_ame"].Count; i++) { LineChartSeriesData lc = new LineChartSeriesData(); lc.name = datamsg["data"]["prop_ame"][i].ToString(); lc.values = new List(); data.sdata.Add(lc); } for (int i = 0; i < datamsg["data"]["items"].Count; i++) { data.xdata.Add(datamsg["data"]["items"][i]["date"].ToString()); for (int j = 0; j < data.sdata.Count; j++) { data.sdata[j].values.Add(float.Parse(datamsg["data"]["items"][i]["prop_value"][j].ToString())); } } callback.Invoke(data); })); return; 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); LineChartSeriesData lc2 = new LineChartSeriesData(); lc2.name = "数据2"; lc2.values = new List(); for (int i = 0; i < 20; i++) { lc2.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc2); LineChartSeriesData lc3 = new LineChartSeriesData(); lc3.name = "数据3"; lc3.values = new List(); for (int i = 0; i < 20; i++) { lc3.values.Add(UnityEngine.Random.Range(1, 1000)); } data.sdata.Add(lc2); callback.Invoke(data); } public void GetGaoJingJiLuData(Action callback) { StartCoroutine(Post_Demo("/overview/warnRecord", "", (string msg) => { JsonData datamsg = JsonMapper.ToObject(msg); GaoJingListData gjl = new GaoJingListData(); gjl.list = new List(); for (int i = 0; i < datamsg["data"]["items"].Count; i++) { GaoJingItemData gjid = new GaoJingItemData(); gjid.lv = datamsg["data"]["items"][i]["level"].ToString(); gjid.time = datamsg["data"]["items"][i]["time"].ToString(); gjid.dev = datamsg["data"]["items"][i]["device"].ToString(); gjid.type = datamsg["data"]["items"][i]["status"].ToString(); gjl.list.Add(gjid); } callback.Invoke(gjl); })); return; GaoJingListData gjl = new GaoJingListData(); gjl.list = new List(); for (int i = 0; i < 2; i++) { GaoJingItemData gjid = new GaoJingItemData(); gjid.lv = "一般"; gjid.time = "2024-1-1 00:11"; gjid.dev = "111机房"; gjid.type = "已解决"; gjl.list.Add(gjid); } for (int i = 0; i < 2; i++) { GaoJingItemData gjid = new GaoJingItemData(); gjid.lv = "紧急"; gjid.time = "2024-1-1 00:11"; gjid.dev = "111机房"; gjid.type = "待解决"; gjl.list.Add(gjid); } for (int i = 0; i < 2; i++) { GaoJingItemData gjid = new GaoJingItemData(); gjid.lv = "严重"; gjid.time = "2024-1-1 00:11"; gjid.dev = "111机房"; gjid.type = "已解决"; gjl.list.Add(gjid); } callback.Invoke(gjl); } }