UcSectionError.xaml.cs 4.21 KB
using CommunityToolkit.Mvvm.Input;
using Misc;
using System;
using System.ComponentModel;
using System.Windows.Controls;

namespace FLY.Thick.Base.UI.CustomSection
{
    /// <summary>
    /// UcSectionWarning.xaml 的交互逻辑
    /// </summary>
    public partial class UcSectionError : UserControl
    {
        UcSectionErrorVm viewModel;

        public UcSectionError()
        {
            InitializeComponent();
        }

        [Unity.InjectionMethod]
        public void Init(ParamDictionary paramDictionary, LCUS1 lCUS1)
        {
            viewModel = new UcSectionErrorVm();
            viewModel.Init(paramDictionary, lCUS1);
            this.DataContext = viewModel;
        }
    }
    public class UcSectionErrorVm : INotifyPropertyChanged
    {
        /// <summary>
        /// 报警音乐路径 
        /// </summary>
        public string WarningTipPath { get; set; }

        /// <summary>
        /// 报警持续时间 单位:秒
        /// </summary>
        public int WarningDurationSec { get; set; }

        /// <summary>
        /// 大屏幕提示扫描报警 
        /// </summary>
        public bool EnableScanErrBigTip { get; set; }

        /// <summary>
        /// USB 继电器 串口地址
        /// </summary>
        public string LCUS1_PortName { get; set; } = "COM1";

        /// <summary>
        /// 使能USB 继电器
        /// </summary>
        public bool LCUS1_Enable { get; set; }



        public event PropertyChangedEventHandler PropertyChanged;


        public RelayCommand PlayCmd { get; }
        public RelayCommand OpenCmd { get; }
        public RelayCommand BellRingCmd { get; }
        public RelayCommand BellOffCmd { get; }


        ParamDictionary paramDictionary;
        LCUS1 lcus1;
        public LCUS1 LCUS1 => lcus1;
        public UcSectionErrorVm()
        {
            PlayCmd = new RelayCommand(Play);
            OpenCmd = new RelayCommand(Open);
            BellRingCmd = new RelayCommand(() => { lcus1.On(); });
            BellOffCmd = new RelayCommand(() => { lcus1.Off(); });
        }

        public void Init(ParamDictionary paramDictionary, LCUS1 lCUS1)
        {
            this.paramDictionary = paramDictionary;
            this.lcus1 = lCUS1;
            paramDictionary.SetBinding(this, nameof(WarningTipPath), ParamDistItemKeys.WarningTipPath, "");
            paramDictionary.SetBinding(this, nameof(WarningDurationSec), ParamDistItemKeys.WarningDurationSec, 5);
            paramDictionary.SetBinding(this, nameof(EnableScanErrBigTip), ParamDistItemKeys.EnableScanErrBigTip, false);
            paramDictionary.SetBinding(this, nameof(LCUS1_PortName), ParamDistItemKeys.LCUS1_PortName, "COM1");
            paramDictionary.SetBinding(this, nameof(LCUS1_Enable), ParamDistItemKeys.LCUS1_Enable, false);
        }

        private void Play()
        {
            //报警!!!!!!!!
            FLY.ControlLibrary.Window_WarningTip.Show(
                "测试",
                WarningTipPath + $" 持续{WarningDurationSec}s",
                TimeSpan.FromSeconds(WarningDurationSec),
                WarningTipPath);
        }

        private void Open()
        {
            System.Windows.Forms.FileDialog open = new System.Windows.Forms.OpenFileDialog();
            open.Filter = "mp3文件|*.mp3|wav文件|*.wav|所有文件|*.*";
            open.Title = "打开音乐文件";

            if (System.IO.Path.IsPathRooted(WarningTipPath))
                open.InitialDirectory = System.IO.Path.GetDirectoryName(WarningTipPath);
            else
                open.InitialDirectory = System.Environment.CurrentDirectory;

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string currentDirectory = System.Environment.CurrentDirectory.ToLower();
                string filename = open.FileName.ToLower();

                if (filename.StartsWith(currentDirectory))
                {
                    //CurrentDirectory没有最后的\ 需要+1
                    WarningTipPath = open.FileName.Substring(System.Environment.CurrentDirectory.Length + 1);
                }
                else
                {
                    WarningTipPath = open.FileName;
                }
            }
        }

    }
}