Beispiel #1
0
        /// <summary>
        /// Заполнить объект dest из файла событий FileName
        /// </summary>
        protected void FillObj(object dest)
        {
            Stream       stream   = null;
            BinaryReader reader   = null;
            DateTime     fillTime = DateTime.Now;

            EventTableLight eventTableLight = null;
            DataTable       dataTable       = null;

            try
            {
                if (dest is EventTableLight)
                {
                    eventTableLight = dest as EventTableLight;
                }
                else if (dest is DataTable)
                {
                    dataTable = dest as DataTable;
                }
                else
                {
                    throw new ScadaException("Destination object is invalid.");
                }

                // определение даты событий в таблице
                DateTime date = ExtractDate(tableName);

                // подготовка объекта для хранения данных
                if (eventTableLight != null)
                {
                    eventTableLight.Clear();
                    eventTableLight.TableName = tableName;
                }
                else // dataTable != null
                {
                    // формирование структуры таблицы
                    dataTable.BeginLoadData();
                    dataTable.DefaultView.Sort = "";

                    if (dataTable.Columns.Count == 0)
                    {
                        dataTable.Columns.Add("Number", typeof(int));
                        dataTable.Columns.Add("DateTime", typeof(DateTime)).DefaultValue = date;
                        dataTable.Columns.Add("ObjNum", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("KPNum", typeof(int)).DefaultValue         = 0;
                        dataTable.Columns.Add("ParamID", typeof(int)).DefaultValue       = 0;
                        dataTable.Columns.Add("CnlNum", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("OldCnlVal", typeof(double)).DefaultValue  = 0.0;
                        dataTable.Columns.Add("OldCnlStat", typeof(int)).DefaultValue    = 0;
                        dataTable.Columns.Add("NewCnlVal", typeof(double)).DefaultValue  = 0.0;
                        dataTable.Columns.Add("NewCnlStat", typeof(int)).DefaultValue    = 0;
                        dataTable.Columns.Add("Checked", typeof(bool)).DefaultValue      = false;
                        dataTable.Columns.Add("UserID", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("Descr", typeof(string));
                        dataTable.Columns.Add("Data", typeof(string));
                        dataTable.DefaultView.AllowNew    = false;
                        dataTable.DefaultView.AllowEdit   = false;
                        dataTable.DefaultView.AllowDelete = false;
                    }
                    else
                    {
                        DataColumn colDateTime = dataTable.Columns["DateTime"];
                        if (colDateTime != null)
                        {
                            colDateTime.DefaultValue = date;
                        }
                        dataTable.Rows.Clear();
                    }
                }

                // заполнение таблицы из файла
                stream = ioStream == null ?
                         new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) :
                         ioStream;
                reader = new BinaryReader(stream);

                Byte[] eventBuf = new byte[EventDataSize]; // буфер данных события
                int    evNum    = 1;                       // порядковый номер события

                while (stream.Position < stream.Length)
                {
                    int readSize = reader.Read(eventBuf, 0, EventDataSize);
                    if (readSize == EventDataSize)
                    {
                        // создание события на основе считанных данных
                        EventTableLight.Event ev = new EventTableLight.Event();
                        ev.Number = evNum;
                        evNum++;

                        double time = BitConverter.ToDouble(eventBuf, 0);
                        ev.DateTime = ScadaUtils.CombineDateTime(date, time);

                        ev.ObjNum     = BitConverter.ToUInt16(eventBuf, 8);
                        ev.KPNum      = BitConverter.ToUInt16(eventBuf, 10);
                        ev.ParamID    = BitConverter.ToUInt16(eventBuf, 12);
                        ev.CnlNum     = BitConverter.ToUInt16(eventBuf, 14);
                        ev.OldCnlVal  = BitConverter.ToDouble(eventBuf, 16);
                        ev.OldCnlStat = eventBuf[24];
                        ev.NewCnlVal  = BitConverter.ToDouble(eventBuf, 25);
                        ev.NewCnlStat = eventBuf[33];
                        ev.Checked    = eventBuf[34] > 0;
                        ev.UserID     = BitConverter.ToUInt16(eventBuf, 35);
                        ev.Descr      = BytesToStr(eventBuf, 37);
                        ev.Data       = BytesToStr(eventBuf, 138);

                        // создание строки заполняемой таблицы
                        if (eventTableLight != null)
                        {
                            eventTableLight.AllEvents.Add(ev); // быстрее, чем eventTableLight.AddEvent(ev)
                        }
                        else // dataTable != null
                        {
                            DataRow row = dataTable.NewRow();
                            row["Number"]     = ev.Number;
                            row["DateTime"]   = ev.DateTime;
                            row["ObjNum"]     = ev.ObjNum;
                            row["KPNum"]      = ev.KPNum;
                            row["ParamID"]    = ev.ParamID;
                            row["CnlNum"]     = ev.CnlNum;
                            row["OldCnlVal"]  = ev.OldCnlVal;
                            row["OldCnlStat"] = ev.OldCnlStat;
                            row["NewCnlVal"]  = ev.NewCnlVal;
                            row["NewCnlStat"] = ev.NewCnlStat;
                            row["Checked"]    = ev.Checked;
                            row["UserID"]     = ev.UserID;
                            row["Descr"]      = ev.Descr;
                            row["Data"]       = ev.Data;
                            dataTable.Rows.Add(row);
                        }
                    }
                }
            }
            catch (EndOfStreamException)
            {
                // нормальная ситуация окончания файла
            }
            catch
            {
                fillTime = DateTime.MinValue;
                throw;
            }
            finally
            {
                if (fileMode)
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }

                if (eventTableLight != null)
                {
                    eventTableLight.LastFillTime = fillTime;
                }
                else if (dataTable != null)
                {
                    dataTable.EndLoadData();
                    dataTable.AcceptChanges();
                    dataTable.DefaultView.Sort = "Number";
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Populate the dest object from the FileName event file.
        /// </summary>
        protected void FillObj(object dest)
        {
            Stream       stream   = null;
            BinaryReader reader   = null;
            var          fillTime = DateTime.Now;

            EventTableLight eventTableLight = null;
            DataTable       dataTable       = null;

            try {
                if (dest is EventTableLight)
                {
                    eventTableLight = dest as EventTableLight;
                }
                else if (dest is DataTable)
                {
                    dataTable = dest as DataTable;
                }
                else
                {
                    throw new ScadaException("Destination object is invalid.");
                }

                // determining the date of events in the table
                var date = ExtractDate(tableName);

                // storage facility preparation
                if (eventTableLight != null)
                {
                    eventTableLight.Clear();
                    eventTableLight.TableName = tableName;
                }
                else     // dataTable != null
                // forming the table structure
                {
                    dataTable.BeginLoadData();
                    dataTable.DefaultView.Sort = "";

                    if (dataTable.Columns.Count == 0)
                    {
                        dataTable.Columns.Add("Number", typeof(int));
                        dataTable.Columns.Add("DateTime", typeof(DateTime)).DefaultValue = date;
                        dataTable.Columns.Add("ObjNum", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("KPNum", typeof(int)).DefaultValue         = 0;
                        dataTable.Columns.Add("ParamID", typeof(int)).DefaultValue       = 0;
                        dataTable.Columns.Add("CnlNum", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("OldCnlVal", typeof(double)).DefaultValue  = 0.0;
                        dataTable.Columns.Add("OldCnlStat", typeof(int)).DefaultValue    = 0;
                        dataTable.Columns.Add("NewCnlVal", typeof(double)).DefaultValue  = 0.0;
                        dataTable.Columns.Add("NewCnlStat", typeof(int)).DefaultValue    = 0;
                        dataTable.Columns.Add("Checked", typeof(bool)).DefaultValue      = false;
                        dataTable.Columns.Add("UserID", typeof(int)).DefaultValue        = 0;
                        dataTable.Columns.Add("Descr", typeof(string));
                        dataTable.Columns.Add("Data", typeof(string));
                        dataTable.DefaultView.AllowNew    = false;
                        dataTable.DefaultView.AllowEdit   = false;
                        dataTable.DefaultView.AllowDelete = false;
                    }
                    else
                    {
                        var colDateTime = dataTable.Columns["DateTime"];
                        if (colDateTime != null)
                        {
                            colDateTime.DefaultValue = date;
                        }
                        dataTable.Rows.Clear();
                    }
                }

                // filling a table from a file
                stream = ioStream ?? new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                reader = new BinaryReader(stream);

                var eventBuf = new byte[EventDataSize]; // event data buffer
                var evNum    = 1;                       // event sequence number

                while (stream.Position < stream.Length)
                {
                    int readSize = reader.Read(eventBuf, 0, EventDataSize);
                    if (readSize == EventDataSize)
                    {
                        // create event based on read data
                        var ev = new EventTableLight.Event {
                            Number = evNum
                        };
                        evNum++;

                        double time = BitConverter.ToDouble(eventBuf, 0);
                        ev.DateTime = ScadaUtils.CombineDateTime(date, time);

                        ev.ObjNum     = BitConverter.ToUInt16(eventBuf, 8);
                        ev.KPNum      = BitConverter.ToUInt16(eventBuf, 10);
                        ev.ParamID    = BitConverter.ToUInt16(eventBuf, 12);
                        ev.CnlNum     = BitConverter.ToUInt16(eventBuf, 14);
                        ev.OldCnlVal  = BitConverter.ToDouble(eventBuf, 16);
                        ev.OldCnlStat = eventBuf[24];
                        ev.NewCnlVal  = BitConverter.ToDouble(eventBuf, 25);
                        ev.NewCnlStat = eventBuf[33];
                        ev.Checked    = eventBuf[34] > 0;
                        ev.UserID     = BitConverter.ToUInt16(eventBuf, 35);
                        ev.Descr      = BytesToStr(eventBuf, 37);
                        ev.Data       = BytesToStr(eventBuf, 138);

                        // create row of filled table
                        if (eventTableLight != null)
                        {
                            eventTableLight.AllEvents.Add(ev); // faster than eventTableLight.AddEvent (ev)
                        }
                        else                                   // dataTable != null
                        {
                            var row = dataTable.NewRow();
                            row["Number"]     = ev.Number;
                            row["DateTime"]   = ev.DateTime;
                            row["ObjNum"]     = ev.ObjNum;
                            row["KPNum"]      = ev.KPNum;
                            row["ParamID"]    = ev.ParamID;
                            row["CnlNum"]     = ev.CnlNum;
                            row["OldCnlVal"]  = ev.OldCnlVal;
                            row["OldCnlStat"] = ev.OldCnlStat;
                            row["NewCnlVal"]  = ev.NewCnlVal;
                            row["NewCnlStat"] = ev.NewCnlStat;
                            row["Checked"]    = ev.Checked;
                            row["UserID"]     = ev.UserID;
                            row["Descr"]      = ev.Descr;
                            row["Data"]       = ev.Data;
                            dataTable.Rows.Add(row);
                        }
                    }
                }
            } catch (EndOfStreamException) {
                // normal file end situation
            } catch {
                fillTime = DateTime.MinValue;
                throw;
            } finally {
                if (fileMode)
                {
                    reader?.Close();
                    stream?.Close();
                }

                if (eventTableLight != null)
                {
                    eventTableLight.LastFillTime = fillTime;
                }

                if (dataTable != null)
                {
                    dataTable.EndLoadData();
                    dataTable.AcceptChanges();
                    dataTable.DefaultView.Sort = "Number";
                }
            }
        }