usingDevExpress.XtraBars.Ribbon;usingFleck;usingIMS.DBHelper;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Windows.Forms;usingWHC.Framework.Commons;usingRfidPositioning.Common;usingIMS.BaseFramework;usingSystem.Threading;namespaceIMS.WfmClient
{public partial classWebMonitor : RibbonForm
{static System.Timers.Timer _refreshDataTimer = newSystem.Timers.Timer();static List _connections = new List();
DataPackage _dataPackage= newDataPackage();
WebSocketServer _wsServer= new WebSocketServer("ws://0.0.0.0:7181");publicWebMonitor()
{
InitializeComponent();//启动线程开始从数据库获取页面所需的数据
Thread th = newThread(GetDataForChart);
th.IsBackground= true;
th.Start();//设置监控界面对应的网页
string appPath =AppDomain.CurrentDomain.BaseDirectory;string chartsPath = "file:///" + appPath + "Resources/eCharts/sample.html";
webBrowser1.Navigate(newUri(chartsPath));//启动webSocket侦听服务
_wsServer.Start(item =>{//与客户端建立连接后触发
item.OnOpen = () =>{
_connections.Add(item);//首次建立连接后立刻发送一次数据,之后由定时器来刷新数据
string jsonData =_dataPackage.ToJson();
_connections.ToList().ForEach(s=>s.Send(jsonData));
};//收到客户端发来的消息后触发
item.OnMessage = message =>{int msg = 0;int.TryParse(message, outmsg);
ClientMessageHandler(msg);
};//客户端关闭连接后触发
item.OnClose = () =>{
_connections.Remove(item);
};
});
}private void FrmIndex_Load(objectsender, EventArgs e)
{//启动定时器
_refreshDataTimer.Enabled = true;
_refreshDataTimer.Interval= 5 * 60 * 1000;
_refreshDataTimer.Elapsed+= newSystem.Timers.ElapsedEventHandler(timersTimer_Elapsed);
_refreshDataTimer.Start();
}private void timersTimer_Elapsed(objectsender, System.Timers.ElapsedEventArgs e)
{
GetDataForChart();string jsonData =_dataPackage.ToJson();
_connections.ToList().ForEach(s=>s.Send(jsonData));
}private void ClientMessageHandler(intmsg)
{if (msg >= 500)
{
SysConfig.FloorNumber= (msg % 100).ToString();this.Invoke(new Action(() =>{
ChildWinManagement.LoadMdiForm(SysConfig.mainform,typeof(frmTracePlayback));
}));
}else if (msg >= 400)
{
SysConfig.FloorNumber= (msg / 10 % 100).ToString();if (msg % 10 == 1)
{
SysConfig.WarningType= "正常";
}if (msg % 10 == 2)
{
SysConfig.WarningType= "缺失报警";
}this.Invoke(new Action(() =>{
ChildWinManagement.LoadMdiForm(SysConfig.mainform,typeof(AssetWatchDetail));
}));
}else{
StaticsTypeEnum type=(StaticsTypeEnum)msg;int typeScale = 0;int typeChecks = 0;int typePosition = 0;int typeInstrument = 0;
getStaticsType(out typeScale, out typeChecks, out typePosition, outtypeInstrument);switch(msg)
{case 10:case 11:
SysConfig.ReportByAssetsType= msg == 10 ? false : true;
NavigateToForm(typePosition,typeof(FrmAssetsValueReport));break;case 12:case 13:
SysConfig.ReportByAssetsType= msg == 12 ? false : true;
NavigateToForm(typePosition,typeof(FrmInventoryReport));break;case 30:
webBrowser1.Refresh();break;case 31:this.Invoke(new Action(() =>{
Form form= newSetStaticsType();
form.Owner= this;
form.StartPosition=FormStartPosition.CenterScreen;
form.ShowDialog();
}));break;default:break;
}
}
}private void getStaticsType(out int typeScale, out int typeChecks, out int typePosition, out inttypeInstrument)
{
typeScale= 0;
typeChecks= 0;
typePosition= 0;
typeInstrument= 0;//查询默认选择的统计方式(从数据库获取已设置过的数据)
string strsql = "select * from HT_MonitorStaticsType;";
DataSet ds=SQLHelper.Query(SQLHelper._connstr, strsql);if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
typeScale= Convert.ToInt32(ds.Tables[0].Rows[0]["Monitor"].ToString());
typeChecks= Convert.ToInt32(ds.Tables[0].Rows[0]["Checks"].ToString());
typePosition= Convert.ToInt32(ds.Tables[0].Rows[0]["Position"].ToString());
typeInstrument= Convert.ToInt32(ds.Tables[0].Rows[0]["Instrument"].ToString());
}
}private void NavigateToForm(inttype, Type formType)
{if (type == 1) //按月
{
SysConfig.ReportStartDate= DateTime.Now.ToString("yyyyMM") + "01";
SysConfig.ReportEndDate= DateTime.Now.AddMonths(1).ToString("yyyyMM") + "01";
}else{
SysConfig.ReportStartDate= DateTime.Now.AddYears(-1).ToString("yyyyMMdd");
SysConfig.ReportEndDate= DateTime.Now.ToString("yyyyMMdd");
}this.Invoke(new Action(() =>{
ChildWinManagement.LoadMdiForm(SysConfig.mainform, formType);
}));
}#region 图表数据
classRealTimeMonitor
{public string FloorNo { get; set; }public string MonitorSum { get; set; }public string LossSum { get; set; }public string PositionSum { get; set; }public string LowSum { get; set; }
}classChartAssetWatch
{public string Name { get; set; }public string Value { get; set; }public string StaticsType { get; set; }
}classChartCheck
{public string Name { get; set; }public string Value { get; set; }public string DataType { get; set; }public string StaticsType { get; set; }
}classChartPosition
{public string Name { get; set; }public string Value { get; set; }public string DataType { get; set; }public string StaticsType { get; set; }
}classChartInstrument
{public string Name { get; set; }public string Value { get; set; }public string StaticsType { get; set; }
}classDataPackage
{public ChartOptionForAssetWatch chartOption1 { get; set; }public ChartOptionForAssetWatch chartOption2 { get; set; }public ChartOption chartOption3 { get; set; }public ChartOption chartOption4 { get; set; }public List realTimeMonitors { get; set; }
}voidGetDataForChart()
{int typeScale = 0;int typeChecks = 0;int typePosition = 0;int typeInstrument = 0;
getStaticsType(out typeScale, out typeChecks, out typePosition, outtypeInstrument);
_dataPackage.chartOption1= getChartDataAssetWatch("HT_ChartAssetWatchDept", typeScale);
_dataPackage.chartOption2= getChartDataAssetWatch("HT_ChartAssetWatchType", typeScale);
_dataPackage.chartOption3= getChartDataCheck("HT_ChartCheckDept", typeChecks);
_dataPackage.chartOption4= getChartDataCheck("HT_ChartCheckType", typeChecks);
List realTimeMonitors =getRealTimeMonitorData();
_dataPackage.realTimeMonitors=realTimeMonitors;
}
ChartOptionForAssetWatch getChartDataAssetWatch(string tableName, intstaticsType)
{
ChartOptionForAssetWatch chartOption= newChartOptionForAssetWatch();try{string sqlStr = @"SELECT [Name],[Value],[StaticsType] FROM XXX.[dbo].[" + tableName + "] WHERE StaticsType =" + staticsType + "order by Name desc;";
DataSet ds=SQLHelper.Query(SQLHelper._connstr, sqlStr);
List chartData = ds.Tables[0].ConvertToModel();//截取长度超出5的字符串
chartData.ForEach(item =>{if (item.Name.Length > 5)
{
item.Name= item.Name.Substring(0, 4) + "..";
}
});//合并数量少的分组
List chartDataNew = new List();
chartDataNew.AddRange(chartData.OrderByDescending(p=> p.Value).Take(5)); //取前5条数据
chartDataNew.ForEach(p =>{ chartData.Remove(p); });
chartDataNew.Add(new ChartAssetWatch { Name = "其他", Value = chartData.Sum(p =>Convert.ToDecimal(p.Value)).ToString() });
chartOption.tooltip= newtooltip();
chartOption.tooltip.trigger= "item";
chartOption.tooltip.formatter= "{a}
{b}: {c} ({d}%)";
chartOption.legend= newlegend();
chartOption.legend.type= "scroll";
chartOption.legend.orient= "vertical";
chartOption.legend.left= "0";
chartOption.legend.top= "0";
chartOption.legend.data= chartDataNew.Select(p =>p.Name).ToArray();
chartOption.series= new SeriesForAssetWatch[1];
SeriesForAssetWatch series= newSeriesForAssetWatch();
series.name= "";
series.type= "pie";
series.radius= new string[] { "60%", "30%"};
series.center= new string[] { "55%", "55%"};
series.avoidLabelOverlap= "false";
series.label= newlabel();
series.label.normal= newnormal();
series.label.normal.show= false;
series.label.normal.position= "center";
series.label.emphasis= newemphasis();
series.label.emphasis.show= "true";
series.label.emphasis.textStyle= newtextStyle();
series.label.emphasis.textStyle.fontSize= "20";
series.labelLine= newlabelLine();
series.labelLine.normal= newnormal();
series.labelLine.normal.show= false;
List d = new List();
chartDataNew.ForEach(p=> { d.Add(new DataNew { name = p.Name, value =p.Value }); });
series.data=d.ToArray();
chartOption.series= new SeriesForAssetWatch[1];
chartOption.series[0] =series;returnchartOption;
}catch { return null; }
}
ChartOption getChartDataCheck(string tableName, intstaticsType)
{
ChartOption chartOption= newChartOption();try{string sqlStr = @"SELECT [Name],[Value],[DataType],[StaticsType] FROM [Lonix_Fas_1].[dbo].[" + tableName + "] WHERE StaticsType =" + staticsType + "order by Name desc;";
DataSet ds=SQLHelper.Query(SQLHelper._connstr, sqlStr);
List chartData = ds.Tables[0].ConvertToModel();//截取长度超出5的字符串
chartData.ForEach(item =>{if (item.Name.Length > 5)
{
item.Name= item.Name.Substring(0, 4) + "..";
}
});//合并数量少的分组
List chartInstrumentsNew1 = new List();
chartInstrumentsNew1.AddRange(chartData.Where(p=> p.DataType == "1").OrderByDescending(p => p.Value).Take(5)); //取前5条数据
chartInstrumentsNew1.ForEach(p =>{ chartData.Remove(p); });
chartInstrumentsNew1.Add(new ChartCheck { Name = "其他", Value = chartData.Where(p => p.DataType == "1").Sum(p =>Convert.ToDecimal(p.Value)).ToString() });
List chartInstrumentsNew2 = new List();
chartInstrumentsNew2.AddRange(chartData.Where(p=> p.DataType == "2").OrderByDescending(p => p.Value).Take(5)); //取前5条数据
chartInstrumentsNew2.ForEach(p =>{ chartData.Remove(p); });
chartInstrumentsNew2.Add(new ChartCheck { Name = "其他", Value = chartData.Where(p => p.DataType == "2").Sum(p =>Convert.ToDecimal(p.Value)).ToString() });
chartOption.tooltip= newtooltip();
chartOption.tooltip.trigger= "axis";
chartOption.tooltip.axisPointer= newaxisPointer();
chartOption.tooltip.axisPointer.type= "shadow";
chartOption.legend= newlegend();
chartOption.legend.orient= "horizontal";
chartOption.legend.data= new string[] { "盘亏数", "已盘数"};
chartOption.grid= newgrid();
chartOption.grid.top= "80";
chartOption.grid.left= "3%";
chartOption.grid.right= "6%";
chartOption.grid.bottom= "30";
chartOption.grid.containLabel= true;
chartOption.xAxis= newAxis();
chartOption.xAxis.type= "value";
chartOption.xAxis.axisLabel= newaxisLabel();
chartOption.xAxis.axisLabel.interval= "0";
chartOption.xAxis.axisLabel.rotate= "0";
chartOption.yAxis= newAxis();
chartOption.yAxis.type= "category";
chartOption.yAxis.data= chartInstrumentsNew1.Select(p =>p.Name).ToArray();
chartOption.yAxis.axisLabel= newaxisLabel();
chartOption.yAxis.axisLabel.interval= "0";
chartOption.yAxis.axisLabel.rotate= "0";
chartOption.series= new Series[2];
Series s1= newSeries();
s1.name= "盘亏数";
s1.type= "bar";
s1.stack= "总量";
s1.label= newlabel();
s1.label.normal= newnormal();
s1.label.normal.show= true;
s1.label.normal.position= "insideRight";
s1.data= chartInstrumentsNew1.Select(p =>p.Value).ToArray();
chartOption.series[0] =s1;
Series s2= newSeries();
s2.name= "已盘数";
s2.type= "bar";
s2.stack= "总量";
s2.label= newlabel();
s2.label.normal= newnormal();
s2.label.normal.show= true;
s2.label.normal.position= "insideRight";
s2.data= chartInstrumentsNew2.Select(p =>p.Value).ToArray();
chartOption.series[1] =s2;returnchartOption;
}catch { return null; }
}
ListgetRealTimeMonitorData()
{string sqlStr = @"select [FloorNo],[MonitorSum],[LossSum],[PositionSum],[LowSum] from XXX.[dbo].[HT_RealTimeMonitor] order by FloorNo desc;";
DataSet ds=SQLHelper.Query(SQLHelper._connstr, sqlStr);return ds.Tables[0].ConvertToModel();
}#endregion}
}