//file 선택
        public void eventlog_parsing_micro_clip()
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title    = "file open";
            ofd.FileName = "test";
            ofd.Filter   = "event log 파일|*.evl";

            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                dt       = new DataTable();
                fileName = ofd.SafeFileName;
                filePath = ofd.FileName;
                FileInfo fi = new FileInfo(filePath);
                fileLength = fi.Length;

                if (!fileName.Contains("MC"))
                {
                    MessageBox.Show("Wrong file!");
                }
                else
                {
                    string[] log_distinct_bits = new string[fileLength / 8];

                    BinaryReader rdr          = new BinaryReader(File.Open(filePath, FileMode.Open));
                    byte[]       Header_bytes = new byte[2];
                    byte[]       info_bytes   = new byte[16];

                    int i = 0;

                    SetUpData();

                    while (rdr.BaseStream.Position < fileLength)
                    {
                        do
                        {
                            Header_bytes[i] = rdr.ReadByte();
                        } while (Header_bytes[i] == 0xff);

                        if (i > 0 && Header_bytes[i - 1] == 0x53 && Header_bytes[i] == 0x4e) //find "SN"
                        {
                            string   sn = ""; int header_cnt = 0; string size = ""; string dock_time = "";
                            DateTime DockTime = new DateTime();
                            byte     s;

                            //header
                            while (rdr.BaseStream.Position < fileLength)
                            {
                                s = rdr.ReadByte();
                                //serial number
                                if (header_cnt == 0 && s == 0x3a)
                                {
                                    s = rdr.ReadByte();
                                    while (s != 0x0d)
                                    {
                                        sn += Char.ConvertFromUtf32(s);
                                        s   = rdr.ReadByte();
                                    }
                                }
                                //DockTime
                                if (header_cnt == 1 && s == 0x3a)
                                {
                                    s = rdr.ReadByte();
                                    while (s != 0x0d)
                                    {
                                        dock_time += Char.ConvertFromUtf32(s);
                                        s          = rdr.ReadByte();
                                    }
                                    string[] logtime = dock_time.Split(':', ' ', '-');
                                    DockTime = new DateTime(int.Parse(logtime[0]), int.Parse(logtime[1]), int.Parse(logtime[2]), int.Parse(logtime[3]), int.Parse(logtime[4]), int.Parse(logtime[5]));
                                }
                                //Size
                                if (header_cnt == 4 && s == 0x3a)
                                {
                                    s = rdr.ReadByte();
                                    while (s != 0x0d)
                                    {
                                        size += Char.ConvertFromUtf32(s);
                                        s     = rdr.ReadByte();
                                    }
                                }
                                if (s == 0x0d && rdr.ReadByte() == 0x0a)
                                {
                                    header_cnt++;
                                }
                                if (header_cnt == 5)
                                {
                                    break;
                                }
                            }

                            int sz = Convert.ToInt32(size);

                            //timeStartsSeconds
                            string[] reverse = new string[4];
                            info_bytes = rdr.ReadBytes(4);
                            BitArray bits = new BitArray(info_bytes);
                            for (int n = 0; n < 32; n++)
                            {
                                reverse[n / 8] = make_bit(bits[n]) + reverse[n / 8];
                            }
                            uint timeStartSeconds = Convert.ToUInt32(reverse[3] + reverse[2] + reverse[1] + reverse[0], 2);

                            //eventlog parsing
                            do
                            {
                                info_bytes = rdr.ReadBytes(16);
                                sz        -= 16;
                                reverse    = new string[16];
                                string distinct = "";
                                bits = new BitArray(info_bytes);
                                for (int n = 0; n < 128; n++)
                                {
                                    reverse[n / 8] = make_bit(bits[n]) + reverse[n / 8];
                                    distinct      += make_bit(bits[n]);
                                }
                                //중복 제외
                                if (!log_distinct_bits.Contains(distinct) && distinct.Contains("0"))
                                {
                                    log_distinct_bits[dt.Rows.Count] = distinct;
                                    uint   log_seconds = Convert.ToUInt32(reverse[3] + reverse[2] + reverse[1] + reverse[0], 2);
                                    double devicetime  = (double)log_seconds - timeStartSeconds;

                                    DateTime date_time = DockTime.AddSeconds(devicetime);

                                    string event_type = status(reverse[15] + reverse[14]);

                                    String H2S = GetOLLimitValuesToString((Convert.ToInt16(reverse[5] + reverse[4], 2) / 10.0).ToString("0.0"));
                                    String CO  = GetOLLimitValuesToString((Convert.ToInt16(reverse[7] + reverse[6], 2) / 10.0).ToString("0.0"));
                                    String O2  = GetOLLimitValuesToString((Convert.ToInt16(reverse[9] + reverse[8], 2) / 10.0).ToString("0.0"));
                                    String LEL = GetOLLimitValuesToString((Convert.ToInt16(reverse[11] + reverse[10], 2) / 10.0).ToString("0.0"));

                                    int Duration = Convert.ToUInt16(reverse[13] + reverse[12], 2);


                                    dt.Rows.Add(sn, date_time.ToString("yyyy/MM/dd HH:mm:ss"), event_type, bump, H2S, CO, O2, LEL, Duration, "Active");
                                }
                            } while (sz >= 16);
                        }
                        i++;
                        i = i % 2;
                    }
                    rdr.Close();
                }
            }
        }
Esempio n. 2
0
        //file 선택
        public void datalog_parsing_micro_clip(TextBox tb, CheckBox cb)
        {
            if (cb.Checked)
            {
                cb_checked = true;
            }
            else
            {
                cb_checked = false;
            }
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title    = "file open";
            ofd.FileName = "test";
            ofd.Filter   = "data log from IDX|*.dal|data log from Microdock|*.BWC";
            byte[]       info_bytes = new byte[28];
            DialogResult dr         = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                dt       = new DataTable();
                fileName = ofd.SafeFileName;
                filePath = ofd.FileName;
                FileInfo fi = new FileInfo(filePath);



                fileLength = fi.Length;
                string[] lines;
                byte[]   file;
                byte[]   line = new byte[4];



                if (Path.GetExtension(ofd.FileName) == ".BWC")
                {
                    DateTime filetime = File.GetLastWriteTime(filePath);

                    int seek = 0;
                    tb.Text = "Reading...";
                    tb.Refresh();
                    file  = File.ReadAllBytes(filePath);
                    lines = File.ReadAllLines(filePath);

                    if (!lines[0].Equals("GasAlert Microclip data log file"))
                    {
                        tb.Text = "It is not Microclip Data log file!\n";
                        tb.Refresh();
                        return;
                    }
                    else if (lines[1].Length > 17 || lines[1].Length < 16)
                    {
                        tb.Text = "Device's Serial Number is invalid!\n";
                        tb.Refresh();
                        return;
                    }
                    sn = lines[1].Split()[1];
                    if (lines[2].Contains("Count:"))
                    {
                        curConst = UInt32.Parse(lines[2].Split(':')[2]);
                    }
                    else
                    {
                        seek = lines[0].Length + 2 + lines[1].Length + 2;
                        for (int j = 0; j < 4; j++)
                        {
                            line[j] = file[seek++];
                        }
                        curConst = BitConverter.ToUInt32(line, 0);
                    }
                    filetime = filetime.AddSeconds(curConst * -1);
                    seek     = lines[0].Length + 2 + lines[1].Length + 2 + lines[2].Length + 2 + lines[3].Length + 2;
                    SetUpData();

                    while (seek < file.Length - 1)
                    {
                        if (file[seek] == 0xff || file[seek] == 0x00) //unused/invalid/garbage byte
                        {
                            seek++;
                        }
                        else
                        {
                            int t = findsize(file[seek]);
                            seek++;
                            if (t != 0)
                            {
                                for (int k = 0; k < t; k++)
                                {
                                    info_bytes[k] = file[seek + k];
                                    if (info_bytes[k] == 0xff)
                                    {
                                        seek += k;
                                        break;
                                    }
                                }
                                sz    = MakeData(workRow, file[seek - 1], file.Length - seek, info_bytes, sn, filetime);
                                seek += t;
                            }
                        }
                    }
                    tb.Text = ("Finish!!");
                }



                else if (Path.GetExtension(ofd.FileName) == ".dal" && fileName.Contains("MC"))
                {
                    tb.Text = "Reading...";
                    tb.Refresh();

                    info_bytes = new byte[28];


                    file  = File.ReadAllBytes(filePath);
                    lines = File.ReadAllLines(filePath);
                    int seek = 0;

                    SetUpData();
                    while (seek < fileLength - 1)
                    {
                        if (file[seek] == 0x53 && file[seek + 1] == 0x4e) //find "SN"
                        {
                            sn = ""; int header_cnt = 0; string size = ""; string dock_time = "";
                            DateTime     DockTime = new DateTime();
                            byte         s;

                            //header
                            while (seek < fileLength - 1)
                            {
                                seek++;
                                s = file[seek];

                                //serial number
                                if (header_cnt == 0 && s == 0x3a)
                                {
                                    seek++;
                                    s = file[seek];
                                    while (s != 0x0d)
                                    {
                                        sn += Char.ConvertFromUtf32(s);
                                        seek++;
                                        s = file[seek];
                                    }
                                }
                                //DockTime
                                if (header_cnt == 1 && s == 0x3a)
                                {
                                    seek++;
                                    s = file[seek];
                                    while (s != 0x0d)
                                    {
                                        dock_time += Char.ConvertFromUtf32(s);

                                        seek++;
                                        s = file[seek];
                                    }
                                    string[] logtime = dock_time.Split(':', ' ', '-');
                                    DockTime = new DateTime(int.Parse(logtime[0]), int.Parse(logtime[1]), int.Parse(logtime[2]), int.Parse(logtime[3]), int.Parse(logtime[4]), int.Parse(logtime[5]));
                                }
                                //Size
                                if (header_cnt == 4 && s == 0x3a)
                                {
                                    seek++;
                                    s = file[seek];
                                    while (s != 0x0d)
                                    {
                                        size += Char.ConvertFromUtf32(s);
                                        seek++;
                                        s = file[seek];
                                    }
                                }
                                if (s == 0x0d && file[seek + 1] == 0x0a)
                                {
                                    header_cnt++;
                                }
                                if (header_cnt == 5)
                                {
                                    break;
                                }
                            }
                            seek += 2;
                            sz    = Convert.ToInt32(size);
                            for (int k = 0; k < 4; k++)
                            {
                                info_bytes[k] = file[seek + k];
                            }
                            seek      += 4;
                            sz        -= 4;
                            curConst   = BitConverter.ToUInt32(info_bytes, 0);
                            deviceTime = DockTime.AddSeconds(curConst * -1);
                            do
                            {
                                s = file[seek];

                                if (s == 0xff || s == 0x00) //unused/invalid/garbage byte
                                {
                                    do
                                    {
                                        seek++;
                                        sz--;
                                    } while ((s == 0xff || s == 0x00) && sz > 2);
                                }
                                else
                                {
                                    int t = findsize(s);
                                    if (t != 0)
                                    {
                                        seek++;
                                        for (int k = 0; k < t; k++)
                                        {
                                            info_bytes[k] = file[seek + k];
                                        }

                                        sz    = MakeData(workRow, file[seek - 1], sz, info_bytes, sn, deviceTime);
                                        seek += t - 1;
                                    }
                                }
                                seek++;
                                sz--;
                            } while (sz > 2);
                        }
                        seek++;
                    }
                    tb.Text = ("Finish!!");
                }
                else
                {
                    MessageBox.Show("Wrong file!");
                    dt = null;
                }
            }
        }