123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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<string, Tuple<string, List<string>>>();
- }
- public Dictionary<string, DataItem> 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<RecipeSyncPoint> RecipeSteps { get; set; }
- public Dictionary<string/*Virtual Device Name*/, Tuple<string/*Function Definition*/, List<string>/*var list in db*/>> VirtualDeviceTable { get; set; }
- public bool GetData(string dataName, DateTime beginTime, DateTime endTime, out DataItem returnDataItem)
- {
- returnDataItem = null;
- List<string> dataIdList = new List<string>();
- dataIdList.Add(dataName);
- Dictionary<string, DataItem> dbDatas;
- bool isSucc = GetDbData(ChamberName, beginTime, endTime, dataIdList, out dbDatas);
- if (isSucc && dbDatas.ContainsKey(dataName))
- returnDataItem = dbDatas[dataName];
- return isSucc;
- }
- public bool GetData(List<string> dataNames, DateTime beginTime, DateTime endTime, out List<DataItem> returnDataItems)
- {
- returnDataItems = new List<DataItem>();
- //convert virtual data here
- Dictionary<string, DataItem> dbDatas;
- List<string> rawDataNames = new List<string>();
- 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<string> dataNamesInDb = VirtualDeviceTable[clientName].Item2;
- List<KeyValuePair<string, float[]>> parameters = new List<KeyValuePair<string, float[]>>();
- foreach (var dName in dataNamesInDb)
- {
- if (!dbDatas.ContainsKey(dName))
- {
- dataGotFailed = true;
- break;
- }
- else
- {
- parameters.Add(new KeyValuePair<string, float[]>(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<float>();
- 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");
- }
- }
- }
- }
- }
|