Ejemplo n.º 1
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            AdapterParams prms = AdapterParams.ParseArgs(e.Args);

            if (prms.RequestType == RequestTypes.Config)
            {
                AdapterConfigWindow win = new AdapterConfigWindow();
                win.Show();
            }
            else if (prms.RequestType == RequestTypes.PickMeas)
            {
                MeasPickerWindow win = new MeasPickerWindow();
                win.Show();
            }
            else
            {
                // we assume that the request is getting data
                ConfigurationManager Config_ = new ConfigurationManager();
                Config_.Initialize();
                DataFetcher dataFetcher = new DataFetcher()
                {
                    Config_ = Config_
                };
                Task.Run(async() => await dataFetcher.FetchAndFlushData(prms)).Wait();
                Current.Shutdown();
            }
        }
        public static void FetchAndFlushData(AdapterParams prms)
        {
            // get measurement id from command line arguments
            string measId = prms.MeasId;
            // get the start and end times
            DateTime startTime = prms.FromTime;
            DateTime endTime   = prms.ToTime;

            // This Configuration data varaible Config_ can be handy here while dealing application secrets or configurations...
            ConfigurationManager Config_ = new ConfigurationManager();

            Config_.Initialize();

            // Create output data string
            string        outStr      = "";
            List <object> fetchResult = new List <object>();
            Random        rand        = new Random();

            for (DateTime dt = startTime; dt <= endTime; dt = dt.AddSeconds(1))
            {
                double ts = TimeUtils.ToMillisSinceUnixEpoch(dt);
                fetchResult.Add(ts);
                fetchResult.Add(rand.Next(-50, 50));
                if (prms.IncludeQuality)
                {
                    DataQuality qual = DataQuality.GOOD;
                    fetchResult.Add((int)qual);
                }
            }
            outStr = String.Join(",", fetchResult);

            // send the output data to console
            ConsoleUtils.FlushChunks(outStr);
        }
Ejemplo n.º 3
0
        public async Task FetchAndFlushData(AdapterParams prms)
        {
            // get measurement id from command line arguments
            string[] measIdFrags = prms.MeasId.Split('|');
            // check if we have a valid measurement Id
            if (measIdFrags.Length < 2)
            {
                ConsoleUtils.FlushChunks("");
                return;
            }
            // get the start and end times
            DateTime startTime = prms.FromTime;
            DateTime endTime   = prms.ToTime;

            List <double> measData;

            // get token
            string token = await GetIdentityServerToken();

            // call api
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(token);

            string outStr = "";
            // http://portal.wrldc.in/dashboard/api/wbesArchive/MOUDA/Total/2020-01-16/2020-01-17
            var response = await apiClient.GetAsync($"{Config_.DataHost}/api/wbesArchive/{measIdFrags[0]}/{measIdFrags[1]}/{startTime.ToString("yyyy-MM-dd")}/{endTime.ToString("yyyy-MM-dd")}");

            if (!response.IsSuccessStatusCode)
            {
                //Console.WriteLine(response.StatusCode);
            }
            else
            {
                // we get the result in the form of ts1,val1,ts2,val2,...
                string content = await response.Content.ReadAsStringAsync();

                if (prms.IncludeQuality)
                {
                    measData = JsonConvert.DeserializeObject <List <double> >(content);
                    outStr   = "";
                    // add good quality in the middle of each point
                    for (int pntIter = 0; pntIter < measData.Count; pntIter += 2)
                    {
                        outStr += $"{((pntIter > 0) ? "," : "")}{measData[pntIter]},{measData[pntIter + 1]}";
                    }
                }
                else
                {
                    if (content.Length > 4)
                    {
                        outStr = content.Remove(content.Length - 1, 1).Substring(1);
                    }
                }
            }
            ConsoleUtils.FlushChunks(outStr);
        }
        public async Task FetchAndFlushData(AdapterParams prms)
        {
            List <double> measData = new List <double>();
            // get measurement id from command line arguments
            string measId = prms.MeasId;
            // get the start and end times
            DateTime startTime = prms.FromTime;
            DateTime endTime   = prms.ToTime;

            // get token
            string token = await GetIdentityServerToken();

            // call api
            var apiClient = new HttpClient();

            apiClient.SetBearerToken(token);

            string outStr   = "";
            var    response = await apiClient.GetAsync($"{Config_.DataHost}/api/scadadata/{measId}/{startTime.ToString("yyyy-MM-dd")}/{endTime.ToString("yyyy-MM-dd")}");

            if (!response.IsSuccessStatusCode)
            {
                //Console.WriteLine(response.StatusCode);
            }
            else
            {
                // we get the result in the form of ts1,val1,ts2,val2,...
                string content = await response.Content.ReadAsStringAsync();

                if (prms.IncludeQuality)
                {
                    measData = JsonConvert.DeserializeObject <List <double> >(content);
                    outStr   = "";
                    // add good quality in the middle of each point
                    for (int pntIter = 0; pntIter < measData.Count; pntIter += 2)
                    {
                        outStr += $"{((pntIter > 0) ? "," : "")}{measData[pntIter]},{measData[pntIter + 1]}";
                    }
                }
                else
                {
                    if (content.Length > 4)
                    {
                        outStr = content.Remove(content.Length - 1, 1).Substring(1);
                    }
                }
            }
            ConsoleUtils.FlushChunks(outStr);
        }
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            AdapterParams prms = AdapterParams.ParseArgs(e.Args);

            if (prms.RequestType == RequestTypes.PickMeas)
            {
                MeasPickerWindow win = new MeasPickerWindow();
                win.Show();
            }
            else
            {
                // we assume that the request is getting data
                DataFetcher.FetchAndFlushData(prms);
                Current.Shutdown();
            }
        }
        public static void FetchAndFlushData(AdapterParams prms)
        {
            // get data from excel
            // https://stackoverflow.com/questions/37495707/how-to-find-datetime-values-in-an-epplus-excelworksheet?noredirect=1&lq=1
            // get measurement id from command line arguments. measurement id is formatted as excelFilename|selectedDataCol|selectedTimeCol
            string measId = prms.MeasId;
            // get the start and end times
            DateTime startTime = prms.FromTime;
            DateTime endTime   = prms.ToTime;

            // get measurement data sources
            string[] measSegments = measId.Split('|');
            if (measSegments.Length != 4)
            {
                ConsoleUtils.FlushChunks("");
                return;
            }
            string measFilename  = measSegments[0];
            string measSheetname = measSegments[1];
            string measDataCol   = measSegments[2];
            string measTimeCol   = measSegments[3];

            // check if file exists
            if (!File.Exists(measFilename))
            {
                ConsoleUtils.FlushChunks("");
                return;
            }
            // get excel column names
            List <string>  colNames      = new List <string>();
            FileInfo       excelFileInfo = new FileInfo(measFilename);
            ExcelPackage   package       = new ExcelPackage(excelFileInfo);
            ExcelWorksheet sheet         = package.Workbook.Worksheets.FirstOrDefault(x => x.Name == measSheetname);

            if (sheet == null)
            {
                ConsoleUtils.FlushChunks("");
                return;
            }
            foreach (var rowCell in sheet.Cells[sheet.Dimension.Start.Row, sheet.Dimension.Start.Column, 1, sheet.Dimension.End.Column])
            {
                colNames.Add(rowCell.Text);
            }
            // check if desired columns exist
            int measDataColInd = colNames.FindIndex(x => x == measDataCol) + 1;
            int measTimeColInd = colNames.FindIndex(x => x == measTimeCol) + 1;

            if (measDataColInd == 0 || measTimeColInd == 0)
            {
                ConsoleUtils.FlushChunks("");
                return;
            }
            // Create output data string
            string        outStr      = "";
            List <object> fetchResult = new List <object>();

            // https://stackoverflow.com/questions/37495707/how-to-find-datetime-values-in-an-epplus-excelworksheet?noredirect=1&lq=1
            for (int rowIter = 2; rowIter <= sheet.Dimension.End.Row; rowIter++)
            {
                var      timeCell     = sheet.Cells[rowIter, measTimeColInd];
                var      valCell      = sheet.Cells[rowIter, measDataColInd];
                DateTime?cellDateTime = null;

                if (timeCell.Value is DateTime valTime)
                {
                    // check if cell type is time
                    // we need to convert data time to utc time before comparing
                    if ((valTime.ToUniversalTime() >= startTime) && (valTime.ToUniversalTime() <= endTime))
                    {
                        cellDateTime = valTime;
                    }
                }
                else if (timeCell.Value is double oaDouble)
                {
                    // check if cell type is double
                    DateTime cellOaTime = DateTime.FromOADate(oaDouble);
                    // we need to convert data time to utc time before comparing
                    if ((cellOaTime.ToUniversalTime() >= startTime) && (cellOaTime.ToUniversalTime() <= endTime))
                    {
                        cellDateTime = cellOaTime;
                    }
                }

                if (cellDateTime.HasValue)
                {
                    fetchResult.Add(TimeUtils.ToMillisSinceUnixEpoch(cellDateTime.Value));
                    // check if we have a numeric value in the data column
                    fetchResult.Add((valCell.Value is double) ? valCell.Value : null);
                }
            }
            outStr = String.Join(",", fetchResult);
            // send the output data to console
            ConsoleUtils.FlushChunks(outStr);
        }