MesWorkspace.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using LocalhostMES.Core;
  2. using LocalhostMES.DataBase;
  3. using LocalhostMES.Models;
  4. using LocalhostMES.ViewModels.Tabs;
  5. using Prism.Mvvm;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Media;
  13. namespace LocalhostMES.ViewModels.Services
  14. {
  15. public sealed class MesWorkspace : BindableBase, IMesWorkspace
  16. {
  17. private LogHelper.LogChangHandler _logHandler;
  18. private bool _initialized;
  19. private string _statusLine;
  20. private Brush _statusForeground = Brushes.Green;
  21. private int _selectedTabIndex = 0;
  22. private string _crossPageSn;
  23. public Management MesManagement => Management.GetManagement();
  24. public ObservableCollection<WorkOrderInfo> WorkOrders { get; } = new ObservableCollection<WorkOrderInfo>();
  25. public ObservableCollection<string> Logs { get; } = new ObservableCollection<string>();
  26. public string StatusLine
  27. {
  28. get => _statusLine;
  29. private set => SetProperty(ref _statusLine, value);
  30. }
  31. public Brush StatusForeground
  32. {
  33. get => _statusForeground;
  34. private set => SetProperty(ref _statusForeground, value);
  35. }
  36. public int SelectedTabIndex
  37. {
  38. get => _selectedTabIndex;
  39. set => SetProperty(ref _selectedTabIndex, value);
  40. }
  41. public string CrossPageSn
  42. {
  43. get => _crossPageSn;
  44. set => SetProperty(ref _crossPageSn, value);
  45. }
  46. public void Initialize()
  47. {
  48. if (_initialized)
  49. {
  50. return;
  51. }
  52. _initialized = true;
  53. DatabaseHelper.CreataDataTable();
  54. ReloadWorkOrders();
  55. _logHandler = msg =>
  56. {
  57. Application.Current?.Dispatcher.Invoke(() =>
  58. {
  59. Logs.Add($"[{DateTime.Now:HH:mm:ss}] {msg}\n");
  60. });
  61. };
  62. LogHelper.logChangHandler += _logHandler;
  63. }
  64. public void Shutdown()
  65. {
  66. if (_logHandler != null)
  67. {
  68. LogHelper.logChangHandler -= _logHandler;
  69. _logHandler = null;
  70. }
  71. }
  72. public void ReloadWorkOrders()
  73. {
  74. WorkOrders.Clear();
  75. foreach (var w in DatabaseHelper.SelectWorkOrderInfo(null))
  76. {
  77. WorkOrders.Add(w);
  78. }
  79. }
  80. public void ShowStatus(string message, bool isError)
  81. {
  82. Application.Current?.Dispatcher.Invoke(() =>
  83. {
  84. StatusLine = message;
  85. StatusForeground = isError ? Brushes.Red : Brushes.Green;
  86. });
  87. }
  88. private void OnOrderNotifyLog(string message)
  89. {
  90. Application.Current?.Dispatcher.Invoke(() =>
  91. {
  92. Logs.Add($"[{DateTime.Now:HH:mm:ss}] {message}\n");
  93. });
  94. }
  95. }
  96. }