终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

263 linhas
8.7 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. //dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };//1秒一流转
  74. //dispatcherTimer.Tick += delegate
  75. //{
  76. // if (TimedClear)
  77. // {
  78. // }
  79. //};
  80. //dispatcherTimer.Start();
  81. }
  82. /// <summary>
  83. /// 导出数据
  84. /// </summary>
  85. public void ExcellOrder()
  86. {
  87. if (LogDataGrid.Count > 0)
  88. {
  89. string text = "时间 类型 日志内容\n";
  90. LogDataGrid?.ToList().ForEach(temp =>
  91. {
  92. text = text + temp.time + " " + temp.type + " " + temp.message+ "\n";
  93. });
  94. SaveFileDialog openfile = new SaveFileDialog();
  95. openfile.Filter = "Txt文件(*.txt)|*.txt";
  96. if (openfile.ShowDialog() == false)
  97. {
  98. return;
  99. }
  100. string path = openfile.FileName;
  101. if (!System.IO.File.Exists(path))
  102. {
  103. //没有则创建这个文件
  104. FileStream fs1 = new FileStream(path,FileMode.Create,FileAccess.Write);//创建写入文件
  105. StreamWriter sw = new StreamWriter(fs1);
  106. sw.WriteLine(text);//开始写入值
  107. sw.Close();
  108. fs1.Close();
  109. }
  110. else
  111. {
  112. FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Write);
  113. StreamWriter sr = new StreamWriter(fs);
  114. sr.WriteLine(text);//开始写入值
  115. sr.Close();
  116. fs.Close();
  117. }
  118. string msg = string.Format("记录导出完成,共导出记录{0}条,是否打开!",LogDataGrid.Count);
  119. if (System.Windows.MessageBox.Show(msg,"提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  120. {
  121. OpenFile(openfile.FileName);
  122. }
  123. }
  124. else
  125. System.Windows.MessageBox.Show("无数据!");
  126. }
  127. /// <summary>
  128. /// 打开指定路径下文件,比如:Word、Excel、Dll、图片等都可以(前提是你已经安装打开程序的对应软件)
  129. /// </summary>
  130. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  131. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  132. private void OpenFile(string NewFileName)
  133. {
  134. Process process = new Process();
  135. ProcessStartInfo processStartInfo = new ProcessStartInfo(NewFileName);
  136. process.StartInfo = processStartInfo;
  137. #region 下面这段被注释掉代码(可以用来全屏打开代码)
  138. //建立新的系统进程
  139. ////System.Diagnostics.Process process = new System.Diagnostics.Process();
  140. //设置文件名,此处为图片的真实路径 + 文件名(需要有后缀)
  141. ////process.StartInfo.FileName = NewFileName;
  142. //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
  143. ////process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
  144. // 此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
  145. process.StartInfo.UseShellExecute = true;
  146. #endregion
  147. try
  148. {
  149. process.Start();
  150. try
  151. {
  152. // process.WaitForExit();
  153. }
  154. catch (Exception ex)
  155. {
  156. throw ex;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. throw ex;
  162. }
  163. finally
  164. {
  165. try
  166. {
  167. if (process != null)
  168. {
  169. process.Close();
  170. process = null;
  171. }
  172. }
  173. catch { }
  174. }
  175. }
  176. private bool _RealTimeModel = true;
  177. public bool RealTimeModel
  178. {
  179. get
  180. {
  181. return _RealTimeModel;
  182. }
  183. set
  184. {
  185. if (_RealTimeModel == value)
  186. return;
  187. _RealTimeModel = value;
  188. OnPropertyChanged("RealTimeModel");
  189. }
  190. }
  191. private bool _TimedClear = true;
  192. public bool TimedClear
  193. {
  194. get
  195. {
  196. return _TimedClear;
  197. }
  198. set
  199. {
  200. if (_TimedClear == value)
  201. return;
  202. _TimedClear = value;
  203. OnPropertyChanged("TimedClear");
  204. }
  205. }
  206. public RelayCommand ExcelCommand { get; set; }
  207. }
  208. public class LogModel :ObservableObject
  209. {
  210. public string time { get; set; }
  211. private string _type;
  212. public string type
  213. {
  214. get
  215. {
  216. return _type;
  217. }
  218. set
  219. {
  220. if (_type == value)
  221. return;
  222. _type = value;
  223. if(_type== "Error") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032"));
  224. OnPropertyChanged("type");
  225. }
  226. }
  227. public string message { get; set; }
  228. private Brush _foreground;
  229. public Brush foreground
  230. {
  231. get
  232. {
  233. return _foreground;
  234. }
  235. set
  236. {
  237. if (_foreground == value)
  238. return;
  239. _foreground = value;
  240. OnPropertyChanged("foreground");
  241. }
  242. }
  243. public LogModel()
  244. {
  245. foreground=new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
  246. time =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  247. }
  248. }
  249. }