private void WriteBnyData(string fileName, string citPath, List <ChannelNew> channelNewList)
        {
            CitFileHelper citHelper = new CitFileHelper();

            using (FileStream fs = new FileStream(citPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                using (BinaryWriter bw = new BinaryWriter(fs, Encoding.Default))
                {
                    DataHeadInfo           dhi         = citHelper.GetDataInfoHead(citPath);
                    long                   pos         = bw.BaseStream.Length;
                    List <DataChannelInfo> channelList = citHelper.GetDataChannelInfoHead(citPath);
                    int    iChannelNumberSize          = dhi.iChannelNumber * 2;
                    byte[] dataArray = new byte[iChannelNumberSize];

                    List <Byte> dataList   = new List <Byte>();
                    short       tmpRmsData = 0;

                    Byte[] tmpBytes = new Byte[2];
                    while (true)
                    {
                        if (channelDataList.ContainsKey(fileName) && channelDataList[fileName].Count > 0)
                        {
                            List <float[]> channelData = channelDataList[fileName].Dequeue();
                            DateTime       dt2         = DateTime.Now;
                            if (channelData == null || isCancel)
                            {
                                break;
                            }
                            long iArrayLen = channelData[0].Length;
                            for (int k = 0; k < iArrayLen; k++)
                            {
                                if (citHelper.IsEncrypt(dhi))
                                {
                                    for (int iTmp = 0; iTmp < channelNewList.Count; iTmp++)
                                    {
                                        if (channelNewList[iTmp].BNYChannelId > 6)
                                        {
                                            tmpRmsData = (short)((channelData[iTmp][k] / 10 - channelList[iTmp].fOffset) * channelList[iTmp].fScale);
                                        }
                                        else
                                        {
                                            tmpRmsData = (short)((channelData[iTmp][k] - channelList[iTmp].fOffset) * channelList[iTmp].fScale);
                                        }
                                        tmpBytes = CitFileHelper.ByteXORByte(BitConverter.GetBytes(tmpRmsData));
                                        dataList.AddRange(tmpBytes);
                                    }
                                }
                                else
                                {
                                    for (int iTmp = 0; iTmp < channelNewList.Count; iTmp++)
                                    {
                                        try
                                        {
                                            if (channelNewList[iTmp].BNYChannelId > 6)
                                            {
                                                tmpRmsData = (short)((channelData[iTmp][k] / 10 - channelList[iTmp].fOffset) * channelList[iTmp].fScale);
                                            }
                                            else
                                            {
                                                tmpRmsData = (short)((channelData[iTmp][k] - channelList[iTmp].fOffset) * channelList[iTmp].fScale);
                                            }
                                            dataList.AddRange(BitConverter.GetBytes(tmpRmsData));
                                        }
                                        catch (Exception ex)
                                        {
                                            throw ex;
                                        }
                                    }
                                }
                                ///bw.BaseStream.Position = 100;
                                bw.Write(dataList.ToArray());
                                bw.Flush();

                                dataList.Clear();
                            }
                        }
                    }
                    //bw.Close();
                    //DateTime dt3 = DateTime.Now;
                    //Console.WriteLine("two :" + (dt3 - dt2).TotalSeconds.ToString());
                }

                //fs.Close();
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取BNY数据并向cit文件中写入数据
        /// </summary>
        /// <param name="channelList"></param>
        private void GetBnyDataAndWriteCit(List <ChannelNew> channelList)
        {
            int[] bnyChannelArr = channelList.Where(s => s.BNYChannelId >= 0).Select(s => s.BNYChannelId).ToArray();

            int  sampleNum     = 1000;
            long startPosition = 0;
            long endPosition   = 0;

            long bnyFileLength = common.GetBNYLastPosition(bnyFilePath);

            int pageCount = Convert.ToInt32(bnyFileLength / (sampleNum * BNYFile.GetChannelSize()));

            List <float[]> bnyList = new List <float[]>();

            try
            {
                using (FileStream fs = new FileStream(createCitFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs, Encoding.Default))
                    {
                        DataHeadInfo           dhi             = citHelper.GetDataInfoHead(createCitFilePath);
                        List <DataChannelInfo> channelInfoList = citHelper.GetDataChannelInfoHead(createCitFilePath);
                        int    iChannelNumberSize = dhi.iChannelNumber * 2;
                        byte[] dataArray          = new byte[iChannelNumberSize];

                        List <Byte> dataList   = new List <Byte>();
                        short       tmpRmsData = 0;;
                        Byte[]      tmpBytes   = new Byte[2];
                        for (int i = 0; i < pageCount; i++)
                        {
                            bnyList       = common.GetBNYData(bnyFilePath, sampleNum, startPosition, ref endPosition);
                            startPosition = endPosition;

                            List <float[]> resultList = new List <float[]>();
                            //Action<float> action = new Action<float>({ });
                            for (int j = 0; j < channelList.Count; j++)
                            {
                                float[] fvalue = new float[sampleNum];

                                if (channelList[j].BNYChannelId >= 0)
                                {
                                    fvalue = bnyList[channelList[j].BNYChannelId];
                                }
                                resultList.Add(fvalue);
                            }

                            long iArrayLen = resultList[0].Length;
                            for (int k = 0; k < iArrayLen; k++)
                            {
                                if (citHelper.IsEncrypt(dhi))
                                {
                                    for (int iTmp = 0; iTmp < channelList.Count; iTmp++)
                                    {
                                        if (channelList[iTmp].BNYChannelId > 6)
                                        {
                                            tmpRmsData = (short)((resultList[iTmp][k] / 10 - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale);
                                        }
                                        else
                                        {
                                            tmpRmsData = (short)((resultList[iTmp][k] - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale);
                                        }
                                        tmpBytes = CitFileHelper.ByteXORByte(BitConverter.GetBytes(tmpRmsData));
                                        dataList.AddRange(tmpBytes);
                                    }
                                }
                                else
                                {
                                    for (int iTmp = 0; iTmp < channelList.Count; iTmp++)
                                    {
                                        try
                                        {
                                            if (channelList[iTmp].BNYChannelId > 6)
                                            {
                                                tmpRmsData = (short)((resultList[iTmp][k] / 10 - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale);
                                            }
                                            else
                                            {
                                                tmpRmsData = (short)((resultList[iTmp][k] - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale);
                                            }
                                            dataList.AddRange(BitConverter.GetBytes(tmpRmsData));
                                        }
                                        catch (Exception ex)
                                        {
                                            throw ex;
                                        }
                                    }
                                }
                                ///bw.BaseStream.Position = 100;
                                bw.Write(dataList.ToArray());
                                bw.Flush();

                                dataList.Clear();
                            }
                            if (token.IsCancellationRequested)
                            {
                                token.ThrowIfCancellationRequested();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }