123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- using AutoMapper;
- using HandyControl.Controls;
- using LogoForceTestApp.Core;
- using LogoForceTestApp.Core.Mvvm;
- using LogoForceTestApp.Modules.MainModule.Models;
- using LogoForceTestApp.Modules.MainModule.Views;
- using Newtonsoft.Json;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Regions;
- using PropertyChanged;
- using Repository;
- using Repository.Entiies;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Management;
- using System.Net.NetworkInformation;
- using System.Runtime.CompilerServices;
- using System.Security.Cryptography;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Team.Utility;
- namespace LogoForceTestApp.Modules.MainModule.ViewModels
- {
- public class ParamPageViewModel: RegionViewModelBase
- {
- private readonly IMapper _mapper;
- private readonly IEventAggregator _eventAggregator;
- private readonly IWritableOptions<AppSettings> _appSettings;
- private readonly IRepository _repository;
- private string _csvPath;
- public string CSVPath
- {
- get=>_csvPath;
- set {
- SetProperty(ref _csvPath, value);
- SetChanged();
- }
- }
- private string _imageSavePath;
- public string ImageSavePath
- {
- get => _imageSavePath;
- set
- {
- SetProperty(ref _imageSavePath, value);
- SetChanged();
- }
- }
- private bool _selected;
- public bool Selected
- {
- get => _selected;
- set
- {
- SetProperty(ref _selected, value);
- SetChanged();
- }
- }
- public bool TraceDisable { get; set; }
- public bool UnUseOee { get; set; }
- //[OnChangedMethod("SetChanged")]
- public string OeeIp { get; set; }
- //[OnChangedMethod("SetChanged")]
- public int OeePort { get; set; }
- //[OnChangedMethod("SetChanged")]
- public int WaitPartDuration { get; set; } = 1;
- public string Site { get; set; }
- public int StopDuration { get; set; } = 5;
- public string Floor { get; set; }
- public string Product { get; set; }
- public string Line { get; set; }
- public string ProcessOee { get; set; }
- public string TerminalId { get; set; }
- public string MachineId { get; set; }
- public string MacAddr { get; set; }
- public List<string> AddressSource { get; set; }
- public string Status { get; set; }
- public string OpId { get; set; }
- public string IpAddr { get; set; }
- public ParamDto Param { get; set; }
- public string OeePath { get; set; }
- public DelegateCommand SaveTraceParamCommand { get; set; }
- public DelegateCommand SelectFolderCommand { get; set; }
- public DelegateCommand SelectImageFolderCommand { get; set; }
- public DelegateCommand SaveOtherCommand { get; set; }
- public DelegateCommand SaveOeeCommand { get; set; }
- public DelegateCommand SelectOeeFolderCommand { get; set; }
- public DelegateCommand DebugCommand { get; set; }
- public ParamPageViewModel(IMapper mapper,IEventAggregator eventAggregator,
- IWritableOptions<AppSettings> appSettings,IRepository repository,IRegionManager regionManager
- ):base(regionManager)
- {
- //AddressSource=new List<string>();
- AddressSource = GetMacAddress();
- _mapper = mapper;
- _eventAggregator = eventAggregator;
- _appSettings = appSettings;
- _repository = repository;
- CSVPath = _appSettings.Value.CSVPath;
- Selected = _appSettings.Value.UnUseUpload;
- TraceDisable = _appSettings.Value.TraceDisable;
- ImageSavePath = _appSettings.Value.ImageSavePath;
- Build = _appSettings.Value.Builds;
- Project=_appSettings.Value.Project;
- Process=_appSettings.Value.Process;
- AIM_UNIQUE=_appSettings.Value.AIM_UNIQUE;
- AimName = _appSettings.Value.AimName;
- INSPECTION_PROGRAM_NAME = _appSettings.Value.INSPECTION_PROGRAM_NAME;
- Part=_appSettings.Value.Part;
- SaveTraceParamCommand = new DelegateCommand(SaveTraceParam);
- SaveOtherCommand = new DelegateCommand(SaveOther);
- SaveOeeCommand = new DelegateCommand(SaveOeeConfig);
- SelectOeeFolderCommand = new DelegateCommand(SelectOeeFolder);
- DebugCommand = new DelegateCommand(GoOeeDebugPage);
- var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "param.json");
- if (File.Exists(filePath))
- {
- using var stream = new StreamReader(filePath);
- var oeejson= stream.ReadToEnd();
- var param = JsonConvert.DeserializeObject<Param>(oeejson);
- Param= _mapper.Map<ParamDto>(param);
- Param.PropertyChanged += Param_PropertyChanged;
- }
- else
- {
- Param = new ParamDto
- {
- station_id = "LkYC_BO7_2F_01_002_TRACE",
- fixture_id = "H-76HO-SMA40-2200-A-00003",
- cavity_id = "\"1/2/3/4\"",
- line_id = "B07-2F-01",
- station_vendor = "Team",
- test = "Logo 30N Non-Destructive Test",
- OpId= "C10088888"
- };
- Param.PropertyChanged += Param_PropertyChanged;
- }
- if (string.IsNullOrWhiteSpace(Param.OpId))
- {
- Param.OpId = "C10088888";
- }
- if (string.IsNullOrWhiteSpace(Param.DownLimit))
- {
- Param.DownLimit = "30";
- }
- if (string.IsNullOrWhiteSpace(Param.UpperLimit))
- {
- Param.UpperLimit = "60";
- }
- var oeePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "oee.json");
-
- if (File.Exists(oeePath))
- {
- var streamOee = new StreamReader(oeePath);
- var json = streamOee.ReadToEnd();
- var oeeConfig=JsonConvert.DeserializeObject<OeeConfig>(json);
- UnUseOee = oeeConfig.UnUse;
- OeeIp = oeeConfig.Ip;
- OeePort = oeeConfig.Port;
- WaitPartDuration = oeeConfig.WaitPartDuration;
- StopDuration = oeeConfig.StopDuration;
- Site = oeeConfig.Site;
- Floor = oeeConfig.Floor;
- Product= oeeConfig.Product;
- Line = oeeConfig.Line;
- ProcessOee = oeeConfig.Process;
- TerminalId = oeeConfig.TerminalId;
- MachineId = oeeConfig.MachineId;
- MacAddr = oeeConfig.Macaddr;
- IpAddr = oeeConfig.Ipaddr;
- OeePath = oeeConfig.OeePath;
- }
- SelectFolderCommand = new DelegateCommand(SelectFolder);
- SelectImageFolderCommand = new DelegateCommand(SelectImageFolder);
- }
- private void GoOeeDebugPage()
- {
- RegionManager.RequestNavigate(RegionNames.ContentRegion, nameof(OeeDebugPage));
- }
- private void SelectOeeFolder()
- {
- FolderBrowserDialog dilog = new()
- {
- Description = "请选择文件夹"
- };
- if (dilog.ShowDialog() is System.Windows.Forms.DialogResult.OK or System.Windows.Forms.DialogResult.Yes)
- {
- OeePath = dilog.SelectedPath;
- }
- }
- public static List<string> GetMacAddress()
- {
- List<string> macAddressList = new();
- string MoAddress = " ";
- ManagementClass mc = new("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc2 = mc.GetInstances();
- foreach (ManagementObject mo in moc2)
- {
- if ((bool)mo["IPEnabled"] == true)
- {
- MoAddress = mo["MacAddress"].ToString();
- MoAddress.Replace(":", "-");
- macAddressList.Add(MoAddress);
- }
-
- mo.Dispose();
- }
- // return MoAddress.ToString()
- return macAddressList;
- }
- public static List<string> GetIpAddress()
- {
- NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
- List<string> ipList = new();
- foreach (NetworkInterface adapter in nics)
- {
- if (adapter.OperationalStatus == OperationalStatus.Up)
- {
- foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
- {
- if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- ipList.Add(ip.Address.ToString());
- }
- }
- }
- }
- return ipList;
- }
- private void SaveOeeConfig()
- {
- if (!Directory.Exists(OeePath))
- {
- Growl.Error("保存路径不存在,请重新选择");
- return;
- }
- if (!GetMacAddress().Contains(MacAddr))
- {
- Growl.Error("mac地址不正确,请重新输入");
- return;
- }
-
- var oeeConfig = new OeeConfig
- {
- Ip = OeeIp,
- Port = OeePort,
- WaitPartDuration = WaitPartDuration,
- StopDuration = StopDuration,
- Site = Site,
- Floor = Floor,
- Product = Product,
- Line = Line,
- Vendor = "Team",
- Process = ProcessOee,
- TerminalId = TerminalId,
- MachineId = MachineId,
- Op_id = Param.OpId,
- Ipaddr = IpAddr,
- Macaddr = MacAddr,
- OeePath = OeePath,
- UnUse = UnUseOee
- };
- var config = JsonConvert.SerializeObject(oeeConfig, Formatting.Indented);
- var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
- "oee.json");
- using var stream = new StreamWriter(filePath);
- stream.Write(config);
- Growl.Info("保存配置完成");
- _eventAggregator.GetEvent<OeeConfigChangedPublish>().Publish(oeeConfig);
- return;
- }
- private bool _loaded;
- public override void OnNavigatedFrom(NavigationContext navigationContext)
- {
- _loaded=false;
- base.OnNavigatedFrom(navigationContext);
- }
- public override void OnNavigatedTo(NavigationContext navigationContext)
- {
- _loaded = true;
- base.OnNavigatedTo(navigationContext);
- }
- private void LoginSuccess(bool obj)
- {
- if (_loaded)
- {
- RegionManager.RequestNavigate(RegionNames.ContentRegion,nameof(LoginPage));
- }
- }
- private readonly List<string> _storageChangedList = new();
- private void Param_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- _storageChangedList.Add(e.PropertyName);
- SetChanged(e.PropertyName);
- }
- private void SetChanged([CallerMemberName] string propertyName = null)
- {
- _repository.Add(new ParamLog
- {
- OperateAction = OperateAction.ChangeParam,
- Content = $"{propertyName}参数修改",
- Id=Guid.NewGuid(),
- Occured=DateTime.Now,
- OperateObject=propertyName,
- UserName=CurrentUser.CurrentUserName
- });
- }
- private async Task HasSaved(string propertyName)
- {
- await _repository.AddAsync(new ParamLog
- {
- OperateAction = OperateAction.ChangeParam,
- Content = $"{propertyName}参数修改并保存成功",
- Id = Guid.NewGuid(),
- Occured = DateTime.Now,
- OperateObject = propertyName,
- UserName = CurrentUser.CurrentUserName
- });
- }
- private void SelectFolder()
- {
- System.Windows.Forms.FolderBrowserDialog dilog = new System.Windows.Forms.FolderBrowserDialog
- {
- Description = "请选择文件夹"
- };
- if (dilog.ShowDialog() == System.Windows.Forms.DialogResult.OK || dilog.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
- {
- CSVPath = dilog.SelectedPath;
- }
- }
- private void SelectImageFolder()
- {
- System.Windows.Forms.FolderBrowserDialog dilog = new System.Windows.Forms.FolderBrowserDialog
- {
- Description = "请选择文件夹"
- };
- if (dilog.ShowDialog() is System.Windows.Forms.DialogResult.OK or System.Windows.Forms.DialogResult.Yes)
- {
- ImageSavePath = dilog.SelectedPath;
- }
- }
-
- private void SaveOther()
- {
- _appSettings.Update(async c =>
- {
- if (c.CSVPath != CSVPath)
- {
- c.CSVPath = CSVPath;
- await HasSaved(nameof(CSVPath));
- }
- if (c.UnUseUpload != Selected)
- {
- c.UnUseUpload = Selected;
- await HasSaved(nameof(c.UnUseUpload));
- }
- if (c.ImageSavePath != ImageSavePath)
- {
- c.ImageSavePath = ImageSavePath;
- await HasSaved(nameof(c.ImageSavePath));
- }
- if (c.Process!=Process)
- {
- c.Process = Process;
- await HasSaved(nameof(c.Process));
- }
- if (c.AIM_UNIQUE != AIM_UNIQUE)
- {
- c.AIM_UNIQUE = AIM_UNIQUE;
- await HasSaved(nameof(c.AIM_UNIQUE));
- }
- if (c.AimName != AimName)
- {
- c.AimName = AimName;
- await HasSaved(nameof(c.AimName));
- }
- if (c.AIM_UNIQUE != AIM_UNIQUE)
- {
- c.AIM_UNIQUE = AIM_UNIQUE;
- await HasSaved(nameof(c.AIM_UNIQUE));
- }
- if (c.INSPECTION_PROGRAM_NAME != INSPECTION_PROGRAM_NAME)
- {
- c.INSPECTION_PROGRAM_NAME = INSPECTION_PROGRAM_NAME;
- await HasSaved(nameof(c.INSPECTION_PROGRAM_NAME));
- }
- if (c.Project != Project)
- {
- c.Project = Project;
- await HasSaved(nameof(c.Project));
- }
- if (c.Part != Part)
- {
- c.Part = Part;
- await HasSaved(nameof(c.Part));
- }
- if (c.Builds != Build)
- {
- c.Builds = Build;
- await HasSaved(nameof(c.Builds));
- }
- if (c.TraceDisable != TraceDisable)
- {
- c.TraceDisable = TraceDisable;
- await HasSaved(nameof(c.TraceDisable));
- }
- //if (c.ID != Build)
- //{
- // c.Builds = Build;
- // await HasSaved(nameof(c.Builds));
- //}
- });
-
- Growl.Info("保存参数成功");
- _eventAggregator.GetEvent<OtherParamEvent>().Publish(true);
- }
- private async void SaveTraceParam()
- {
- var param = _mapper.Map<Param>(Param);
- var json = JsonConvert.SerializeObject(param);
- var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "param.json");
- using var stream = new StreamWriter(filePath);
- await stream.WriteAsync(json);
- await stream.FlushAsync();
- Growl.Info("保存trace参数成功");
- foreach (var item in _storageChangedList)
- {
- await HasSaved(item);
- }
- _storageChangedList.Clear();
- _eventAggregator.GetEvent<SaveTraceParam>().Publish(param);
- }
- private string aimName;
- public string AimName
- {
- get => aimName;
- set
- {
- SetProperty(ref aimName, value);
- SetChanged();
- }
- }
- private string aIM_UNIQUE;
- public string AIM_UNIQUE
- {
- get => aIM_UNIQUE;
- set
- {
- SetProperty(ref aIM_UNIQUE, value);
- SetChanged();
- }
- }
- private string iNSPECTION_PROGRAM_NAME;
- public string INSPECTION_PROGRAM_NAME
- {
- get => iNSPECTION_PROGRAM_NAME;
- set {
- SetProperty(ref iNSPECTION_PROGRAM_NAME, value);
- SetChanged();
- }
- }
- private string project;
- public string Project {
- get => project;
- set
- {
- SetProperty(ref project, value);
- SetChanged();
- }
- }
- private string part;
- public string Part {
- get => part;
- set {
- SetProperty(ref part, value);
- SetChanged();
- }
- }
- private string process;
- public string Process
- {
- get => process;
- set
- {
- SetProperty(ref process, value);
- SetChanged();
- }
- }
- private string build;
- public string Build
- {
- get => build;
- set {
- SetProperty(ref build, value);
- SetChanged();
- }
- }
- }
- }
|