终端一体化运控平台
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.
 
 
 

309 lines
11 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.IoT;
  3. using BPASmartClient.Message;
  4. using Microsoft.Toolkit.Mvvm.ComponentModel;
  5. using Microsoft.Toolkit.Mvvm.Input;
  6. using Microsoft.Win32;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Media;
  17. using System.Windows.Threading;
  18. namespace BPASmartClient.ViewModel
  19. {
  20. /// <summary>
  21. /// 日志界面
  22. /// </summary>
  23. public class LogViewModel : ObservableObject
  24. {
  25. public DispatcherTimer dispatcherTimer;
  26. public string ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  27. private ObservableCollection<LogModel> _LogModels;
  28. public ObservableCollection<LogModel> LogDataGrid
  29. {
  30. get
  31. {
  32. return _LogModels;
  33. }
  34. set
  35. {
  36. if (_LogModels == value)
  37. return;
  38. _LogModels = value;
  39. OnPropertyChanged("LogDataGrid");
  40. }
  41. }
  42. private volatile static LogViewModel _Instance;
  43. public static LogViewModel GetInstance() => _Instance ?? (_Instance = new LogViewModel());
  44. private LogViewModel()
  45. {
  46. logHelper.Fun_InitLog(System.AppDomain.CurrentDomain.BaseDirectory);
  47. if (LogDataGrid == null) LogDataGrid = new ObservableCollection<LogModel>();
  48. //一般日志
  49. MessageLog.GetInstance.InfoNotify = new Action<string>((s) =>
  50. {
  51. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  52. {
  53. LogDataGrid.Insert(0, new LogModel { message = s, type = "Info" });
  54. logHelper.GetLogConfigInstance().WriteLog(LogLevel.INFO, s);
  55. }));
  56. });
  57. //设备日志
  58. MessageLog.GetInstance.DeviceProcessLogNotify = new Action<string, string>((id, s) =>
  59. {
  60. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  61. {
  62. LogDataGrid.Insert(0, new LogModel { message = s, type = "DeviceLog" });
  63. logHelper.GetLogConfigInstance().WriteLog(LogLevel.DEBUG, s);
  64. }));
  65. });
  66. //设备告警日志
  67. MessageLog.GetInstance.DeviceAlarmLogNotify = new Action<string, string>((id, s) =>
  68. {
  69. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  70. {
  71. LogDataGrid.Insert(0, new LogModel { message = id, type = "DeviceAlarm" });
  72. logHelper.GetLogConfigInstance().WriteLog(LogLevel.WARN, id);
  73. }));
  74. });
  75. //错误日志
  76. MessageLog.GetInstance.ExInfoNotify = new Action<string>((s) =>
  77. {
  78. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  79. {
  80. LogDataGrid.Insert(0, new LogModel { message = s, type = "Error" });
  81. logHelper.GetLogConfigInstance().WriteLog(LogLevel.ERROR, s);
  82. DataVClient.GetInstance().HttpAddLog(new LogTable
  83. {
  84. ClientId = ClientId,
  85. LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  86. LogType = "严重",
  87. LogMessage = s,
  88. LogVla = "错误日志"
  89. });
  90. }));
  91. });
  92. ExcelCommand = new RelayCommand(() =>
  93. {
  94. ExcellOrder();
  95. });
  96. OpenCommand = new RelayCommand(() =>
  97. {
  98. string msg = string.Format("已找到日志文件,是否打开! \n 1.如选中不打开,则弹出日志文件夹目录。\n 2.打开,则弹出日志文件夹目录,并打开文件。");
  99. if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  100. {
  101. System.Diagnostics.Process.Start("Explorer", "/select," + logHelper.GetLogConfigInstance().directRollfileAppender.File);
  102. OpenFile(logHelper.GetLogConfigInstance().directRollfileAppender.File);
  103. }else
  104. System.Diagnostics.Process.Start("Explorer", "/select," + logHelper.GetLogConfigInstance().directRollfileAppender.File);
  105. });
  106. dispatcherTimer = new DispatcherTimer();
  107. dispatcherTimer.Tick += delegate
  108. {
  109. System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
  110. {
  111. if (LogDataGrid.Count > 100)
  112. {
  113. LogDataGrid.RemoveAt(LogDataGrid.Count - 1);
  114. }
  115. }));
  116. };
  117. dispatcherTimer.Interval = TimeSpan.FromSeconds(10);
  118. dispatcherTimer.Start();
  119. }
  120. /// <summary>
  121. /// 导出数据
  122. /// </summary>
  123. public void ExcellOrder()
  124. {
  125. if (LogDataGrid.Count > 0)
  126. {
  127. string text = "时间 类型 日志内容\n";
  128. LogDataGrid?.ToList().ForEach(temp =>
  129. {
  130. text = text + temp.time + " " + temp.type + " " + temp.message + "\n";
  131. });
  132. SaveFileDialog openfile = new SaveFileDialog();
  133. openfile.Filter = "Txt文件(*.txt)|*.txt";
  134. if (openfile.ShowDialog() == false)
  135. {
  136. return;
  137. }
  138. string path = openfile.FileName;
  139. if (!System.IO.File.Exists(path))
  140. {
  141. //没有则创建这个文件
  142. FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件
  143. StreamWriter sw = new StreamWriter(fs1);
  144. sw.WriteLine(text);//开始写入值
  145. sw.Close();
  146. fs1.Close();
  147. }
  148. else
  149. {
  150. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);
  151. StreamWriter sr = new StreamWriter(fs);
  152. sr.WriteLine(text);//开始写入值
  153. sr.Close();
  154. fs.Close();
  155. }
  156. string msg = string.Format("记录导出完成,共导出记录{0}条,是否打开!", LogDataGrid.Count);
  157. if (System.Windows.MessageBox.Show(msg, "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  158. {
  159. OpenFile(openfile.FileName);
  160. }
  161. }
  162. else
  163. System.Windows.MessageBox.Show("无数据!");
  164. }
  165. /// <summary>
  166. /// 打开指定路径下文件,比如:Word、Excel、Dll、图片等都可以(前提是你已经安装打开程序的对应软件)
  167. /// </summary>
  168. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  169. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  170. private void OpenFile(string NewFileName)
  171. {
  172. Process process = new Process();
  173. ProcessStartInfo processStartInfo = new ProcessStartInfo(NewFileName);
  174. process.StartInfo = processStartInfo;
  175. #region 下面这段被注释掉代码(可以用来全屏打开代码)
  176. //建立新的系统进程
  177. ////System.Diagnostics.Process process = new System.Diagnostics.Process();
  178. //设置文件名,此处为图片的真实路径 + 文件名(需要有后缀)
  179. ////process.StartInfo.FileName = NewFileName;
  180. //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
  181. ////process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
  182. // 此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
  183. process.StartInfo.UseShellExecute = true;
  184. #endregion
  185. try
  186. {
  187. process.Start();
  188. try
  189. {
  190. // process.WaitForExit();
  191. }
  192. catch (Exception ex)
  193. {
  194. throw ex;
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. throw ex;
  200. }
  201. finally
  202. {
  203. try
  204. {
  205. if (process != null)
  206. {
  207. process.Close();
  208. process = null;
  209. }
  210. }
  211. catch { }
  212. }
  213. }
  214. private bool _RealTimeModel = true;
  215. public bool RealTimeModel
  216. {
  217. get
  218. {
  219. return _RealTimeModel;
  220. }
  221. set
  222. {
  223. if (_RealTimeModel == value)
  224. return;
  225. _RealTimeModel = value;
  226. OnPropertyChanged("RealTimeModel");
  227. }
  228. }
  229. private bool _TimedClear = true;
  230. public bool TimedClear
  231. {
  232. get
  233. {
  234. return _TimedClear;
  235. }
  236. set
  237. {
  238. if (_TimedClear == value)
  239. return;
  240. _TimedClear = value;
  241. OnPropertyChanged("TimedClear");
  242. }
  243. }
  244. public RelayCommand ExcelCommand { get; set; }
  245. public RelayCommand OpenCommand { get; set; }
  246. }
  247. public class LogModel : ObservableObject
  248. {
  249. public string time { get; set; }
  250. private string _type;
  251. public string type
  252. {
  253. get
  254. {
  255. return _type;
  256. }
  257. set
  258. {
  259. if (_type == value)
  260. return;
  261. _type = value;
  262. if (_type == "Error") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032"));
  263. OnPropertyChanged("type");
  264. }
  265. }
  266. public string message { get; set; }
  267. private Brush _foreground;
  268. public Brush foreground
  269. {
  270. get
  271. {
  272. return _foreground;
  273. }
  274. set
  275. {
  276. if (_foreground == value)
  277. return;
  278. _foreground = value;
  279. OnPropertyChanged("foreground");
  280. }
  281. }
  282. public LogModel()
  283. {
  284. foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
  285. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  286. }
  287. }
  288. }