123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using HandyControl.Interactivity.Commands;
- using Shaker.Model;
- using Shaker.ViewModel;
- using ShakerManger.Data;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace ShakerManger.ViewModel
- {
- internal class GPIOControlViewModel:DisplayViewModel<GPIOModel>,ISystemPageViewModel
- {
-
- private bool isSupport = false;
- private CancellationTokenSource source = new CancellationTokenSource();
- private bool level = false;
- private bool isEnabled = true;
- public GPIOControlViewModel()
- {
- for(int i=0;i<16;i++)
- {
- Pins.Add(new KeyValuePair<string, int>($"GPIO{i}",i));
- }
- }
- public override Type View { get; } = typeof(View.GPIOPageView);
- public ICommand SaveCommand => new DelegateCommand(Save);
- private void Save()
- {
- Sql.Default.Replace(x => x.Id, Model.Id, Model);
- PromptViewModel.Default.Init();
- PromptViewModel.Default.IconType = IconType.Info;
- PromptViewModel.Default.NoVisibility = System.Windows.Visibility.Collapsed;
- PromptViewModel.Default.Message = "保存成功,重启后生效.";
- PromptViewModel.Default.IsOpen = true;
- }
- public override void Init()
- {
- var model = Sql.Default.FindFirst<Shaker.Model.GPIOModel>(x => true);
- if (model == null)
- {
- Sql.Default.Insert(Model);
- }
- else
- {
- LoopTime = model.LoopTime;
- PinIndex = model.PinIndex;
- }
- Susi4.SusiGPIOControl.Default.PinIndex = PinIndex;
- Susi4.SusiGPIOControl.Default.Init();
- IsSupport = Susi4.SusiGPIOControl.Default.IsSupport;
- IsVisibily = IsSupport;
- Start();
- }
- public void Start()
- {
- if (!IsSupport) return;
- source?.Cancel();
- source = new CancellationTokenSource();
- Task.Run(async () =>
- {
- while(!source.IsCancellationRequested)
- {
- Tools.DispatherInovke.Inovke(() =>
- {
- Level = Susi4.SusiGPIOControl.Default.GetLevel();
- });
- await Task.Delay(LoopTime);
- }
- }, source.Token);
- }
- public void Stop()
- {
- source?.Cancel();
- Level = false;
- }
- public void Close()
- {
- Level = false;
- source.Cancel();
- Susi4.SusiGPIOControl.Default.Close();
- }
- public bool Level
- {
- get => level;
- set
- {
- if (level == value) return;
- UpdateProperty(ref level, value);
- if (!value) return;
- foreach(var val in MainWindowViewModel.Default.Shakers.Shakers)
- {
- if (!val.IsConnected) continue;
- val.StopNoResult(true);
- }
- }
- }
- public ObservableCollection<KeyValuePair<string,int>> Pins { get; } = new ObservableCollection<KeyValuePair<string,int>>();
- public bool IsSupport { get => isSupport; set =>UpdateProperty(ref isSupport ,value); }
- public int PinIndex { get => Model.PinIndex; set => UpdateProperty(ref Model.PinIndex,Math.Clamp(value,0,32)); }
- public int LoopTime { get => Model.LoopTime; set => UpdateProperty(ref Model.LoopTime,Math.Clamp(value,10,int.MaxValue)); }
- public SystemPageType SystemPageType => SystemPageType.GPIOPage;
- public string IconPath { get; } = (string)App.Current.FindResource("GPIOIcon");
- public bool IsEnabled { get => isEnabled; set => UpdateProperty(ref isEnabled, value); }
- private bool isVisibily = false;
- public bool IsVisibily { get => isVisibily; set =>UpdateProperty(ref isVisibily,value); }
- }
- }
|