using Ionic.Utils.Zip; using Newtonsoft.Json; using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Net; using System.Net.Http; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Fuel01 { partial class f_about : Form { string runApp = ""; public f_about() { InitializeComponent(); this.Text = String.Format("À propos de {0}", AssemblyTitle); this.labelProductName.Text = AssemblyProduct; var versionAppli = new Version(AssemblyVersion); Version versionWeb = new Version(0,0,0,0); WebRequest request = WebRequest.Create(Properties.Settings.Default.server_adress_bin + ".txt"); try { WebResponse response = request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.ASCII); string retour = sr.ReadToEnd(); sr.Close(); versionWeb=new Version(retour); if(versionWeb.VersionIsSup(versionAppli)) { bt_maj.BackColor = Color.LightGreen; } } catch (Exception e) { MessageBox.Show(e.Message); } this.labelVersion.Text = String.Format("Version {0} Prête:{1}", AssemblyVersion, versionWeb.ToString()); this.labelCopyright.Text = AssemblyCopyright; this.labelCompanyName.Text = AssemblyCompany; this.textBoxDescription.Text = AssemblyDescription + "\r\n" + this.textBoxDescription.Text; this.textBoxDescription.Text += "\r\n" + Program.folder; this.textBoxDescription.Text += "\r\n" + Program.subfolder; textSrvDatas.Text = Properties.Settings.Default.server_adress_datas; textSrvBin.Text = Properties.Settings.Default.server_adress_bin; textSignalR.Text = Properties.Settings.Default.server_signalr; textCopieLocal.Text = Properties.Settings.Default.chemin_copie_locale; checkBoxLocale.Checked = Properties.Settings.Default.use_locale; textCopieAPI.Text = Properties.Settings.Default.adresse_copie_api; bt_send.Enabled = File.Exists(Program.subfolder + @"\station.json"); } #region Accesseurs d'attribut de l'assembly public static string AssemblyTitle { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; if (titleAttribute.Title != "") { return titleAttribute.Title; } } return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); } } public static string AssemblyVersion { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } public static string AssemblyDescription { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyDescriptionAttribute)attributes[0]).Description; } } public static string AssemblyProduct { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute)attributes[0]).Product; } } public static string AssemblyCopyright { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } } public static string AssemblyCompany { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCompanyAttribute)attributes[0]).Company; } } #endregion private void button2_Click(object sender, EventArgs e) { DialogResult dr = opfile.ShowDialog(); if (dr == DialogResult.OK) { string filename = opfile.FileName; f_extract res = new f_extract(filename); res.ShowDialog(); } } private void button1_Click(object sender, EventArgs e) { fold.Description = "Sélectionnez le répertoire où sera enregistrée l'archive"; DialogResult dr = fold.ShowDialog(); if (dr == DialogResult.OK) { DateTime dt = DateTime.Now; string filename = fold.SelectedPath + @"\Result_" + dt.ToString("yy_MM_dd_hh_mm_ss") + ".txt"; StreamWriter writer = File.CreateText(filename); var zip = new ZipFile(fold.SelectedPath + @"\Backup_" + dt.ToString("yy_MM_dd_hh_mm_ss") + ".zip", writer); string tmpDir = Path.GetTempPath(); if (!Directory.Exists(tmpDir)) tmpDir = Environment.GetEnvironmentVariable("temp"); if (!Directory.Exists(tmpDir)) tmpDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); zip.TempFileFolder = tmpDir; zip.AddDirectory(Program.folder, "AsoFuel"); zip.Save(); writer.Close(); f_result res = new f_result(filename, zip.Name); res.ShowDialog(); } } private void bt_send_Click(object sender, EventArgs e) { label1.Visible = true; this.Cursor = Cursors.WaitCursor; System.Windows.Forms.Application.DoEvents(); int i = 1; Properties.Settings.Default.chemin_copie_locale = textCopieLocal.Text; if (Properties.Settings.Default.use_locale) { File.Copy(Program.subfolder + @"\station.json", Properties.Settings.Default.chemin_copie_locale + @"\station.json", true); File.Copy(Program.subfolder + @"\presta.json", Properties.Settings.Default.chemin_copie_locale + @"\presta.json", true); File.Copy(Program.subfolder + @"\vehicule.json", Properties.Settings.Default.chemin_copie_locale + @"\vehicule.json", true); File.Copy(Program.folder + @"\param\produit.json", Properties.Settings.Default.chemin_copie_locale + @"\produit.json", true); SendFile(Program.subfolder + @"\station.json").Wait(); UploadProgressChanged(i++, 4); SendFile(Program.subfolder + @"\presta.json").Wait(); UploadProgressChanged(i++, 4); SendFile(Program.subfolder + @"\vehicule.json").Wait() ; UploadProgressChanged(i++, 4); SendFile(Program.folder + @"\param\produit.json").Wait(); UploadProgressChanged(i, 4); } else { SendFile(Program.subfolder + @"\station.json").Wait(); UploadProgressChanged(i++, 4); SendFile(Program.subfolder + @"\presta.json").Wait(); UploadProgressChanged(i++, 4); SendFile(Program.subfolder + @"\vehicule.json").Wait(); UploadProgressChanged(i++, 4); SendFile(Program.folder + @"\param\produit.json").Wait(); UploadProgressChanged(i, 4); } Thread.Sleep(500); label1.Visible = false; this.Cursor = Cursors.Default; System.Windows.Forms.Application.DoEvents(); } private async Task SendFile(string fileName) { var file = new TdfFile { Name = Path.GetFileName(fileName), Data = Encoding.UTF8.GetBytes(File.ReadAllText(fileName)) }; try { var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Post, $"{Properties.Settings.Default.adresse_copie_api}GetFile"); var content = new StringContent(JsonConvert.SerializeObject(file), null, "application/json"); request.Content = content; var response = client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); client = new HttpClient(); request = new HttpRequestMessage(HttpMethod.Post, $"https://tdf.axpower.eu/api/FileUpload/GetFile"); content = new StringContent(JsonConvert.SerializeObject(file), null, "application/json"); request.Content = content; var responseWeb = client.SendAsync(request).Result; responseWeb.EnsureSuccessStatusCode(); Console.WriteLine(await response.Content.ReadAsStringAsync()); } catch (Exception ex) { Console.WriteLine($"ERRRRRRRRRRRRRRRREUUUUUUUUUURRRRRRRRRRR {ex.Message}"); } } private void UploadProgressChanged(int value,int maxValue) { double percentage = value / maxValue * 100; progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); System.Windows.Forms.Application.DoEvents(); } private void bt_clean_Click(object sender, EventArgs e) { f_clean fclean = new f_clean(); fclean.ShowDialog(); } private void button2_Click_1(object sender, EventArgs e) { string appName = Properties.Settings.Default.server_adress_bin.Substring(Properties.Settings.Default.server_adress_bin.LastIndexOf('/') + 1); string chaine = Properties.Settings.Default.server_adress_bin; string tmpDir = Path.GetTempPath(); if (!Directory.Exists(tmpDir)) tmpDir = Environment.GetEnvironmentVariable("temp"); if (!Directory.Exists(tmpDir)) tmpDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.Dispose(); try { runApp = tmpDir + appName; if (File.Exists(runApp)) File.Delete(runApp); client.DownloadFileAsync(new Uri(chaine), tmpDir + appName); } catch { runApp = ""; MessageBox.Show("Le téléchargement a échoué"); } bt_maj.Text = "En cours.."; bt_maj.Enabled = false; } void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { double bytesIn = double.Parse(e.BytesReceived.ToString()); double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = bytesIn / totalBytes * 100; progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString()); } void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if (MessageBox.Show("L'application va se terminer et l'installation de la nouvelle commencer!!", "Téléchargement Terminé", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK) { ProcessStartInfo myprocessStartInfo = new ProcessStartInfo(runApp); Process install = new Process(); install.StartInfo = myprocessStartInfo; install.Start(); System.Windows.Forms.Application.Exit(); } bt_maj.Text = "MAJ"; bt_maj.Enabled = true; bt_maj.BackColor = Color.Transparent; } private void bt_check_Click(object sender, EventArgs e) { TextBox from = null; CheckBox chk = null; if ((Button)sender == bt_checkBin) { from = textSrvBin; chk = checkBoxBin; } if ((Button)sender == bt_checkDatas) { from = textSrvDatas; chk = checkBoxDatas; } if ((Button)sender == bt_checkR) { from = textSignalR; chk = checkBoxSignalR; } if ((Button)sender == bt_chekAPI) { from = textCopieAPI; chk = checkBoxAPI; } lblRes.Text = "...."; bool result = checkUrl(from.Text + ((Button)sender == bt_chekAPI ? "test" : "")); chk.CheckState = result ? CheckState.Checked : CheckState.Unchecked; lblRes.Text = result.ToString(); } private static bool checkUrl(string url) { bool ret = false; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 5000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); ret = true; response.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return ret; } private void bt_sav_Click(object sender, EventArgs e) { if (checkBoxDatas.CheckState == CheckState.Checked || checkAll.Checked) Properties.Settings.Default.server_adress_datas = textSrvDatas.Text; if (checkBoxBin.CheckState == CheckState.Checked || checkAll.Checked) Properties.Settings.Default.server_adress_bin = textSrvBin.Text; if (checkBoxSignalR.CheckState == CheckState.Checked || checkAll.Checked) Properties.Settings.Default.server_signalr = textSignalR.Text; Properties.Settings.Default.chemin_copie_locale = textCopieLocal.Text; Properties.Settings.Default.use_locale = checkBoxLocale.Checked; if (checkBoxAPI.CheckState == CheckState.Checked || checkAll.Checked) Properties.Settings.Default.adresse_copie_api = textCopieAPI.Text; Properties.Settings.Default.Save(); } private void bt_fold_Click(object sender, EventArgs e) { fold2.SelectedPath = textCopieLocal.Text; DialogResult res = fold2.ShowDialog(); if (res == DialogResult.OK) textCopieLocal.Text = fold2.SelectedPath; } } }