123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using Helper;
- using HZY.Framework.DependencyInjection;
- using IoTClient.Clients.PLC;
- using IoTClient.Common.Enums;
- using IoTClient.Enums;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using MiniExcelLibs;
- using Model;
- using Scada.Page;
- using Sunny.UI;
- namespace Scada
- {
- public partial class FormMain : UIHeaderAsideMainFooterFrame,IScopedSelfDependency
- {
- private bool plcIsConnected;
- private readonly ILogger<FormMain> _logger;
- private Point mPoint;
- private CancellationTokenSource cts = new CancellationTokenSource();
- public FormMain(ILogger<FormMain> logger)
- {
- _logger = logger ?? throw new ArgumentNullException(nameof(logger));
- InitializeComponent();
- InitAsideUI();
- InitHeaderUI();
- InitConfig();
- InitPlcClient();
- //显示默认界面
- //Aside.SelectFirst();
- }
- private void InitConfig()
- {
- //读取PLC配置 192.168.1.180 127.0.0.1
- Globals.IpAddress = Globals.IniFile.ReadString("PLC参数", "IP", "127.0.0.1");
- Globals.Port = Globals.IniFile.ReadInt("PLC参数", "Port", 102);
- Globals.CpuType = Enum.Parse<SiemensVersion>(Globals.IniFile.ReadString("PLC参数", "CpuType", SiemensVersion.S7_1200.ToString()));
- Globals.Slot = Globals.IniFile.ReadByte("PLC参数", "Slot", 0);
- Globals.Rack = Globals.IniFile.ReadByte("PLC参数", "Rack", 0);
- Globals.ConnectTimeOut = Globals.IniFile.ReadInt("PLC参数", "ConnectTimeOut", 3000);
- Globals.ReadTimeInterval = Globals.IniFile.ReadInt("PLC参数", "ReadTimeInterval", 300);
- Globals.ReConnectTimeInterval = Globals.IniFile.ReadInt("PLC参数", "ReConnectTimeInterval", 3000);
- //PLC变量表路径
- Globals.PlcVarConfigPath = Globals.IniFile.ReadString("PLC参数", "变量表地址", Path.Combine(Application.StartupPath, "PLC_Var_Config.xlsx"));
- //删除文件夹路径
- Globals.DelFilePath = Globals.IniFile.ReadString("系统参数", "删除文件夹路径", Path.Combine(Application.StartupPath, "Logs"));
- //保存天数
- Globals.SaveDay = Globals.IniFile.ReadString("系统参数", "保存天数", "30");
- //试用时长
- Globals.SYTime = Globals.IniFile.ReadInt("软件参数", "试用时间", 100);
- //软件版本
- Globals.SoftwareVersion = Globals.IniFile.ReadString("软件参数", "软件版本", "V1.0");
- _logger.LogInformation("读取配置文件成功");
- }
- private void InitPlcClient()
- {
- var plcVarList = MiniExcel.Query<PLCVarConfigModel>(Globals.PlcVarConfigPath).ToList();
- Globals.SiemensClient = new SiemensClient(Globals.CpuType, Globals.IpAddress, Globals.Port, Globals.Slot, Globals.Rack, Globals.ConnectTimeOut);
- var connectResult = Globals.SiemensClient.Open();
- if(connectResult.IsSucceed)
- {
- plcIsConnected = true;
- this.led_PlcState.On = true;
- }
- else
- {
- plcIsConnected = false;
- this.led_PlcState.On = false;
- }
- for (int i = 0; i < plcVarList.Count; i++)
- {
- //初始化PLC变量地址字典 地址-类型
- Globals.ReadDic.Add(plcVarList[i].PLC地址, Enum.Parse<DataTypeEnum>(plcVarList[i].变量类型, true));//忽略大小写
- //初始化PLC变量写入 名称-地址 //初始化PLC变量值字典 名称-值
- Globals.WriteDic.Add(plcVarList[i].名称, plcVarList[i].PLC地址);
- //初始化PLC变量值字典 名称-值
- Globals.DataDic.Add(plcVarList[i].名称, "NA");
- if (plcVarList[i].是否保存.ToLower() == "true")
- {
- Globals.SaveList.Add(plcVarList[i].名称);
- }
- }
- _logger.LogInformation("初始化PLC客户端成功");
- try
- {
- Task.Run(async () =>
- {
- while (!cts.IsCancellationRequested)
- {
- if (plcIsConnected)
- {
- //这个方法的目的是将大量的数据读取请求分批处理,以提高性能和可管理性。
- var readResult = Globals.SiemensClient.BatchRead(Globals.ReadDic);
- if (readResult.IsSucceed)
- {
- for (int i = 0; i < plcVarList.Count; i++)
- {
- Globals.DataDic[plcVarList[i].名称] = readResult.Value[plcVarList[i].PLC地址];
- }
- }
- else
- {
- Globals.SiemensClient.Close();
- plcIsConnected = false;
- this.Invoke(() =>
- {
- this.led_PlcState.On = false;
- });
- }
- await Task.Delay(Globals.ReadTimeInterval);
- }
- else
- {
- //重连PLC
- var reConnectResult = Globals.SiemensClient.Open();
- if (reConnectResult.IsSucceed)
- {
- plcIsConnected = true;
- this.Invoke(() =>
- {
- this.led_PlcState.On = true;
- });
- }
- else
- {
- plcIsConnected = false;
- this.Invoke(() =>
- {
- this.led_PlcState.On = false;
- });
- await Task.Delay(Globals.ReConnectTimeInterval);
- }
- }
- }
- await Task.Delay(1000);
- }, cts.Token);
- }
- catch (Exception)
- {
- throw;
- }
- }
- private void InitAsideUI()
- {
- int pageIndex = 1000;
- TreeNode parent0 = Aside.CreateNode("控制模块", 361461, 34, pageIndex);
- Aside.CreateChildNode(parent0, AddPage(Globals.ServiceProvider.GetRequiredService<PageTotalEquipmentControl>(), ++pageIndex));
- TreeNode parent1 = Aside.CreateNode("用户模块", 61447, 34, pageIndex);
- Aside.CreateChildNode(parent1, AddPage(Globals.ServiceProvider.GetRequiredService<PageUserManage>(), ++pageIndex));
- Aside.CreateChildNode(parent1, AddPage(Globals.ServiceProvider.GetRequiredService<PageAuthManage>(), ++pageIndex));
- TreeNode parent2 = Aside.CreateNode("监控模块", 560066, 34, pageIndex);
- Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor1>(), ++pageIndex));
- Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor2>(), ++pageIndex));
- Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor3>(), ++pageIndex));
- TreeNode parent3 = Aside.CreateNode("配方模块", 162677, 34, pageIndex);
- Aside.CreateChildNode(parent3, AddPage(Globals.ServiceProvider.GetRequiredService<PageRecipeManage>(), ++pageIndex));
- TreeNode parent4 = Aside.CreateNode("日志模块", 57557, 34, pageIndex);
- Aside.CreateChildNode(parent4, AddPage(Globals.ServiceProvider.GetRequiredService<PageLogManage>(), ++pageIndex));
- TreeNode parent5 = Aside.CreateNode("报表模块", 57586, 34, pageIndex);
- Aside.CreateChildNode(parent5, AddPage(Globals.ServiceProvider.GetRequiredService<PageReportManage>(), ++pageIndex));
- TreeNode parent6 = Aside.CreateNode("图表模块", 61950, 34, pageIndex);
- Aside.CreateChildNode(parent6, AddPage(Globals.ServiceProvider.GetRequiredService<PageChartManage>(), ++pageIndex));
- TreeNode parent7 = Aside.CreateNode("参数模块", 559576, 34, pageIndex);
- Aside.CreateChildNode(parent7, AddPage(Globals.ServiceProvider.GetRequiredService<PageSystemParameterSet>(), ++pageIndex));
- }
- private void InitHeaderUI()
- {
- //设置关联
- Header.TabControl = MainTabControl;
- Header.Nodes.Add("");
- Header.Nodes.Add("");
- Header.Nodes.Add("");
- Header.SetNodeSymbol(Header.Nodes[0], 558295, 34);
- Header.SetNodeSymbol(Header.Nodes[1], 61489, 34);
- Header.SetNodeSymbol(Header.Nodes[2], 557925, 34);
- var styles = UIStyles.PopularStyles();
- foreach (UIStyle style in styles)
- {
- Header.CreateChildNode(Header.Nodes[0], style.DisplayText(), style.Value());
- }
- //获取枚举FontsType的所有字体名称
- for (int i = 0; i < Enum.GetValues(typeof(SystemEnums.FontsType)).Length; i++)
- {
- Header.CreateChildNode(Header.Nodes[1], Enum.GetName(typeof(SystemEnums.FontsType), i), i + 1);
- }
- //获取枚举FontSize的所有字体大小 75-125的范围 75 80 85 90 95 100 105 110 115 120 125
- for (int i = 75; i <= 125; i += 5)
- {
- Header.CreateChildNode(Header.Nodes[2], i.ToString(), i);
- }
- }
- private void uiSymbolLabel2_Click(object sender, EventArgs e)
- {
- this.Close(); //关闭当前窗体
- }
- private void uiSymbolLabel1_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- private void Header_MenuItemClick(string itemText, int menuIndex, int pageIndex)
- {
- switch (menuIndex)
- {
- case 0:
- UIStyle style = (UIStyle)pageIndex;
- if (pageIndex < UIStyle.Colorful.Value())
- {
- StyleManager.Style = style;
- if (UIExtension.SetStyleManager != null)
- {
- UIExtension.SetStyleManager(StyleManager);
- }
- }
- break;
- case 1:
- UIStyles.DPIScale = true;
- UIStyles.GlobalFont = true;
- UIStyles.GlobalFontName = itemText;
- UIStyles.GlobalFontScale = SystemConsts.DefaultFontScale;
- UIStyles.SetDPIScale();
- break;
- case 2:
- UIStyles.GlobalFontScale = int.Parse(itemText);
- UIStyles.SetDPIScale();
- break;
- default:
- break;
- }
- }
- private void Panel_MouseDown(object sender, MouseEventArgs e)
- {
- mPoint = new Point(e.X, e.Y);
- }
- private void Panel_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
- }
- }
- }
- }
|