308 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			308 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Drawing;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using COMM;
 | |
| using AR;
 | |
| using OpenTK.Graphics.ES11;
 | |
| 
 | |
| namespace vmsnet
 | |
| {
 | |
|     public partial class FMain
 | |
|     {
 | |
|         DateTime lasterrortime = DateTime.Now.AddDays(-10);
 | |
|         DateTime lastsavetime = DateTime.Now.AddDays(-10); // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
 | |
|         System.Text.StringBuilder SaveError;
 | |
|         int SaveCount = 0;
 | |
|         int SaveErrCount = 0;
 | |
|       
 | |
| 
 | |
|         /// <summary>
 | |
|         /// 데이터베이스에 자료를 저장한다.
 | |
|         /// </summary>
 | |
|         /// <remarks></remarks>
 | |
|         public void RUN_SAVEDATA(List<NotifyData> items)
 | |
|         {
 | |
|             //항목이 없다면 처리하지 않음
 | |
|             if (items.Any() == false) return;
 | |
|             var sorteditems = items.Where(t => t.use == true).OrderBy(t => t.chno).ToList();
 | |
|             if (sorteditems.Any() == false) return;
 | |
| 
 | |
|             //시작 후 최소 몇 초동안은 저장하지 않는다(환경설정추가)
 | |
|             TimeSpan ts = DateTime.Now - PUB.StartupTime;
 | |
|             if (ts.TotalSeconds < PUB.CONFIG.Startup_DelayTime_Save) return;
 | |
| 
 | |
|             SaveError = new System.Text.StringBuilder();
 | |
|             if (DateTime.Now.Minute % 30 == 0 && DateTime.Now.Second == 10) ////30분단위로 수행한다.
 | |
|             {
 | |
|                 ////구데이터삭제기능수행
 | |
|                 PUB.CheckFreeSpace();
 | |
|             }
 | |
| 
 | |
|             ////첫번째데이터으 시간정보를 이용한다.
 | |
|             bool SaveOn = true;
 | |
|             int Time = -1; ////데이터저장시간(일기준TIck))
 | |
|             DateTime TimeDate = DateTime.Now;// default(DateTime); ////데이터저장시간(날짜)
 | |
|             string timestr = "";
 | |
| 
 | |
|             bool[] USE = new bool[10];
 | |
|             bool[] ISNULLDATA = new bool[10];
 | |
|             System.Text.StringBuilder[] Buffer = new System.Text.StringBuilder[10];
 | |
|             for (int i = 0; i < USE.Length; i++)
 | |
|             {
 | |
|                 USE[i] = false;
 | |
|                 ISNULLDATA[i] = true;
 | |
|                 Buffer[i] = new System.Text.StringBuilder();
 | |
|             }
 | |
| 
 | |
| 
 | |
| 
 | |
|             //제공된 배열의 데이터를 추가한다.
 | |
|             foreach (var I in sorteditems)
 | |
|             {
 | |
|                 ////필수정보가 없는경우라면 넘어간다.
 | |
|                 if (I.idx_c == -1 || I.idx_u == -1 || I.idx_m == -1) continue;
 | |
| 
 | |
|                 ////처음저장데이터라면 패스한다.
 | |
|                 if (PUB.ChfirstSave[I.idx_m, I.idx_u, I.idx_c] == false)
 | |
|                 {
 | |
|                     PUB.ChfirstSave[I.idx_m, I.idx_u, I.idx_c] = true;
 | |
|                     continue;
 | |
|                 }
 | |
| 
 | |
|                 //버퍼용인덱스계산 (인덱스값을가지고 버퍼를 찾는다. 버퍼는 160개가 최대이다)
 | |
|                 var ValueIdx = PUB.ConvertChIndexToBufferIndex(I.chno);
 | |
| 
 | |
|                 //최초의 시간정보이다.
 | |
|                 if (Time == -1)
 | |
|                 {
 | |
|                     timestr = sorteditems.First().time; //  PUB.Times[I.idx_dev, I.idx_unit, I.idx_ch].Trim();
 | |
|                     if (string.IsNullOrEmpty(timestr))
 | |
|                     {
 | |
|                         //빈데이터를 생성한다. 240729
 | |
|                         Buffer[ValueIdx].Append("\t" + "");
 | |
|                         continue;
 | |
|                     }
 | |
| 
 | |
|                     TimeDate = DateTime.Parse(timestr);
 | |
|                     Time = (int)(PUB.get_TimeNumber(TimeDate)); // dt.Day * 3600 * 24 + dt.Hour * 3600 + dt.Minute * 60 + dt.Second
 | |
|                 }
 | |
| 
 | |
|                 ////최종저장시간이랑 다르면 저장한다.
 | |
|                 if (lastsavetime == TimeDate)
 | |
|                 {
 | |
|                     SaveOn = false;
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     var LastSaveTerm = TimeDate - lastsavetime;
 | |
|                     if (LastSaveTerm.TotalSeconds < PUB.CONFIG.saveterm) ////저장주기확인
 | |
|                     {
 | |
|                         SaveOn = false;
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 ////실제데이터를 저장한다.  decpos*1000 + idx ,   value  각각 2byte 씩 총 4byte * 셀카운트
 | |
|                 if (SaveOn)
 | |
|                 {
 | |
|                     float V = (float)((I.value / Math.Pow(10, I.decpos)) + I.offset);
 | |
| 
 | |
|                     ////파라미터추가
 | |
|                     USE[ValueIdx] = true;
 | |
|                     if (ISNULLDATA[ValueIdx] && V != 0) ISNULLDATA[ValueIdx] = false;
 | |
|                     Buffer[ValueIdx].Append("\t" + V.ToString());
 | |
|                 }
 | |
|             }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|             ////메인그룹 데이터 저장
 | |
|             //if (PUB.MainGroup != null && PUB.MainGroup.Any())
 | |
|             //{
 | |
|             //    foreach (var G in PUB.MainGroup)
 | |
|             //    {
 | |
|             //        foreach (var I in G.Items)
 | |
|             //        {
 | |
|             //            ////필수정보가 없는경우라면 넘어간다.
 | |
|             //            if (I.idx_ch == -1 || I.idx_dev == -1 || I.idx_unit == -1 || I.Onset == false || I.사용 == false)
 | |
|             //            {
 | |
|             //                continue;
 | |
|             //            }
 | |
| 
 | |
|             //            ////처음저장데이터라면 패스한다.
 | |
|             //            if (I.firstsave)
 | |
|             //            {
 | |
|             //                I.firstsave = false;
 | |
|             //                continue;
 | |
|             //            }
 | |
| 
 | |
|             //            //버퍼용인덱스계산 (인덱스값을가지고 버퍼를 찾는다. 버퍼는 160개가 최대이다)
 | |
|             //            var ValueIdx = PUB.ConvertChIndexToBufferIndex(I.idx);
 | |
| 
 | |
| 
 | |
|             //            if (Time == -1) ////최초의 시간정보이다.
 | |
|             //            {
 | |
|             //                timestr = PUB.Times[I.idx_dev, I.idx_unit, I.idx_ch].Trim();
 | |
|             //                if (string.IsNullOrEmpty(timestr))
 | |
|             //                {
 | |
|             //                    //빈데이터를 생성한다. 240729
 | |
|             //                    Buffer[ValueIdx].Append("\t" + "");
 | |
|             //                    continue;
 | |
|             //                }
 | |
| 
 | |
|             //                TimeDate = DateTime.Parse(timestr);
 | |
|             //                Time = (int)(PUB.get_TimeNumber(TimeDate)); // dt.Day * 3600 * 24 + dt.Hour * 3600 + dt.Minute * 60 + dt.Second
 | |
|             //            }
 | |
| 
 | |
|             //            ////최종저장시간이랑 다르면 저장한다.
 | |
|             //            if (lastsavetime == TimeDate)
 | |
|             //            {
 | |
|             //                SaveOn = false;
 | |
|             //            }
 | |
|             //            else
 | |
|             //            {
 | |
|             //                var LastSaveTerm = TimeDate - lastsavetime;
 | |
|             //                if (LastSaveTerm.TotalSeconds < PUB.CONFIG.saveterm) ////저장주기확인
 | |
|             //                {
 | |
|             //                    SaveOn = false;
 | |
|             //                }
 | |
|             //            }
 | |
| 
 | |
|             //            ////실제데이터를 저장한다.  decpos*1000 + idx ,   value  각각 2byte 씩 총 4byte * 셀카운트
 | |
|             //            if (SaveOn)
 | |
|             //            {
 | |
|             //                float V = (float)(I.value2 / Math.Pow(10, I.decpos));
 | |
| 
 | |
| 
 | |
| 
 | |
|             //                ////파라미터추가
 | |
|             //                USE[ValueIdx] = true;
 | |
|             //                if (ISNULLDATA[ValueIdx] && V != 0) ISNULLDATA[ValueIdx] = false;
 | |
|             //                Buffer[ValueIdx].Append("\t" + V.ToString());
 | |
|             //            }
 | |
|             //        }
 | |
|             //    }
 | |
| 
 | |
|             //}
 | |
| 
 | |
|             ////보조그룹 데이터 저장
 | |
|             //if (PUB.SubGroup != null && PUB.SubGroup.Any()) ////subwindow가 켜졌다면 서브윈도우도 변경한다.
 | |
|             //{
 | |
|             //    foreach (HMI.CGROUP G in PUB.SubGroup)
 | |
|             //    {
 | |
|             //        foreach (HMI.CITEM I in G.Items)
 | |
|             //        {
 | |
| 
 | |
|             //            if (I.idx_ch == -1 || I.idx_dev == -1 || I.idx_unit == -1 || I.Onset == false || I.사용 == false) continue;
 | |
| 
 | |
|             //            if (I.firstsave)
 | |
|             //            {
 | |
|             //                I.firstsave = false; ////처음저장데이터라면 패스한다.
 | |
|             //                continue;
 | |
|             //            }
 | |
| 
 | |
|             //            if (Time == -1) ////최초의 시간정보이다.
 | |
|             //            {
 | |
|             //                timestr = PUB.Times[I.idx_dev, I.idx_unit, I.idx_ch].Trim();
 | |
|             //                if (timestr.isEmpty()) continue;
 | |
| 
 | |
|             //                TimeDate = DateTime.Parse(timestr);
 | |
|             //                Time = (int)(PUB.get_TimeNumber(TimeDate));
 | |
|             //            }
 | |
| 
 | |
|             //            ////최종저장시간이랑 다르면 저장한다.
 | |
|             //            if (lastsavetime == TimeDate) SaveOn = false;
 | |
| 
 | |
|             //            ////실제데이터를 저장한다.  decpos*1000 + idx ,   value  각각 2byte 씩 총 4byte * 셀카운트
 | |
|             //            if (SaveOn)
 | |
|             //            {
 | |
|             //                float V = (float)(I.value2 / Math.Pow(10, I.decpos));
 | |
| 
 | |
|             //                //버퍼용인덱스계산 (인덱스값을가지고 버퍼를 찾는다. 버퍼는 160개가 최대이다)
 | |
|             //                var ValueIdx = PUB.ConvertChIndexToBufferIndex(I.idx);
 | |
| 
 | |
|             //                ////파라미터추가
 | |
|             //                USE[ValueIdx] = true;
 | |
|             //                if (ISNULLDATA[ValueIdx] && V != 0) ISNULLDATA[ValueIdx] = false;
 | |
|             //                Buffer[ValueIdx].Append("\t" + V.ToString());
 | |
|             //            }
 | |
|             //        }
 | |
|             //    }
 | |
|             //}
 | |
| 
 | |
| 
 | |
| 
 | |
|             ////최종저장시간을 저장한다.
 | |
|             var useCnt = USE.Where(t => t == true).Count();
 | |
|             if (useCnt > 0 && SaveOn && (TimeDate.Year > 2000) && (TimeDate.Year < 2050))
 | |
|             {
 | |
|                 SaveErrCount = 0;
 | |
|                 SaveCount = 0;
 | |
| 
 | |
|                 for (int i = 0; i < USE.Length; i++)
 | |
|                 {
 | |
|                     //데이터가있다면 저장한다.
 | |
|                     if (USE[i] && ISNULLDATA[i] == false)
 | |
|                     {
 | |
|                         try
 | |
|                         {
 | |
|                             PUB.DB.InsertData(TimeDate, $"DATAB{i + 1}", Buffer[i]);
 | |
|                             SaveCount++;
 | |
|                         }
 | |
|                         catch (Exception ex)
 | |
|                         {
 | |
|                             SaveErrCount++;
 | |
|                             SaveError.AppendLine($"{i + 1}:" + ex.ToString());
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 ////ka정보를 그룹별로 저장한다. 150310
 | |
|                 System.Text.StringBuilder KaBuffer = new System.Text.StringBuilder();
 | |
|                 System.Text.StringBuilder KaHeader = new System.Text.StringBuilder();
 | |
|                 KaHeader.Append("\t" + "Time");
 | |
| 
 | |
|                 if (PUB.MainGroup != null && PUB.MainGroup.Any())
 | |
|                 {
 | |
|                     foreach (HMI.CGROUP G in PUB.MainGroup)
 | |
|                     {
 | |
|                         KaHeader.Append("\t" + G.이름);
 | |
|                         KaBuffer.Append("\t" + G._amp.ToString());
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 if (PUB.SubGroup != null && PUB.SubGroup.Any())
 | |
|                 {
 | |
|                     foreach (HMI.CGROUP G in PUB.SubGroup)
 | |
|                     {
 | |
|                         KaHeader.Append("\t" + G.이름);
 | |
|                         KaBuffer.Append("\t" + G._amp.ToString());
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 KaHeader.Append("\r\n");
 | |
|                 PUB.KADB.InsertData(TimeDate, KaBuffer.ToString(), KaHeader);
 | |
| 
 | |
|                 if (SaveErrCount == 0)
 | |
|                 {
 | |
|                     lastsavetime = TimeDate; ////최종저장시간
 | |
|                     //this.lb_Saved.Text = "<LastSaved(" + System.Convert.ToString(savcnt) + "):" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ">";
 | |
|                 }
 | |
|                 else
 | |
|                 {
 | |
|                     lasterrortime = DateTime.Now;
 | |
|                     PUB.log.Add(AR.Log.ETYPE.ERROR, "Save Error Message=" + SaveError.ToString());
 | |
|                     //this.lb_Saved.Text = "<Save Error-" + System.Convert.ToString(errcnt) + ">";
 | |
|                 }
 | |
|             }
 | |
|             else { }
 | |
|             //log.Add(AR.Log.ETYPE.NORMAL, "No Save : " & SaveOn.ToString() & ", year = " + Year.ToString() & ", month=" & Month.ToString)
 | |
|         }
 | |
|     }
 | |
| }
 |