示例#1
0
        public void GetStatonPIEs(string jsonFilePath)
        {
            // 1. 讀取路徑內檔案
            DirectoryInfo dir = new DirectoryInfo(jsonFilePath);

            FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories);

            Stations = new List <Station1>();

            // 2. 讀取 staion 資料
            foreach (FileInfo file in files)
            {
                // 2. read 所有設定
                using (StreamReader r = new StreamReader(file.FullName))
                {
                    string   json2  = r.ReadToEnd();
                    Station1 items2 = JsonConvert.DeserializeObject <Station1>(json2);
                    Stations.Add(items2);
                }
            }
        }
示例#2
0
        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);
        }
示例#3
0
        private void CSVInput(Station1 sta)
        {
            logView.Add("讀取資料:" + sta.StationName);

            // 更新最後讀取資料
            cgiLastFiles.Reload();

            // 清除舊資料
            sta.InputItems.Clear();

            sta.LastFileName = cgiLastFiles.DataItems.Where(x => x.Key == sta.StationName).Count() > 0
                ? cgiLastFiles.DataItems.Where(x => x.Key == sta.StationName).FirstOrDefault().Value : "";

            // 讀取 CSV file
            DirectoryInfo dir = new DirectoryInfo(sta.InputPath);

            String sLastFileName72Hr = default(DateTime).ToString("yyyyMMddHHmmss") + ".txt";

            if (sta.LastFileTime != default(DateTime))
            {
                sLastFileName72Hr = sta.LastFileTime.AddHours(-72).ToString("yyyyMMddHHmmss") + ".txt";
            }

            // 1.找出最後一筆記錄及之前 72 小時記錄
            FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories)
                               .Where(x => x.Name.Contains(".csv") && string.Compare(x.Name, sLastFileName72Hr) >= 0)
                               .OrderBy(x => x.Name).ToArray();

            // 增加到 staion
            foreach (FileInfo file in files)
            {
                try
                {   // 2.Open the text file using a stream reader.
                    List <dynamic> records;
                    using (var reader = new StreamReader(file.FullName))
                    {
                        using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                        {
                            records = csv.GetRecords <dynamic>().ToList();

                            var                record   = records[0];
                            var                obj1     = record as System.Dynamic.ExpandoObject;
                            DateTime           tim      = default(DateTime);
                            Station1.InputItem dataitem = new Station1.InputItem()
                            {
                                Header = obj1.Select(a => a.Key).ToList(),
                                Line   = obj1.Select(a => a.Value.ToString()).ToList()
                            };

                            // 加入 tim
                            DateTime.TryParseExact(dataitem.Line[dataitem.Header.IndexOf("TIM")], "yyyy-MM-ddTHH:mm:ss+08:00", null, System.Globalization.DateTimeStyles.None, out tim);
                            dataitem.TIM = tim;

                            dataitem.FileName = file.Name;

                            sta.InputItems.Add(dataitem);
                        }
                    }
                    // 記錄最後一次檔名
                    sta.NewLastFileName = file.Name;
                }
                catch (IOException err)
                {
                    logView.Add("讀取CGI檔案錯誤: " + file.Name + ", ERROR." + err.Message);
                }
            }

            foreach (Station1.ConfigFile cfg in sta.ConfigFiles)
            {
                logView.Add("統計資料中: " + cfg.Tag);

                // 計算資料
                cfg.Lines = DoAcc(sta, cfg.Tag);

                // 正式寫入檔案 HH
                DoOutputHH(cfg, sta.OutputPath);
                // 正式寫入檔案 month
                DoOutputMonth(cfg, sta.OutputPath);
            }

            // 最新最後資料
            cgiLastFiles.UpdateLastFile(sta.StationName, sta.NewLastFileName);
        }