f_about.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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", 2);
  177. sendFile(Program.subfolder + @"\presta.json", 2);
  178. sendFile(Program.subfolder + @"\vehicule.json", 2);
  179. sendFile(Program.folder + @"\param\produit.json", 2);
  180. }
  181. else
  182. {
  183. sendFile(Program.subfolder + @"\station.json", 3);
  184. sendFile(Program.subfolder + @"\presta.json", 3);
  185. sendFile(Program.subfolder + @"\vehicule.json", 3);
  186. sendFile(Program.folder + @"\param\produit.json", 3);
  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, int mode)
  194. {
  195. //if ((mode & 1) == 1)
  196. //{
  197. // string chaine = Properties.Settings.Default.adresse_copie_api;
  198. // WebClient client = new WebClient();
  199. // client.UploadProgressChanged += new UploadProgressChangedEventHandler(Client_UploadProgressChanged);
  200. // client.UploadFileAsync(new Uri(chaine), "POST", fileName);
  201. // client.Dispose();
  202. //}
  203. //if ((mode & 2) == 2)
  204. //{
  205. // string chaine2 = @"http://www.aformatix.com/ASOStationService/api/FileUpLoad/upLoadFiles";
  206. // WebClient clientAfo = new WebClient();
  207. // clientAfo.UploadFileAsync(new Uri(chaine2), "POST", fileName);
  208. // clientAfo.Dispose();
  209. //}
  210. var file = new TdfFile
  211. {
  212. Name = Path.GetFileName(fileName),
  213. Data = Encoding.UTF8.GetBytes(File.ReadAllText(fileName))
  214. };
  215. try
  216. {
  217. var client = new HttpClient();
  218. var request = new HttpRequestMessage(HttpMethod.Post, $"{Properties.Settings.Default.adresse_copie_api}GetFile");
  219. var content = new StringContent(JsonConvert.SerializeObject(file), null, "application/json");
  220. request.Content = content;
  221. var response = await client.SendAsync(request);
  222. response.EnsureSuccessStatusCode();
  223. Console.WriteLine(await response.Content.ReadAsStringAsync());
  224. }
  225. catch (Exception ex)
  226. {
  227. Console.WriteLine("ERRRRRRRRRRRRRRRREUUUUUUUUUURRRRRRRRRRR POST :" + ex.ToString());
  228. }
  229. }
  230. private void Client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
  231. {
  232. double bytesIn = double.Parse(e.BytesSent.ToString());
  233. double totalBytes = double.Parse(e.TotalBytesToSend.ToString());
  234. double percentage = bytesIn / totalBytes * 100;
  235. progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  236. }
  237. private void bt_clean_Click(object sender, EventArgs e)
  238. {
  239. f_clean fclean = new f_clean();
  240. fclean.ShowDialog();
  241. }
  242. private void button2_Click_1(object sender, EventArgs e)
  243. {
  244. string appName = Properties.Settings.Default.server_adress_bin.Substring(Properties.Settings.Default.server_adress_bin.LastIndexOf('/') + 1);
  245. string chaine = Properties.Settings.Default.server_adress_bin;
  246. string tmpDir = Path.GetTempPath();
  247. if (!Directory.Exists(tmpDir))
  248. tmpDir = Environment.GetEnvironmentVariable("temp");
  249. if (!Directory.Exists(tmpDir))
  250. tmpDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  251. WebClient client = new WebClient();
  252. client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  253. client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
  254. client.Dispose();
  255. try
  256. {
  257. runApp = tmpDir + appName;
  258. if (File.Exists(runApp))
  259. File.Delete(runApp);
  260. client.DownloadFileAsync(new Uri(chaine), tmpDir + appName);
  261. }
  262. catch
  263. {
  264. runApp = "";
  265. MessageBox.Show("Le téléchargement a échoué");
  266. }
  267. bt_maj.Text = "En cours..";
  268. bt_maj.Enabled = false;
  269. }
  270. void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  271. {
  272. double bytesIn = double.Parse(e.BytesReceived.ToString());
  273. double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  274. double percentage = bytesIn / totalBytes * 100;
  275. progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
  276. }
  277. void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  278. {
  279. 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)
  280. {
  281. ProcessStartInfo myprocessStartInfo = new ProcessStartInfo(runApp);
  282. Process install = new Process();
  283. install.StartInfo = myprocessStartInfo;
  284. install.Start();
  285. System.Windows.Forms.Application.Exit();
  286. }
  287. bt_maj.Text = "MAJ";
  288. bt_maj.Enabled = true;
  289. bt_maj.BackColor = Color.Transparent;
  290. }
  291. private void bt_check_Click(object sender, EventArgs e)
  292. {
  293. TextBox from = null;
  294. CheckBox chk = null;
  295. if ((Button)sender == bt_checkBin)
  296. {
  297. from = textSrvBin;
  298. chk = checkBoxBin;
  299. }
  300. if ((Button)sender == bt_checkDatas)
  301. {
  302. from = textSrvDatas;
  303. chk = checkBoxDatas;
  304. }
  305. if ((Button)sender == bt_checkR)
  306. {
  307. from = textSignalR;
  308. chk = checkBoxSignalR;
  309. }
  310. if ((Button)sender == bt_chekAPI)
  311. {
  312. from = textCopieAPI;
  313. chk = checkBoxAPI;
  314. }
  315. lblRes.Text = "....";
  316. bool result = checkUrl(from.Text + ((Button)sender == bt_chekAPI ? "test" : ""));
  317. chk.CheckState = result ? CheckState.Checked : CheckState.Unchecked;
  318. lblRes.Text = result.ToString();
  319. }
  320. private static bool checkUrl(string url)
  321. {
  322. bool ret = false;
  323. try
  324. {
  325. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  326. request.Timeout = 5000;
  327. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  328. ret = true;
  329. response.Dispose();
  330. }
  331. catch (Exception ex)
  332. {
  333. MessageBox.Show(ex.Message);
  334. }
  335. return ret;
  336. }
  337. private void bt_sav_Click(object sender, EventArgs e)
  338. {
  339. if (checkBoxDatas.CheckState == CheckState.Checked || checkAll.Checked)
  340. Properties.Settings.Default.server_adress_datas = textSrvDatas.Text;
  341. if (checkBoxBin.CheckState == CheckState.Checked || checkAll.Checked)
  342. Properties.Settings.Default.server_adress_bin = textSrvBin.Text;
  343. if (checkBoxSignalR.CheckState == CheckState.Checked || checkAll.Checked)
  344. Properties.Settings.Default.server_signalr = textSignalR.Text;
  345. Properties.Settings.Default.chemin_copie_locale = textCopieLocal.Text;
  346. Properties.Settings.Default.use_locale = checkBoxLocale.Checked;
  347. if (checkBoxAPI.CheckState == CheckState.Checked || checkAll.Checked)
  348. Properties.Settings.Default.adresse_copie_api = textCopieAPI.Text;
  349. Properties.Settings.Default.Save();
  350. }
  351. private void bt_fold_Click(object sender, EventArgs e)
  352. {
  353. fold2.SelectedPath = textCopieLocal.Text;
  354. DialogResult res = fold2.ShowDialog();
  355. if (res == DialogResult.OK)
  356. textCopieLocal.Text = fold2.SelectedPath;
  357. }
  358. }
  359. }