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

254 line
8.4 KiB

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