IO1ViewModel.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 IO1ViewModel : BaseModel
  15. {
  16. protected override void OnInitialize()
  17. {
  18. base.OnInitialize();
  19. TwoColumnMode = true;
  20. //init I/O get the whole I/O info
  21. this.AIs = IO1Provider.Instance.InitIOData<short>("PLC1.AIList");
  22. this.AOs = IO1Provider.Instance.InitIOData("PLC1.AOList");
  23. this.DIs = IO1Provider.Instance.InitIOData<bool>("PLC1.DIList");
  24. this.DOs = IO1Provider.Instance.InitIOData<bool>("PLC1.DOList");
  25. }
  26. protected override void OnActivate()
  27. {
  28. base.OnActivate();
  29. BaseApp.Instance.UIHandler.Register("IO1View", GetIOs);
  30. }
  31. protected override void OnDeactivate(bool close)
  32. {
  33. base.OnDeactivate(close);
  34. BaseApp.Instance.UIHandler.UnRegister("IO1View");
  35. }
  36. public void SetDO(IOItem<bool> _do)
  37. {
  38. IO1Provider.Instance.SetDO(_do.Name, !_do.Value);
  39. }
  40. public void SetAO(AOItem _ao)
  41. {
  42. IO1Provider.Instance.SetAO(_ao.Name, _ao.Value);
  43. }
  44. public void GetIOs()
  45. {
  46. //get I/O value and update UI
  47. Dictionary<string, short> _ai = IO1Provider.Instance.GetIOData<short>("PLC1.AIList");
  48. Dictionary<string, short> _ao = IO1Provider.Instance.GetIOData<short>("PLC1.AOList");
  49. Dictionary<string, bool> _di = IO1Provider.Instance.GetIOData<bool>("PLC1.DIList");
  50. Dictionary<string, bool> _do = IO1Provider.Instance.GetIOData<bool>("PLC1.DOList");
  51. foreach (IOItem<short> item in AIs)
  52. {
  53. if (_ai.ContainsKey(item.Name))
  54. item.Value = _ai[item.Name];
  55. }
  56. foreach (IOItem<short> item in AOs)
  57. {
  58. if (_ao.ContainsKey(item.Name))
  59. item.Value = _ao[item.Name];
  60. }
  61. foreach (IOItem<bool> item in DIs)
  62. {
  63. if (_di.ContainsKey(item.Name))
  64. item.Value = _di[item.Name];
  65. }
  66. foreach (IOItem<bool> item in DOs)
  67. {
  68. if (_do.ContainsKey(item.Name))
  69. item.Value = _do[item.Name];
  70. }
  71. }
  72. private bool _TwoColumnMode = false;
  73. public bool TwoColumnMode
  74. {
  75. get { return _TwoColumnMode; }
  76. set { _TwoColumnMode = value; NotifyOfPropertyChange("TwoColumnMode"); }
  77. }
  78. public ObservableCollection<IOItem<short>> AIs { get; private set; }
  79. public ObservableCollection<AOItem> AOs { get; private set; }
  80. public ObservableCollection<IOItem<bool>> DIs { get; private set; }
  81. public ObservableCollection<IOItem<bool>> DOs { get; private set; }
  82. }
  83. }