using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
using System.Data;
using Aitex.UI.Charting.ViewModel;
using Aitex.UI.Charting.Model;
namespace Aitex.UI.Charting.View
{
///
/// Interaction logic for VirtualDeviceFunctionEditor.xaml
///
public partial class VirtualDeviceFunctionEditor : Window
{
///
/// 构造函数
///
/// 被编辑的变量名,如果null则表明新建变量
///
public VirtualDeviceFunctionEditor(string varName, DataSelectFrame parentWindow)
{
InitializeComponent();
_parentWindow = parentWindow;
_varName = varName;
Loaded += new RoutedEventHandler(VirtualDeviceFunctionEditor_Loaded);
}
DataSelectFrame _parentWindow;
string _varName;
void VirtualDeviceFunctionEditor_Loaded(object sender, RoutedEventArgs e)
{
textBox.SetHotWords(CommonViewModel.Instance.CurrentSelectedDataSource.Datas.Keys.ToList());
if (string.IsNullOrWhiteSpace(_varName))
{
this.timeLabel.Content = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
this.textBox.Text = @"//名称:Example.TMGa_1.Press
//作者:administrator
//时间:2014/03/18
//描述:TMGa_1源瓶的虚拟压力读值(tor)
var1 = PM.PCs.P_TMGa_1_Press.Feedback; //unit: mbar
var2 = var1 * 1.33;
value = var2;
return value;";
}
else
{
deviceNameBox.Text = _varName;
Task.Factory.StartNew(() =>
{
try
{
this.Dispatcher.Invoke(new Action(() =>
{
this.textBox.Text = "正在读取...";
}));
string sql = string.Format("SELECT \"DeviceName\", \"LastModifyDate\", \"FunctionBlock\" FROM \"VirtualDevice\" where \"ChamberID\"='{0}' and \"DeviceName\"='{1}'",
ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName,
_varName);
var ds = Charting.ViewModel.ChartingBaseViewModel.ExecuteDataset(sql, null);
var deviceName = ds.Tables[0].Rows[0]["DeviceName"].ToString();
var lastTime = ds.Tables[0].Rows[0]["LastModifyDate"].ToString();
var funcCode = ds.Tables[0].Rows[0]["FunctionBlock"].ToString();
this.Dispatcher.Invoke(new Action(() =>
{
this.timeLabel.Content = lastTime;
this.textBox.Text = funcCode;
}));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
});
}
}
private void Button_Save_Click(object sender, RoutedEventArgs e)
{
try
{
string curChamName = ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName;
string funcContent = textBox.Text;
string deviceName = this.deviceNameBox.Text.Trim();
if (string.IsNullOrWhiteSpace(deviceName))
{
MessageBox.Show("变量名称为空", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (string.IsNullOrEmpty(funcContent))
{
MessageBox.Show("变量公式为空", "输入错误", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var ass = Assembly.GetExecutingAssembly();
var rs = ass.GetManifestResourceStream("DataAnalysisControl.VirtualDataTable.sql");
using (StreamReader fs = new StreamReader(rs))
{
//create virtual device table?
string sql = fs.ReadToEnd();
Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(sql);
}
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}');",
curChamName, _varName, curChamName, deviceName, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), funcContent);
Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(insertSQL);
//update tree view
var currentDataSource = ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource;
#if false
var keyList = currentDataSource.Datas.Keys.ToList();
string compareObj = "PM.VirtualDevice.";
int compareLen = compareObj.Length;
foreach(var dataId in keyList)
{
if (string.Compare(dataId, 0, compareObj, 0, compareLen) == 0)
{
currentDataSource.Datas.Remove(dataId);
}
}
string sqlQuery = string.Format("SELECT * FROM \"VirtualDevice\" where \"ChamberID\" ='{0}'", curChamName);
var ds = Charting.ViewModel.ChartingBaseViewModel.ExecuteDataset(sqlQuery);
foreach (DataRow row in ds.Tables[0].Rows)
{
string virtualDeviceName = row["DeviceName"].ToString();
}
#endif
string fullDeviceName = "PM.VirtualDevice." + deviceName;
if (string.IsNullOrEmpty(_varName))
{
//create
currentDataSource.Datas.Add(fullDeviceName, new DataItem() { DataName = fullDeviceName, RawData = new List(), TimeStamp = new List() });
if (currentDataSource is Charting.Model.PostgreSqlDataSource)
{
var varList = new List();
var collection = System.Text.RegularExpressions.Regex.Matches(funcContent, @"PM(\.\w+)+");
foreach (var item in collection)
{
varList.Add(item.ToString());
}
var source = ((Charting.Model.PostgreSqlDataSource)currentDataSource);
source.VirtualDeviceTable.Add(fullDeviceName, new Tuple>(funcContent, varList));
}
}
else
{
//modify
string oldName = "PM.VirtualDevice." + _varName;
if (currentDataSource.Datas.ContainsKey(oldName))
currentDataSource.Datas.Remove(oldName);
currentDataSource.Datas.Add(fullDeviceName, new DataItem() { DataName = fullDeviceName, RawData = new List(), TimeStamp = new List() });
if (currentDataSource is Charting.Model.PostgreSqlDataSource)
{
var varList = new List();
var collection = System.Text.RegularExpressions.Regex.Matches(funcContent, @"PM(\.\w+)+");
foreach (var item in collection)
{
varList.Add(item.ToString());
}
var source = ((Charting.Model.PostgreSqlDataSource)currentDataSource);
if (source.VirtualDeviceTable.ContainsKey(oldName))
source.VirtualDeviceTable.Remove(oldName);
source.VirtualDeviceTable.Add(fullDeviceName, new Tuple>(funcContent, varList));
}
}
_parentWindow.UpdateTreeView(ViewModel.CommonViewModel.Instance.CurrentSelectedDataSourceName);
MessageBox.Show("保存成功!", "公式编辑", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
}
catch (Exception ex)
{
MessageBox.Show("公式保存失败\n\n" + ex.Message, "公式编辑", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
finally
{
Close();
}
}
private void Button_Close_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}