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

310 lines
11 KiB

  1. using BPA.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Collections.ObjectModel;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using BPASmartClient.Model;
  10. using System.Windows.Media;
  11. using BPASmartClient.CustomResource.Pages.View;
  12. using BPASmartClient.CustomResource.UserControls;
  13. namespace BPASmartClient.CustomResource.Pages.Model
  14. {
  15. public class MessageNotify
  16. {
  17. private volatile static MessageNotify _Instance;
  18. public static MessageNotify GetInstance => _Instance ??= new MessageNotify();
  19. public static readonly object runLock = new object();
  20. public static readonly object userlock = new object();
  21. public static readonly object alarmlock = new object();
  22. public static readonly object recipeLogslock = new object();
  23. private MessageNotify() { }
  24. public Action<string> UserLog { get; set; }
  25. public Action<string> RunLog { get; set; }
  26. public Action<string> AlarmLog { get; set; }
  27. public Action<string> RecipeCompleteLogs { get; set; }
  28. public ObservableCollection<RunLog> runLogs { get; set; } = new();
  29. public ObservableCollection<UserLog> userLogs { get; set; } = new();
  30. public ObservableCollection<BPASmartClient.Model.Alarm> alarmLogs { get; set; } = new();
  31. public ObservableCollection<RecipeCompleteLog> recipeLogs { get; set; } = new();
  32. public void LogSave()
  33. {
  34. try
  35. {
  36. Sqlite<UserLog>.GetInstance.Save();
  37. Sqlite<RunLog>.GetInstance.Save();
  38. //Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Save();
  39. Sqlite<RecipeCompleteLog>.GetInstance.Save();
  40. }
  41. catch (Exception)
  42. {
  43. // throw;
  44. }
  45. }
  46. public void ShowRecipeLog(string info)
  47. {
  48. lock (recipeLogslock)
  49. {
  50. try
  51. {
  52. RecipeCompleteLog runLog = new RecipeCompleteLog()
  53. {
  54. Date = DateTime.Now.ToString("yyyy-MM-dd"),
  55. Time = DateTime.Now.ToString("HH:mm:ss"),
  56. RecipeName = info
  57. };
  58. Sqlite<RecipeCompleteLog>.GetInstance.Base.Add(runLog);
  59. Application.Current.Dispatcher.Invoke(new Action(() => { recipeLogs.Insert(0, runLog); }));
  60. RecipeCompleteLogs?.Invoke(info);
  61. }
  62. catch (Exception)
  63. {
  64. // throw;
  65. }
  66. }
  67. }
  68. public void ShowUserLog(string info)
  69. {
  70. lock (userlock)
  71. {
  72. if (!string.IsNullOrEmpty(Global.userInfo.UserName) && !string.IsNullOrEmpty(Global.userInfo.permission.ToString()) && !string.IsNullOrEmpty(info))
  73. {
  74. UserLog userLog = new UserLog()
  75. {
  76. Date = DateTime.Now.ToString("yyyy-MM-dd"),
  77. Time = DateTime.Now.ToString("HH:mm:ss"),
  78. Permission = Global.userInfo.permission.ToString(),
  79. UserName = Global.userInfo.UserName,
  80. LogInfo = info
  81. };
  82. Sqlite<UserLog>.GetInstance.Base.Add(userLog);
  83. Application.Current.Dispatcher.Invoke(new Action(() => { userLogs.Insert(0, userLog); }));
  84. UserLog?.Invoke(info);
  85. }
  86. }
  87. }
  88. public void ShowRunLog(string info)
  89. {
  90. lock (runLock)
  91. {
  92. try
  93. {
  94. RunLog runLog = new RunLog()
  95. {
  96. Date = DateTime.Now.ToString("yyyy-MM-dd"),
  97. Time = DateTime.Now.ToString("HH:mm:ss"),
  98. RunLogInfo = info
  99. };
  100. Sqlite<RunLog>.GetInstance.Base.Add(runLog);
  101. Application.Current.Dispatcher.Invoke(new Action(() => { runLogs.Insert(0, runLog); }));
  102. RunLog?.Invoke(info);
  103. }
  104. catch (Exception)
  105. {
  106. // throw;
  107. }
  108. }
  109. }
  110. int AlarmID;
  111. public void ShowAlarmLog(string info, string AlarmNumber = "_", AlarmLevel level = AlarmLevel.一般报警)
  112. {
  113. lock (alarmlock)
  114. {
  115. LogHelper.GetInstance.AddAlarm(info, AlarmNumber);
  116. //AlarmID++;
  117. // BPASmartClient.Model.Alarm alarmLog = new BPASmartClient.Model.Alarm()
  118. // {
  119. // NumId = AlarmID,
  120. // Date = DateTime.Now.ToString("yyyy-MM-dd"),
  121. // Time = DateTime.Now.ToString("HH:mm:ss"),
  122. // Info = info,
  123. // Value = AlarmNumber,
  124. // Grade = (level) + ""
  125. // };
  126. // Sqlite<BPASmartClient.Model.Alarm>.GetInstance.Base.Add(alarmLog);
  127. // Application.Current.Dispatcher.Invoke(new Action(() => { alarmLogs.Insert(0, alarmLog); }));
  128. // AlarmLog?.Invoke(info);
  129. }
  130. }
  131. public bool ShowDialog(string info, DialogType dialogType = DialogType.Information)
  132. {
  133. bool result = false;
  134. Application.Current.Dispatcher.Invoke(() =>
  135. {
  136. PromptView PV = new PromptView();
  137. PV.TextBlockInfo = info;
  138. switch (dialogType)
  139. {
  140. case DialogType.Warning:
  141. PV.TextBlockIcon = "&#xe61f;";
  142. PV.TextBlockForeground = Brushes.Yellow;
  143. PV.infoType.Text = "警告:";
  144. //PV.Cancel.Visibility = Visibility.Collapsed;
  145. break;
  146. case DialogType.Error:
  147. PV.TextBlockIcon = "&#xed1a;";
  148. PV.TextBlockForeground = Brushes.Red;
  149. PV.infoType.Text = "错误:";
  150. //PV.Cancel.Visibility = Visibility.Collapsed;
  151. break;
  152. case DialogType.Information:
  153. PV.TextBlockIcon = "&#xe657;";
  154. PV.TextBlockForeground = Brushes.DeepSkyBlue;
  155. PV.infoType.Text = "提示:";
  156. //PV.Cancel.Visibility = Visibility.Visible;
  157. break;
  158. default:
  159. break;
  160. }
  161. PV.infoType.Foreground = PV.TextBlockForeground;
  162. //要设置弹框和主窗体跟随,需要设置弹框的打开位置为默认
  163. PV.Width = MainView.CurrentWidth;
  164. PV.Height = MainView.CurrentHeight;
  165. PV.Left = MainView.LeftLoc;
  166. PV.Top = MainView.TopLoc;
  167. var res = PV.ShowDialog();
  168. result = res == null ? false : (bool)res;
  169. });
  170. return result;
  171. }
  172. #region 弹框提示信息
  173. public Notifiaction NotifiactionShow { get; set; } = new Notifiaction();
  174. #region 右侧弹框
  175. /// <summary>
  176. /// 手动关闭
  177. /// </summary>
  178. public void OnExit(string e)
  179. {
  180. switch (e)
  181. {
  182. case "Error":
  183. NotifiactionShow.Clear(EnumPromptType.Error);
  184. return;
  185. case "Success":
  186. NotifiactionShow.Clear(EnumPromptType.Success);
  187. return;
  188. case "Warm":
  189. NotifiactionShow.Clear(EnumPromptType.Warn);
  190. return;
  191. case "Info":
  192. NotifiactionShow.Clear(EnumPromptType.Info);
  193. return;
  194. default:
  195. break;
  196. }
  197. //NoticeManager.ExitNotifiaction();
  198. }
  199. /// <summary>
  200. /// 屏幕右下角信息提示弹窗
  201. /// </summary>
  202. /// <param name="content">弹窗消息内容</param>
  203. /// <param name="type">弹窗类型:Error、Success、Warm、Info,分别对应不同颜色</param>
  204. /// <param name="title">弹窗消息类型:属性判证、XX告警...</param>
  205. public void OpenMsg(string content, EnumPromptType type = EnumPromptType.Info, string title = "提示")
  206. {
  207. //if (window == null) return;
  208. string text = string.Empty;
  209. if (content != null)
  210. {
  211. if (content.Length < 40)
  212. {
  213. int count = 40 - content.Length;
  214. for (int i = 0; i < count * 2; i++)
  215. {
  216. content += " ";
  217. }
  218. }
  219. text = content;
  220. }
  221. Application.Current.Dispatcher.Invoke(() =>
  222. {
  223. NotifiactionShow.AddNotifiaction(new NotifiactionModel()
  224. {
  225. Title = title,//"这是Error通知标题",
  226. Content = text,//"这条通知不会自动关闭,需要点击关闭按钮",
  227. ContentToolTip = content,
  228. NotifiactionType = type,
  229. window = Application.Current.MainWindow
  230. });
  231. });
  232. }
  233. #endregion
  234. public void OpenMsg(EnumPromptType type, string title, string content, byte viewcount, int viewtime)
  235. {
  236. //if (window == null) return;
  237. string text = string.Empty;
  238. if (content != null)
  239. {
  240. if (content.Length < 40)
  241. {
  242. int count = 40 - content.Length;
  243. for (int i = 0; i < count * 2; i++)
  244. {
  245. content += " ";
  246. }
  247. }
  248. text = content;
  249. }
  250. Application.Current.Dispatcher.Invoke(() =>
  251. {
  252. NotifiactionShow.MAX_NOTIFICATIONS = viewcount;
  253. NotifiactionShow.AddNotifiaction(new NotifiactionModel()
  254. {
  255. Title = title,//"这是Error通知标题",
  256. Content = text,//"这条通知不会自动关闭,需要点击关闭按钮",
  257. ContentToolTip = content,
  258. NotifiactionType = type,
  259. time = viewtime,
  260. window = Application.Current.MainWindow
  261. });
  262. });
  263. }
  264. #endregion
  265. }
  266. public enum DialogType
  267. {
  268. Warning,
  269. Error,
  270. Information,
  271. }
  272. }