VirtualDeviceFunctionEditor.xaml.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Reflection;
  15. using System.IO;
  16. using System.Threading.Tasks;
  17. using System.Data;
  18. using Aitex.UI.Charting.ViewModel;
  19. using Aitex.UI.Charting.Model;
  20. namespace Aitex.UI.Charting.View
  21. {
  22. /// <summary>
  23. /// Interaction logic for VirtualDeviceFunctionEditor.xaml
  24. /// </summary>
  25. public partial class VirtualDeviceFunctionEditor : Window
  26. {
  27. /// <summary>
  28. /// 构造函数
  29. /// </summary>
  30. /// <param name="varName">被编辑的变量名,如果null则表明新建变量</param>
  31. /// <param name="dataSelectFrame"></param>
  32. public VirtualDeviceFunctionEditor(string varName, DataSelectFrame parentWindow)
  33. {
  34. InitializeComponent();
  35. _parentWindow = parentWindow;
  36. _varName = varName;
  37. Loaded += new RoutedEventHandler(VirtualDeviceFunctionEditor_Loaded);
  38. }
  39. DataSelectFrame _parentWindow;
  40. string _varName;
  41. void VirtualDeviceFunctionEditor_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. textBox.SetHotWords(CommonViewModel.Instance.CurrentSelectedDataSource.Datas.Keys.ToList());
  44. if (string.IsNullOrWhiteSpace(_varName))
  45. {
  46. this.timeLabel.Content = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
  47. this.textBox.Text = @"//名称:Example.TMGa_1.Press
  48. //作者:administrator
  49. //时间:2014/03/18
  50. //描述:TMGa_1源瓶的虚拟压力读值(tor)
  51. var1 = PM.PCs.P_TMGa_1_Press.Feedback; //unit: mbar
  52. var2 = var1 * 1.33;
  53. value = var2;
  54. return value;";
  55. }
  56. else
  57. {
  58. deviceNameBox.Text = _varName;
  59. Task.Factory.StartNew(() =>
  60. {
  61. try
  62. {
  63. this.Dispatcher.Invoke(new Action(() =>
  64. {
  65. this.textBox.Text = "正在读取...";
  66. }));
  67. string sql = string.Format("SELECT \"DeviceName\", \"LastModifyDate\", \"FunctionBlock\" FROM \"VirtualDevice\" where \"ChamberID\"='{0}' and \"DeviceName\"='{1}'",
  68. ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName,
  69. _varName);
  70. var ds = Charting.ViewModel.ChartingBaseViewModel.ExecuteDataset(sql, null);
  71. var deviceName = ds.Tables[0].Rows[0]["DeviceName"].ToString();
  72. var lastTime = ds.Tables[0].Rows[0]["LastModifyDate"].ToString();
  73. var funcCode = ds.Tables[0].Rows[0]["FunctionBlock"].ToString();
  74. this.Dispatcher.Invoke(new Action(() =>
  75. {
  76. this.timeLabel.Content = lastTime;
  77. this.textBox.Text = funcCode;
  78. }));
  79. }
  80. catch (Exception ex)
  81. {
  82. System.Diagnostics.Debug.WriteLine(ex.Message);
  83. }
  84. });
  85. }
  86. }
  87. private void Button_Save_Click(object sender, RoutedEventArgs e)
  88. {
  89. try
  90. {
  91. string curChamName = ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName;
  92. string funcContent = textBox.Text;
  93. string deviceName = this.deviceNameBox.Text.Trim();
  94. if (string.IsNullOrWhiteSpace(deviceName))
  95. {
  96. MessageBox.Show("变量名称为空", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
  97. return;
  98. }
  99. if (string.IsNullOrEmpty(funcContent))
  100. {
  101. MessageBox.Show("变量公式为空", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
  102. return;
  103. }
  104. var ass = Assembly.GetExecutingAssembly();
  105. var rs = ass.GetManifestResourceStream("DataAnalysisControl.VirtualDataTable.sql");
  106. using (StreamReader fs = new StreamReader(rs))
  107. {
  108. //create virtual device table?
  109. string sql = fs.ReadToEnd();
  110. Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(sql);
  111. }
  112. string insertSQL = string.Format("DELETE FROM \"VirtualDevice\" where \"ChamberID\" ='{0}' and \"DeviceName\" = '{1}';\nINSERT INTO \"VirtualDevice\"(\"ChamberID\", \"DeviceName\", \"LastModifyDate\", \"FunctionBlock\") VALUES ('{2}', '{3}', '{4}', '{5}');",
  113. curChamName, _varName, curChamName, deviceName, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), funcContent);
  114. Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(insertSQL);
  115. //update tree view
  116. var currentDataSource = ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource;
  117. #if false
  118. var keyList = currentDataSource.Datas.Keys.ToList();
  119. string compareObj = "PM.VirtualDevice.";
  120. int compareLen = compareObj.Length;
  121. foreach(var dataId in keyList)
  122. {
  123. if (string.Compare(dataId, 0, compareObj, 0, compareLen) == 0)
  124. {
  125. currentDataSource.Datas.Remove(dataId);
  126. }
  127. }
  128. string sqlQuery = string.Format("SELECT * FROM \"VirtualDevice\" where \"ChamberID\" ='{0}'", curChamName);
  129. var ds = Charting.ViewModel.ChartingBaseViewModel.ExecuteDataset(sqlQuery);
  130. foreach (DataRow row in ds.Tables[0].Rows)
  131. {
  132. string virtualDeviceName = row["DeviceName"].ToString();
  133. }
  134. #endif
  135. string fullDeviceName = "PM.VirtualDevice." + deviceName;
  136. if (string.IsNullOrEmpty(_varName))
  137. {
  138. //create
  139. currentDataSource.Datas.Add(fullDeviceName, new DataItem() { DataName = fullDeviceName, RawData = new List<float>(), TimeStamp = new List<DateTime>() });
  140. if (currentDataSource is Charting.Model.PostgreSqlDataSource)
  141. {
  142. var varList = new List<string>();
  143. var collection = System.Text.RegularExpressions.Regex.Matches(funcContent, @"PM(\.\w+)+");
  144. foreach (var item in collection)
  145. {
  146. varList.Add(item.ToString());
  147. }
  148. var source = ((Charting.Model.PostgreSqlDataSource)currentDataSource);
  149. source.VirtualDeviceTable.Add(fullDeviceName, new Tuple<string, List<string>>(funcContent, varList));
  150. }
  151. }
  152. else
  153. {
  154. //modify
  155. string oldName = "PM.VirtualDevice." + _varName;
  156. if (currentDataSource.Datas.ContainsKey(oldName))
  157. currentDataSource.Datas.Remove(oldName);
  158. currentDataSource.Datas.Add(fullDeviceName, new DataItem() { DataName = fullDeviceName, RawData = new List<float>(), TimeStamp = new List<DateTime>() });
  159. if (currentDataSource is Charting.Model.PostgreSqlDataSource)
  160. {
  161. var varList = new List<string>();
  162. var collection = System.Text.RegularExpressions.Regex.Matches(funcContent, @"PM(\.\w+)+");
  163. foreach (var item in collection)
  164. {
  165. varList.Add(item.ToString());
  166. }
  167. var source = ((Charting.Model.PostgreSqlDataSource)currentDataSource);
  168. if (source.VirtualDeviceTable.ContainsKey(oldName))
  169. source.VirtualDeviceTable.Remove(oldName);
  170. source.VirtualDeviceTable.Add(fullDeviceName, new Tuple<string, List<string>>(funcContent, varList));
  171. }
  172. }
  173. _parentWindow.UpdateTreeView(ViewModel.CommonViewModel.Instance.CurrentSelectedDataSourceName);
  174. MessageBox.Show("保存成功!", "公式编辑", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
  175. }
  176. catch (Exception ex)
  177. {
  178. MessageBox.Show("公式保存失败\n\n" + ex.Message, "公式编辑", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  179. }
  180. finally
  181. {
  182. Close();
  183. }
  184. }
  185. private void Button_Close_Click(object sender, RoutedEventArgs e)
  186. {
  187. Close();
  188. }
  189. }
  190. }