终端一体化运控平台
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.
 
 
 

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