Newer
Older
RbFreqStand / RbFreqStandMeasure / tools / DetectionHelper.cs
yangqianqian on 12 Apr 2021 17 KB counter test
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.Tools;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Casic.Birmm.RbFreqStandMeasure.tools
{
    static class DetectionHelper
    {
        private static System.Threading.Timer timerAccuracy;
        delegate void TimerDelegate(string text);
        private static long devIdAccuracy = -1;
        private static SerialPort portAccuracy;

        private static System.Threading.Timer timerStability;
        private static long devIdStability = -1;
        private static SerialPort portStability;
        private static List<string> resultStability = new List<string>();

        private static System.Threading.Timer timerBootFeature;
        private static long devIdBootFeature = -1;
        private static SerialPort portBootFeature;
        private static List<string> resultBootFeature = new List<string>();

        private static System.Threading.Timer timerAgeRate;
        private static long devIdAgeRate = -1;
        private static SerialPort portAgeRate;
        private static List<string> resultAgeRate = new List<string>();

        private static DetectionService detectionService = new DetectionServiceImpl();
        private static DetectionItemService detectionItemService = new DetectionItemServiceImpl();
        private static DeviceService deviceService = new DeviceServiceImpl();

        //1-STABILITY,2-ACCURACY,3-BOOT_FEATURE,4-AGE_RATE

        


        public static void detecAccuracy(long deviceId, SerialPort port, DateTime endTime)
        {
            portAccuracy = port;
            devIdAccuracy = deviceId;
            TimeSpan secondSpan = new TimeSpan(endTime.Ticks - DateTime.Now.Ticks);
            int delay = secondSpan.Milliseconds;
            timerAccuracy = new System.Threading.Timer(exeAccuracy, null, delay + 200 ,60000);            
        }

        private static void exeAccuracy(Object State)
        {
            string result = "";
            double sum = 0.0;
            for (int i = 0; i < 3; i++)
            {
                string fre = getFrequencyData(portAccuracy);
                if (fre.Equals(""))
                {
                    MessageBox.Show("从"+ portAccuracy.PortName + "获取数据失败!");
                    return;
                }
                sum = sum + Convert.ToDouble(fre);
                detectionService.add(devIdAccuracy, fre, "2");

                Thread.Sleep(1000);
            }

            result = sum / 3 + "";
            detectionItemService.updateDetecStatus(devIdAccuracy, "", result, "", "", "");

            deviceService.updateStatus(devIdAccuracy, "3", "");
            timerAccuracy.Dispose();
        }

        public static void detecStability(long deviceId, DateTime startTime, string interval, SerialPort port)
        {
            portStability = port;
            devIdStability = deviceId;
            TimeSpan secondSpan = new TimeSpan(startTime.Ticks - DateTime.Now.Ticks);
            int delay = secondSpan.Milliseconds;
            timerStability = new System.Threading.Timer(exeStability, null, delay + 100, Convert.ToInt32(interval));
        }
        private static void exeStability(Object State)
        {
            string result = "";
            
           string fre = getFrequencyData(portAccuracy);
            if (fre.Equals(""))
            {
                MessageBox.Show("从" + portAccuracy.PortName + "获取数据失败!");
                return;
            }
            resultStability.Add(fre);
                
            detectionService.add(devIdStability, fre, "1");

            if (resultStability.Count == 101)
            {
                ///////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////
                ///


                detectionItemService.updateDetecStatus(devIdStability, result, "", "", "", "");

                List<DetectionItem> detectionItemList = detectionItemService.search(devIdStability, true);
                if (detectionItemList != null && detectionItemList.Count > 0)
                {
                    DetectionItem detectionItem = detectionItemList[0];
                    if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1"))
                    {
                        deviceService.updateStatus(devIdStability, "3", "");
                    }
                }               
                timerStability.Dispose();
                resultStability.Clear();
            }

            
        }

        public static void detecBootFeature(long deviceId, DateTime startTime, SerialPort port)
        {
            portBootFeature = port;
            devIdBootFeature = deviceId;
            TimeSpan secondSpan = new TimeSpan(startTime.Ticks - DateTime.Now.Ticks);
            int delay = secondSpan.Milliseconds;
            timerBootFeature = new System.Threading.Timer(exeBootFeature, null, delay + 100, 60*60*1000);
        }
        private static void exeBootFeature(Object State)
        {
            string result = "";
            double sum = 0.0;
            for(int i = 0; i<3;i++)
            {
                string fre = getFrequencyData(portAccuracy);
                if (fre.Equals(""))
                {
                    MessageBox.Show("从" + portAccuracy.PortName + "获取数据失败!");
                    return;
                }
                detectionService.add(devIdStability, fre, "3");
                sum = sum + Convert.ToDouble(fre);
            }
            resultBootFeature.Add(sum / 3 + "");
           
            if (resultBootFeature.Count == 8)
            {
                string max = "0.0";
                string min = "0.0";
                foreach (string fre in resultBootFeature)
                {
                    if (Convert.ToDouble(fre) > Convert.ToDouble(max)) max = fre;
                    if (Convert.ToDouble(fre) < Convert.ToDouble(min) || min.Equals("0.0")) min = fre;
                }
                result = (Convert.ToDouble(max) - Convert.ToDouble(min)) + "";

                detectionItemService.updateDetecStatus(devIdBootFeature, "", "", result, "", "");

                List<DetectionItem> detectionItemList = detectionItemService.search(devIdBootFeature, true);
                if (detectionItemList != null && detectionItemList.Count > 0)
                {
                    DetectionItem detectionItem = detectionItemList[0];
                    if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1"))
                    {
                        deviceService.updateStatus(devIdBootFeature, "3", "");
                    }
                }
                timerBootFeature.Dispose();
                resultBootFeature.Clear();
            }


        }

        public static void detecAgeRate(long deviceId, DateTime startTime, SerialPort port)
        {
            portAgeRate = port;
            devIdAgeRate = deviceId;
            TimeSpan secondSpan = new TimeSpan(startTime.Ticks - DateTime.Now.Ticks);
            int delay = secondSpan.Milliseconds;
            timerAgeRate = new System.Threading.Timer(exeAgeRate, null, delay + 100, 12 * 60 * 60 * 1000);
        }
        private static void exeAgeRate(Object State)
        {
            string result = "";
            double sum = 0.0;
            for (int i = 0; i < 3; i++)
            {
                string fre = getFrequencyData(portAccuracy);
                if (fre.Equals(""))
                {
                    MessageBox.Show("从" + portAccuracy.PortName + "获取数据失败!");
                    return;
                }
                detectionService.add(devIdStability, fre, "4");
                sum = sum + Convert.ToDouble(fre);
            }
            resultAgeRate.Add(sum / 3 + "");

            if (resultAgeRate.Count == 15)
            {
                ///////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////
                ///


                detectionItemService.updateDetecStatus(devIdAgeRate, "", "","", result, "");

                List<DetectionItem> detectionItemList = detectionItemService.search(devIdAgeRate, true);
                if (detectionItemList != null && detectionItemList.Count > 0)
                {
                    DetectionItem detectionItem = detectionItemList[0];
                    if (!detectionItem.Stability.Equals("-1") && !detectionItem.Accuracy.Equals("-1") && !detectionItem.AgeRate.Equals("-1") && !detectionItem.BootFeature.Equals("-1"))
                    {
                        deviceService.updateStatus(devIdAgeRate, "3", "");
                    }
                }
                timerAgeRate.Dispose();
                resultAgeRate.Clear();
            }


        }



        public static SerialPort portOpen(string portName, int bandRate)
        {
            try
            {
                SerialPort port = new SerialPort();
                // 搜索串口
                string[] names = SerialPort.GetPortNames();
                if (names.Length == 0 || Array.IndexOf(names, portName) == -1)
                {
                    MessageBox.Show("没有搜索到串口" + portName + "!");
                    return null;
                }
                // 设置串口参数
                port.PortName = names[0];
                port.BaudRate = bandRate;
                port.Parity = Parity.None;
                port.DataBits = 8;
                port.StopBits = StopBits.One;
                port.Handshake = Handshake.None;
                port.ReadTimeout = -1;
                port.WriteTimeout = 3000;

                // 打开串口
                if (!port.IsOpen)
                {
                    port.Open();
                }
                if (port.IsOpen)
                {
                    return port;
                }

                return null;
            }
            catch (Exception ex)
            {
                LogHelper.WriteInfoLog(MethodBase.GetCurrentMethod().DeclaringType, "打开串口失败!" + ex.Message);
                return null;
            }
        }

        // 获取检测数据
        public static string getFrequencyData(SerialPort port)
        {
            string received = "";
            if (ConfigHelper.GetAppConfig("deviceType").ToString().Equals("1"))
            {
                int count = 0;
                while (count < 5)
                {
                    int readLen = port.BytesToRead;
                    if (readLen > 0)
                    {
                        byte[] buffer = new byte[readLen];
                        port.Read(buffer, 0, readLen);// 接收数据到buffer里面
                        string data = Encoding.ASCII.GetString(buffer);
                        string[] dataArray = data.Split(' ');
                        if (data.StartsWith("$") && dataArray.Length == 2 && data.LastIndexOf('$') == 0)
                        {
                            received = dataArray[0].Replace("$", "") + "." + data.Split(' ')[1].Replace(".", "").TrimEnd('0');
                            break;
                        }
                    }
                    count++;
                }
            }
            else
            {
                int count = 0;
                while (count < 11)
                {
                    int readLen = port.BytesToRead;
                    if (readLen > 0)
                    {
                        byte[] buffer = new byte[readLen];
                        port.Read(buffer, 0, readLen);// 接收数据到buffer里面
                        string data = Encoding.ASCII.GetString(buffer);
                        string[] dataArray = data.Split(' ');
                        if (dataArray.Length == 3)
                        {
                            received = dataArray[2];
                            break;
                        }
                    }
                    count++;
                }
            }
            return received;
        }

        // 获取铷钟数据
        public static string getClockData(SerialPort port)
        {
            string clockStatus = "";
            int count = 0;
            while (count < 5)
            {
                int Readlen = port.BytesToRead;
                // 接收到数据
                if (Readlen > 0)
                {
                    byte[] buffer = new byte[Readlen];
                    port.Read(buffer, 0, Readlen);// 接收数据到buffer里面                    
                    string data = Encoding.ASCII.GetString(buffer);                    
                    if (data.StartsWith("$") && data.Contains("*"))
                    {      
                        string clockStatusCode = data.Split(',')[9];
                        switch (clockStatusCode)
                        {
                            case "1": clockStatus = "预热"; break;
                            case "2": clockStatus = "自由运动"; break;
                            case "3": clockStatus = "捕获"; break;
                            case "4": clockStatus = "快锁"; break;
                            case "5": clockStatus = "慢锁"; break;
                            case "7": clockStatus = "保持"; break;
                            default: break;
                        }
                        break;
                    }                    
                }
                count++;
            }

            return clockStatus;


        }

        // 获取卫星RMC数据
        public static string getSatelliteRMCData(SerialPort port)
        {
            // RMC
            string strRMC = "";

            int count = 0;
            while (count < 5)
            {
                int Readlen = port.BytesToRead;
                if (Readlen > 0)
                {
                    byte[] buffer = new byte[Readlen];
                    port.Read(buffer, 0, Readlen);// 接收数据到buffer里面
                    string data = Encoding.ASCII.GetString(buffer);
                    string[] dataArray = data.Split('$');
                    if (dataArray.Length > 9)
                    {
                        foreach (string sss in dataArray)
                        {
                            if (sss.Contains("GNRMC") && sss.Contains("*"))
                            {
                                string[] resultArray = sss.Split(',');

                                string time = resultArray[1];
                                time = time.Substring(0, 2) + ":" + time.Substring(2, 2) + ":" + time.Substring(4);
                                string status = resultArray[2]=="A"?"定位有效":"警告";
                                string date = resultArray[9];
                                date = date.Substring(4, 2) + "-" + date.Substring(2, 2) + "-" + date.Substring(0, 2);

                                strRMC = date + "," + time + "," + status;
                            }
                        }
                    }                    
                }
                count++;
            }

            return strRMC;
        }

        // 获取卫星GSV数据
        public static string getSatelliteGSVData(SerialPort port)
        {
            // RMC
            string strRMC = "";

            int count = 0;
            while (count < 5)
            {
                int Readlen = port.BytesToRead;
                if (Readlen > 0)
                {
                    byte[] buffer = new byte[Readlen];
                    port.Read(buffer, 0, Readlen);// 接收数据到buffer里面
                    string data = Encoding.ASCII.GetString(buffer);
                    string[] dataArray = data.Split('$');
                    if (dataArray.Length > 9)
                    {
                        foreach (string sss in dataArray)
                        {
                            if (sss.Contains("GNRMC") && sss.Contains("*"))
                            {
                                string[] resultArray = sss.Split(',');

                                string time = resultArray[1];
                                time = time.Substring(0, 2) + ":" + time.Substring(2, 2) + ":" + time.Substring(4);
                                string status = resultArray[2] == "A" ? "定位有效" : "警告";
                                string date = resultArray[9];
                                date = date.Substring(4, 2) + "-" + date.Substring(2, 2) + "-" + date.Substring(0, 2);

                                strRMC = date + "," + time + "," + status;
                            }
                        }
                    }
                }
                count++;
            }

            return strRMC;
        }
    }
}