Exemple #1
0
        public static IEnumerable <MeterData> QueryData(this YokogawaFile yokoFile, DataQueryCriteria criteria)
        {
            var data = GetData(yokoFile);

            if (data == null)
            {
                return(null);
            }

            if (!string.IsNullOrEmpty(criteria.HeaderPattern))
            {
                data = data.Where(x => Regex.IsMatch(x.Header, criteria.HeaderPattern));
            }

            if (criteria.StartDate.HasValue)
            {
                data = data.Where(x => x.TimeStamp >= criteria.StartDate.Value);
            }

            if (criteria.EndDate.HasValue)
            {
                data = data.Where(x => x.TimeStamp < criteria.EndDate.Value);
            }

            return(data);
        }
Exemple #2
0
 public static string GetContent(this YokogawaFile yokoFile)
 {
     if (yokoFile == null)
     {
         return(null);
     }
     return(File.ReadAllText(yokoFile.FilePath));
 }
Exemple #3
0
        public FileImport ImportFile(int index)
        {
            using (var uow = DataManager.Current.StartUnitOfWork())
            {
                YokogawaFile yokoFile = FileUtility.GetFile(index);

                if (yokoFile == null)
                {
                    return(null);
                }

                FileImport result = uow.ImportFile(yokoFile);

                return(result);
            }
        }
Exemple #4
0
        public static MeterData CreateData(this YokogawaFile yokoFile, int lineIndex, DateTime timeStamp, string header, double value)
        {
            if (yokoFile == null)
            {
                return(null);
            }

            return(new MeterData
            {
                FileIndex = yokoFile.Index,
                LineIndex = lineIndex,
                TimeStamp = timeStamp,
                Header = header,
                Value = value
            });
        }
Exemple #5
0
        public static IEnumerable <MeterData> GetData(this YokogawaFile yokoFile)
        {
            if (yokoFile != null)
            {
                string[] headers   = null;
                int      lineIndex = 1;

                foreach (string line in File.ReadAllLines(yokoFile.FilePath))
                {
                    if (line.StartsWith("Time Stamp,"))
                    {
                        headers = line.Split(',');
                    }
                    else if (headers != null && line.Length > 23)
                    {
                        string   datestr = line.Substring(0, 23);
                        DateTime date    = DateTime.MinValue;

                        if (DateTime.TryParse(datestr, out date))
                        {
                            //we only take the sample at midnight, otherwise there are way too many rows
                            if (FileUtility.TakeSample(date))
                            {
                                string[] items = line.Split(',');
                                if (items.Length == headers.Length)
                                {
                                    //start at 1 to skip the timestamp column
                                    for (int i = 1; i < headers.Length; i++)
                                    {
                                        //example: we only care about headers than end with "[Liters]- Max"
                                        //  HeaderPattern: \[Liters\] - Max$
                                        if (Regex.IsMatch(headers[i], Config.Current.HeaderPattern))
                                        {
                                            yield return(yokoFile.CreateData(lineIndex, date, headers[i], double.Parse(items[i])));
                                        }
                                    }
                                }
                            }
                        }
                    }

                    lineIndex += 1;
                }
            }
        }
Exemple #6
0
        public static YokogawaFile Create(string filePath)
        {
            // "F:\\yokogawa-reports\\recorder-01\\002567_170202_065400_DAD_TABULAR.csv";

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(string.Format("File not found: {0}", filePath));
            }

            var fileInfo = new FileInfo(filePath);

            if (fileInfo.Length == 0)
            {
                throw new Exception(string.Format("File is empty: {0}", filePath));
            }

            var fileName = Path.GetFileNameWithoutExtension(filePath);

            var matches = Regex.Match(fileName, "([0-9]{1,6})_([0-9]{1,6})_([0-9]{1,6})_DAD_TABULAR");

            if (matches.Success)
            {
                YokogawaFile result = new YokogawaFile();

                result.FilePath = filePath;

                result.Index = int.Parse(matches.Groups[1].Value);

                var datePart = DateTime.ParseExact(matches.Groups[2].Value, "yyMMdd", CultureInfo.InvariantCulture);

                var timePart = TimeSpan.ParseExact(matches.Groups[3].Value, "hhmmss", CultureInfo.InvariantCulture);;

                result.Date = datePart.Add(timePart);

                return(result);
            }
            else
            {
                throw new ArgumentException("Invalid file path.", "filePath");
            }
        }
Exemple #7
0
        public IEnumerable <MeterData> QueryFileData([FromBody] DataQueryCriteria criteria, int index)
        {
            YokogawaFile yokoFile = FileUtility.GetFile(index);

            return(yokoFile.QueryData(criteria));
        }
Exemple #8
0
        public IEnumerable <MeterData> GetFileData(int index)
        {
            YokogawaFile yokoFile = FileUtility.GetFile(index);

            return(yokoFile.GetData());
        }
Exemple #9
0
        public string GetFileContent(int index)
        {
            YokogawaFile yokoFile = FileUtility.GetFile(index);

            return(yokoFile.GetContent());
        }
Exemple #10
0
        public FileImport ImportFile(YokogawaFile yokoFile)
        {
            FileImport result = GetFileImport(yokoFile.Index);

            if (result != null)
            {
                return(result);
            }

            var data = yokoFile.GetData();

            var count = 0;

            if (data != null)
            {
                count = data.Count();
            }

            using (var statelessSession = _sessionFactory.OpenStatelessSession())
                using (var statelessTransaction = statelessSession.BeginTransaction())
                {
                    try
                    {
                        result = new FileImport()
                        {
                            FileIndex  = yokoFile.Index,
                            FilePath   = yokoFile.FilePath,
                            ImportDate = DateTime.Now,
                            LineCount  = count
                        };

                        statelessSession.Insert(result);

                        if (count > 0)
                        {
                            var items = data.Select(x => new MeterData()
                            {
                                FileIndex = yokoFile.Index,
                                LineIndex = x.LineIndex,
                                Header    = x.Header,
                                TimeStamp = x.TimeStamp,
                                Value     = x.Value
                            });

                            foreach (var item in items)
                            {
                                statelessSession.Insert(item);
                            }
                        }

                        statelessTransaction.Commit();

                        return(result);
                    }
                    catch
                    {
                        statelessTransaction.Rollback();
                        throw;
                    }
                    finally
                    {
                        statelessSession.Close();
                    }
                }
        }
Exemple #11
0
        private void TimerElapsed(object stateInfo)
        {
            if (Monitor.TryEnter(_lockerObject))
            {
                try
                {
                    using (var uow = DataManager.Current.StartUnitOfWork())
                    {
                        lock (ImportIndex.Instance)
                        {
                            YokogawaFile yokoFile = null;

                            if (ImportIndex.Instance.Value == -1)
                            {
                                var importFileIndex = uow.GetMaxFileIndex().GetValueOrDefault(-1);

                                if (importFileIndex == -1)
                                {
                                    yokoFile = FileUtility.GetFirstFile();

                                    if (yokoFile != null)
                                    {
                                        ImportIndex.Instance.Value = yokoFile.Index;
                                    }
                                }
                                else
                                {
                                    ImportIndex.Instance.Value = importFileIndex + 1;
                                }
                            }

                            if (ImportIndex.Instance.Value == -1)
                            {
                                Program.Log("[timer] nothing to import, index is -1");
                                return;
                            }

                            if (yokoFile == null)
                            {
                                yokoFile = FileUtility.GetFile(ImportIndex.Instance.Value);
                            }

                            if (yokoFile != null)
                            {
                                Program.Log("[timer] found file: {0}", Path.GetFileName(yokoFile.FilePath));

                                var importFile = uow.ImportFile(yokoFile);

                                if (importFile != null)
                                {
                                    if (importFile.LineCount == 0)
                                    {
                                        // happens when there is no data found in the file
                                        Program.Log("[timer] no data found");
                                    }
                                    else
                                    {
                                        Program.Log("[timer] imported: {0} lines at {1:yyyy-MM-dd HH:mm:ss}", importFile.LineCount, importFile.ImportDate.ToLocalTime());
                                    }
                                }
                                else
                                {
                                    Program.Log("[timer] null result from ImportManager.ImportFile");
                                }

                                ImportIndex.Instance.Increment();

                                Program.Log("[timer] next index: {0}", ImportIndex.Instance.Value);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Program.Log("[timer] ERROR: {0}", ex.Message);
                }
                finally
                {
                    Monitor.Exit(_lockerObject);
                }
            }
        }