using Logic.ProductionToProductMapper.Files.Fw; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using Xylem.Common.CommonCore.Configuration; using Xylem.Common.Logic.ProductionOrderCore.FW; using Xylem.Common.Logic.ProductionOrderCore.OrderData; using Xylem.Common.Logic.SoftwareAccessHelper; namespace Xylem.Common.Ui.GenesisToolBox { public partial class FrmFileCURD : Form { //string fileUrl = "http://localhost:56011/api/FileUpToDate/"; String fileUrl = ServiceUrls.FileContentControllerUrl(); public FrmFileCURD() { InitializeComponent(); cbxFilterFwDn.Items.Add("ALL"); cbxFilterFWType.Items.Add("ALL"); foreach (MeterSize suit in (MeterSize[])Enum.GetValues(typeof(MeterSize))) { cbxFwDn.Items.Add(suit); cbxFilterFwDn.Items.Add(suit); } cbxFwDn.SelectedIndex = 0; cbxFilterFwDn.SelectedIndex = 0; cbxFWType.Items.Add(CordonelFWTypeEnum.EMEA_433); cbxFWType.Items.Add(CordonelFWTypeEnum.EMEA_868); cbxFWType.Items.Add(CordonelFWTypeEnum.NA); cbxFWType.SelectedIndex = 0; cbxFilterFWType.Items.Add(CordonelFWTypeEnum.EMEA_433); cbxFilterFWType.Items.Add(CordonelFWTypeEnum.EMEA_868); cbxFilterFWType.Items.Add(CordonelFWTypeEnum.NA); cbxFilterFWType.SelectedIndex = 0; } //private void btnUpload_Click(object sender, EventArgs e) //{ // if (string.IsNullOrEmpty(txtFile.Text)) // { // return; // } // var fi = new FileInfo(txtFile.Text); // if (!fi.Exists) // { // MessageBox.Show("File not found!"); // return; // } // var txt = File.ReadAllText(txtFile.Text); // var upFile = new BaseFile() // { // FileName = fi.Name, // FileContent = txt, // ForDeveloper = chbIsDeveloper.Checked // }; // var r = LocalWebRequest.PostRequestAsync($"{ServiceUrls.FileContentControllerUrl()}PostFileJsonContent", 10000, upFile); // if (r) // { // MessageBox.Show("Success"); // LoadGrv(); // } // else // { // MessageBox.Show("not successful"); // var rawPostBody = JsonConvert.SerializeObject(upFile); // } //} private void btnRefesh_Click(Object sender, EventArgs e) { } private void chbIsDeveloper_CheckedChanged(Object sender, EventArgs e) { } private void FrmFileCURD_Load(Object sender, EventArgs e) { } private void dgvFileContent_RowsRemoved(Object sender, DataGridViewRowsRemovedEventArgs e) { } private void textBox1_TextChanged(Object sender, EventArgs e) { } private void button1_Click(Object sender, EventArgs e) { } private void btnOpenDglBin_Click(Object sender, EventArgs e) { txtBin.Text = openDialog("", true, txtBin.Text); } private void btnConfigDia_Click(Object sender, EventArgs e) { txtConfig.Text = openDialog("Json (*.Json)|*.Json;", false, txtConfig.Text); } private void btnPackageFileDia_Click(Object sender, EventArgs e) { txtPackageFile.Text = openDialog("package files (*.txt)|*.txt;", false, txtPackageFile.Text); lblFWName.Text = GenerateName(); } private String openDialog(String Filter, Boolean folder = false, String current = null) { //if (folder) //{ // var dlgOpenPackageFile = new FolderBrowserDialog(); // if (!string.IsNullOrEmpty(current)) // { // dlgOpenPackageFile.SelectedPath = current; // } // if (dlgOpenPackageFile.ShowDialog(this) == DialogResult.OK) // { // return dlgOpenPackageFile.SelectedPath; // } //} //else //{ var dlgOpenPackageFile = new OpenFileDialog(); dlgOpenPackageFile.Filter = Filter; if (folder) { dlgOpenPackageFile.Multiselect = true; } else { if (!string.IsNullOrEmpty(current)) { dlgOpenPackageFile.FileName = current; } } if (dlgOpenPackageFile.ShowDialog(this) == DialogResult.OK) { if (folder) { var sb = new StringBuilder(); foreach (var item in dlgOpenPackageFile.FileNames) { sb.Append($"{item}|"); } return sb.ToString(); } return dlgOpenPackageFile.FileName; } //} return null; } private void btnUploadFW_Click(Object sender, EventArgs e) { //chbIsDeveloper var filePackage = new FileInfo(txtPackageFile.Text); var FileList = new Dictionary(); FileList.Add(filePackage.Name, filePackage.FullName); foreach (var item in txtBin.Text.Split('|')) { if (!string.IsNullOrEmpty(item)) { var fileBin = new FileInfo(item); FileList.Add(fileBin.Name, fileBin.FullName); } } var IsDevloper = chbIsDeveloper.Checked; var MyMeterSize = ((MeterSize)cbxFwDn.SelectedItem); var FWTypeEnum = ((CordonelFWTypeEnum)cbxFWType.SelectedItem); var FWname = GenerateName(); var result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{fileUrl}/PostFWFileBinaryContent?IsDevloper={IsDevloper}&MeterSize={MyMeterSize}&FWTypeEnum={FWTypeEnum}&FWname={FWname}&FileCategoryID=1", null, FileList); if (string.IsNullOrEmpty(txtConfig.Text) || !new FileInfo(txtConfig.Text).Exists) { return; } var fileConfig = new FileInfo(txtConfig.Text); FileList = new Dictionary(); FileList.Add(fileConfig.Name, fileConfig.FullName); result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{fileUrl}/PostFWFileBinaryContent?IsDevloper={IsDevloper}&MeterSize={MyMeterSize}&FWTypeEnum={FWTypeEnum}&FWname={fileConfig.Name}&FileCategoryID=2", null, FileList); } private String GenerateName() { var sb = new StringBuilder(); if (chbIsDeveloper.Checked) { sb.Append("DEV_"); } else { sb.Append("PROD_"); } sb.Append(cbxFWType.SelectedItem); sb.Append("_"); sb.Append(cbxFwDn.SelectedItem); sb.Append("_"); if (string.IsNullOrEmpty(txtPackageFile.Text)) { sb.Append("NOVERSION!"); return sb.ToString(); } var a = new DirectoryInfo(txtPackageFile.Text); sb.Append(a.Parent.Name.Split('_').Last()); return sb.ToString(); //Version } private void chbIsDeveloper_CheckedChanged_1(Object sender, EventArgs e) { lblFWName.Text = GenerateName(); } private void cbxFwDn_SelectedIndexChanged(Object sender, EventArgs e) { lblFWName.Text = GenerateName(); } private void cbxFWType_SelectedIndexChanged(Object sender, EventArgs e) { lblFWName.Text = GenerateName(); } private void txtBin_TextChanged(Object sender, EventArgs e) { lblFWName.Text = GenerateName(); } private void btnGetFWFiles_Click(Object sender, EventArgs e) { var IsDevloper = chbFilterIsDeveloper.Checked; var url = $"{fileUrl}/GetFwFiles?FileIsForDeveloperOnly={IsDevloper}"; if (cbxFilterFwDn.SelectedItem.ToString() != "ALL") { var MyMeterSize = ((MeterSize)cbxFilterFwDn.SelectedItem).GetHashCode(); url = url + $"&CordonelFwFile_Dn={MyMeterSize}"; } if (cbxFilterFWType.SelectedItem.ToString() != "ALL") { var FWTypeEnum = ((CordonelFWTypeEnum)cbxFilterFWType.SelectedItem).GetHashCode(); url = url + $"&CordonelFwFile_Type={FWTypeEnum}"; } if (cbxFilterFileType.SelectedItem != null) { switch (cbxFilterFileType.SelectedItem.ToString()) { case "FW": url = url + $"&FileCategoryID=1"; break; case "Configuration": url = url + $"&FileCategoryID=2"; break; case "Cordonel Release Config": url = url + $"&FileCategoryID=6"; break; case "Cordonel Update Software": url = url + $"&FileCategoryID=5"; break; // default: //"ALL" break; } } var result = LocalWebRequest.GetRequest(url, 10000); var a = JsonConvert.DeserializeObject>(result); dgvFiles.DataSource = a; } private void dgvFiles_CellContentDoubleClick(Object sender, DataGridViewCellEventArgs e) { var parentFilId = (Int32)dgvFiles.Rows[e.RowIndex].Cells[1].Value; var url = $"{fileUrl}/GetFileParts?ParentFileId={parentFilId}&readContent=false"; var result = LocalWebRequest.GetRequest(url, 20000); var a = JsonConvert.DeserializeObject>(result); dgvFileParts.DataSource = a; } private void button1_Click_1(Object sender, EventArgs e) { var dlgOpenPackageFile = new FolderBrowserDialog(); if (dlgOpenPackageFile.ShowDialog(this) == DialogResult.OK) { textBox1.Text = dlgOpenPackageFile.SelectedPath; } var LatestJsonDate = DateTime.MinValue; var LatestJsonPath = ""; var di = new DirectoryInfo(textBox1.Text); foreach (var item in di.GetDirectories()) { try { var folderName = item.Name.Split('_'); CordonelFWTypeEnum FWTypeEnum; var searchstring = ""; if (!folderName[1].ToLower().Contains("emea")) { FWTypeEnum = CordonelFWTypeEnum.NA; searchstring = "naproduct"; } else if (folderName[2].ToLower().Contains("433")) { FWTypeEnum = CordonelFWTypeEnum.EMEA_433; searchstring = "rowproduct_433"; } else if (folderName[2].ToLower().Contains("868")) { FWTypeEnum = CordonelFWTypeEnum.EMEA_868; searchstring = "rowproduct_868"; } else { break; } var dateString = ""; // var LatestJsonDate = DateTime.MinValue; //var LatestJsonPath = ""; var sb = new StringBuilder(); sb.Append("PROD_"); sb.Append(FWTypeEnum); sb.Append("_"); sb.Append(MeterSize.DN50); sb.Append("_"); if (FWTypeEnum == CordonelFWTypeEnum.NA) { sb.Append(folderName[3]); dateString = folderName[2]; } else { sb.Append(folderName[4]); dateString = folderName[3]; } var configs = item.GetFiles($"configuration.json"); if (configs != null && configs.Any()) { DateTime tmpDate; if (DateTime.TryParseExact(dateString, "yyyyMMdd", new CultureInfo("en-US"), DateTimeStyles.None, out tmpDate) && tmpDate > LatestJsonDate) { LatestJsonDate = tmpDate; LatestJsonPath = configs.First().FullName; } } var filePackage = item.GetFiles($"{searchstring}.txt").First(); var BinDir = Path.Combine(item.FullName, "binaries", searchstring); var FileList = new Dictionary(); var FWname = sb.ToString(); FileList.Add(filePackage.Name, filePackage.FullName); foreach (var binItem in new DirectoryInfo(BinDir).GetFiles()) { FileList.Add(binItem.Name, binItem.FullName); } //TODO THW var baseUrl = $"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&MeterSize={MeterSize.DN50}&FWTypeEnum={FWTypeEnum}&"; var baseUrl = $"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&MeterSize={MeterSize.DN50}&FWTypeEnum={FWTypeEnum}&"; var result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{baseUrl}FWname={FWname}&FileCategoryID=1&FileDateString=CONVERT(datetime , '{dateString}',112 )", null, FileList); } catch (Exception ex) { MessageBox.Show($@"Error On{item.Name}"); } } if (!string.IsNullOrEmpty(LatestJsonPath)) { var fileConfig = new FileInfo(LatestJsonPath); var fileList = new Dictionary(); fileList.Add(fileConfig.Name, fileConfig.FullName); //TODO THW var result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&FWname={fileConfig.Name}&FileCategoryID=2", null, fileList); var result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&FWname={fileConfig.Name}&FileCategoryID=2", null, fileList); } } private void button2_Click(Object sender, EventArgs e) { } private void btnAddReleaseConfig_Click(Object sender, EventArgs e) { //6 Cordonel Release Config AddFilePack(6, txtReleaseConfig.Text); } private void button3_Click(Object sender, EventArgs e) { //5 Cordonel Update Sotware AddFilePack(5, txtUpdateSW.Text); } private void AddFilePack(Int32 FileCategoryID, String Name) { var dlgOpenPackageFile = new FolderBrowserDialog(); if (dlgOpenPackageFile.ShowDialog(this) == DialogResult.OK) { var di = new DirectoryInfo(dlgOpenPackageFile.SelectedPath); var FileList = new Dictionary(); foreach (var dirRoot in di.GetDirectories()) { foreach (var item in dirRoot.GetFiles()) { try { FileList.Add(dirRoot.Name + "\\" +item.Name, item.FullName); } catch (Exception ex) { MessageBox.Show($@"Error On{item.Name}"); } } } foreach (var item in di.GetFiles()) { try { FileList.Add(item.Name, item.FullName); } catch (Exception ex) { MessageBox.Show($@"Error On{item.Name}"); } } //TODO THW var baseUrl = $"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&MeterSize={MeterSize.DN50}&FWTypeEnum={FWTypeEnum}&"; //var baseUrl = $"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&"; var baseUrl = $"{fileUrl}/PostFWFileBinaryContent?IsDevloper={false}&"; var result = LocalWebRequest.PostBinaryRequestAndGetResponseAsync($"{baseUrl}FWname={Name}&FileCategoryID={FileCategoryID}", null, FileList); if (result.StatusCode == System.Net.HttpStatusCode.OK) { MessageBox.Show(@"done"); } else { MessageBox.Show($@"error{result.StatusCode} : {result.Content}"); } } } private void dgvFileParts_CellContentClick(Object sender, DataGridViewCellEventArgs e) { } private void dgvFileParts_CellClick(Object sender, DataGridViewCellEventArgs e) { } private void button3_Click_1(Object sender, EventArgs e) { //var fwUpdRpt = new List(); //try //{ // var url = ServiceUrls.DownloadFwUpdateReportServiceUrl(); // if (pcbId > 0) // { // url += $"PcbId={pcbId}"; // if (userId > 0) url += $"&UserId={userId}"; // if (orderNr > 0) url += $"&OrderNr={orderNr}"; // } // else if (userId > 0) // { // url += $"UserId={userId}"; // if (orderNr > 0) url += $"&OrderNr={orderNr}"; // } // else if (orderNr > 0) // { // url += $"OrderNr={orderNr}"; // } // url += $"&readContent={readContent}"; // var requestResponse = LocalWebRequest.GetRequest(url, 20000); // if (string.IsNullOrEmpty(requestResponse)) // { // DbIsConnected = false; // return false; // } // DbIsConnected = true; // fwUpdRpt = JsonConvert.DeserializeObject>(requestResponse); } } }