EventViewModel.cs 17 KB

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