f_about.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using Ionic.Utils.Zip;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace Fuel01
  16. {
  17. partial class f_about : Form
  18. {
  19. string runApp = "";
  20. public f_about()
  21. {
  22. InitializeComponent();
  23. this.Text = String.Format("À propos de {0}", AssemblyTitle);
  24. this.labelProductName.Text = AssemblyProduct;
  25. string[] strVersion = AssemblyVersion.Split('.');
  26. double version = Convert.ToDouble(strVersion[0]) * 100000 + Convert.ToDouble(strVersion[1]) * 1000 + Convert.ToDouble(strVersion[2]) + Convert.ToDouble(strVersion[3]) / 10000;
  27. double baseVersion = 0;
  28. WebRequest request = WebRequest.Create(Properties.Settings.Default.server_adress_datas);
  29. try
  30. {
  31. WebResponse response = request.GetResponse();
  32. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
  33. string retour = sr.ReadToEnd();
  34. sr.Close();
  35. if (double.TryParse(retour, out baseVersion) && baseVersion > version)
  36. {
  37. bt_maj.BackColor = Color.LightGreen;
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. MessageBox.Show(e.Message);
  43. }
  44. this.labelVersion.Text = String.Format("Version {0}-{1}-{2}", AssemblyVersion, version, baseVersion);
  45. this.labelCopyright.Text = AssemblyCopyright;
  46. this.labelCompanyName.Text = AssemblyCompany;
  47. this.textBoxDescription.Text = AssemblyDescription + "\r\n" + this.textBoxDescription.Text;
  48. this.textBoxDescription.Text += "\r\n" + Program.folder;
  49. this.textBoxDescription.Text += "\r\n" + Program.subfolder;
  50. textSrvDatas.Text = Properties.Settings.Default.server_adress_datas;
  51. textSrvBin.Text = Properties.Settings.Default.server_adress_bin;
  52. textSignalR.Text = Properties.Settings.Default.server_signalr;
  53. textCopieLocal.Text = Properties.Settings.Default.chemin_copie_locale;
  54. checkBoxLocale.Checked = Properties.Settings.Default.use_locale;
  55. textCopieAPI.Text = Properties.Settings.Default.adresse_copie_api;
  56. bt_send.Enabled = File.Exists(Program.subfolder + @"\station.json");
  57. }
  58. #region Accesseurs d'attribut de l'assembly
  59. public static string AssemblyTitle
  60. {
  61. get
  62. {
  63. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
  64. if (attributes.Length > 0)
  65. {
  66. AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
  67. if (titleAttribute.Title != "")
  68. {
  69. return titleAttribute.Title;
  70. }
  71. }
  72. return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
  73. }
  74. }
  75. public static string AssemblyVersion
  76. {
  77. get
  78. {
  79. return Assembly.GetExecutingAssembly().GetName().Version.ToString();
  80. }
  81. }
  82. public static string AssemblyDescription
  83. {
  84. get
  85. {
  86. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
  87. if (attributes.Length == 0)
  88. {
  89. return "";
  90. }
  91. return ((AssemblyDescriptionAttribute)attributes[0]).Description;
  92. }
  93. }
  94. public static string AssemblyProduct
  95. {
  96. get
  97. {
  98. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
  99. if (attributes.Length == 0)
  100. {
  101. return "";
  102. }
  103. return ((AssemblyProductAttribute)attributes[0]).Product;
  104. }
  105. }
  106. public static string AssemblyCopyright
  107. {
  108. get
  109. {
  110. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
  111. if (attributes.Length == 0)
  112. {
  113. return "";
  114. }
  115. return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
  116. }
  117. }
  118. public static string AssemblyCompany
  119. {
  120. get
  121. {
  122. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
  123. if (attributes.Length == 0)
  124. {
  125. return "";
  126. }
  127. return ((AssemblyCompanyAttribute)attributes[0]).Company;
  128. }
  129. }
  130. #endregion
  131. private void button2_Click(object sender, EventArgs e)
  132. {
  133. DialogResult dr = opfile.ShowDialog();
  134. if (dr == DialogResult.OK)
  135. {
  136. string filename = opfile.FileName;
  137. f_extract res = new f_extract(filename);
  138. res.ShowDialog();
  139. }
  140. }
  141. private void button1_Click(object sender, EventArgs e)
  142. {
  143. fold.Description = "Sélectionnez le répertoire où sera enregistrée l'archive";
  144. DialogResult dr = fold.ShowDialog();
  145. if (dr == DialogResult.OK)
  146. {
  147. DateTime dt = DateTime.Now;
  148. string filename = fold.SelectedPath + @"\Result_" + dt.ToString("yy_MM_dd_hh_mm_ss") + ".txt";
  149. StreamWriter writer = File.CreateText(filename);
  150. var zip = new ZipFile(fold.SelectedPath + @"\Backup_" + dt.ToString("yy_MM_dd_hh_mm_ss") + ".zip", writer);
  151. string tmpDir = Path.GetTempPath();
  152. if (!Directory.Exists(tmpDir))
  153. tmpDir = Environment.GetEnvironmentVariable("temp");
  154. if (!Directory.Exists(tmpDir))
  155. tmpDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  156. zip.TempFileFolder = tmpDir;
  157. zip.AddDirectory(Program.folder, "AsoFuel");
  158. zip.Save();
  159. writer.Close();
  160. f_result res = new f_result(filename, zip.Name);
  161. res.ShowDialog();
  162. }
  163. }
  164. private void bt_send_Click(object sender, EventArgs e)
  165. {
  166. label1.Visible = true;
  167. this.Cursor = Cursors.WaitCursor;
  168. System.Windows.Forms.Application.DoEvents();
  169. Properties.Settings.Default.chemin_copie_locale = textCopieLocal.Text;
  170. if (Properties.Settings.Default.use_locale)
  171. {
  172. File.Copy(Program.subfolder + @"\station.json", Properties.Settings.Default.chemin_copie_locale + @"\station.json", true);
  173. File.Copy(Program.subfolder + @"\presta.json", Properties.Settings.Default.chemin_copie_locale + @"\presta.json", true);
  174. File.Copy(Program.subfolder + @"\vehicule.json", Properties.Settings.Default.chemin_copie_locale + @"\vehicule.json", true);
  175. File.Copy(Program.folder + @"\param\produit.json", Properties.Settings.Default.chemin_copie_locale + @"\produit.json", true);
  176. SendFile(Program.subfolder + @"\station.json").Wait();
  177. SendFile(Program.subfolder + @"\presta.json").Wait();
  178. SendFile(Program.subfolder + @"\vehicule.json").Wait() ;
  179. SendFile(Program.folder + @"\param\produit.json").Wait();
  180. }
  181. else
  182. {
  183. SendFile(Program.subfolder + @"\station.json").Wait();
  184. SendFile(Program.subfolder + @"\presta.json").Wait();
  185. SendFile(Program.subfolder + @"\vehicule.json").Wait();
  186. SendFile(Program.folder + @"\param\produit.json").Wait();
  187. }
  188. Thread.Sleep(5000);
  189. label1.Visible = false;
  190. this.Cursor = Cursors.Default;
  191. System.Windows.Forms.Application.DoEvents();
  192. }
  193. private async Task SendFile(string fileName)
  194. {
  195. var file = new TdfFile
  196. {
  197. Name = Path.GetFileName(fileName),
  198. Data = Encoding.UTF8.GetBytes(File.ReadAllText(fileName))
  199. };
  200. try
  201. {
  202. var client = new HttpClient();
  203. var request = new HttpRequestMessage(HttpMethod.Post, $"{Properties.Settings.Default.adresse_copie_api}GetFile");
  204. var content = new StringContent(JsonConvert.SerializeObject(file), null, "application/json");
  205. request.Content = content;
  206. var response = await client.SendAsync(request);
  207. response.EnsureSuccessStatusCode();
  208. Console.WriteLine(await response.Content.ReadAsStringAsync());
  209. }
  210. catch (Exception ex)
  211. {
  212. Console.WriteLine("ERRRRRRRRRRRRRRRREUUUUUUUUUURRRRRRRRRRR POST :" + ex.ToString());
  213. }
  214. }
  215. private void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
  216. {
  217. double bytesIn = double.Parse(e.BytesSent.ToString());
  218. double totalBytes = double.Parse(e.TotalBytesToSend.ToString());
  219. double percentage = bytesIn / totalBytes * 100;
  220. progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  221. }
  222. private void bt_clean_Click(object sender, EventArgs e)
  223. {
  224. f_clean fclean = new f_clean();
  225. fclean.ShowDialog();
  226. }
  227. private void button2_Click_1(object sender, EventArgs e)
  228. {
  229. string appName = Properties.Settings.Default.server_adress_bin.Substring(Properties.Settings.Default.server_adress_bin.LastIndexOf('/') + 1);
  230. string chaine = Properties.Settings.Default.server_adress_bin;
  231. string tmpDir = Path.GetTempPath();
  232. if (!Directory.Exists(tmpDir))
  233. tmpDir = Environment.GetEnvironmentVariable("temp");
  234. if (!Directory.Exists(tmpDir))
  235. tmpDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  236. WebClient client = new WebClient();
  237. client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  238. client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  239. client.Dispose();
  240. try
  241. {
  242. runApp = tmpDir + appName;
  243. if (File.Exists(runApp))
  244. File.Delete(runApp);
  245. client.DownloadFileAsync(new Uri(chaine), tmpDir + appName);
  246. }
  247. catch
  248. {
  249. runApp = "";
  250. MessageBox.Show("Le téléchargement a échoué");
  251. }
  252. bt_maj.Text = "En cours..";
  253. bt_maj.Enabled = false;
  254. }
  255. void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  256. {
  257. double bytesIn = double.Parse(e.BytesReceived.ToString());
  258. double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  259. double percentage = bytesIn / totalBytes * 100;
  260. progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  261. }
  262. void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  263. {
  264. 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)
  265. {
  266. ProcessStartInfo myprocessStartInfo = new ProcessStartInfo(runApp);
  267. Process install = new Process();
  268. install.StartInfo = myprocessStartInfo;
  269. install.Start();
  270. System.Windows.Forms.Application.Exit();
  271. }
  272. bt_maj.Text = "MAJ";
  273. bt_maj.Enabled = true;
  274. bt_maj.BackColor = Color.Transparent;
  275. }
  276. private void bt_check_Click(object sender, EventArgs e)
  277. {
  278. TextBox from = null;
  279. CheckBox chk = null;
  280. if ((Button)sender == bt_checkBin)
  281. {
  282. from = textSrvBin;
  283. chk = checkBoxBin;
  284. }
  285. if ((Button)sender == bt_checkDatas)
  286. {
  287. from = textSrvDatas;
  288. chk = checkBoxDatas;
  289. }
  290. if ((Button)sender == bt_checkR)
  291. {
  292. from = textSignalR;
  293. chk = checkBoxSignalR;
  294. }
  295. if ((Button)sender == bt_chekAPI)
  296. {
  297. from = textCopieAPI;
  298. chk = checkBoxAPI;
  299. }
  300. lblRes.Text = "....";
  301. bool result = checkUrl(from.Text + ((Button)sender == bt_chekAPI ? "test" : ""));
  302. chk.CheckState = result ? CheckState.Checked : CheckState.Unchecked;
  303. lblRes.Text = result.ToString();
  304. }
  305. private static bool checkUrl(string url)
  306. {
  307. bool ret = false;
  308. try
  309. {
  310. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  311. request.Timeout = 5000;
  312. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  313. ret = true;
  314. response.Dispose();
  315. }
  316. catch (Exception ex)
  317. {
  318. MessageBox.Show(ex.Message);
  319. }
  320. return ret;
  321. }
  322. private void bt_sav_Click(object sender, EventArgs e)
  323. {
  324. if (checkBoxDatas.CheckState == CheckState.Checked || checkAll.Checked)
  325. Properties.Settings.Default.server_adress_datas = textSrvDatas.Text;
  326. if (checkBoxBin.CheckState == CheckState.Checked || checkAll.Checked)
  327. Properties.Settings.Default.server_adress_bin = textSrvBin.Text;
  328. if (checkBoxSignalR.CheckState == CheckState.Checked || checkAll.Checked)
  329. Properties.Settings.Default.server_signalr = textSignalR.Text;
  330. Properties.Settings.Default.chemin_copie_locale = textCopieLocal.Text;
  331. Properties.Settings.Default.use_locale = checkBoxLocale.Checked;
  332. if (checkBoxAPI.CheckState == CheckState.Checked || checkAll.Checked)
  333. Properties.Settings.Default.adresse_copie_api = textCopieAPI.Text;
  334. Properties.Settings.Default.Save();
  335. }
  336. private void bt_fold_Click(object sender, EventArgs e)
  337. {
  338. fold2.SelectedPath = textCopieLocal.Text;
  339. DialogResult res = fold2.ShowDialog();
  340. if (res == DialogResult.OK)
  341. textCopieLocal.Text = fold2.SelectedPath;
  342. }
  343. }
  344. }