Beispiel #1
0
        /// <summary>
        /// 将数据写入数据库
        /// </summary>
        /// <param name="dataString"></param>
        private static void WriteIntoDatabase(byte[] dataByte, DateTime dateTime, OleDbConnection oleDbCon)
        {
            DatabaseCore dataCore = new DatabaseCore(oleDbCon);

            //数据库列名
            string[] colName = { "ID",                    "Year",                  "Month",                 "Day", "Hour", "Minute", "Second",
                                 "WindSpeed(m/s)",        "AirTemperayure",        "Rasiation(W/m2)",
                                 "WindDirection",         "Humidity(%RH)",
                                 "Component2Temperature", "Component3Temperature",
                                 "Component4Temperature", "Component5Temperature", "Component6Temperature", };
            //string colString = "Year, Month, Day, Hour, Minute, Second, WindSpeed(m/s), AirTemperayure, Rasiation(W/m2), WindDirection, Humidity(%RH), Component1Temperature, Component2Temperature, Component3Temperature,Component4Temperature, Component5Temperature, Component6Temperature";
            //string valueString = "";//要插入的语句
            Dictionary <string, string> QXdataDic = new Dictionary <string, string>();

            //添加日期数据
            int year   = dateTime.Year;
            int month  = dateTime.Month;
            int day    = dateTime.Day;
            int hour   = dateTime.Hour;
            int minute = dateTime.Minute;
            int second = dateTime.Second;

            int[] time = { year, month, day, hour, minute, second };
            for (int i = 0; i < time.Length; i++)
            {
                QXdataDic.Add(colName[i + 1], time[i].ToString());                                                              //第一列不添加
            }

            //添加气象仪数据
            int[]    index     = { 1, 3, 6, 7, 9, 11, 12, 13, 14, 15 };                                 //有效通道
            double[] precision = { 0.1, 0.1, 1.0, 1.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, };                 //每个有效通道数值的精度
            for (int i = 0; i < index.Length; i++)
            {
                int k     = (index[i] + 1) * 2;                                         //前面两个字内容无效, 所以+2 , 但由于dataByte的下标从零开始, 所以要-1 即: (index[i]-1+2),  每个数据占两个字节, 所以*2
                int value = (dataByte[k] << 8) + dataByte[k + 1];                       //高位左移8位地位  =  实际值
                if (value >> 15 == 1)                                                   //如果最高位为1, 则取补,  否则不改变
                {
                    value = -(0x10000 - value);
                }
                double dvalue = value * precision[i];                                           //取精度
                QXdataDic.Add(colName[i + 7], dvalue.ToString());
            }
            try
            {
                dataCore.InsertData("MeteorologicalData", QXdataDic);                   //插入数据
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool Analy(DateTime time, List <KeyValuePair <byte, bool> > messageQueue, OleDbConnection oleDbCon, int componentId, int azimuth, int obliquity)
        {
            DatabaseCore dataCore = new DatabaseCore(oleDbCon);
            Dictionary <string, string> IVdataDic = new Dictionary <string, string>();

            byte[] byteArray = messageQueue.Select(b => b.Key).ToArray();
            string byteStr   = Transfer.BaToS(byteArray);

            string Regex = @"AA0012019001([A-Za-z0-9_]{800})CC33C33CAA0012029090([A-Za-z0-9_]{800})CC33C33CAA0012091100([A-Za-z0-9_]{34})CC33C33C";
            Regex  Re    = new Regex(Regex);


            if (Re.IsMatch(byteStr))
            {
                Match  byteMatch  = Re.Match(byteStr);
                string reasultStr = byteMatch.Groups[3].Value;
                int    Tep        = Convert.ToInt32(Inverse(reasultStr.Substring(2, 4)), 16);
                double Vo         = Convert.ToInt32(Inverse(reasultStr.Substring(10, 4)), 16) / 10.0;
                double Is         = Convert.ToInt32(Inverse(reasultStr.Substring(14, 4)), 16) / 100.0;
                double Vm         = Convert.ToInt32(Inverse(reasultStr.Substring(18, 4)), 16) / 10.0;
                double Im         = Convert.ToInt32(Inverse(reasultStr.Substring(22, 4)), 16) / 100.0;
                double Pm         = Convert.ToInt64(Inverse(reasultStr.Substring(26, 8)), 16) / 10.0;

                int index = -1;
                //不用while,方朝增本来也这么做,也死循环;每次解析一条就好了
                if ((index = byteStr.IndexOf(byteMatch.Value, index + 1)) != -1)
                {
                    for (int i = index / 2; i < (index + byteMatch.Length) / 2; i++)
                    {
                        messageQueue[i] = new KeyValuePair <byte, bool>(messageQueue[i].Key, false);
                    }
                    index = -1;
                }

                int year   = time.Year;
                int month  = time.Month;
                int day    = time.Day;
                int hour   = time.Hour;
                int minute = time.Minute;
                int second = time.Second;
                IVdataDic.Add("Year", year.ToString());
                IVdataDic.Add("Month", month.ToString());
                IVdataDic.Add("Day", day.ToString());
                IVdataDic.Add("Hour", hour.ToString());
                IVdataDic.Add("Minute", minute.ToString());
                IVdataDic.Add("Second", second.ToString());
                IVdataDic.Add("ComponentId", componentId.ToString());
                IVdataDic.Add("Component1Temperature", Tep.ToString());
                IVdataDic.Add("OpenCircuitVoltage", Vo.ToString());
                IVdataDic.Add("ShortCircuitCurrent", Is.ToString());
                IVdataDic.Add("MaxPowerVoltage", Vm.ToString());
                IVdataDic.Add("MaxPowerCurrent", Im.ToString());
                IVdataDic.Add("MaxPower", Pm.ToString());
                IVdataDic.Add("Azimuth", azimuth.ToString());
                IVdataDic.Add("Obliquity", obliquity.ToString());
                IVdataDic.Add("CurrentSeq", "'" + byteMatch.Groups[1].Value + "'");
                IVdataDic.Add("VoltageSeq", "'" + byteMatch.Groups[2].Value + "'");

                try
                {
                    dataCore.InsertData("dbo_IVTable", IVdataDic);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


            return(true);
        }
        public override bool Analyze(Status status)
        {
            DatabaseCore dataCore = new DatabaseCore(status.OleDbCon);
            Dictionary <string, string> IVdataDic = new Dictionary <string, string>();

            byte[] byteArray = status.MessageQueue.Select(b => b.Key).ToArray();
            string byteStr   = Transfer.BaToS(byteArray);

            string headString = "AA000105CC33C33CAA00002102CC33C33CAA00142402CC33C33C";
            int    matchIndex = FaultTolerantMatch.Match(byteStr, headString, 10);

            if (matchIndex == -1)
            {
                return(false);
            }

            string regexHead = byteStr.Substring(matchIndex, headString.Length);

            //数据部分不检查;
            string regexData = @"([A-Za-z0-9_]{12})([A-Za-z0-9_]{800})([A-Za-z0-9_]{20})([A-Za-z0-9_]{800})([A-Za-z0-9_]{20})([A-Za-z0-9_]{34})([A-Za-z0-9_]{8})";
            //                   Groups1            Groups2            Groups3          Groups4             Groups5          Groups6           Groups7

            Regex Re = new Regex(regexHead + regexData);

            if (!Re.IsMatch(byteStr))
            {
                return(false);
            }

            Match  byteMatch  = Re.Match(byteStr);
            string reasultStr = byteMatch.Groups[6].Value;
            int    Tep        = Convert.ToInt32(Inverse(reasultStr.Substring(2, 4)), 16);
            double Vo         = Convert.ToInt32(Inverse(reasultStr.Substring(10, 4)), 16) / 10.0;
            double Is         = Convert.ToInt32(Inverse(reasultStr.Substring(14, 4)), 16) / 100.0;
            double Vm         = Convert.ToInt32(Inverse(reasultStr.Substring(18, 4)), 16) / 10.0;
            double Im         = Convert.ToInt32(Inverse(reasultStr.Substring(22, 4)), 16) / 100.0;
            double Pm         = Convert.ToInt64(Inverse(reasultStr.Substring(26, 8)), 16) / 10.0;

            int index = -1;

            if ((index = byteStr.IndexOf(byteMatch.Value, index + 1)) != -1)
            {
                for (int i = index / 2; i < (index + byteMatch.Length) / 2; i++)
                {
                    status.MessageQueue[i] = new KeyValuePair <byte, bool>(status.MessageQueue[i].Key, false);
                }
            }

            int year   = status.Time.Year;
            int month  = status.Time.Month;
            int day    = status.Time.Day;
            int hour   = status.Time.Hour;
            int minute = status.Time.Minute;
            int second = status.Time.Second;

            IVdataDic.Add("Year", year.ToString());
            IVdataDic.Add("Month", month.ToString());
            IVdataDic.Add("Day", day.ToString());
            IVdataDic.Add("Hour", hour.ToString());
            IVdataDic.Add("Minute", minute.ToString());
            IVdataDic.Add("Second", second.ToString());
            IVdataDic.Add("ComponentId", status.ComponentId.ToString());
            IVdataDic.Add("Component1Temperature", Tep.ToString());
            IVdataDic.Add("OpenCircuitVoltage", Vo.ToString());
            IVdataDic.Add("ShortCircuitCurrent", Is.ToString());
            IVdataDic.Add("MaxPowerVoltage", Vm.ToString());
            IVdataDic.Add("MaxPowerCurrent", Im.ToString());
            IVdataDic.Add("MaxPower", Pm.ToString());
            IVdataDic.Add("Azimuth", status.Azimuth.ToString());
            IVdataDic.Add("Obliquity", status.Obliquity.ToString());
            IVdataDic.Add("CurrentSeq", "'" + byteMatch.Groups[2].Value + "'");
            IVdataDic.Add("VoltageSeq", "'" + byteMatch.Groups[4].Value + "'");

            try
            {
                dataCore.InsertData("dbo_IVTable", IVdataDic);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }