public static string CreateCSVMeasurementRow(XDocument measurementDoc) { string delim = ","; string[] arguments = { "IPaL", "IPaA", "IPaH", "IPbL", "IPbA", "IPbH", "IPcL", "IPcA", "IPcH", "IPtL", "IPtA", "IPtH", "VPaL", "VPaA", "VPaH", "VPbL", "VPbA", "VPbH", "VPcL", "VPcA", "VPcH", "VabL", "VabA", "VabH", "VbcL", "VbcA", "VbcH", "VcaL", "VcaA", "VcaH", "PPaL", "PPaA", "PPaH", "PPbL", "PPbA", "PPbH", "PPcL", "PPcA", "PPcH", "PPtL", "PPtA", "PPtH", "PQaL", "PQaA", "PQaH", "PQbL", "PQbA", "PQbH", "PQcL", "PQcA", "PQcH", "PQtL", "PQtA", "PQtH", "PSaL", "PSaA", "PSaH", "PSbL", "PSbA", "PSbH", "PScL", "PScA", "PScH", "PStL", "PStA", "PStH", "EPaP", "EPbP", "EPcP", "EPtP", "EPaC", "EPbC", "EPcC", "EPtC", "EQaP", "EQbP", "EQcP", "EQtP", "EQaC", "EQbC", "EQcC", "EQtC", "TPE0", "TPE1", "TPE2", "TPE3", "TQE0", "TQE1", "TQE2", "TQE3", "DemA", "DemB", "DemC", "DemT", "HIaL", "HIaA", "HIaH", "HIbL", "HIbA", "HIbH", "HIcL", "HIcA", "HIcH", "HUaL", "HUaA", "HUaH", "HUbL", "HUbA", "HUbH", "HUcL", "HUcA", "HUcH", "TemL", "TemA", "TemH", "FreL", "FreA", "FreH", "PFaL", "PFaA", "PFaH", "PFbL", "PFbA", "PFbH", "PFcL", "PFcA", "PFcH", "PFtL", "PFtA", "PFtH" }; StringBuilder sb = new StringBuilder(); // Get measurement values XElement dataMeasure = measurementDoc.Root.Element("data"); // Data element //string mac = dataMeasure.Attribute("mac").Value; string type = dataMeasure.Attribute("type").Value; string sCout = dataMeasure.Attribute("count").Value; // Now form CSV int numOfPoints = Convert.ToInt32(sCout); for (int i = 0; i < numOfPoints; i++) { // Get "point" XElement for current seq attribute XElement pointXE = dataMeasure.Elements("point").Where(x => x.Attribute("seq").Value == i.ToString()).FirstOrDefault(); // Get time and status from point XElement string time = pointXE.Attribute("time").Value; string status = pointXE.Attribute("status").Value; FormCustomConsole.WriteLine("Status tag: " + status); sb.Append(time + delim); switch (status) { case "valid": case "live": foreach (var item in arguments) { string temp = pointXE.Element(item).Value + delim; sb.Append(temp); } sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); break; case "null": foreach (var item in arguments) // I just want to iterate number of arguments times and append "null" { sb.Append("null" + delim); } sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); break; default: throw new Exception("Status tag not recognised"); } } sb.Remove(sb.Length - 2, 2); // Remove last newline return(sb.ToString()); }
public static T Do <T>(Func <T> action, TimeSpan retryInterval) { //var exceptions = new List<Exception>(); for (int attempted = 0; attempted < MAX_ATTEMPS; attempted++) { try { if (attempted > 0) { Thread.Sleep(retryInterval); } FormCustomConsole.WriteLineWithConsole("Do<T>, attempted: " + attempted); return(action()); } catch (Exception ex) { //exceptions.Add(ex); //Console.WriteLine("Exception: " + ex.Message); FormCustomConsole.WriteLineWithConsole("Exception in Do<T>: " + ex.Message); } } //throw new AggregateException(exceptions); throw new Exception("Action not executed successfully " + MAX_ATTEMPS + " times"); }
/// <summary> /// Populate chart based on DataClass argument /// </summary> /// <param name="data"></param> public void SetChartData(DataClass data) { // So many things can go wrong try { PieChart.Series["Validity"].Points.AddXY("Valid", (double)data.summary.numOfValid / data.summary.totalNumber); PieChart.Series["Validity"].Points[0].Color = ConfigClass.colors.colorValid; PieChart.Series["Validity"].Points.AddXY("Duplicate", (double)data.summary.numOfDuplicate / data.summary.totalNumber); PieChart.Series["Validity"].Points[1].Color = ConfigClass.colors.colorDuplicate; PieChart.Series["Validity"].Points.AddXY("Missing", (double)data.summary.numOfMissing / data.summary.totalNumber); PieChart.Series["Validity"].Points[2].Color = ConfigClass.colors.colorMissing; PieChart.Series["Validity"].Points.AddXY("Zero", (double)data.summary.numOfZeroes / data.summary.totalNumber); PieChart.Series["Validity"].Points[3].Color = ConfigClass.colors.colorZero; PieChart.Series["Validity"].Points.AddXY("Null", (double)data.summary.numOfNullValues / data.summary.totalNumber); PieChart.Series["Validity"].Points[4].Color = ConfigClass.colors.colorNull; PieChart.Series["Validity"].Points.AddXY("Data repeat", (double)data.summary.numOfDataRepeat / data.summary.totalNumber); PieChart.Series["Validity"].Points[5].Color = ConfigClass.colors.colorRepeat; PieChart.Series["Validity"].Points.AddXY("Invalid", (double)data.summary.numOfInvalid / data.summary.totalNumber); PieChart.Series["Validity"].Points[6].Color = ConfigClass.colors.colorInvalid; } catch (Exception e) { FormCustomConsole.WriteLine(e.Message); } }
private void consoleToolStripMenuItem_Click(object sender, EventArgs e) { if (FormCustomConsole.isActive == true) // Only open form if it's not active { return; } FormCustomConsole fc = new FormCustomConsole(); fc.Show(); }
private void TaskDataIntegrityProcess(ICancelableForm fw) // Task when process button is pressed { Thread.Sleep(100); try { ProcessDataIntegrity(fw); } catch (Exception e) { FormCustomConsole.Write("Exception at process data integrity \r\n" + e.ToString() + "\r\n\r\n"); } }
//async public static void GetRequest(string url, Action<string> callback) //{ // try // { // var client = new HttpClient(); // client.Timeout = TimeSpan.FromSeconds(timeoutSec); // //Console.WriteLine("Before await, Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString()); // HttpResponseMessage response = await client.GetAsync(url); // HttpContent content = response.Content; // string mycontent = ""; // if (response.IsSuccessStatusCode) // { // mycontent = await content.ReadAsStringAsync(); // } // callback(mycontent); // Execute callback with result // } // catch (Exception e) // { // Console.WriteLine("Exception at GetRequest()"); // System.Windows.Forms.MessageBox.Show("Exception at GetRequest()" + e.Message); // throw; // } //} async public static Task <string> GetRequest(string url) { var client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(timeoutSec); FormCustomConsole.WriteLine(url); //Console.WriteLine("Before await, Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString()); HttpResponseMessage response = await client.GetAsync(url); HttpContent content = response.Content; string mycontent = ""; if (response.IsSuccessStatusCode) { mycontent = await content.ReadAsStringAsync(); } return(mycontent); }
async private void downloadCSVToolStripMenuItem_Click(object sender, EventArgs e) { var cells = dataGridView1.SelectedCells; Console.WriteLine("Number of cells: " + dataGridView1.SelectedCells.Count); List <int> indexes = new List <int>(); if (dataGridView1.SelectedCells.Count < 0) // Check if nothing is selected { return; } // ------------- FIND FIRST AND LAST (SELECTED) INDEX IN DATAGRID foreach (DataGridViewTextBoxCell item in cells) { int temp = item.RowIndex * 96 + item.ColumnIndex - 1; // Convert to data array index if (temp < 0) // Check if header is selected { return; } indexes.Add(temp); } // Find min and max indexes int indexMin = indexes.First(); int indexMax = indexes.First(); foreach (var item in indexes) { if (indexMin > item) { indexMin = item; } } foreach (var item in indexes) { if (indexMax < item) { indexMax = item; } } // ---------- INDEXES FOUND //DEBUG Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // debug // Now select time period based on min-max var startDate = dataPtr.data[indexMin].date; var endDate = dataPtr.data[indexMax].date; // Get username and password from textboxes string username = textBoxUsername.Text; string password = textBoxPassword.Text; // Create array of date pairs if xml data needs to be fragmented List <CustomXmlDatePair> datePeriodsList = CustomXmlDatePair.FragmentDate(startDate, endDate); // ----- Instantiate ntpm object string ip; try { // Get IP and Port from database ip = GetIPAndPort(); } catch (Exception) { MessageBox.Show("Couldn't get IP and Port from database"); return; // abort } // ----------------------------------- Get and parse report.xml string reportRes; XDocument reportDoc; try { reportRes = await NTPMControllerClass.GetReport(ip); // Get xml report from NTPM reportDoc = XDocument.Parse(reportRes); } catch (Exception) { MessageBox.Show("Couldn't get or parse report.xml"); return; // abort } // Create header dates string headerStartTime = HelperClass.ConvertToNtpmCustomXmlTime(startDate); string headerEndTime = HelperClass.ConvertToNtpmCustomXmlTime(endDate); string csvHeader = NTPMControllerClass.CreateCSVHeader(headerStartTime, headerEndTime, reportDoc); FormCustomConsole.WriteLine("StartTime: " + headerStartTime); FormCustomConsole.WriteLine("EndTime: " + headerEndTime); // now we only need to fill values with each row // --------------------------------Get and parse measurement.xml List <XDocument> measurementDoc = new List <XDocument>(); List <string> measurementRows = new List <string>(); foreach (var datePeriod in datePeriodsList) { Console.WriteLine("Parsing period: " + datePeriod.startHeader + "---" + datePeriod.endHeader); await HelperRetryClass.DoWithRetryAsync(async() => { FormCustomConsole.WriteLine("Loading: " + datePeriod.startHeader + "---" + datePeriod.endHeader); List <KeyValuePair <string, string> > pairs = NTPMControllerClass.GetPairsForCustomXmlPostMethod(datePeriod.startHeader, datePeriod.endHeader, username, password); string measurementRes = await HTTPClientClass.PostRequest("http://" + ip + "/custom.xml", pairs); XDocument tempMeasure = XDocument.Parse(measurementRes); NTPMControllerClass.CheckErrorTag(tempMeasure); string measureRow = NTPMControllerClass.CreateCSVMeasurementRow(tempMeasure); // At this point, download and parsing is ok measurementRows.Add(measureRow); measurementDoc.Add(tempMeasure); }, TimeSpan.FromSeconds(1)); } // now form whole string (HEADER + TAGS + MEASUREMENT ROWS) StringBuilder sFinal = new StringBuilder(); sFinal.Append(csvHeader); foreach (var item in measurementRows) { sFinal.Append(item); sFinal.Append("\r\n"); } sFinal.Remove(sFinal.Length - 2, 2); // Remove last new line // Write elapsed time stopWatch.Stop(); FormCustomConsole.WriteLine("XML fetching operation finished in: " + (stopWatch.ElapsedMilliseconds / 1000).ToString() + "seconds"); // --------------------------------------- Finaly save file string fileName = XmlHelper.FindFirstDescendant("hostname", reportDoc).Trim(); string fileMac = XmlHelper.FindFirstDescendant("mac", reportDoc).Replace(':', '-'); string fileType = "by_15min"; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel File|*.csv"; // Create file name (based on downloaded data) sfd.FileName = fileName + "_" + fileMac + "_" + headerStartTime + "_" + headerEndTime + "_" + fileType; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = sfd.FileName; StreamWriter sw = new StreamWriter(File.Create(path)); sw.Write(sFinal.ToString()); sw.Dispose(); } }
/// <summary> /// Remove duplicates for all 15min periods, duplicates are removed if all measurment values are same /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void removeDuplicatesToolStripMenuItem_Click(object sender, EventArgs e) { #region OLD WAY //List<string> commands = new List<string>(); // List of database commands //// Loop thru every 15min point, and generate remove database queries for true duplicate data //foreach (Data15MinClass d15 in dataPtr.data) //{ // if (d15.numberOfPoints < 2) // { // continue; // If point have less than 2 values ignore // } // var uniqueValues = new List<FloatValues>(); // Create new list which will hold unique values // var all15minValues = d15.GetAllValues(); // Some 15min point can have multiple values, duplicates // // First value is always unique // uniqueValues.Add(all15minValues[0]); // // Generate remove command for each duplicated value, leave values that are different in float values // for (int i = 1; i < d15.GetAllValues().Count; i++) // Skip first // { // bool equalFlag = false; // This will be set to true if any of data point is true duplicate // foreach (var item2 in uniqueValues) // { // if (FloatHelper.CompareDuplicateValues(item2, all15minValues[i])) // Check if they are equal // { // equalFlag = true; // // Generate command for row removal from database and add it to list of commands // commands.Add(GenerateDatabaseRemoveCommand(all15minValues[i])); // } // } // if (!equalFlag) // { // // This garantiues unique value // uniqueValues.Add(all15minValues[i]); // } // equalFlag = false; // } //} #endregion old way List <string> commands = CreateDBRemoveCommands(); // Make dialog box, for connfirming deletion DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete: " + commands.Count + " items from database?", "Warning", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.No) { return; } // Yes was selected Action action = () => { //Console.WriteLine("Starting request"); foreach (var item in commands) { FormCustomConsole.WriteLine("Sending command: " + item); try { #warning Deleteing items from database can be configured here DatabaseControllerClass.SendCommandToDatabase(DatabaseSettingsClass.ReadConnectionString(), item); FormCustomConsole.WriteLine("Successfully executed command:" + item); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }; ICancelableForm cf = new FormWaitMarquee(this); HelperInterruptibleWorker worker = new HelperInterruptibleWorker(this, cf); worker.ExecuteTask(action); }
private void ProcessDataIntegrity(ICancelableForm fw) { string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern; FormCustomConsole.WriteLine("CurrentCulture: " + sysFormat); FormCustomConsole.WriteLine("CurrentUICulture: " + sysUIFormat); DateTime dateStart = GetDateFiltered(dateTimePickerStartDataIntegrity); DateTime dateEnd = GetDateFiltered(dateTimePickerEndDataIntegrity); // Check if day selection is valid TimeSpan dayDiff = dateEnd.Subtract(dateStart); // It will hold days difference if (dayDiff.Days <= 0) { MessageBox.Show("Select valid time!"); return; } string oracleDateStart; string oracleDateEnd; string querryCommand; string result = ""; int currentRow = -1; // If row is enabled, process it foreach (DataGridViewRow item in dataGridView2.Rows) { currentRow++; // Start from -1 so it is 0 at first iteration if (Convert.ToBoolean(item.Cells[0].Value) == true) { // Selected element is enabled // Create oracle time selection string oracleDateStart = HelperClass.ConvertToOracleTime(dateStart); oracleDateEnd = HelperClass.ConvertToOracleTime(dateEnd); // Create command querryCommand = "SELECT VALID,IPAA,IPBA,IPCA,VABA,VBCA,VCAA, READ_TIME, ID FROM DATA_HISTORY WHERE DEVICE_ID=" + item.Cells[1].Value + " AND READ_TIME >= " + oracleDateStart + " AND READ_TIME < " + oracleDateEnd + "AND ROWNUM <= " + ConfigClass.MAX_ROWS_GET.ToString(); char delim = '|'; try { result = DatabaseControllerClass.FetchValuesFromDatabase(DatabaseSettingsClass.ReadConnectionString(), querryCommand, delim); } catch (Exception) { Task.Run(() => MessageBox.Show("Timeout for device ID:" + item.Cells[1].Value)); return; } // Instantiate dataclass for current row allData[currentRow] = new DataClass(dayDiff.Days, currentRow, dateStart, dateEnd); // Fill it with values from database allData[currentRow].FillDataRow(result, Convert.ToInt32(item.Cells[1].Value), delim); // Process data row for: null values, zero values, duplicate values and generaly some way of corrupted data allData[currentRow].ProcessData(); allData[currentRow].RunAnalysis(); // This will calculate all needed info string tempSummary = allData[currentRow].GenerateSummary(); string[] summary = tempSummary.Split(delim); // Fill cells item.Cells[4].Value = summary[0]; // valid item.Cells[5].Value = summary[1]; // flag status item.Cells[6].Value = dateStart.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); item.Cells[7].Value = dateEnd.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); item.Cells[8].Value = "Ready"; item.Cells[8].Style.BackColor = Color.Lavender; // Update percentage bar and check if aborted fw.ProgresStep(); // Update progress bar if (fw.AlreadyClosed) { return; } } else // Cell is not selected { } } }
private const int texboxDeleteOffset = 1000; // It will delete this number of chars + how much it needs to display whole event public FormCustomConsole() { InitializeComponent(); isActive = true; FormCustomConsolePtr = this; // Static pointer to last opened instance }