PostgreSqlDataSource.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Npgsql;
  6. using System.Threading;
  7. using Aitex.UI.Charting.ViewModel;
  8. namespace Aitex.UI.Charting.Model
  9. {
  10. public class PostgreSqlDataSource : ChartingBaseViewModel, IDataSource
  11. {
  12. public PostgreSqlDataSource()
  13. {
  14. VirtualDeviceTable = new Dictionary<string, Tuple<string, List<string>>>();
  15. }
  16. public Dictionary<string, DataItem> Datas { get; set; }
  17. public string Title { get; set; }
  18. public string ChamberName { get; set; }
  19. public string Description { get; set; }
  20. public DateTime BeginTime { get; set; }
  21. public DateTime EndTime { get; set; }
  22. public List<RecipeSyncPoint> RecipeSteps { get; set; }
  23. public Dictionary<string/*Virtual Device Name*/, Tuple<string/*Function Definition*/, List<string>/*var list in db*/>> VirtualDeviceTable { get; set; }
  24. public bool GetData(string dataName, DateTime beginTime, DateTime endTime, out DataItem returnDataItem)
  25. {
  26. returnDataItem = null;
  27. List<string> dataIdList = new List<string>();
  28. dataIdList.Add(dataName);
  29. Dictionary<string, DataItem> dbDatas;
  30. bool isSucc = GetDbData(ChamberName, beginTime, endTime, dataIdList, out dbDatas);
  31. if (isSucc && dbDatas.ContainsKey(dataName))
  32. returnDataItem = dbDatas[dataName];
  33. return isSucc;
  34. }
  35. public bool GetData(List<string> dataNames, DateTime beginTime, DateTime endTime, out List<DataItem> returnDataItems)
  36. {
  37. returnDataItems = new List<DataItem>();
  38. //convert virtual data here
  39. Dictionary<string, DataItem> dbDatas;
  40. List<string> rawDataNames = new List<string>();
  41. foreach (var item in dataNames)
  42. {
  43. if (System.Text.RegularExpressions.Regex.IsMatch(item, "PM.VirtualDevice."))
  44. {
  45. if (VirtualDeviceTable != null && VirtualDeviceTable.ContainsKey(item))
  46. {
  47. var dataNamesInVirtualDevice = VirtualDeviceTable[item].Item2;
  48. foreach (var item2 in dataNamesInVirtualDevice)
  49. {
  50. if (!rawDataNames.Contains(item2) && Datas.ContainsKey(item2))
  51. rawDataNames.Add(item2);
  52. }
  53. }
  54. }
  55. else
  56. {
  57. if (!rawDataNames.Contains(item))
  58. rawDataNames.Add(item);
  59. }
  60. }
  61. if (rawDataNames.Count == 0)
  62. return false;
  63. var isSucc = GetDbData(ChamberName, beginTime, endTime, rawDataNames, out dbDatas);
  64. if (isSucc)
  65. {
  66. foreach (var clientName in dataNames)
  67. {
  68. if (System.Text.RegularExpressions.Regex.IsMatch(clientName, "PM.VirtualDevice."))
  69. {
  70. bool dataGotFailed = false;
  71. if (VirtualDeviceTable == null || !VirtualDeviceTable.ContainsKey(clientName))
  72. {
  73. dataGotFailed = true;
  74. }
  75. else if (VirtualDeviceTable != null && VirtualDeviceTable.ContainsKey(clientName))
  76. {
  77. string functionBlock = VirtualDeviceTable[clientName].Item1;
  78. List<string> dataNamesInDb = VirtualDeviceTable[clientName].Item2;
  79. List<KeyValuePair<string, float[]>> parameters = new List<KeyValuePair<string, float[]>>();
  80. foreach (var dName in dataNamesInDb)
  81. {
  82. if (!dbDatas.ContainsKey(dName))
  83. {
  84. dataGotFailed = true;
  85. break;
  86. }
  87. else
  88. {
  89. parameters.Add(new KeyValuePair<string, float[]>(dName, dbDatas[dName].RawData.ToArray()));
  90. }
  91. }
  92. if (!dataGotFailed)
  93. {
  94. var result = LuaFunction.CalcFunction(functionBlock, parameters, clientName);
  95. if (result == null)
  96. {
  97. dataGotFailed = true;
  98. }
  99. else
  100. {
  101. DataItem data = new DataItem() { DataName = clientName, TimeStamp = dbDatas.First().Value.TimeStamp, RawData = result.ToList() };
  102. returnDataItems.Add(data);
  103. }
  104. }
  105. }
  106. if (dataGotFailed)
  107. {
  108. DataItem data = new DataItem() { DataName = clientName, TimeStamp = dbDatas.First().Value.TimeStamp };
  109. data.RawData = new List<float>();
  110. int size = dbDatas.First().Value.TimeStamp.Count;
  111. for (int i = 0; i < size; i++)
  112. data.RawData.Add(0);
  113. returnDataItems.Add(data);
  114. }
  115. }
  116. else
  117. {
  118. if (dbDatas.ContainsKey(clientName))
  119. returnDataItems.Add(dbDatas[clientName]);
  120. }
  121. }
  122. return true;
  123. }
  124. return false;
  125. }
  126. public void Dispose()
  127. {
  128. }
  129. public TimeSpan TimeMove { get; set; }
  130. private RecipeSyncPoint _syncPoint;
  131. public RecipeSyncPoint SyncPoint
  132. {
  133. get
  134. {
  135. return _syncPoint;
  136. }
  137. set
  138. {
  139. if (value != _syncPoint)
  140. {
  141. CommonViewModel.Instance.SyncSourceTime(Title, value.StepTime, value.StepName);
  142. _syncPoint = value;
  143. InvokePropertyChanged("SyncPoint");
  144. }
  145. }
  146. }
  147. }
  148. }