FormMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using Helper;
  2. using HZY.Framework.DependencyInjection;
  3. using IoTClient.Clients.PLC;
  4. using IoTClient.Common.Enums;
  5. using IoTClient.Enums;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using MiniExcelLibs;
  9. using Model;
  10. using Scada.Page;
  11. using Sunny.UI;
  12. namespace Scada
  13. {
  14. public partial class FormMain : UIHeaderAsideMainFooterFrame,IScopedSelfDependency
  15. {
  16. private bool plcIsConnected;
  17. private readonly ILogger<FormMain> _logger;
  18. private Point mPoint;
  19. private CancellationTokenSource cts = new CancellationTokenSource();
  20. public FormMain(ILogger<FormMain> logger)
  21. {
  22. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  23. InitializeComponent();
  24. InitAsideUI();
  25. InitHeaderUI();
  26. InitConfig();
  27. InitPlcClient();
  28. //显示默认界面
  29. //Aside.SelectFirst();
  30. }
  31. private void InitConfig()
  32. {
  33. //读取PLC配置 192.168.1.180 127.0.0.1
  34. Globals.IpAddress = Globals.IniFile.ReadString("PLC参数", "IP", "127.0.0.1");
  35. Globals.Port = Globals.IniFile.ReadInt("PLC参数", "Port", 102);
  36. Globals.CpuType = Enum.Parse<SiemensVersion>(Globals.IniFile.ReadString("PLC参数", "CpuType", SiemensVersion.S7_1200.ToString()));
  37. Globals.Slot = Globals.IniFile.ReadByte("PLC参数", "Slot", 0);
  38. Globals.Rack = Globals.IniFile.ReadByte("PLC参数", "Rack", 0);
  39. Globals.ConnectTimeOut = Globals.IniFile.ReadInt("PLC参数", "ConnectTimeOut", 3000);
  40. Globals.ReadTimeInterval = Globals.IniFile.ReadInt("PLC参数", "ReadTimeInterval", 300);
  41. Globals.ReConnectTimeInterval = Globals.IniFile.ReadInt("PLC参数", "ReConnectTimeInterval", 3000);
  42. //PLC变量表路径
  43. Globals.PlcVarConfigPath = Globals.IniFile.ReadString("PLC参数", "变量表地址", Path.Combine(Application.StartupPath, "PLC_Var_Config.xlsx"));
  44. //删除文件夹路径
  45. Globals.DelFilePath = Globals.IniFile.ReadString("系统参数", "删除文件夹路径", Path.Combine(Application.StartupPath, "Logs"));
  46. //保存天数
  47. Globals.SaveDay = Globals.IniFile.ReadString("系统参数", "保存天数", "30");
  48. //试用时长
  49. Globals.SYTime = Globals.IniFile.ReadInt("软件参数", "试用时间", 100);
  50. //软件版本
  51. Globals.SoftwareVersion = Globals.IniFile.ReadString("软件参数", "软件版本", "V1.0");
  52. _logger.LogInformation("读取配置文件成功");
  53. }
  54. private void InitPlcClient()
  55. {
  56. var plcVarList = MiniExcel.Query<PLCVarConfigModel>(Globals.PlcVarConfigPath).ToList();
  57. Globals.SiemensClient = new SiemensClient(Globals.CpuType, Globals.IpAddress, Globals.Port, Globals.Slot, Globals.Rack, Globals.ConnectTimeOut);
  58. var connectResult = Globals.SiemensClient.Open();
  59. if(connectResult.IsSucceed)
  60. {
  61. plcIsConnected = true;
  62. this.led_PlcState.On = true;
  63. }
  64. else
  65. {
  66. plcIsConnected = false;
  67. this.led_PlcState.On = false;
  68. }
  69. for (int i = 0; i < plcVarList.Count; i++)
  70. {
  71. //初始化PLC变量地址字典 地址-类型
  72. Globals.ReadDic.Add(plcVarList[i].PLC地址, Enum.Parse<DataTypeEnum>(plcVarList[i].变量类型, true));//忽略大小写
  73. //初始化PLC变量写入 名称-地址 //初始化PLC变量值字典 名称-值
  74. Globals.WriteDic.Add(plcVarList[i].名称, plcVarList[i].PLC地址);
  75. //初始化PLC变量值字典 名称-值
  76. Globals.DataDic.Add(plcVarList[i].名称, "NA");
  77. if (plcVarList[i].是否保存.ToLower() == "true")
  78. {
  79. Globals.SaveList.Add(plcVarList[i].名称);
  80. }
  81. }
  82. _logger.LogInformation("初始化PLC客户端成功");
  83. try
  84. {
  85. Task.Run(async () =>
  86. {
  87. while (!cts.IsCancellationRequested)
  88. {
  89. if (plcIsConnected)
  90. {
  91. //这个方法的目的是将大量的数据读取请求分批处理,以提高性能和可管理性。
  92. var readResult = Globals.SiemensClient.BatchRead(Globals.ReadDic);
  93. if (readResult.IsSucceed)
  94. {
  95. for (int i = 0; i < plcVarList.Count; i++)
  96. {
  97. Globals.DataDic[plcVarList[i].名称] = readResult.Value[plcVarList[i].PLC地址];
  98. }
  99. }
  100. else
  101. {
  102. Globals.SiemensClient.Close();
  103. plcIsConnected = false;
  104. this.Invoke(() =>
  105. {
  106. this.led_PlcState.On = false;
  107. });
  108. }
  109. await Task.Delay(Globals.ReadTimeInterval);
  110. }
  111. else
  112. {
  113. //重连PLC
  114. var reConnectResult = Globals.SiemensClient.Open();
  115. if (reConnectResult.IsSucceed)
  116. {
  117. plcIsConnected = true;
  118. this.Invoke(() =>
  119. {
  120. this.led_PlcState.On = true;
  121. });
  122. }
  123. else
  124. {
  125. plcIsConnected = false;
  126. this.Invoke(() =>
  127. {
  128. this.led_PlcState.On = false;
  129. });
  130. await Task.Delay(Globals.ReConnectTimeInterval);
  131. }
  132. }
  133. }
  134. await Task.Delay(1000);
  135. }, cts.Token);
  136. }
  137. catch (Exception)
  138. {
  139. throw;
  140. }
  141. }
  142. private void InitAsideUI()
  143. {
  144. int pageIndex = 1000;
  145. TreeNode parent0 = Aside.CreateNode("控制模块", 361461, 34, pageIndex);
  146. Aside.CreateChildNode(parent0, AddPage(Globals.ServiceProvider.GetRequiredService<PageTotalEquipmentControl>(), ++pageIndex));
  147. TreeNode parent1 = Aside.CreateNode("用户模块", 61447, 34, pageIndex);
  148. Aside.CreateChildNode(parent1, AddPage(Globals.ServiceProvider.GetRequiredService<PageUserManage>(), ++pageIndex));
  149. Aside.CreateChildNode(parent1, AddPage(Globals.ServiceProvider.GetRequiredService<PageAuthManage>(), ++pageIndex));
  150. TreeNode parent2 = Aside.CreateNode("监控模块", 560066, 34, pageIndex);
  151. Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor1>(), ++pageIndex));
  152. Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor2>(), ++pageIndex));
  153. Aside.CreateChildNode(parent2, AddPage(Globals.ServiceProvider.GetRequiredService<PageEquipmentMonitor3>(), ++pageIndex));
  154. TreeNode parent3 = Aside.CreateNode("配方模块", 162677, 34, pageIndex);
  155. Aside.CreateChildNode(parent3, AddPage(Globals.ServiceProvider.GetRequiredService<PageRecipeManage>(), ++pageIndex));
  156. TreeNode parent4 = Aside.CreateNode("日志模块", 57557, 34, pageIndex);
  157. Aside.CreateChildNode(parent4, AddPage(Globals.ServiceProvider.GetRequiredService<PageLogManage>(), ++pageIndex));
  158. TreeNode parent5 = Aside.CreateNode("报表模块", 57586, 34, pageIndex);
  159. Aside.CreateChildNode(parent5, AddPage(Globals.ServiceProvider.GetRequiredService<PageReportManage>(), ++pageIndex));
  160. TreeNode parent6 = Aside.CreateNode("图表模块", 61950, 34, pageIndex);
  161. Aside.CreateChildNode(parent6, AddPage(Globals.ServiceProvider.GetRequiredService<PageChartManage>(), ++pageIndex));
  162. TreeNode parent7 = Aside.CreateNode("参数模块", 559576, 34, pageIndex);
  163. Aside.CreateChildNode(parent7, AddPage(Globals.ServiceProvider.GetRequiredService<PageSystemParameterSet>(), ++pageIndex));
  164. }
  165. private void InitHeaderUI()
  166. {
  167. //设置关联
  168. Header.TabControl = MainTabControl;
  169. Header.Nodes.Add("");
  170. Header.Nodes.Add("");
  171. Header.Nodes.Add("");
  172. Header.SetNodeSymbol(Header.Nodes[0], 558295, 34);
  173. Header.SetNodeSymbol(Header.Nodes[1], 61489, 34);
  174. Header.SetNodeSymbol(Header.Nodes[2], 557925, 34);
  175. var styles = UIStyles.PopularStyles();
  176. foreach (UIStyle style in styles)
  177. {
  178. Header.CreateChildNode(Header.Nodes[0], style.DisplayText(), style.Value());
  179. }
  180. //获取枚举FontsType的所有字体名称
  181. for (int i = 0; i < Enum.GetValues(typeof(SystemEnums.FontsType)).Length; i++)
  182. {
  183. Header.CreateChildNode(Header.Nodes[1], Enum.GetName(typeof(SystemEnums.FontsType), i), i + 1);
  184. }
  185. //获取枚举FontSize的所有字体大小 75-125的范围 75 80 85 90 95 100 105 110 115 120 125
  186. for (int i = 75; i <= 125; i += 5)
  187. {
  188. Header.CreateChildNode(Header.Nodes[2], i.ToString(), i);
  189. }
  190. }
  191. private void uiSymbolLabel2_Click(object sender, EventArgs e)
  192. {
  193. this.Close(); //关闭当前窗体
  194. }
  195. private void uiSymbolLabel1_Click(object sender, EventArgs e)
  196. {
  197. this.WindowState = FormWindowState.Minimized;
  198. }
  199. private void Header_MenuItemClick(string itemText, int menuIndex, int pageIndex)
  200. {
  201. switch (menuIndex)
  202. {
  203. case 0:
  204. UIStyle style = (UIStyle)pageIndex;
  205. if (pageIndex < UIStyle.Colorful.Value())
  206. {
  207. StyleManager.Style = style;
  208. if (UIExtension.SetStyleManager != null)
  209. {
  210. UIExtension.SetStyleManager(StyleManager);
  211. }
  212. }
  213. break;
  214. case 1:
  215. UIStyles.DPIScale = true;
  216. UIStyles.GlobalFont = true;
  217. UIStyles.GlobalFontName = itemText;
  218. UIStyles.GlobalFontScale = SystemConsts.DefaultFontScale;
  219. UIStyles.SetDPIScale();
  220. break;
  221. case 2:
  222. UIStyles.GlobalFontScale = int.Parse(itemText);
  223. UIStyles.SetDPIScale();
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. private void Panel_MouseDown(object sender, MouseEventArgs e)
  230. {
  231. mPoint = new Point(e.X, e.Y);
  232. }
  233. private void Panel_MouseMove(object sender, MouseEventArgs e)
  234. {
  235. if (e.Button == MouseButtons.Left)
  236. {
  237. this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
  238. }
  239. }
  240. }
  241. }