EventViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.WCF;
  4. using MECF.Framework.Common.DataCenter;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Media.Imaging;
  15. using Venus_MainPages.Unity;
  16. using Venus_MainPages.Views;
  17. namespace Venus_MainPages.ViewModels
  18. {
  19. public class EventViewModel : BindableBase
  20. {
  21. #region 私有字段
  22. //private int MenuPermission;
  23. #endregion
  24. #region 属性
  25. public bool SearchAlarmEvent { get; set; }
  26. public bool SearchWarningEvent { get; set; }
  27. public bool SearchInfoEvent { get; set; }
  28. public bool SearchOpeLog { get; set; }
  29. public bool SearchPMA { get; set; }
  30. public bool SearchPMB { get; set; }
  31. public bool SearchPMC { get; set; }
  32. public bool SearchPMD { get; set; }
  33. //public bool SearchCoolDown { get; set; }
  34. public bool SearchTM { get; set; }
  35. public bool SearchLL { get; set; }
  36. //public bool SearchBuf1 { get; set; }
  37. public bool SearchSystem { get; set; }
  38. public string SearchKeyWords { get; set; }
  39. private bool m_IsAuto;
  40. public bool IsAuto
  41. {
  42. get { return m_IsAuto; }
  43. set
  44. {
  45. if (value == true)
  46. {
  47. //EventClient.Instance.Start();
  48. GlobalEvents.Instance.EventItemRaiseChangedEvent += Instance_OnEvent;
  49. }
  50. else
  51. {
  52. GlobalEvents.Instance.EventItemRaiseChangedEvent -= Instance_OnEvent;
  53. }
  54. SetProperty(ref m_IsAuto, value);
  55. }
  56. }
  57. public DateTime SearchBeginTime { get; set; }
  58. public DateTime SearchEndTime { get; set; }
  59. public string Keywords { get; set; }
  60. public ObservableCollection<string> EventList { get; set; }
  61. public string SelectedEvent { get; set; }
  62. public ObservableCollection<string> UserList { get; set; }
  63. public string SelectedUser { get; set; }
  64. public ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem> SearchedResult { get; set; } = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  65. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  66. public Func<List<string>> QueryEventList { get; set; }
  67. #endregion
  68. #region 命令
  69. private DelegateCommand _SearchCommand;
  70. public DelegateCommand SearchCommand =>
  71. _SearchCommand ?? (_SearchCommand = new DelegateCommand(Search));
  72. private DelegateCommand _ExportCommand;
  73. public DelegateCommand ExportCommand =>
  74. _ExportCommand ?? (_ExportCommand = new DelegateCommand(Export));
  75. private DelegateCommand<object> _LoadCommand;
  76. public DelegateCommand<object> LoadCommand =>
  77. _LoadCommand ?? (_LoadCommand = new DelegateCommand<object>(OnLoad));
  78. #endregion
  79. public EventViewModel()
  80. {
  81. this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  82. this.QueryEventList = () =>
  83. {
  84. List<string> result = new List<string>();
  85. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  86. result.Add(eventName);
  87. return result;
  88. };
  89. var now = DateTime.Today;
  90. SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0);
  91. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  92. SelectedUser = "All";
  93. SearchKeyWords = string.Empty;
  94. SearchAlarmEvent = true;
  95. SearchWarningEvent = true;
  96. SearchInfoEvent = true;
  97. SearchOpeLog = false;
  98. SearchPMA = false;
  99. SearchPMB = false;
  100. SearchPMC = false;
  101. SearchPMD = false;
  102. //SearchCoolDown = false;
  103. SearchTM = false;
  104. SearchLL = false;
  105. //SearchBuf1 = false;
  106. SearchSystem = false;
  107. //EventClient.Instance.OnEvent += Instance_OnEvent;
  108. }
  109. private void Instance_OnEvent(EventItem eventItem)
  110. {
  111. switch (eventItem.Type)
  112. {
  113. case EventType.EventUI_Notify:
  114. Application.Current.Dispatcher.Invoke(delegate
  115. {
  116. SearchedResult.Insert(0,new Aitex.Core.UI.View.Common.SystemLogItem()
  117. {
  118. ID=eventItem.Id.ToString(),
  119. LogType=eventItem.Level.ToString(),
  120. Time=eventItem.OccuringTime.ToString(),
  121. TargetChamber=eventItem.Source,
  122. Detail=eventItem.Description,
  123. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", eventItem.Level.ToString()), UriKind.Absolute))
  124. }
  125. );
  126. });
  127. break;
  128. case EventType.Dialog_Nofity:
  129. //PopDialog(obj);
  130. break;
  131. case EventType.KickOut_Notify:
  132. break;
  133. case EventType.Sound_Notify:
  134. break;
  135. case EventType.UIMessage_Notify:
  136. //PopUIMessage(obj);
  137. break;
  138. }
  139. }
  140. #region 方法
  141. private void OnLoad(Object eventView)
  142. {
  143. this.view = (EventView)eventView;
  144. this.view.wfTimeFrom.Value = this.SearchBeginTime;
  145. this.view.wfTimeTo.Value = this.SearchEndTime;
  146. this.Preload();
  147. }
  148. /// <summary>
  149. /// 预先载入数据
  150. /// </summary>
  151. public void Preload()
  152. {
  153. //初始化所有的枚举事件类型
  154. EventList = new ObservableCollection<string>();
  155. EventList.Add("All");
  156. if (QueryEventList != null)
  157. {
  158. List<string> evList = QueryEventList();
  159. foreach (string ev in evList)
  160. EventList.Add(ev);
  161. }
  162. SelectedEvent = "All";
  163. //初始化所有的用户
  164. //PreloadUsers();
  165. //所有属性更新
  166. //NotifyOfPropertyChange();
  167. //第一次默认查询
  168. //if (SearchedResult == null)
  169. //Search();
  170. }
  171. /// <summary>
  172. /// 导出
  173. /// </summary>
  174. public void Export()
  175. {
  176. try
  177. {
  178. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  179. dlg.DefaultExt = ".xls"; // Default file extension
  180. dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // Filter files by extension
  181. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  182. if (result == true) // Process open file dialog box results
  183. {
  184. System.Data.DataSet ds = new System.Data.DataSet();
  185. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  186. ds.Tables[0].Columns.Add("类型");
  187. ds.Tables[0].Columns.Add("时间");
  188. ds.Tables[0].Columns.Add("腔体");
  189. ds.Tables[0].Columns.Add("发起源");
  190. ds.Tables[0].Columns.Add("内容描述");
  191. foreach (var item in SearchedResult)
  192. {
  193. var row = ds.Tables[0].NewRow();
  194. row[0] = item.LogType;
  195. row[1] = item.Time;
  196. row[2] = item.TargetChamber;
  197. row[3] = item.Initiator;
  198. row[4] = item.Detail;
  199. ds.Tables[0].Rows.Add(row);
  200. }
  201. ds.WriteXml(dlg.FileName);
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. //LOG.Write(ex);
  207. LOG.WriteExeption(ex);
  208. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  209. }
  210. }
  211. private string GetSourceWhere()
  212. {
  213. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  214. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  215. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  216. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  217. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  218. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  219. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  220. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  221. return "";
  222. }
  223. /*
  224. *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  225. event_id integer,
  226. event_enum text,
  227. type text,
  228. source text,
  229. description text,
  230. level text,
  231. occur_time timestamp without time zone,
  232. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  233. */
  234. /// <summary>
  235. /// 查询
  236. /// </summary>
  237. public void Search()
  238. {
  239. Task.Factory.StartNew(() =>
  240. {
  241. try
  242. {
  243. this.SearchBeginTime = this.view.wfTimeFrom.Value;
  244. this.SearchEndTime = this.view.wfTimeTo.Value;
  245. string sqlEvent = "";
  246. string sqlOperationLog = "";
  247. string sql = "";
  248. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  249. {
  250. sqlEvent = string.Format("SELECT \"event_id\", \"event_enum\", \"type\", \"occur_time\", \"level\",\"source\" , \"description\" FROM \"event_data\" where \"occur_time\" >='{0}' and \"occur_time\" <='{1}' ", SearchBeginTime.ToString("yyyyMMdd HHmmss"), SearchEndTime.ToString("yyyyMMdd HHmmss"));
  251. sqlEvent += GetSourceWhere();
  252. sqlEvent += " and (FALSE ";
  253. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  254. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  255. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  256. sqlEvent += " ) ";
  257. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  258. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  259. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' or lower(\"type\") like '%{1}%'", SearchKeyWords.ToLower(), SearchKeyWords.ToLower());
  260. }
  261. if (SearchOpeLog)
  262. {
  263. //sqlOperationLog = string.Format(" SELECT \"UserName\" as \"Initiator\", 'UserOperation' as \"LogType\", \"Time\", \"ChamberId\" as \"TargetChamber\", \"Content\" as \"Description\" FROM \"OperationLog\" where \"Time\" >='{0}' and \"Time\" <='{1}' ", SearchBeginTime.ToString("yyyy/MM/dd HH:mm:ss"), SearchEndTime.ToString("yyyy/MM/dd HH:mm:ss"));
  264. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  265. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  266. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  267. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  268. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  269. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  270. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  271. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  272. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  273. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  274. }
  275. sql = sqlEvent;
  276. if (!string.IsNullOrEmpty(sqlOperationLog))
  277. {
  278. if (string.IsNullOrEmpty(sql))
  279. {
  280. sql = sqlOperationLog;
  281. }
  282. else
  283. {
  284. sql += " UNION ALL " + sqlOperationLog;
  285. }
  286. }
  287. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  288. {
  289. sql += " order by \"occur_time\" DESC limit 2000;";
  290. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  291. if (lstEvent == null)
  292. return;
  293. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  294. {
  295. string logTypeStr;
  296. foreach (EventItem ev in lstEvent)
  297. {
  298. switch (ev.Level)
  299. {
  300. case EventLevel.Information: logTypeStr = "Info"; break;
  301. case EventLevel.Warning: logTypeStr = "Warning"; break;
  302. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  303. default: logTypeStr = "Undefine"; break;
  304. }
  305. SearchedResult.Add(new Aitex.Core.UI.View.Common.SystemLogItem()
  306. {
  307. ID = ev.Id.ToString() ,
  308. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  309. LogType = logTypeStr,
  310. Detail = ev.Description,
  311. TargetChamber = ev.Source,
  312. Initiator = "",
  313. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  314. }); ;
  315. }
  316. RaisePropertyChanged("SearchedResult");
  317. if (SearchedResult.Count >= 2000)
  318. {
  319. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  320. }
  321. }));
  322. }
  323. else
  324. {
  325. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  326. {
  327. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  328. RaisePropertyChanged("SearchedResult");
  329. }));
  330. }
  331. }
  332. catch (Exception ex)
  333. {
  334. //LOG.Write(ex);
  335. LOG.WriteExeption(ex);
  336. }
  337. });
  338. }
  339. private EventView view;
  340. #endregion
  341. }
  342. public class SystemLogItem
  343. {
  344. /// <summary>
  345. /// 时间
  346. /// </summary>
  347. public string Time { get; set; }
  348. /// <summary>
  349. /// ICON
  350. /// </summary>
  351. public object Icon { get; set; }
  352. /// <summary>
  353. /// 类型:操作日志|事件|其他
  354. /// </summary>
  355. public string LogType { get; set; }
  356. /// <summary>
  357. /// 针对腔体
  358. /// </summary>
  359. public string TargetChamber { get; set; }
  360. /// <summary>
  361. /// 发起方
  362. /// </summary>
  363. public string Initiator { get; set; }
  364. /// <summary>
  365. /// 详情
  366. /// </summary>
  367. public string Detail { get; set; }
  368. }
  369. }