终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

409 regels
15 KiB

  1. using BPASmartClient.Business;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.IoT;
  4. using BPASmartClient.Message;
  5. using BPASmartClient.Model;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using Microsoft.Toolkit.Mvvm.Input;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  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;
  20. using System.Windows.Threading;
  21. namespace BPASmartClient.ViewModel
  22. {
  23. /// <summary>
  24. /// 日志界面
  25. /// </summary>
  26. public class LogViewModel : ObservableObject
  27. {
  28. #region 变量
  29. //定时上报文件到阿里云
  30. public DispatcherTimer UpDataFileTimer;
  31. //定时清除界面日志
  32. public DispatcherTimer dispatcherTimer;
  33. //public string ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  34. public ObservableCollection<LogModel> LogDataGridData { get; set; }
  35. private ObservableCollection<LogModel> _LogModels; public ObservableCollection<LogModel> LogDataGrid
  36. {
  37. get
  38. {
  39. return _LogModels;
  40. }
  41. set
  42. {
  43. if (_LogModels == value)
  44. return;
  45. _LogModels = value;
  46. OnPropertyChanged("LogDataGrid");
  47. }
  48. }
  49. private bool _RealTimeModel = true; public bool RealTimeModel
  50. {
  51. get
  52. {
  53. return _RealTimeModel;
  54. }
  55. set
  56. {
  57. if (_RealTimeModel == value)
  58. return;
  59. _RealTimeModel = value;
  60. OnPropertyChanged("RealTimeModel");
  61. }
  62. }
  63. private bool _TimedClear = true; public bool TimedClear
  64. {
  65. get
  66. {
  67. return _TimedClear;
  68. }
  69. set
  70. {
  71. if (_TimedClear == value)
  72. return;
  73. _TimedClear = value;
  74. OnPropertyChanged("TimedClear");
  75. }
  76. }
  77. private string _SelectedText = "";
  78. public string SelectedText
  79. {
  80. get
  81. {
  82. return _SelectedText;
  83. }
  84. set
  85. {
  86. if (_SelectedText == value)
  87. return;
  88. _SelectedText = value;
  89. OnPropertyChanged("SelectedText");
  90. }
  91. }
  92. public ObservableCollection<BookEx> SelectBookExs { set; get; }
  93. private ObservableCollection<BookEx> _books;
  94. public ObservableCollection<BookEx> BookExs
  95. {
  96. get
  97. {
  98. if (_books == null)
  99. {
  100. _books = new ObservableCollection<BookEx>();
  101. _books.CollectionChanged += (sender, e) =>
  102. {
  103. if (e.OldItems != null)
  104. {
  105. foreach (BookEx bookEx in e.OldItems)
  106. {
  107. bookEx.PropertyChanged -= ItemPropertyChanged;
  108. }
  109. }
  110. if (e.NewItems != null)
  111. {
  112. foreach (BookEx bookEx in e.NewItems)
  113. {
  114. bookEx.PropertyChanged += ItemPropertyChanged;
  115. }
  116. }
  117. };
  118. }
  119. return _books;
  120. }
  121. }
  122. #endregion
  123. #region 单一
  124. private volatile static LogViewModel _Instance;
  125. public static LogViewModel GetInstance() => _Instance ?? (_Instance = new LogViewModel());
  126. private LogViewModel()
  127. {
  128. Init();
  129. }
  130. #endregion
  131. #region 函数
  132. /// <summary>
  133. /// 初始化
  134. /// </summary>
  135. public void Init()
  136. {
  137. logHelper.Fun_InitLog(System.AppDomain.CurrentDomain.BaseDirectory);
  138. if (LogDataGrid == null) LogDataGrid = new ObservableCollection<LogModel>();
  139. if (LogDataGridData == null) LogDataGridData = new ObservableCollection<LogModel>();
  140. BookExs.Add(new BookEx(new Book() { Name = "一般日志", Tag = "Info" }) { IsChecked = true });
  141. BookExs.Add(new BookEx(new Book() { Name = "设备日志", Tag = "DeviceLog" }) { IsChecked = true });
  142. BookExs.Add(new BookEx(new Book() { Name = "错误日志", Tag = "Error" }) { IsChecked = true });
  143. BookExs.Add(new BookEx(new Book() { Name = "告警日志", Tag = "DeviceAlarm" }) { IsChecked = true });
  144. SelectBookExs = new ObservableCollection<BookEx>();
  145. ItemPropertyChanged(new BookEx(new Book()) { IsChecked = true }, new PropertyChangedEventArgs("IsChecked"));
  146. //一般日志
  147. MessageLog.GetInstance.InfoNotify = new Action<string>((s) =>
  148. {
  149. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  150. {
  151. LogModel logModel = new LogModel { message = s, type = "Info" };
  152. LogDataGridData.Insert(0, logModel);
  153. AddLog(logModel);
  154. logHelper.GetLogConfigInstance().WriteLog(LogLevel.INFO, s);
  155. }));
  156. });
  157. //设备日志
  158. MessageLog.GetInstance.DeviceProcessLogNotify = new Action<string, string>((id, s) =>
  159. {
  160. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  161. {
  162. LogModel logModel = new LogModel { message = s, type = "DeviceLog" };
  163. LogDataGridData.Insert(0, logModel);
  164. AddLog(logModel);
  165. logHelper.GetLogConfigInstance().WriteLog(LogLevel.DEBUG, s);
  166. }));
  167. });
  168. //设备告警日志
  169. MessageLog.GetInstance.DeviceAlarmLogNotify = new Action<string, string>((id, s) =>
  170. {
  171. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  172. {
  173. LogModel logModel = new LogModel { message = id, type = "DeviceAlarm" };
  174. LogDataGridData.Insert(0, logModel);
  175. AddLog(logModel);
  176. logHelper.GetLogConfigInstance().WriteLog(LogLevel.WARN, id);
  177. }));
  178. });
  179. //错误日志
  180. MessageLog.GetInstance.ExInfoNotify = new Action<string>((s) =>
  181. {
  182. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  183. {
  184. LogModel logModel = new LogModel { message = s, type = "Error" };
  185. LogDataGridData.Insert(0, logModel);
  186. AddLog(logModel);
  187. logHelper.GetLogConfigInstance().WriteLog(LogLevel.ERROR, s);
  188. DataVClient.GetInstance().HttpAddLog(new LogTable
  189. {
  190. ClientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId.ToString(),
  191. LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  192. LogType = "严重",
  193. LogMessage = s,
  194. LogVla = "错误日志"
  195. });
  196. }));
  197. });
  198. ExcelCommand = new RelayCommand(() =>
  199. {
  200. ExcellOrder();
  201. });
  202. OpenCommand = new RelayCommand(() =>
  203. {
  204. string msg = string.Format("已找到日志文件,是否打开! \n 1.如选中不打开,则弹出日志文件夹目录。\n 2.打开,则弹出日志文件夹目录,并打开文件。");
  205. if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  206. {
  207. System.Diagnostics.Process.Start("Explorer", "/select," + logHelper.GetLogConfigInstance().directRollfileAppender.File);
  208. logHelper.GetLogConfigInstance().OpenFile(logHelper.GetLogConfigInstance().directRollfileAppender.File);
  209. }
  210. else
  211. System.Diagnostics.Process.Start("Explorer", "/select," + logHelper.GetLogConfigInstance().directRollfileAppender.File);
  212. });
  213. dispatcherTimer = new DispatcherTimer();
  214. dispatcherTimer.Tick += delegate
  215. {
  216. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  217. {
  218. if (LogDataGridData.Count > 200)
  219. {
  220. LogModel logModel = LogDataGridData.Last();
  221. DeleteLog(logModel);
  222. LogDataGridData.Remove(logModel);
  223. }
  224. }));
  225. };
  226. dispatcherTimer.Interval = TimeSpan.FromSeconds(10);
  227. dispatcherTimer.Start();
  228. UpDataFileTimer = new DispatcherTimer();
  229. UpDataFileTimer.Tick += delegate
  230. {
  231. DataVClient.GetInstance().UpDataFile();
  232. };
  233. UpDataFileTimer.Interval = TimeSpan.FromHours(0.5);
  234. UpDataFileTimer.Start();
  235. }
  236. /// <summary>
  237. /// 增加日志
  238. /// </summary>
  239. public void AddLog(LogModel logModel)
  240. {
  241. BookEx book = SelectBookExs?.ToList().Find(par => par.IsChecked && par.BookN.Tag == logModel.type);
  242. if (book != null)
  243. {
  244. LogDataGrid.Insert(0, logModel);
  245. }
  246. }
  247. /// <summary>
  248. /// 刷新日志
  249. /// </summary>
  250. /// <param name="logModel"></param>
  251. public void RefreshLog()
  252. {
  253. LogDataGrid.Clear();
  254. LogDataGridData?.ToList().ForEach(b =>
  255. {
  256. BookEx book = SelectBookExs?.ToList().Find(par => par.IsChecked && par.BookN.Tag == b.type);
  257. if (book != null)
  258. {
  259. LogDataGrid.Add(b);
  260. }
  261. });
  262. }
  263. /// <summary>
  264. /// 删除日志
  265. /// </summary>
  266. public void DeleteLog(LogModel logModel)
  267. {
  268. LogModel log = LogDataGrid?.ToList().Find(par => par == logModel);
  269. if (log != null) LogDataGrid.Remove(log);
  270. }
  271. /// <summary>
  272. /// 导出数据
  273. /// </summary>
  274. public void ExcellOrder()
  275. {
  276. if (LogDataGrid.Count > 0)
  277. {
  278. string text = "时间 类型 日志内容\n";
  279. LogDataGrid?.ToList().ForEach(temp =>
  280. {
  281. text = text + temp.time + " " + temp.type + " " + temp.message + "\n";
  282. });
  283. SaveFileDialog openfile = new SaveFileDialog();
  284. openfile.Filter = "Txt文件(*.txt)|*.txt";
  285. if (openfile.ShowDialog() == false)
  286. {
  287. return;
  288. }
  289. string path = openfile.FileName;
  290. if (!System.IO.File.Exists(path))
  291. {
  292. //没有则创建这个文件
  293. FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件
  294. StreamWriter sw = new StreamWriter(fs1);
  295. sw.WriteLine(text);//开始写入值
  296. sw.Close();
  297. fs1.Close();
  298. }
  299. else
  300. {
  301. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
  302. StreamWriter sr = new StreamWriter(fs);
  303. sr.WriteLine(text);//开始写入值
  304. sr.Close();
  305. fs.Close();
  306. }
  307. string msg = string.Format("记录导出完成,共导出记录{0}条,是否打开!", LogDataGrid.Count);
  308. if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  309. {
  310. logHelper.GetLogConfigInstance().OpenFile(openfile.FileName);
  311. }
  312. }
  313. else
  314. System.Windows.MessageBox.Show("无数据!");
  315. }
  316. /// <summary>
  317. /// 选中改变
  318. /// </summary>
  319. /// <param name="sender"></param>
  320. /// <param name="e"></param>
  321. private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
  322. {
  323. if (e.PropertyName == "IsChecked")
  324. {
  325. BookEx bookEx = sender as BookEx;
  326. if (bookEx != null)
  327. {
  328. IEnumerable<BookEx> bookExs = BookExs.Where(b => b.IsChecked == true);
  329. StringBuilder builder = new StringBuilder();
  330. SelectBookExs.Clear();
  331. foreach (BookEx item in bookExs)
  332. {
  333. builder.Append(item.BookN.Name.Replace("日志", "") + ",");
  334. SelectBookExs.Add((BookEx)item);
  335. }
  336. SelectedText = builder == null ? string.Empty : builder.ToString();
  337. RefreshLog();
  338. }
  339. }
  340. }
  341. #endregion
  342. #region Command
  343. public RelayCommand ExcelCommand { get; set; }
  344. public RelayCommand OpenCommand { get; set; }
  345. #endregion
  346. }
  347. public class LogModel : ObservableObject
  348. {
  349. public string time { get; set; }
  350. private string _type;
  351. public string type
  352. {
  353. get
  354. {
  355. return _type;
  356. }
  357. set
  358. {
  359. if (_type == value)
  360. return;
  361. _type = value;
  362. if (_type == "Error" || _type == "Error".ToUpper() || _type == "DeviceAlarm".ToUpper() || _type == "DeviceAlarm") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032"));
  363. OnPropertyChanged("type");
  364. OnPropertyChanged("foreground");
  365. }
  366. }
  367. public string message { get; set; }
  368. private Brush _foreground;
  369. public Brush foreground
  370. {
  371. get
  372. {
  373. return _foreground;
  374. }
  375. set
  376. {
  377. if (_foreground == value)
  378. return;
  379. _foreground = value;
  380. OnPropertyChanged("foreground");
  381. }
  382. }
  383. public LogModel()
  384. {
  385. foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
  386. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  387. }
  388. }
  389. }