终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

455 lines
17 KiB

  1. using BeDesignerSCADA.ViewModel;
  2. using BPASmart.Model;
  3. using BPASmartClient.Compiler;
  4. using BPASmartClient.DATABUS;
  5. using BPASmartClient.MessageName;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Reflection.Emit;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Controls.Primitives;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Forms;
  21. using System.Windows.Input;
  22. using System.Windows.Markup;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Animation;
  25. using System.Windows.Media.Imaging;
  26. using System.Windows.Navigation;
  27. using System.Windows.Shapes;
  28. namespace BeDesignerSCADA
  29. {
  30. /// <summary>
  31. /// Interaction logic for MainWindow.xaml
  32. /// </summary>
  33. public partial class MainWindow : Window
  34. {
  35. MainViewModel viewModel=new MainViewModel();
  36. public MainWindow()
  37. {
  38. InitializeComponent();
  39. this.DataContext = viewModel;
  40. viewModel.Loaded(cav, runCanvas);
  41. //控件加载
  42. Assembly assembly = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.SCADAControl.dll"); //Assembly.GetExecutingAssembly();
  43. var controls = assembly.GetTypes().Where(t => t.GetInterface("IExecutable") != null).OrderBy(o=>o.Name);
  44. CtlList.ItemsSource = controls;
  45. this.GotFocus += MainWindow_GotFocus;
  46. this.LostFocus += MainWindow_LostFocus;
  47. }
  48. private void MainWindow_LostFocus(object sender, RoutedEventArgs e)
  49. {
  50. System.Diagnostics.Trace.WriteLine("失去焦点:" + e.OriginalSource.ToString());
  51. // Console.WriteLine(e.OriginalSource.ToString());
  52. }
  53. private void MainWindow_GotFocus(object sender, RoutedEventArgs e)
  54. {
  55. System.Diagnostics.Trace.WriteLine("获得焦点:"+e.OriginalSource.ToString());
  56. }
  57. #region 加载数据中心与事件中心
  58. /// <summary>
  59. /// 加载数据中心
  60. /// </summary>
  61. public void Loading()
  62. {
  63. //try
  64. //{
  65. // Assembly assembly1 = Assembly.LoadFile($"{System.AppDomain.CurrentDomain.BaseDirectory}\\BPASmartClient.MessageName.dll");
  66. // Type type = assembly1.GetType("BPASmartClient.MessageName.MessageName");
  67. // var control = Activator.CreateInstance(type);
  68. // var fildes = control.GetType().GetFields();
  69. // foreach (var fi in fildes)
  70. // {
  71. // EventModel eventModel = new EventModel();
  72. // eventModel.EventValue = fi.GetValue(control)?.ToString();
  73. // eventModel.EventName = fi.Name;
  74. // foreach (var item in fi.CustomAttributes)
  75. // {
  76. // if (item.AttributeType.Name == "CategoryAttribute")
  77. // {
  78. // eventModel.Category = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
  79. // }
  80. // else if (item.AttributeType.Name == "DescriptionAttribute")
  81. // {
  82. // eventModel.Description = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
  83. // }
  84. // else if (item.AttributeType.Name == "BrowsableAttribute")
  85. // {
  86. // eventModel.Browsable = item.ConstructorArguments.Count() > 0 ? (bool)item.ConstructorArguments[0].Value : true;
  87. // }
  88. // }
  89. // if (!string.IsNullOrEmpty(eventModel.Category))
  90. // {
  91. // if (Class_DataBus.GetInstance().EventData.ContainsKey(eventModel.Category))
  92. // Class_DataBus.GetInstance().EventData[eventModel.Category].Add(eventModel);
  93. // else
  94. // {
  95. // Class_DataBus.GetInstance().EventData[eventModel.Category] = new List<EventModel> { eventModel };
  96. // }
  97. // }
  98. // }
  99. // type = assembly1.GetType("BPASmartClient.MessageName.DataName");
  100. // control = Activator.CreateInstance(type);
  101. // fildes = control.GetType().GetFields();
  102. // foreach (var fi in fildes)
  103. // {
  104. // ALLModel aLLModel = new ALLModel();
  105. // aLLModel.DataValue = fi.GetValue(control)?.ToString();
  106. // aLLModel.DataName = fi.Name;
  107. // foreach (var item in fi.CustomAttributes)
  108. // {
  109. // if (item.AttributeType.Name == "CategoryAttribute")
  110. // {
  111. // aLLModel.Category = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
  112. // }
  113. // else if (item.AttributeType.Name == "DescriptionAttribute")
  114. // {
  115. // aLLModel.Description = item.ConstructorArguments.Count() > 0 ? item.ConstructorArguments[0].Value?.ToString() : "";
  116. // }
  117. // else if (item.AttributeType.Name == "BrowsableAttribute")
  118. // {
  119. // aLLModel.Browsable = item.ConstructorArguments.Count() > 0 ? (bool)item.ConstructorArguments[0].Value : true;
  120. // }
  121. // }
  122. // if (!string.IsNullOrEmpty(aLLModel.Category))
  123. // {
  124. // if (Class_DataBus.GetInstance().ALLData.ContainsKey(aLLModel.Category))
  125. // Class_DataBus.GetInstance().ALLData[aLLModel.Category].Add(aLLModel);
  126. // else
  127. // {
  128. // Class_DataBus.GetInstance().ALLData[aLLModel.Category] = new List<ALLModel> { aLLModel };
  129. // }
  130. // }
  131. // }
  132. //}
  133. //catch (Exception ex)
  134. //{
  135. // System.Windows.MessageBox.Show(ex.Message);
  136. //}
  137. }
  138. /// <summary>
  139. /// 加载本地文件
  140. /// </summary>
  141. public void LoadFile()
  142. {
  143. //加载配置数据
  144. FJson<CommunicationPar>.Read();
  145. viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
  146. viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
  147. FJson<CommunicationPar>.Data.CommunicationDevices?.ToList().ForEach(data =>
  148. {
  149. viewModel.DevNameList.Add(data.DeviceName);
  150. data.VarTableModels?.ToList().ForEach((data) => {
  151. if(!viewModel.DevValueList.Contains(data.VarName) && !string.IsNullOrEmpty(data.VarName))
  152. viewModel.DevValueList.Add(data.VarName);
  153. });
  154. });
  155. //CommunicationModel model = new CommunicationModel();
  156. //Type typeData = model.GetType();
  157. //PropertyInfo[] properties = typeData.GetProperties();
  158. //foreach (PropertyInfo property in properties)
  159. //{
  160. // viewModel.DevValueList.Add(property.Name);
  161. //}
  162. //ICommunicationDevice DeviceType;
  163. //DeviceType = FJson<CommunicationPar>.Data.CommunicationDevices.ElementAt(0).CommDevice;
  164. //VariableInfo variable = new VariableInfo(DeviceType);
  165. //PropertyInfo[] prope = variable.GetType().GetProperties();
  166. //foreach (var item in prope)
  167. //{
  168. // viewModel.DevValueList.Add(item.Name);
  169. //}
  170. }
  171. #endregion
  172. #region 位置调整
  173. /// <summary>
  174. /// 左对齐
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void AglinLeftBtn_Click(object sender, RoutedEventArgs e)
  179. {
  180. cav.AlignLeft();
  181. }
  182. /// <summary>
  183. /// 底部对齐
  184. /// </summary>
  185. /// <param name="sender"></param>
  186. /// <param name="e"></param>
  187. private void AglinBottomBtn_Click(object sender, RoutedEventArgs e)
  188. {
  189. cav.AlignBottom();
  190. }
  191. /// <summary>
  192. /// 顶部对齐
  193. /// </summary>
  194. /// <param name="sender"></param>
  195. /// <param name="e"></param>
  196. private void AglinTopBtn_Click(object sender, RoutedEventArgs e)
  197. {
  198. cav.AlignTop();
  199. }
  200. /// <summary>
  201. /// 右对齐
  202. /// </summary>
  203. /// <param name="sender"></param>
  204. /// <param name="e"></param>
  205. private void AglinRightBtn_Click(object sender, RoutedEventArgs e)
  206. {
  207. cav.AlignRight();
  208. }
  209. /// <summary>
  210. /// 居中
  211. /// </summary>
  212. /// <param name="sender"></param>
  213. /// <param name="e"></param>
  214. private void AglinCenterBtn_Click(object sender, RoutedEventArgs e)
  215. {
  216. cav.AlignCenter();
  217. }
  218. /// <summary>
  219. /// 垂直分布
  220. /// </summary>
  221. /// <param name="sender"></param>
  222. /// <param name="e"></param>
  223. private void VerticalLayoutBtn_Click(object sender, RoutedEventArgs e)
  224. {
  225. cav.VertialLayout();
  226. }
  227. /// <summary>
  228. /// 水平分布
  229. /// </summary>
  230. /// <param name="sender"></param>
  231. /// <param name="e"></param>
  232. private void HorizontalLayoutBtn_Click(object sender, RoutedEventArgs e)
  233. {
  234. cav.HorizontalLayout();
  235. }
  236. #endregion
  237. #region 其他事件操作
  238. /// <summary>
  239. /// 运行
  240. /// </summary>
  241. /// <param name="sender"></param>
  242. /// <param name="e"></param>
  243. private void RunBtn_Click(object sender, RoutedEventArgs e)
  244. {
  245. if (sender is System.Windows.Controls.Button btn)
  246. {
  247. if (btn.Tag.ToString() == "运行")
  248. {
  249. cav.ClearSelection();
  250. runCanvas.Run(cav.Generator());
  251. }
  252. else if (btn.Tag.ToString() == "停止")
  253. {
  254. runCanvas.Destory();
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// 加载
  260. /// </summary>
  261. /// <param name="sender"></param>
  262. /// <param name="e"></param>
  263. private void LoadBtn_Click(object sender, RoutedEventArgs e)
  264. {
  265. OpenFileDialog ofd = new OpenFileDialog();
  266. ofd.Filter = "布局文件|*.lay";
  267. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  268. {
  269. cav.Load(ofd.FileName);
  270. }
  271. DoubleAnimation da = new DoubleAnimation(-200, 0, new Duration(TimeSpan.FromMilliseconds(250)));
  272. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  273. CanvasTranslate.BeginAnimation(TranslateTransform.XProperty, da);
  274. DoubleAnimation daop = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(250)));
  275. daop.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  276. cav.BeginAnimation(OpacityProperty, daop);
  277. }
  278. /// <summary>
  279. /// 保存
  280. /// </summary>
  281. /// <param name="sender"></param>
  282. /// <param name="e"></param>
  283. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  284. {
  285. SaveFileDialog sfd = new SaveFileDialog();
  286. sfd.Filter = "布局文件|*.lay";
  287. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  288. {
  289. string str = cav.Save();
  290. File.WriteAllText(sfd.FileName, str, Encoding.Unicode);
  291. }
  292. }
  293. /// <summary>
  294. /// 模拟消息发送
  295. /// </summary>
  296. /// <param name="sender"></param>
  297. /// <param name="e"></param>
  298. private void MNBtn_Click(object sender, RoutedEventArgs e)
  299. {
  300. }
  301. #endregion
  302. #region 左侧控件栏移动
  303. /// <summary>
  304. /// 移动到右侧
  305. /// </summary>
  306. /// <param name="sender"></param>
  307. /// <param name="e"></param>
  308. private void CtlList_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  309. {
  310. if (CtlList.SelectedItem != null && e.LeftButton == MouseButtonState.Pressed)
  311. {
  312. DragDrop.DoDragDrop(CtlList, CtlList.SelectedItem, System.Windows.DragDropEffects.Copy);
  313. codeEditor.Text = cav.Save();
  314. }
  315. }
  316. /// <summary>
  317. /// 显示代码
  318. /// </summary>
  319. /// <param name="sender"></param>
  320. /// <param name="e"></param>
  321. private void showCode_Click(object sender, RoutedEventArgs e)
  322. {
  323. codeEditor.Text = cav.Save();
  324. }
  325. /// <summary>
  326. /// 编辑
  327. /// </summary>
  328. /// <param name="sender"></param>
  329. /// <param name="e"></param>
  330. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  331. {
  332. try
  333. {
  334. if (sender is ToggleButton)
  335. {
  336. ToggleButton toggle = (ToggleButton)sender;
  337. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  338. if (propertyGridCommand != null)
  339. {
  340. viewModel.Edit(propertyGridCommand);
  341. }
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. }
  347. }
  348. /// <summary>
  349. /// 变量选择
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void ComboBoxValue_TextChanged(object sender,TextChangedEventArgs e)
  354. {
  355. try
  356. {
  357. if (sender is System.Windows.Controls.ComboBox)
  358. {
  359. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  360. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  361. if (toggle.Tag != null && !string.IsNullOrEmpty(toggle.Text))
  362. propertyGridCommand.Value = "{" + $"Binding {toggle.Tag}.{toggle.Text}" + "}";
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. }
  368. }
  369. /// <summary>
  370. /// 变量下拉框打开事件
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void valuebox_DropDownOpened(object sender,EventArgs e)
  375. {
  376. try
  377. {
  378. viewModel.DevValueList = new System.Collections.ObjectModel.ObservableCollection<string>();
  379. if (sender is System.Windows.Controls.ComboBox)
  380. {
  381. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  382. if (toggle.Tag == null) return;
  383. if (Class_DataBus.GetInstance().Dic_DeviceData.ContainsKey(toggle.Tag.ToString()))
  384. {
  385. Class_DataBus.GetInstance().Dic_DeviceData[toggle.Tag.ToString()].Keys?.ToList().ForEach(key => { viewModel.DevValueList.Add(key); });
  386. }
  387. }
  388. }
  389. catch (Exception ex)
  390. {
  391. }
  392. }
  393. /// <summary>
  394. /// 设备名称选择
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. private void ComboBoxName_TextChanged(object sender,TextChangedEventArgs e)
  399. {
  400. try
  401. {
  402. if (sender is System.Windows.Controls.ComboBox)
  403. {
  404. System.Windows.Controls.ComboBox toggle = (System.Windows.Controls.ComboBox)sender;
  405. Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyGridCommand = toggle.DataContext as Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem;
  406. if(toggle.Tag!=null && !string.IsNullOrEmpty(toggle.Text))
  407. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}.{toggle.Tag}" + "}";
  408. else if(!string.IsNullOrEmpty(toggle.Text))
  409. propertyGridCommand.Value = "{" + $"Binding {toggle.Text}." + "}";
  410. }
  411. }
  412. catch (Exception ex)
  413. {
  414. }
  415. }
  416. /// <summary>
  417. /// 设备名称下拉框打开事件
  418. /// </summary>
  419. /// <param name="sender"></param>
  420. /// <param name="e"></param>
  421. private void namebox_DropDownOpened(object sender,EventArgs e)
  422. {
  423. viewModel.DevNameList = new System.Collections.ObjectModel.ObservableCollection<string>();
  424. Class_DataBus.GetInstance().Dic_DeviceData.Keys?.ToList().ForEach(key => { viewModel.DevNameList.Add(key); });
  425. }
  426. #endregion
  427. }
  428. }