Newer
Older
RbFreqStand / RbFreqStandMeasure / home / SetDevChannelDlg.cs
yxw on 1 Jun 2021 15 KB 20210601合并测试
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Dto;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service;
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Service.Impl;
using Casic.Birmm.RbFreqStandMeasure.R_DevService.Dto;
using Casic.Birmm.RbFreqStandMeasure.R_DevService.Service;
using Casic.Birmm.RbFreqStandMeasure.R_DevService.Service.Impl;
using Casic.Birmm.RbFreqStandMeasure.tools;
using Casic.Birmm.RbFreqStandMeasure.Tools;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace Casic.Birmm.RbFreqStandMeasure.home
{
    public partial class SetDevChannelDlg : Form
    {
        #region 无边框拖动效果/最小化窗口/关闭窗口
        [DllImport("user32.dll")]//拖动无窗体的控件
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //拖动窗体
            ReleaseCapture();
            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion

        public long deviceId = -1;
        DeviceService deviceService = new DeviceServiceImpl();
        DetectionItemService detectionItemService = new DetectionItemServiceImpl();

        DateTime endTimeBySystem = new DateTime();
        CounterDataService CounterDataService = new CounterDataServiceImpl();
        CounterDetecInitService counterDetecInitService = new CounterDetecInitServiceImpl();
        DictService dictService = new DictServiceImpl();
        SensitivityService sensitivityService = new SensitivityServiceImpl();
        CounterDataService counterDataService = new CounterDataServiceImpl();
        int channel = 0;

     

        public Action HideMaskAction
        {
            get;
            set;
        }
        
        public SetDevChannelDlg(int channelNo)
        {
            channel = channelNo;
            
            InitializeComponent();

            // 设置双缓冲
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            // 跨线程访问控件
            CheckForIllegalCrossThreadCalls = false;
        }

        private void btnExitChannel_Click(object sender, EventArgs e)
        {
            this.HideMaskAction();
            RbFreqStdMeas.rbFreqStdMeas.TopMost = true;
            RbFreqStdMeas.rbFreqStdMeas.TopMost = false;
        }

        private void SetDevChannelDlg_Shown(object sender, EventArgs e)
        {
            textBox_channelNo.Text = channel.ToString();
            timePicker_startTime.Value = DateTime.Now;
            timePicker_startTime.MinDate = DateTime.Now;
            getEndTime();
            List<DeviceDto> listToTest = deviceService.search("", "", "", "", "", "1");
            if (listToTest != null && listToTest.Count > 0)
            {
                foreach (DeviceDto deviceDto in listToTest)
                {
                    cbBox_toTestDevList.Items.Add(deviceDto.DevCode);
                }
            }

            text_customerDev.Visible = true;
            text_customerName.Visible = true;
            text_devName.Visible = true;
            text_devType.Visible = true;
            textBox_channelNo.Visible = true;
            text_devModel.Visible = true;
            
        }

        private void checkBox_stability_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_stability.Checked)
            {
                groupBox3.Enabled = true;
            }
            else
            {
                groupBox3.Enabled = false;
            }
            getEndTime();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (text_devCode.Text.Equals(""))
                {
                    MessageBox.Show("请选择待检仪器!");
                    return;
                }
                if (checkBox_stability.Checked && !checkBox1s.Checked && !checkBox10s.Checked && !checkBox20s.Checked && !checkBox_100s.Checked)
                {
                    MessageBox.Show("请选择频率稳定度采样时间!");
                    return;
                }
                if (!checkBox_accuracy.Checked && !checkBox_stability.Checked && !checkBox_ageRate.Checked && !checkBox_bootFeature.Checked)
                {
                    MessageBox.Show("请选择检测项目!");
                    return;
                }
                timePicker_endTime.Value = endTimeBySystem;

                // 打开串口
                DetectionHelper detectionHelper = new DetectionHelper();

                SerialPort port = RbFreqStdMeas.portList[Convert.ToInt32(textBox_channelNo.Text) - 1];
                string portName = ConfigHelper.GetAppConfig("channel" + textBox_channelNo.Text).Split(' ')[0];
                int band = Convert.ToInt32(ConfigHelper.GetAppConfig("channel" + textBox_channelNo.Text).Split(' ')[1]);

                if (port.IsOpen) port.Close();

                port.PortName = portName;
                port.BaudRate = band;
                port.Parity = Parity.None;
                port.DataBits = 8;
                port.StopBits = StopBits.One;
                port.Handshake = Handshake.None;
                port.ReadTimeout = -1;
                port.WriteTimeout = 3000;
                try
                {
                    port.Open();
                    if (!port.IsOpen)
                    {
                        MessageBox.Show("无法打开通道" + textBox_channelNo.Text + ",添加检测失败!");
                        return;
                    }
                    else
                    {
                        RbFreqStdMeas.portList[Convert.ToInt32(textBox_channelNo.Text) - 1] = port;
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show("无法打开通道" + textBox_channelNo.Text + ",添加检测失败!");
                    return;
                }

                label_tip.Text = "正在从通道[" + textBox_channelNo.Text + "]获取数据,请稍侯......";

                if (DetectionHelper.detectionHelper.getFrequencyData(port, true).Equals(""))
                {
                    MessageBox.Show("请连接仪器到通道[" + textBox_channelNo.Text + "]!");
                    label_tip.Text = "";

                    return;
                }
                label_tip.Text = "";

                string startTime = timePicker_startTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                string endTime = timePicker_endTime.Value.ToString("yyyy-MM-dd HH:mm:ss");

                string stability = "-3";
                string accuracy = "-3";
                string bootFeature = "-3";
                string ageRate = "-3";
                string interval = "";
                string stability1 = "-3";
                string stability10 = "-3";
                string stability20 = "-3";
                string stability100 = "-3";
                //开始检测
                if (checkBox_stability.Checked)
                {
                    if (checkBox1s.Checked) interval = "1,";
                    if (checkBox10s.Checked) interval = interval + "10,";
                    if (checkBox20s.Checked) interval = interval + "20,";
                    if(checkBox_100s.Checked) interval = interval + "100";

                    stability = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value <= DateTime.Now)
                    {
                        stability = "-1";
                        if (checkBox1s.Checked) stability1 = "-1";
                        if (checkBox10s.Checked) stability10 = "-1";
                        if (checkBox20s.Checked) stability20 = "-1";
                        if (checkBox_100s.Checked) stability100 = "-1";
                        detectionHelper.detecStability(deviceId, startTime, interval, port, true, false);
                    }
                    else
                    {
                        if (checkBox1s.Checked) stability1 = "-2";
                        if (checkBox10s.Checked) stability10 = "-2";
                        if (checkBox20s.Checked) stability20 = "-2";
                        if (checkBox_100s.Checked) stability100 = "-2";
                        detectionHelper.detecStability(deviceId, startTime, interval, port, false, false);
                    }


                }
                if (checkBox_accuracy.Checked)
                {
                    accuracy = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value <= DateTime.Now)
                    {
                        accuracy = "-1";
                        detectionHelper.detecAccuracy(deviceId, port, endTime, true, false);
                    }
                    else
                    {
                        detectionHelper.detecAccuracy(deviceId, port, endTime, false, false);
                    }
                }
                if (checkBox_bootFeature.Checked)
                {
                    bootFeature = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value <= DateTime.Now)
                    {
                        bootFeature = "-1";
                        detectionHelper.detecBootFeature(deviceId, startTime, port, true);
                    }
                    else
                    {
                        detectionHelper.detecBootFeature(deviceId, startTime, port, false);
                    }

                }
                if (checkBox_ageRate.Checked)
                {
                    ageRate = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value <= DateTime.Now)
                    {
                        ageRate = "-1";
                        detectionHelper.detecAgeRate(deviceId, startTime, port, true);
                    }
                    else
                    {
                        detectionHelper.detecAgeRate(deviceId, startTime, port, false);
                    }
                }
                long detectionId0 = detectionItemService.add(deviceId, startTime, endTime, stability, accuracy, bootFeature, ageRate, interval, textBox_channelNo.Text,stability1,stability10,stability20,stability100);
                detectionHelper.detectionItemId = detectionId0;
                
                this.HideMaskAction();
                HomeCtrlForm.homeCtrlForm.LoadChannelStatus();
                HomeCtrlForm.homeCtrlForm.currentPage = 1;
                HomeCtrlForm.homeCtrlForm.LoadDevToBeTested();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "开始检测错误:" + ex.Message);

            }
        }

        private void btn_downChannelList_Click(object sender, EventArgs e)
        {
            cbBox_toTestDevList.DroppedDown = true;
        }

        private void text_devCode_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void cbBox_toTestDevList_SelectedIndexChanged(object sender, EventArgs e)
        {
            text_devCode.Text = cbBox_toTestDevList.SelectedItem.ToString();
            List<DeviceDto> list = deviceService.search("", text_devCode.Text.Trim(), "", "", "", "");
            DeviceDto deviceDto = list[0];
            deviceId = deviceDto.Id;
            text_devCode.Text = deviceDto.DevCode;
            text_devName.Text = deviceDto.DevName;
            text_devType.Text = deviceDto.DevTypeName;
            text_devModel.Text = deviceDto.DevModel;
            text_customerDev.Text = deviceDto.CustomerDev;
            text_customerName.Text = deviceDto.CustomerName;
        }

       

        // 选择开始时间
        private void timePicker_startTime_ValueChanged(object sender, EventArgs e)
        {
            getEndTime();                     
        }

        private void check_startNow_CheckedChanged(object sender, EventArgs e)
        {
            if (check_startNow.Checked)
            {
                timePicker_startTime.Value = DateTime.Now;
            }
            getEndTime();
        }

        private void getEndTime()
        {
            if (checkBox_ageRate.Checked) endTimeBySystem = timePicker_startTime.Value.AddDays(7).AddMinutes(1).AddSeconds(30);
            else
            {
                if (checkBox_bootFeature.Checked) endTimeBySystem = timePicker_startTime.Value.AddHours(7).AddMinutes(1).AddSeconds(30);
                else
                { 
                    if (checkBox_stability.Checked)
                    {
                        if (checkBox_100s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 100).AddMinutes(2).AddSeconds(30);
                        else if (checkBox20s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 20).AddMinutes(2).AddSeconds(30);
                        else if (checkBox10s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101 * 10).AddMinutes(2).AddSeconds(30);
                        else if (checkBox1s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(101).AddMinutes(2).AddSeconds(30);
                    }
                    else endTimeBySystem = timePicker_startTime.Value.AddMinutes(1).AddSeconds(30);
                }
            }

            timePicker_endTime.Value = endTimeBySystem;
        }

        private void checkBox_accuracy_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox_bootFeature_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox_ageRate_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox_count_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void timePicker_endTime_ValueChanged(object sender, EventArgs e)
        {            
            
        }

        private void SetDevChannelDlg_Load(object sender, EventArgs e)
        {

        }

        private void checkBox1s_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox10s_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox20s_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void checkBox_100s_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}