public static object Acc(Station1.ConfigFile cfg, List <Station1.InputItem> rawdata, Expression expression, int CountAcc) { var result = CountAcc; // init 計算式 Expression _e = new Expression(expression.ParsedExpression, EvaluateOptions.NoCache); DateTime currentTIM = cfg.CurrentTIM; List <Station1.InputItem> lastrawdata = rawdata.Where(x => x.TIM <= currentTIM).Select(s => s).OrderByDescending(t => t.TIM).Take(CountAcc + 1).ToList(); Station1.InputItem lastDataItem = lastrawdata.FirstOrDefault(); // 處理客制 function _e.EvaluateFunction += delegate(string name, FunctionArgs args) { switch (name) { case "diff": args.Result = Diff(lastrawdata, args.Parameters[0], currentTIM); break; } }; return(_e.Evaluate()); }
private void DoOutputMonth(Station1.ConfigFile cfg, string staOutputPath) { logView.Add("執行寫入 Month 檔案程式: " + cfg.Tag); if (cfg.Lines.Count > 0) { // 2.正式寫入檔案 HH DateTime MinTIM = cfg.Lines.Min(x => x.TIM); DateTime MaxTIM = cfg.Lines.Max(x => x.TIM); TimeSpan timeSpan = MaxTIM - MinTIM; for (int imonth = 0; imonth <= E1Functions.GetMonthDifference(MaxTIM, MinTIM); imonth++) { string filename = MinTIM.AddMonths(imonth).ToString("yyyyMM") + ".txt"; DateTime dtFrom = MinTIM.AddMonths(imonth).TruncateToMonthStart(); DateTime dtTo = MinTIM.AddMonths(imonth + 1).TruncateToMonthStart(); List <List <string> > lines = (cfg.Lines.Where(x => x.TIM >= dtFrom && x.TIM < dtTo).Select(l => l.ColItems)).ToList(); if (lines.Count > 0) { CSVWriteFile(cfg.Title, lines, staOutputPath + @"\" + cfg.OutputPath + @"\" + filename); } } logView.Add("寫入 Month 檔案完成: " + cfg.Tag); } else { logView.Add("無 Month 資料更新: " + cfg.Tag); } }
private void DoOutputHH(Station1.ConfigFile cfg, string staOutputPath) { logView.Add("執行寫入HH檔案程式: " + cfg.Tag); if (cfg.Lines.Count > 0) { // 2.正式寫入檔案 HH DateTime MinTIM = cfg.Lines.Min(x => x.TIM); DateTime MaxTIM = cfg.Lines.Max(x => x.TIM); TimeSpan timeSpan = MaxTIM - MinTIM; for (int ihour = 0; ihour <= Math.Ceiling((MaxTIM - MinTIM).TotalHours); ihour++) { string filename = MinTIM.AddHours(ihour).ToString("yyyyMMddHH") + ".txt"; DateTime dtFrom = MinTIM.AddHours(ihour).TruncateToHourStart(); DateTime dtTo = MinTIM.AddHours(ihour + 1).TruncateToHourStart(); List <List <string> > lines = (cfg.Lines.Where(x => x.TIM >= dtFrom && x.TIM < dtTo).Select(l => l.ColItems)).ToList(); if (lines.Count > 0) { CSVWriteFile(cfg.Title, lines, staOutputPath + @"\" + cfg.OutputPath + @"\" + filename); } } logView.Add("寫入 HH 檔案完成: " + cfg.Tag); } else { logView.Add("無 HH 資料更新: " + cfg.Tag); } }
private static List <Station1.ConfigFile.Line> DoAcc(Station1 sta, string tag) { List <Station1.InputItem> processInputItem = sta.InputItems.Where(x => string.Compare(x.FileName, sta.LastFileName) > 0).ToList(); Station1.ConfigFile cfg = sta.ConfigFiles.Where(x => x.Tag == tag).FirstOrDefault(); cfg.Lines.Clear(); // 先清空, 避免累積 // 無資料返回 if (cfg == null) { return(null); } // 1.讀取資料 foreach (Station1.InputItem inputitem in processInputItem) { List <string> outputline = new List <string>(); DateTime tim = default(DateTime); // 處理 TIM if (inputitem.Header.IndexOf("TIM") != -1) { DateTime.TryParseExact(inputitem.Line[inputitem.Header.IndexOf("TIM")], "yyyy-MM-ddTHH:mm:ss+08:00", null, System.Globalization.DateTimeStyles.None, out tim); cfg.CurrentTIM = tim; } foreach (string col in cfg.Col) { int colIndex = inputitem.Header.IndexOf(col); string colData = ""; // 處理特殊欄位 if (col == "TIMD") { colIndex = inputitem.Header.IndexOf("TIM"); colData = inputitem.Line[colIndex].Substring(0, 10); } else if (col == "TIMT") { colIndex = inputitem.Header.IndexOf("TIM"); colData = inputitem.Line[colIndex].Substring(11, 8); } else if (col == "TIM") { // 特別客製, TIM 產生二個欄位 colIndex = inputitem.Header.IndexOf("TIM"); colData = inputitem.Line[colIndex].Substring(0, 10); outputline.Add(colData); colData = inputitem.Line[colIndex].Substring(11, 8); } else if (colIndex == -1) { // 關鍵字 calc() 為 string exp = Regex.Match(col, @"(?<=calc\()(.*)(?=\))").Groups[1].Value; if (string.IsNullOrEmpty(exp)) { // 輸入文字,直接輸入 colData = col; } else { // 檢查是否為 acc, 得到 #筆數 bool isAcc = (Regex.Match(col, @"(?<=acc\()(.*)(?=\))").Groups[1].Value != ""); int countAcc = 0; if (isAcc) { countAcc = int.TryParse(Regex.Match(col, @"(?<=#)([^\)]*)").Groups[1].Value, out countAcc) ? countAcc : 0; exp = exp.Replace("#" + countAcc.ToString(), ""); } // init 計算式 Expression e = new Expression(exp, EvaluateOptions.NoCache); // 處理參數 e.EvaluateParameter += delegate(string name, ParameterArgs args) { int _colIndex = inputitem.Header.IndexOf(name); if (_colIndex != -1) { args.Result = inputitem.Line[_colIndex].Trim(); } }; // 處理客制 function e.EvaluateFunction += delegate(string name, FunctionArgs args) { switch (name) { case "diff": //args.Result = Diff(pe, sta.DataItems, args.Parameters[0]); DateTime currentTIM = cfg.CurrentTIM; List <Station1.InputItem> lastrawdata = sta.InputItems.Where(x => x.TIM <= currentTIM).Select(s => s).OrderByDescending(t => t.TIM).Take(2).ToList(); Station1.InputItem lastDataItem = lastrawdata.FirstOrDefault(); args.Result = Diff(lastrawdata, args.Parameters[0], currentTIM); break; case "acc": args.Result = Acc(cfg, sta.InputItems, args.Parameters[0], countAcc); break; } }; // 計算式,數字去尾數 0.100 -> 0.1 colData = DoExpressionEvaluate(e); } } else { // 一般取值,不計算 colData = inputitem.Line[colIndex].Trim(); } outputline.Add(colData); } if (outputline.Count > 0) { cfg.Lines.Add(new Station1.ConfigFile.Line { TIM = tim, ColItems = outputline }); } } return(cfg.Lines); }