Newer
Older
RbFreqStand / RbFreqStandMeasure / home / SetDevChannelDlg.cs
yangqianqian on 15 Apr 2021 11 KB after test
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();

        public Action HideMaskAction
        {
            get;
            set;
        }
        public object SeriralPort { get; private set; }

        public SetDevChannelDlg(int channelNo)
        {
            InitializeComponent();

            textBox_channelNo.Text = channelNo.ToString();
            timePicker_startTime.Value = DateTime.Now;
            timePicker_startTime.MinDate = DateTime.Now;

            List<DeviceDto> listToTest = deviceService.search("", "", "", "", "", "1");
            foreach (DeviceDto deviceDto in listToTest)
            {
                cbBox_toTestDevList.Items.Add(deviceDto.DevCode);
            }
        }

        private void btnExitChannel_Click(object sender, EventArgs e)
        {
            this.HideMaskAction();
        }

        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 && !radio_1s.Checked && !radio_10s.Checked && !radio_20s.Checked && !radio_100s.Checked)
                {
                    MessageBox.Show("请选择频率稳定度采样时间!");
                    return;
                }
                if (!checkBox_accuracy.Checked && !checkBox_stability.Checked && !checkBox_ageRate.Checked && !checkBox_bootFeature.Checked)
                {
                    MessageBox.Show("请选择检测项目!");
                    return;
                }
                // 打开串口
                DetectionHelper detectionHelper = new DetectionHelper();
                string portName = ConfigHelper.GetAppConfig("channel" + textBox_channelNo.Text).Split(' ')[0];
                int band = Convert.ToInt32(ConfigHelper.GetAppConfig("channel" + textBox_channelNo.Text).Split(' ')[1]);
                SerialPort port = detectionHelper.portOpen(portName, band);
                if (port == null)
                {
                    MessageBox.Show("无法打开串口,添加检测失败!");
                    return;
                }
                DateTime startTime = timePicker_startTime.Value;
                DateTime endTime = timePicker_endTime.Value;

                string stability = "-3";
                string accuracy = "-3";
                string bootFeature = "-3";
                string ageRate = "-3";
                string interval = "";
                //开始检测
                if (checkBox_stability.Checked)
                {

                    if (radio_1s.Checked) interval = "1";
                    else if (radio_10s.Checked) interval = "10";
                    else if (radio_20s.Checked) interval = "20";
                    else if (radio_100s.Checked) interval = "100";

                    stability = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value == DateTime.Now)
                    {
                        stability = "-1";
                        detectionHelper.detecStability(deviceId, startTime, interval, port, true);
                    }
                    else
                    {
                        detectionHelper.detecStability(deviceId, startTime, interval, port, false);
                    }


                }
                if (checkBox_accuracy.Checked)
                {
                    accuracy = "-2";
                    if (check_startNow.Checked || timePicker_startTime.Value == DateTime.Now)
                    {
                        accuracy = "-1";
                        detectionHelper.detecAccuracy(deviceId, port, endTime, true);
                    }
                    else
                    {
                        detectionHelper.detecAccuracy(deviceId, port, endTime, 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);
                    }
                }

                detectionItemService.add(deviceId, timePicker_startTime.Value, timePicker_endTime.Value, stability, accuracy, bootFeature, ageRate, interval, textBox_channelNo.Text);

                this.HideMaskAction();
                HomeCtrlForm.homeCtrlForm.LoadChannelStatus();
                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;
            }
        }

        private void getEndTime()
        {
            if (checkBox_ageRate.Checked) endTimeBySystem = timePicker_startTime.Value.AddDays(7);
            else
            {
                if (checkBox_bootFeature.Checked) endTimeBySystem = timePicker_startTime.Value.AddHours(7);
                else
                { 
                    if (checkBox_stability.Checked)
                    {
                        if (radio_1s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(100 * 1);
                        else if (radio_10s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(100 * 10);
                        else if (radio_20s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(100 * 20);
                        else if (radio_100s.Checked) endTimeBySystem = timePicker_startTime.Value.AddSeconds(100 * 100);
                    }
                    else endTimeBySystem = DateTime.Now;
                }
            }

            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)
        {            
            if (timePicker_endTime.Value < endTimeBySystem)
            {
                MessageBox.Show("选择的结束时间早于检测需要的时间,选择无效!");
                timePicker_endTime.Value = endTimeBySystem;
            }
        }
    }
}