| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- using MaterialDesignThemes.Wpf;
- using NPOI.SS.Formula.Functions;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Ioc;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using TeamAAS_VP;
- using TeamAAS_VP.Controls;
- using TeamAAS_VP.Core;
- using TeamAAS_VP.Events;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Resources.Languages;
- using TeamAAS_VP.Views.DebugMod;
- using static MaterialDesignThemes.Wpf.Theme.ToolBar;
- namespace TeamAAS_VP.ViewModels.DebugMod
- {
- public class InchingControlViewModel : BindableBase
- {
- IContainerProvider _container;
- IEventAggregator _eventAggregator;
- ISystemDatabaseService _systemDatabaseService;
- Timer timer;
- private Management management;
- #region 属性
- private ObservableCollection<UserInching> _UserInchingCollection;
- public ObservableCollection<UserInching> UserInchingCollection
- {
- get { return _UserInchingCollection; }
- set { SetProperty(ref _UserInchingCollection,value); }
- }
- #endregion
- #region 命令
- private DelegateCommand _LoadedCommand;
- public DelegateCommand LoadedCommand =>
- _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
-
- private DelegateCommand _AddCommand;
- public DelegateCommand AddCommand =>
- _AddCommand ?? (_AddCommand = new DelegateCommand(ExecuteAddCommand));
- private DelegateCommand<object> _RemoveCommand;
- public DelegateCommand<object> RemoveCommand =>
- _RemoveCommand ?? (_RemoveCommand = new DelegateCommand<object>(ExecuteRemoveCommand));
- private DelegateCommand<object> _EditCommand;
- public DelegateCommand<object> EditCommand =>
- _EditCommand ?? (_EditCommand = new DelegateCommand<object>(ExecuteEditCommand));
- private DelegateCommand<object> _InchingCommand;
- public DelegateCommand<object> InchingCommand =>
- _InchingCommand ?? (_InchingCommand = new DelegateCommand<object>(ExecuteInchingCommand));
- private DelegateCommand _SaveCommand;
- public DelegateCommand SaveCommand =>
- _SaveCommand ?? (_SaveCommand = new DelegateCommand(ExecuteSaveCommand));
- #endregion
- #region 事件
- #endregion
- public InchingControlViewModel(IContainerProvider container, IEventAggregator ea, ISystemDatabaseService systemDatabaseService)
- {
- _container = container;
- _eventAggregator = ea;
- _systemDatabaseService = systemDatabaseService;
- management = _container.Resolve<Management>();
- UserInchingCollection =new ObservableCollection<UserInching>();
- //订阅权限登录事件
- _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
- }
- #region 方法
- private bool isLoad = false;
- void ExecuteLoadedCommand()
- {
- if (!isLoad)
- {
- LoadInchingConfig();
- timer = new Timer(DoTime, null, 1000, 1000);
- isLoad = true;
- }
- if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- async void ExecuteEditCommand(object obj)
- {
- var inch = obj as UserInching;
- var view = new AddSimpleInching();
- ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = inch;
- //show the dialog
- var result = await DialogHost.Show(view, "RootDialog", null, null, null);
- if (result != null && result is UserInching)
- {
- UserInching inching = (UserInching)result;
- var c = UserInchingCollection.FirstOrDefault(i => i.ID == inching.ID);
- c = inching;
- }
- }
- void ExecuteRemoveCommand(object obj)
- {
- var inch = obj as UserInching;
- if (MessageBox.Show($"{Lang.是否删除点动模块}:{inch.Name}?", Lang.移除点动模块, MessageBoxButton.YesNo,MessageBoxImage.Question)==MessageBoxResult.Yes)
- {
- UserInchingCollection.Remove(inch);
- }
- }
- async void ExecuteAddCommand()
- {
- var view = new AddSimpleInching();
- ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = new UserInching() { ID=Guid.NewGuid()};
- //show the dialog
- var result = await DialogHost.Show(view, "RootDialog", null, null, null);
- if (result != null && result is UserInching)
- {
- UserInching inching = (UserInching)result;
- if (string.IsNullOrEmpty(inching.Name)) return;
- UserInchingCollection.Add(inching);
- }
- }
- void ExecuteSaveCommand()
- {
- SaveInchingConfig();
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
- }
- /// <summary>
- /// 执行点动命令
- /// </summary>
- /// <param name="obj"></param>
- void ExecuteInchingCommand(object obj)
- {
- if (management.BgModbusTcpCommunicate == null || !management.BgModbusTcpCommunicate.IsEnabled) return;
- try
- {
- var inch = obj as UserInching;
- switch (inch.DataStore)
- {
- case Enums.ModbusDataStore.CoilDiscretes:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress+1])
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = false;
- }
- else
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = true;
- }
- break;
- case Enums.ModbusDataStore.InputDiscretes:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1])
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = false;
- }
- else
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = true;
- }
- break;
- case Enums.ModbusDataStore.InputRegisters:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] == inch.OnValue)
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OffValue;
- }
- else
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OnValue;
- }
- break;
- case Enums.ModbusDataStore.HoldingRegisters:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] == inch.OnValue)
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OffValue;
- }
- else
- {
- management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OnValue;
- }
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
- }
-
- }
- private async void DoTime(object state)
- {
- try
- {
- if (management.BgModbusTcpCommunicate==null || !management.BgModbusTcpCommunicate.IsEnabled)
- {
- return;
- }
- await App.Current.Dispatcher.InvokeAsync(new
- Action(() =>
- {
- for (int i = 0; i < UserInchingCollection.Count; i++)
- {
- switch (UserInchingCollection[i].DataStore)
- {
- case Enums.ModbusDataStore.CoilDiscretes:
- UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[UserInchingCollection[i].DataAddress + 1];
- break;
- case Enums.ModbusDataStore.InputDiscretes:
- UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[UserInchingCollection[i].DataAddress + 1];
- break;
- case Enums.ModbusDataStore.InputRegisters:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue)
- {
- UserInchingCollection[i].State = true;
- }
- else
- {
- UserInchingCollection[i].State = false;
- }
- break;
- case Enums.ModbusDataStore.HoldingRegisters:
- if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue)
- {
- UserInchingCollection[i].State = true;
- }
- else
- {
- UserInchingCollection[i].State = false;
- }
- break;
- default:
- break;
- }
- }
- foreach (var item in UserInchingCollection)
- {
-
- }
- }));
- }
- catch (Exception)
- {
- }
- }
- private void LoadInchingConfig()
- {
- try
- {
- UserInchingCollection = FileHelper.ReadJsonFile<ObservableCollection<UserInching>>(FilePath.UserInchingConfigurationPath);
- }
- catch (Exception ex)
- {
- UserInchingCollection = new ObservableCollection<UserInching>();
- LogHelper.WriteLogError("加载用户点动集合配置文件时出错!", ex);
- }
- }
- public void SaveInchingConfig()
- {
- try
- {
- FileHelper.WriteJsonFile(UserInchingCollection, FilePath.UserInchingConfigurationPath);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLogError("保存用户点动集合配置至文件时出错!", ex);
- }
- }
- #endregion
- private bool _IsAllowEdit = false;
- public bool IsAllowEdit
- {
- get { return _IsAllowEdit; }
- set { SetProperty(ref _IsAllowEdit, value); }
- }
- private void LoginChange(Models.User user)
- {
- if (user.userPart == Enums.UserPart.Operator)
- {
- IsAllowEdit = false;
- }
- else
- {
- IsAllowEdit = true;
- }
- }
- }
- }
|