f_about.cs 16 KB

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