using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPASmart.Model; using Microsoft.Toolkit.Mvvm.Input; using System.Collections.ObjectModel; using System.Windows.Forms; using System.IO; using BPA.Helper; using System.Diagnostics; using BeDesignerSCADA; using BeDesignerSCADA.Controls; using System.Windows; using System.Reflection; using System.Collections.Concurrent; namespace BPASmart.ConfigurationSoftware { public class MainWindowViewModel : NoticeBase { //public ushort CalcCRC(byte[] bytes) //{ // ushort uiCrcValue = 0xffff; // ushort polynomial = 0x8408; // for (int i = 0; i < bytes.Length; i++) // { // uiCrcValue = (ushort)(uiCrcValue ^ bytes[i]); // for (int m = 0; m < 8; m++) // { // if ((uiCrcValue & 0x0001) == 1) // { // uiCrcValue = (ushort)((uiCrcValue >> 1) ^ polynomial); // } // else // { // uiCrcValue = (ushort)(uiCrcValue >> 1); // } // } // } // return uiCrcValue; //} //public byte[] GetCrc(ushort value, bool IsReverse = true) //{ // byte[] bytes = new byte[2]; // if (IsReverse) // { // bytes[0] = (byte)value; // bytes[1] = (byte)(value >> 8); // } // else // { // bytes[1] = (byte)value; // bytes[0] = (byte)(value >> 8); // } // return bytes; //} public MainWindowViewModel() { Init(); ActionManage.GetInstance.Register(new Action((o) => { Pages.Add(o.ToString()); string path = $"{ Json.Data.ProjectPath}\\{Global.PageDirectoryName}"; Directory.CreateDirectory(path); File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode); Json.Data.Pages.TryAdd(o.ToString(), $"{path}\\{o.ToString()}.lay"); }), "AddPage"); NewProjectCommand = new RelayCommand(() => { NewProjectView newProjectView = new NewProjectView(); bool? result = newProjectView.ShowDialog(); if (result != null && result == true) { if (newProjectView.Tag != null && newProjectView.Tag is NewDataModel objModel) { string path = $"{objModel.ProjectPath}\\{objModel.ProjectName}"; Directory.CreateDirectory(path); Json.Data.ProjectPath = path; Json.Data.ProjectName = objModel.ProjectName; Head = objModel.ProjectName; Save(); FileHelper.GetInstance.RegisterOpenFileType(); } } }); NewPageCommand = new RelayCommand(() => { NewPageView newPageView = new NewPageView(); newPageView.ShowDialog(); }); OpenVarManagerCommand = new RelayCommand(() => { Json.Data.ProjectPath = Json.Data.ProjectPath; Json.Save(); if (File.Exists(Global.VarManagerPath)) { Process[] pro = Process.GetProcesses(); if (pro?.ToList().FirstOrDefault(p => p.ProcessName.Contains("BPASmart.VariableManager")) == null) { Process.Start(Global.VarManagerPath); } } }); SelectedPageCommand = new RelayCommand((o) => { string path = $"{Json.Data.ProjectPath}\\{Global.PageDirectoryName}\\{o.ToString()}.lay"; if (mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString()) == null) { mainCanvasPanels.Add(new MainCanvasPageModel() { MainCanvasPanelModel = new MainCanvasPanel(path), PageName = o.ToString(), }); } var res = mainCanvasPanels.FirstOrDefault(p => p.PageName == o.ToString()); if (res != null) { MainContent = res.MainCanvasPanelModel; } }); SaveProjectCommand = new RelayCommand(() => { for (int i = 0; i < mainCanvasPanels.Count; i++) { mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.FileSave(); } Save(); }); } private void Init() { if (Json.Data.ProjectName != null && Json.Data.ProjectName.Length > 0) { Head = Json.Data.ProjectName; } if (Json.Data.Pages?.Count > 0) { for (int i = 0; i < Json.Data.Pages.Count; i++) { Pages.Add(Json.Data.Pages.ElementAt(i).Key); } } } public void Save() { string path = Json.Data.ProjectPath; string name = Json.Data.ProjectName; var pages = Json.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value); if (pages != null) Json.Data.Pages = pages; Json.Save($"{path}\\{name}.project"); } public RelayCommand NewProjectCommand { get; set; } public RelayCommand OpenVarManagerCommand { get; set; } public RelayCommand SelectedPageCommand { get; set; } public RelayCommand SaveProjectCommand { get; set; } public RelayCommand NewPageCommand { get; set; } public string Head { get { return _mHead; } set { _mHead = value; OnPropertyChanged(); } } private string _mHead; public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } } private FrameworkElement _mMainContent; public List mainCanvasPanels { get; set; } = new List(); public ObservableCollection Pages { get; set; } = new ObservableCollection(); } }