IO2ViewModel.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections.ObjectModel;
  7. using System.Threading;
  8. using System.Windows;
  9. using System.Windows.Threading;
  10. using OpenSEMI.ClientBase;
  11. using OpenSEMI.ClientBase.IO;
  12. namespace VirgoUI.Client.Models.Common.IO
  13. {
  14. public class IO2ViewModel : BaseModel
  15. {
  16. protected override void OnInitialize()
  17. {
  18. base.OnInitialize();
  19. //init I/O get the whole I/O info
  20. this.AIs = IO2Provider.Instance.InitIOData<short>("PLC1.AIList");
  21. this.AOs = IO2Provider.Instance.InitIOData("PLC1.AOList");
  22. this.DIs = IO2Provider.Instance.InitIOData<bool>("PLC1.DIList");
  23. this.DOs = IO2Provider.Instance.InitIOData<bool>("PLC1.DOList");
  24. }
  25. protected override void OnActivate()
  26. {
  27. base.OnActivate();
  28. BaseApp.Instance.UIHandler.Register("IO2View", GetIOs);
  29. }
  30. protected override void OnDeactivate(bool close)
  31. {
  32. base.OnDeactivate(close);
  33. BaseApp.Instance.UIHandler.UnRegister("IO2View");
  34. }
  35. public void SetDO(IOItem<bool> _do)
  36. {
  37. IO2Provider.Instance.SetDO(_do.Name, !_do.Value);
  38. }
  39. public void SetAO(AOItem _ao)
  40. {
  41. IO2Provider.Instance.SetAO(_ao.Name, _ao.Value);
  42. }
  43. public void GetIOs()
  44. {
  45. //get I/O value and update UI
  46. Dictionary<string, short> _ai = IO2Provider.Instance.GetIOData<short>("PLC1.AIList");
  47. Dictionary<string, short> _ao = IO2Provider.Instance.GetIOData<short>("PLC1.AOList");
  48. Dictionary<string, bool> _di = IO2Provider.Instance.GetIOData<bool>("PLC1.DIList");
  49. Dictionary<string, bool> _do = IO2Provider.Instance.GetIOData<bool>("PLC1.DOList");
  50. foreach (IOItem<short> item in AIs)
  51. {
  52. if (_ai.ContainsKey(item.Name))
  53. item.Value = _ai[item.Name];
  54. }
  55. foreach (IOItem<short> item in AOs)
  56. {
  57. if (_ao.ContainsKey(item.Name))
  58. item.Value = _ao[item.Name];
  59. }
  60. foreach (IOItem<bool> item in DIs)
  61. {
  62. if (_di.ContainsKey(item.Name))
  63. item.Value = _di[item.Name];
  64. }
  65. foreach (IOItem<bool> item in DOs)
  66. {
  67. if (_do.ContainsKey(item.Name))
  68. item.Value = _do[item.Name];
  69. }
  70. }
  71. public ObservableCollection<IOItem<short>> AIs { get; private set; }
  72. public ObservableCollection<AOItem> AOs { get; private set; }
  73. public ObservableCollection<IOItem<bool>> DIs { get; private set; }
  74. public ObservableCollection<IOItem<bool>> DOs { get; private set; }
  75. }
  76. }