Newer
Older
RbFreqStand / RbFreqStandMeasure / info / AddDevDlg.cs
yxw on 1 Jun 2021 19 KB 20210601合并测试
using Casic.Birmm.RbFreqStandMeasure.counter;
using Casic.Birmm.RbFreqStandMeasure.home;
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 DevComponents.DotNetBar.Controls;
using DevComponents.Editors;
using Newtonsoft.Json.Linq;
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.info
{
    public partial class AddDevDlg : 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

        DictService dictService = new DictServiceImpl();
        DetectionItemService detectionItemService = new DetectionItemServiceImpl();
        CounterDetecInitService counterDetecInitService = new CounterDetecInitServiceImpl();
        CounterDataService counterDataService = new CounterDataServiceImpl();
        SensitivityService sensitivityService = new SensitivityServiceImpl();


        public long deviceId = -1;
        public static bool isInDetection = false;
        public Action HideMaskAction
        {
            get;
            set;
        }

        public AddDevDlg()
        {
            InitializeComponent();

            List<Dict> dictList = dictService.getTypeListByCodeType("devType");
            cbBox_devType.DataSource = dictList;
            cbBox_devType.DisplayMember = "Name";
            cbBox_devType.ValueMember = "Code";
           
            btnSave.Text = "保存";
            if (HomeCtrlForm.channelFreeList.Count > 0)
            {
                cbBox_channelFree.Items.Add("");
                foreach (string item in HomeCtrlForm.channelFreeList)
                {
                    cbBox_channelFree.Items.Add(item);
                }
            }
            else
            {
                text_channelNo.WatermarkText = "无空闲通道";
                groupBox1.Enabled = false;
            }


        }

        // 关闭对话框
        private void btnAddDevCancel_Click(object sender, EventArgs e)
        {
            this.HideMaskAction();
            RbFreqStdMeas.rbFreqStdMeas.TopMost = true;
            RbFreqStdMeas.rbFreqStdMeas.TopMost = false;
        }

        // 添加并开始检测
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (inputDevName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入仪器名称!");return;
            }
            if (inputDevCode.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入仪器编号!"); return;
            }
            if (inputDevModel.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入仪器型号!"); return;
            }
            if (text_devType.Text.Trim().Equals(""))
            {
                MessageBox.Show("请选择仪器类型!"); return;
            }
            if (inputCustComp.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入送检单位!"); return;
            }
            if (inputCustName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入联系人!"); return;
            }
            if (text_channelNo.Text.Trim() != "")
            {
                if (checkBox_stability.Checked && !checkBox1s.Checked && !checkBox10s.Checked && !checkBox20s.Checked && !checkBox100s.Checked)
                {
                    MessageBox.Show("请选择频率稳定度采样时间!");
                    return;
                }
                if (!checkBox_accuracy.Checked && !checkBox_stability.Checked && !checkBox_ageRate.Checked && !checkBox_bootFeature.Checked)
                {
                    MessageBox.Show("请选择检测项目!");
                    return;
                }
            }
            if (checkBox_accuracy.Checked || checkBox_stability.Checked || checkBox_ageRate.Checked || checkBox_bootFeature.Checked)
            {
                if (text_channelNo.Text.Trim() == "")
                {
                    MessageBox.Show("请选择通道号!");
                    return;
                }
            }
            

            // 取得输入框的参数
            string devName = ((TextBoxX) this.Controls.Find("inputDevName", true)[0]).Text; 
            string devCode = ((TextBoxX) this.Controls.Find("inputDevCode", true)[0]).Text;
            string devModel = ((TextBoxX)this.Controls.Find("inputDevModel", true)[0]).Text;
            string devCustomComp = ((TextBoxX)this.Controls.Find("inputCustComp", true)[0]).Text;
            string devCustomName = ((TextBoxX)this.Controls.Find("inputCustName", true)[0]).Text;
            string devTypeCode = "1";
            foreach (Dict dict in (List<Dict>)cbBox_devType.DataSource)
            {
                if (text_devType.Text.Equals(dict.Name)) devTypeCode = dict.Code;
            }
            //string devTypeCode = cbBox_devType.SelectedValue.ToString();
            string channelNo = text_channelNo.Text.Trim(); 
            string statusId = "";

           
            // 初始化数据访问接口
            DeviceService devService = new DeviceServiceImpl();
            bool needDetec = false;
            string startTime = "";
            string endTime = "";


            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";
            string portName = "";
            SerialPort port = new SerialPort();

            DetectionHelper detectionHelper = new DetectionHelper();
            if (!isInDetection && !channelNo.Equals(""))
            {                
                port.Dispose();
                port = RbFreqStdMeas.portList[Convert.ToInt32(text_channelNo.Text) - 1];
                picker_endTime.Value = endTimeBySystem;
                startTime = picker_startTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                endTime = picker_endTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "结束时间: " + endTime);
                needDetec = true;
                portName =   ConfigHelper.GetAppConfig("channel" + text_channelNo.Text).Split(' ')[0];
                int band =Convert.ToInt32(ConfigHelper.GetAppConfig("channel" + text_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("无法打开通道" + text_channelNo.Text + ",添加检测失败!");
                        return;
                    }
                    else
                    {
                        RbFreqStdMeas.portList[Convert.ToInt32(text_channelNo.Text) - 1] = port;
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show("无法打开通道" + text_channelNo.Text + ",添加检测失败!");
                    return;
                }

                try
                {
                    label_tip.Text = "正在从通道[" + channelNo + "]获取数据,请稍侯......";
                    if (DetectionHelper.detectionHelper.getFrequencyData(port, true).Equals(""))
                    {
                        label_tip.Text = "";
                        MessageBox.Show("请连接仪器到通道[" + channelNo + "]!");
                        return;
                    }
                    label_tip.Text = "";
                }
                catch (Exception exc)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "。。。..."+exc.Message);
                }
            }
            
            if (labelTitle.Text.Equals("添加"))
            {
                statusId = "1";
                if(needDetec) statusId = "2";
                deviceId = devService.add(devName, devCode, devTypeCode, devModel, devCustomComp, devCustomName, channelNo, statusId);
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "添加"+ deviceId);

                CounterCtrlForm.GetInstance().addCounterItems();

                if (deviceId == -1)
                {
                    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "添加仪器[" + devName + "]失败");
                    MessageBox.Show("添加仪器[" + devName + "]失败", "错误");
                    this.HideMaskAction();
                    return;
                }
            }
            else if (labelTitle.Text.Equals("修改"))
            {
                if (needDetec) statusId = "2";
                devService.update(deviceId, devName, devCode, devTypeCode, devModel, devCustomComp, devCustomName, channelNo,statusId);
                CounterCtrlForm.GetInstance().addCounterItems();
            }


            //添加检测
            if (deviceId != -1 && needDetec)
            {                
                
                //开始检测
                //稳定度
                if (checkBox_stability.Checked)
                {
                    if (checkBox1s.Checked) interval = "1,";
                    if (checkBox10s.Checked) interval = interval + "10,";
                    if (checkBox20s.Checked) interval = interval + "20,";
                    if (checkBox100s.Checked) interval = interval + "100";

                    stability = "-2";

                    if (checkBox_now.Checked || picker_startTime.Value <= DateTime.Now)
                    {
                        stability = "-1";
                        if (checkBox1s.Checked) stability1 = "-1";
                        if (checkBox10s.Checked) stability10 = "-1";
                        if (checkBox20s.Checked) stability20 = "-1";
                        if (checkBox100s.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 (checkBox100s.Checked) stability100 = "-2";
                        detectionHelper.detecStability(deviceId, startTime, interval, port, false, false);
                    }
                }
                // 准确度
                if (checkBox_accuracy.Checked)
                {
                    accuracy = "-2";
                    if (checkBox_now.Checked || picker_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 (checkBox_now.Checked || picker_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 (checkBox_now.Checked || picker_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, text_channelNo.Text, stability1, stability10, stability20, stability100);
                detectionHelper.detectionItemId = detectionId0;

                HomeCtrlForm.homeCtrlForm.refreshChannelList();
            }

            if (labelTitle.Text.Equals("添加")) MessageBox.Show("添加成功!");            
            else MessageBox.Show("修改成功!");

            // 添加成功,继续保存检测项目和开始时间
            this.HideMaskAction();
            InfoCtrlForm.infoCtrlForm.LoadDevList(new JObject());

        }

        private void btn_downChannelList_Click(object sender, EventArgs e)
        {
            if (isInDetection)
            {
                MessageBox.Show("此仪器正在检测中,无法修改通道!");
                return;
            }
            cbBox_channelFree.DroppedDown = true;
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {
            cbBox_channelFree.DroppedDown = false;
            cbBox_devType.DroppedDown = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            cbBox_channelFree.DroppedDown = false;
            cbBox_devType.DroppedDown = false;
        }

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

        private void checkBox_startNow_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_now.Checked)picker_startTime.Value = DateTime.Now;
            getEndTime();
        }

        private void cbBox_devType_SelectedIndexChanged(object sender, EventArgs e)
        {
             text_devType.Text = ((Dict)cbBox_devType.SelectedItem).Name;           
        }

        private void cbBox_channelFree_SelectedIndexChanged(object sender, EventArgs e)
        {
            text_channelNo.Text = cbBox_channelFree.SelectedItem.ToString();
            if (text_channelNo.Text.Equals(""))
            {
                groupBox1.Enabled = false;
                checkBox_accuracy.Checked = false;
                checkBox_stability.Checked = false;
                checkBox_ageRate.Checked = false;
                checkBox_bootFeature.Checked = false;
            }
            else
            {
                groupBox1.Enabled = true;
                checkBox_now.Enabled = true;
            }
        }

        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {

        }

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

        private void AddDevDlg_Shown(object sender, EventArgs e)
        {
            //if (isInDetection) btn_downChannelList.Visible = false;
            //else btn_downChannelList.Visible = true;
        }

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

        DateTime endTimeBySystem = new DateTime();

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

            picker_endTime.Value = endTimeBySystem;
        }

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

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

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

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

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

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

        private void picker_endTime_ValueChanged(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 checkBox100s_CheckedChanged(object sender, EventArgs e)
        {
            getEndTime();
        }
    }
}