using System; using System.Collections.Generic; using System.Linq; using System.Text; using Npgsql; using System.Threading; using Aitex.UI.Charting.ViewModel; namespace Aitex.UI.Charting.Model { public class PostgreSqlDataSource : ChartingBaseViewModel, IDataSource { public PostgreSqlDataSource() { VirtualDeviceTable = new Dictionary>>(); } public Dictionary Datas { get; set; } public string Title { get; set; } public string ChamberName { get; set; } public string Description { get; set; } public DateTime BeginTime { get; set; } public DateTime EndTime { get; set; } public List RecipeSteps { get; set; } public Dictionary/*var list in db*/>> VirtualDeviceTable { get; set; } public bool GetData(string dataName, DateTime beginTime, DateTime endTime, out DataItem returnDataItem) { returnDataItem = null; List dataIdList = new List(); dataIdList.Add(dataName); Dictionary dbDatas; bool isSucc = GetDbData(ChamberName, beginTime, endTime, dataIdList, out dbDatas); if (isSucc && dbDatas.ContainsKey(dataName)) returnDataItem = dbDatas[dataName]; return isSucc; } public bool GetData(List dataNames, DateTime beginTime, DateTime endTime, out List returnDataItems) { returnDataItems = new List(); //convert virtual data here Dictionary dbDatas; List rawDataNames = new List(); foreach (var item in dataNames) { if (System.Text.RegularExpressions.Regex.IsMatch(item, "PM.VirtualDevice.")) { if (VirtualDeviceTable != null && VirtualDeviceTable.ContainsKey(item)) { var dataNamesInVirtualDevice = VirtualDeviceTable[item].Item2; foreach (var item2 in dataNamesInVirtualDevice) { if (!rawDataNames.Contains(item2) && Datas.ContainsKey(item2)) rawDataNames.Add(item2); } } } else { if (!rawDataNames.Contains(item)) rawDataNames.Add(item); } } if (rawDataNames.Count == 0) return false; var isSucc = GetDbData(ChamberName, beginTime, endTime, rawDataNames, out dbDatas); if (isSucc) { foreach (var clientName in dataNames) { if (System.Text.RegularExpressions.Regex.IsMatch(clientName, "PM.VirtualDevice.")) { bool dataGotFailed = false; if (VirtualDeviceTable == null || !VirtualDeviceTable.ContainsKey(clientName)) { dataGotFailed = true; } else if (VirtualDeviceTable != null && VirtualDeviceTable.ContainsKey(clientName)) { string functionBlock = VirtualDeviceTable[clientName].Item1; List dataNamesInDb = VirtualDeviceTable[clientName].Item2; List> parameters = new List>(); foreach (var dName in dataNamesInDb) { if (!dbDatas.ContainsKey(dName)) { dataGotFailed = true; break; } else { parameters.Add(new KeyValuePair(dName, dbDatas[dName].RawData.ToArray())); } } if (!dataGotFailed) { var result = LuaFunction.CalcFunction(functionBlock, parameters, clientName); if (result == null) { dataGotFailed = true; } else { DataItem data = new DataItem() { DataName = clientName, TimeStamp = dbDatas.First().Value.TimeStamp, RawData = result.ToList() }; returnDataItems.Add(data); } } } if (dataGotFailed) { DataItem data = new DataItem() { DataName = clientName, TimeStamp = dbDatas.First().Value.TimeStamp }; data.RawData = new List(); int size = dbDatas.First().Value.TimeStamp.Count; for (int i = 0; i < size; i++) data.RawData.Add(0); returnDataItems.Add(data); } } else { if (dbDatas.ContainsKey(clientName)) returnDataItems.Add(dbDatas[clientName]); } } return true; } return false; } public void Dispose() { } public TimeSpan TimeMove { get; set; } private RecipeSyncPoint _syncPoint; public RecipeSyncPoint SyncPoint { get { return _syncPoint; } set { if (value != _syncPoint) { CommonViewModel.Instance.SyncSourceTime(Title, value.StepTime, value.StepName); _syncPoint = value; InvokePropertyChanged("SyncPoint"); } } } } }