|
- 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<object>((o) =>
- {
- Pages.Add(o.ToString());
- string path = $"{ Json<ProjectModel>.Data.ProjectPath}\\{Global.PageDirectoryName}";
- Directory.CreateDirectory(path);
- File.WriteAllText($"{path}\\{o.ToString()}.lay", null, Encoding.Unicode);
- Json<ProjectModel>.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<ProjectModel>.Data.ProjectPath = path;
- Json<ProjectModel>.Data.ProjectName = objModel.ProjectName;
- Head = objModel.ProjectName;
- Save();
- FileHelper.GetInstance.RegisterOpenFileType();
- }
- }
- });
-
- NewPageCommand = new RelayCommand(() =>
- {
- NewPageView newPageView = new NewPageView();
- newPageView.ShowDialog();
- });
-
- OpenVarManagerCommand = new RelayCommand(() =>
- {
- Json<LocalPar>.Data.ProjectPath = Json<ProjectModel>.Data.ProjectPath;
- Json<LocalPar>.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<object>((o) =>
- {
- string path = $"{Json<ProjectModel>.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<ProjectModel>.Data.ProjectName != null && Json<ProjectModel>.Data.ProjectName.Length > 0)
- {
- Head = Json<ProjectModel>.Data.ProjectName;
- }
-
- if (Json<ProjectModel>.Data.Pages?.Count > 0)
- {
- for (int i = 0; i < Json<ProjectModel>.Data.Pages.Count; i++)
- {
- Pages.Add(Json<ProjectModel>.Data.Pages.ElementAt(i).Key);
- }
- }
-
- }
-
- public void Save()
- {
- string path = Json<ProjectModel>.Data.ProjectPath;
- string name = Json<ProjectModel>.Data.ProjectName;
- var pages = Json<ProjectModel>.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
- if (pages != null) Json<ProjectModel>.Data.Pages = pages;
- Json<ProjectModel>.Save($"{path}\\{name}.project");
- }
-
- public RelayCommand NewProjectCommand { get; set; }
-
- public RelayCommand OpenVarManagerCommand { get; set; }
-
- public RelayCommand<object> 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<MainCanvasPageModel> mainCanvasPanels { get; set; } = new List<MainCanvasPageModel>();
-
- public ObservableCollection<string> Pages { get; set; } = new ObservableCollection<string>();
-
-
- }
- }
|