using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace _8082_Waymo_PC
{
public class SaveResult
{
public string ACTION = "http://www.flextronics.com/FFTester20/SaveResult";
public string strSerialNumber { get; set; }
public string strStationName { get; set; }
public string strStationType { get; set; }
public DateTime dtStartBatchTimestamp { get; set; }
public DateTime dtEndBatchTimestamp { get; set; }
public string strOperator { get; set; }
public string strTestResults { get; set; }
public string strBenchtopRefNumber { get; set; }
public string strParameters { get; set; } // test parameters from serial
public I28FlexStatus i28FlexStatus { get; set; }
public string strDeltaValue {get; set; }
public string strDeltaMinimum {get; set; }
public string strDeltaMaximum {get; set; }
public string strPTValue {get; set; }
public string strPTMinimum {get; set; }
public string strPTMaximum {get; set; }
public string Body()
{
return String.Join(
Environment.NewLine,
"",
"",
$"",
" ",
$" ",
" ",
$" ",
$" ",
" ",
" ",
" ",
"",
"]]>",
"",
$"{strBenchtopRefNumber}"
);
}
public void Dumpy()
{
//Directory.CreateDirectory("C:\\dumpy"); // force make the dumpy directory
//Directory.CreateDirectory("C:\\dumpy\\inbox"); // force make the dumpy directory
LogText("to dumpy: " + strStationType + " " + strDeltaValue + " " + strDeltaMinimum + " " + strDeltaMaximum);
var dump = String.Join(
Environment.NewLine,
"",
"",
$" {dtStartBatchTimestamp.ToUniversalTime().ToString("o")}",
$" {dtEndBatchTimestamp.ToUniversalTime().ToString("o")}",
$" {strSerialNumber}",
$" {Properties.Settings.Default.StationName}",
$" {strStationType.ToLower()}_leakdecay", // e.g.: sv_leakdecay
$" {i28FlexStatus.outcome}",
$" {strOperator}",
i28FlexStatus.error ? ErrorElement() : SuccessElement(),
""
);
var filename = $"C:\\dumpy\\inbox\\benchtop_8082_{dtStartBatchTimestamp.ToString("h_mm_ss")}.xml";
File.WriteAllText(filename, dump);
}
private string SuccessElement()
{
return String.Join(
Environment.NewLine,
" ",
$" Delta Pa",
$" {i28FlexStatus.parameterStatus}",
$" {strDeltaValue}",
$" {strDeltaMinimum}",
$" {strDeltaMaximum}",
" ",
" ",
" Volumetric Pressure (mbar)",
$" {i28FlexStatus.parameterStatus}",
$" {strPTValue}",
$" {strPTMinimum}",
$" {strPTMaximum}",
" ",
" ",
" TEST_PARAMETERS.txt",
" TXT",
$" {base64(strParameters)}",
" ");
}
private string ErrorElement()
{
return String.Join(
Environment.NewLine,
"",
$"{i28FlexStatus.failureCode}
",
$"{i28FlexStatus.failureDetails} ",
""
);
}
public string base64(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static void LogText(string message)
{
if (Properties.Settings.Default.DebugLogs)
{
if (!Directory.Exists(Properties.Settings.Default.LogDirectory))
{
Directory.CreateDirectory(Properties.Settings.Default.LogDirectory);
}
using (StreamWriter w = File.AppendText($"{Properties.Settings.Default.LogDirectory}\\log_form1.txt"))
{
w.Write("\r\nLog Entry : ");
w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
w.WriteLine($" :{message}");
w.WriteLine("-------------------------------");
}
}
}
}
public class SaveResultResponse
{
[XmlElement]
public string SaveResultResult { get; set; }
public string Message { get; set; }
public string strTestRefNo { get; set; }
public SaveResultResponse(SaveResult sr)
{
// If Flex Flow is not enabled, don't make an API call, and set the messages appropriately.
if (!Properties.Settings.Default.EnableFlexFlow)
{
SaveResultResult = "0";
Message = "Skipping FlexFlow because Disabled";
strTestRefNo = sr.strBenchtopRefNumber;
return;
}
var response = FlexFlow.Send(sr.GetType().Name, sr.Body(), sr.ACTION);
var dict = XMLParser.ToDict(response);
if (dict == null)
{
SaveResultResult = "-100";
Message = $"Error reading XML, please check your FlexFlow URL Setting ({Properties.Settings.Default.FlexURL}) is correct, or check the FlexFlow logs.";
return;
}
dict.TryGetValue("SaveResultResult", out string lSRR);
dict.TryGetValue("strTestRefNo", out string lTRN);
SaveResultResult = lSRR;
strTestRefNo = lTRN;
Message = response; // Just set message as response.
}
public Boolean IsOkay()
{
/// If not okay, Message is error message NEED TO Display to screen.
if (SaveResultResult == "0")
{
return true;
}
return false;
}
public static void LogText(string message)
{
if (Properties.Settings.Default.DebugLogs)
{
if (!Directory.Exists(Properties.Settings.Default.LogDirectory))
{
Directory.CreateDirectory(Properties.Settings.Default.LogDirectory);
}
using (StreamWriter w = File.AppendText($"{Properties.Settings.Default.LogDirectory}\\log_form1.txt"))
{
w.Write("\r\nLog Entry : ");
w.WriteLine($"{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}");
w.WriteLine($" :{message}");
w.WriteLine("-------------------------------");
}
}
}
}
}