FormMain.cs 12 KB

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