GPIOControlViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using HandyControl.Interactivity.Commands;
  2. using Shaker.Model;
  3. using Shaker.ViewModel;
  4. using ShakerManger.Data;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Input;
  12. namespace ShakerManger.ViewModel
  13. {
  14. internal class GPIOControlViewModel:DisplayViewModel<GPIOModel>,ISystemPageViewModel
  15. {
  16. private bool isSupport = false;
  17. private CancellationTokenSource source = new CancellationTokenSource();
  18. private bool level = false;
  19. private bool isEnabled = true;
  20. public GPIOControlViewModel()
  21. {
  22. for(int i=0;i<16;i++)
  23. {
  24. Pins.Add(new KeyValuePair<string, int>($"GPIO{i}",i));
  25. }
  26. }
  27. public override Type View { get; } = typeof(View.GPIOPageView);
  28. public ICommand SaveCommand => new DelegateCommand(Save);
  29. private void Save()
  30. {
  31. Sql.Default.Replace(x => x.Id, Model.Id, Model);
  32. PromptViewModel.Default.Init();
  33. PromptViewModel.Default.IconType = IconType.Info;
  34. PromptViewModel.Default.NoVisibility = System.Windows.Visibility.Collapsed;
  35. PromptViewModel.Default.Message = "保存成功,重启后生效.";
  36. PromptViewModel.Default.IsOpen = true;
  37. }
  38. public override void Init()
  39. {
  40. var model = Sql.Default.FindFirst<Shaker.Model.GPIOModel>(x => true);
  41. if (model == null)
  42. {
  43. Sql.Default.Insert(Model);
  44. }
  45. else
  46. {
  47. LoopTime = model.LoopTime;
  48. PinIndex = model.PinIndex;
  49. }
  50. Susi4.SusiGPIOControl.Default.PinIndex = PinIndex;
  51. Susi4.SusiGPIOControl.Default.Init();
  52. IsSupport = Susi4.SusiGPIOControl.Default.IsSupport;
  53. IsVisibily = IsSupport;
  54. Start();
  55. }
  56. public void Start()
  57. {
  58. if (!IsSupport) return;
  59. source?.Cancel();
  60. source = new CancellationTokenSource();
  61. Task.Run(async () =>
  62. {
  63. while(!source.IsCancellationRequested)
  64. {
  65. Tools.DispatherInovke.Inovke(() =>
  66. {
  67. Level = Susi4.SusiGPIOControl.Default.GetLevel();
  68. });
  69. await Task.Delay(LoopTime);
  70. }
  71. }, source.Token);
  72. }
  73. public void Stop()
  74. {
  75. source?.Cancel();
  76. Level = false;
  77. }
  78. public void Close()
  79. {
  80. Level = false;
  81. source.Cancel();
  82. Susi4.SusiGPIOControl.Default.Close();
  83. }
  84. public bool Level
  85. {
  86. get => level;
  87. set
  88. {
  89. if (level == value) return;
  90. UpdateProperty(ref level, value);
  91. if (!value) return;
  92. foreach(var val in MainWindowViewModel.Default.Shakers.Shakers)
  93. {
  94. if (!val.IsConnected) continue;
  95. val.StopNoResult(true);
  96. }
  97. }
  98. }
  99. public ObservableCollection<KeyValuePair<string,int>> Pins { get; } = new ObservableCollection<KeyValuePair<string,int>>();
  100. public bool IsSupport { get => isSupport; set =>UpdateProperty(ref isSupport ,value); }
  101. public int PinIndex { get => Model.PinIndex; set => UpdateProperty(ref Model.PinIndex,Math.Clamp(value,0,32)); }
  102. public int LoopTime { get => Model.LoopTime; set => UpdateProperty(ref Model.LoopTime,Math.Clamp(value,10,int.MaxValue)); }
  103. public SystemPageType SystemPageType => SystemPageType.GPIOPage;
  104. public string IconPath { get; } = (string)App.Current.FindResource("GPIOIcon");
  105. public bool IsEnabled { get => isEnabled; set => UpdateProperty(ref isEnabled, value); }
  106. private bool isVisibily = false;
  107. public bool IsVisibily { get => isVisibily; set =>UpdateProperty(ref isVisibily,value); }
  108. }
  109. }