MainWindow.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Domain.Models.Database;
  3. using EVCB_OCPP.Packet.Features;
  4. using EVCB_OCPP.Packet.Messages;
  5. using EVCB_OCPP.Packet.Messages.Core;
  6. using Microsoft.Win32;
  7. using Newtonsoft.Json;
  8. using OCPPPackage.Profiles;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Configuration;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Security.Cryptography;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. using System.Data.Entity;
  28. using EVCB_OCPP.Packet.Messages.RemoteTrigger;
  29. using EVCB_OCPP.Packet.Messages.SubTypes;
  30. using EVCB_OCPP.Packet.Messages.LocalAuthListManagement;
  31. using EVCB_OCPP.Packet.Messages.FirmwareManagement;
  32. using EVCB_OCPP.Packet.Messages.Reservation;
  33. using EVCB_OCPP.Packet.Messages.SmartCharging;
  34. namespace TestTool.RemoteTriggerAPP
  35. {
  36. /// <summary>
  37. /// MainWindow.xaml 的互動邏輯
  38. /// </summary>
  39. public partial class MainWindow : Window
  40. {
  41. string action = "";
  42. List<PublishVersion> publishes = new List<PublishVersion>();
  43. int selectedPublish = -1;
  44. public MainWindow()
  45. {
  46. InitializeComponent();
  47. }
  48. private void UxSubmitBtn_Click(object sender, RoutedEventArgs e)
  49. {
  50. switch (action)
  51. {
  52. #region Core Profile
  53. case "ChangeAvailability_Inoperative":
  54. {
  55. SetChangeAvailability_Inoperative();
  56. }
  57. break;
  58. case "ChangeAvailability_Operative":
  59. {
  60. SetChangeAvailability_Operative();
  61. }
  62. break;
  63. case "GetConfiguration":
  64. {
  65. if (uxConfigKeyTb.Text == "")
  66. {
  67. SetGetConfiguration_ALL();
  68. }
  69. else
  70. {
  71. SetGetConfiguration();
  72. }
  73. }
  74. break;
  75. case "ChangeConfiguration":
  76. {
  77. SetChangeConfiguration();
  78. }
  79. break;
  80. case "ClearCache":
  81. {
  82. SetClearCache();
  83. }
  84. break;
  85. case "RemoteStartTransaction":
  86. {
  87. SetRemoteStartTransaction();
  88. }
  89. break;
  90. case "RemoteStartTransaction_ChargingProfile":
  91. {
  92. SetRemoteStartTransaction(true);
  93. }
  94. break;
  95. case "RemoteStopTransaction":
  96. {
  97. SetRemoteStopTransaction();
  98. }
  99. break;
  100. case "HardReset":
  101. {
  102. SetHardReset();
  103. }
  104. break;
  105. case "SoftReset":
  106. {
  107. SetSoftReset();
  108. }
  109. break;
  110. case "UnlockConnector":
  111. {
  112. SetUnlockConnector();
  113. }
  114. break;
  115. #endregion
  116. #region Local Authorization
  117. case "GetLocalListVersion":
  118. {
  119. GetLocalListVersion();
  120. }
  121. break;
  122. case "SendLocalAuthorizationList_Full":
  123. {
  124. SendLocalAuthorizationList(true);
  125. }
  126. break;
  127. case "SendLocalAuthorizationList_Diff":
  128. {
  129. SendLocalAuthorizationList(false);
  130. }
  131. break;
  132. #endregion
  133. case "GetDiagnostics":
  134. {
  135. GetDiagnostics();
  136. }
  137. break;
  138. case "ReserveNow":
  139. {
  140. ReserveNow();
  141. }
  142. break;
  143. case "CancelReservation":
  144. {
  145. CancelReservation();
  146. }
  147. break;
  148. case "ClearChargingProfile":
  149. {
  150. ClearChargingProfile();
  151. }
  152. break;
  153. case "GetCompositeSchedule":
  154. {
  155. GetCompositeSchedule();
  156. }
  157. break;
  158. case "SetChargingProfile_TxDefault":
  159. {
  160. SetChargingProfile(true);
  161. }
  162. break;
  163. case "SetChargingProfile_Tx":
  164. {
  165. SetChargingProfile(false);
  166. }
  167. break;
  168. #region Trigger Profile
  169. case "TriggerMessage_BootNotification":
  170. case "TriggerMessage_DiagnosticsStatusNotification":
  171. case "TriggerMessage_FirmwareStatusNotification":
  172. case "TriggerMessage_Heartbeat":
  173. case "TriggerMessage_MeterValues":
  174. case "TriggerMessage_StatusNotification":
  175. {
  176. SetRemoteTrigger(action);
  177. }
  178. break;
  179. #endregion
  180. default:
  181. break;
  182. }
  183. }
  184. private void SetChargingProfile(bool isDefault)
  185. {
  186. try
  187. {
  188. var uuid = Guid.NewGuid().ToString();
  189. var request = new SetChargingProfileRequest()
  190. {
  191. connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
  192. csChargingProfiles = new csChargingProfiles()
  193. {
  194. chargingProfileId = Convert.ToInt32(DateTime.Now.ToUniversalTime().ToString("yyMMddHHmm")),
  195. chargingProfileKind = ChargingProfileKindType.Absolute,
  196. chargingProfilePurpose = isDefault ? ChargingProfilePurposeType.TxDefaultProfile : ChargingProfilePurposeType.TxProfile,
  197. recurrencyKind = RecurrencyKindType.Daily,
  198. stackLevel = 1,
  199. chargingSchedule = new ChargingSchedule()
  200. {
  201. chargingRateUnit = ChargingRateUnitType.A,
  202. duration = 300,
  203. minChargingRate = 0,
  204. startSchedule = DateTime.Now.ToUniversalTime().Date,
  205. chargingSchedulePeriod = new List<ChargingSchedulePeriod>()
  206. {
  207. new ChargingSchedulePeriod()
  208. { limit=10, startPeriod=0, numberPhases=3 },
  209. new ChargingSchedulePeriod()
  210. { limit=2, startPeriod=60, numberPhases=3 },
  211. new ChargingSchedulePeriod()
  212. { limit=8, startPeriod=120, numberPhases=3 }
  213. }
  214. }
  215. }
  216. };
  217. WritetoDB(uuid, request);
  218. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  219. }
  220. catch (Exception ex)
  221. {
  222. MessageBox.Show(ex.ToString());
  223. }
  224. }
  225. private void GetCompositeSchedule()
  226. {
  227. try
  228. {
  229. var uuid = Guid.NewGuid().ToString();
  230. var request = new GetCompositeScheduleRequest()
  231. {
  232. connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
  233. duration = 300
  234. };
  235. WritetoDB(uuid, request);
  236. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  237. }
  238. catch (Exception ex)
  239. {
  240. MessageBox.Show(ex.ToString());
  241. }
  242. }
  243. private void ClearChargingProfile()
  244. {
  245. try
  246. {
  247. var uuid = Guid.NewGuid().ToString();
  248. var request = new ClearChargingProfileRequest()
  249. {
  250. connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
  251. };
  252. WritetoDB(uuid, request);
  253. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  254. }
  255. catch (Exception ex)
  256. {
  257. MessageBox.Show(ex.ToString());
  258. }
  259. }
  260. private void CancelReservation()
  261. {
  262. try
  263. {
  264. var uuid = Guid.NewGuid().ToString();
  265. var request = new CancelReservationRequest()
  266. {
  267. reservationId = Convert.ToInt32(uxReservationTb.Text),
  268. };
  269. WritetoDB(uuid, request);
  270. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  271. }
  272. catch (Exception ex)
  273. {
  274. MessageBox.Show(ex.ToString());
  275. }
  276. }
  277. private void ReserveNow()
  278. {
  279. bool isError = false;
  280. DateTime expiryTime = DateTime.Now;
  281. try
  282. {
  283. try
  284. {
  285. expiryTime = Convert.ToDateTime(uxExpiryTimeTb.Text);
  286. if (expiryTime < DateTime.Now.AddSeconds(60))
  287. {
  288. isError = true;
  289. uxMsgTb.Text = string.Format("ExpiryTime is too close or too early to the current time (60 seconds).");
  290. }
  291. }
  292. catch (Exception ex)
  293. {
  294. if (ex is FormatException)
  295. {
  296. isError = true;
  297. uxMsgTb.Text = string.Format("ExpiryTime's Format is wrong Example:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
  298. }
  299. }
  300. if (!isError)
  301. {
  302. var uuid = Guid.NewGuid().ToString();
  303. var request = new ReserveNowRequest()
  304. {
  305. connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
  306. expiryDate = expiryTime.ToUniversalTime(),
  307. idTag = uxIdTagTb.Text,
  308. parentIdTag = "PTAG",
  309. reservationId = Convert.ToInt32(expiryTime.ToUniversalTime().ToString("yyMMddHHmm")),
  310. };
  311. WritetoDB(uuid, request);
  312. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. MessageBox.Show(ex.ToString());
  318. }
  319. }
  320. private void GetDiagnostics()
  321. {
  322. try
  323. {
  324. var uuid = Guid.NewGuid().ToString();
  325. var request = new GetDiagnosticsRequest()
  326. {
  327. location = new Uri("ftp://phihong:y42j%2f4cj84@test.evsocket.phihong.com.cn/"),
  328. retries = 1,
  329. retryInterval = 30,
  330. startTime = DateTime.Now.AddHours(-1).ToUniversalTime(),
  331. stopTime = DateTime.Now.ToUniversalTime()
  332. };
  333. WritetoDB(uuid, request);
  334. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  335. }
  336. catch (Exception ex)
  337. {
  338. MessageBox.Show(ex.ToString());
  339. }
  340. }
  341. #region Local Authorization
  342. private void GetLocalListVersion()
  343. {
  344. try
  345. {
  346. var uuid = Guid.NewGuid().ToString();
  347. var request = new GetLocalListVersionRequest()
  348. {
  349. };
  350. WritetoDB(uuid, request);
  351. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  352. }
  353. catch (Exception ex)
  354. {
  355. MessageBox.Show(ex.ToString());
  356. }
  357. }
  358. private void SendLocalAuthorizationList(bool isFull)
  359. {
  360. try
  361. {
  362. var uuid = Guid.NewGuid().ToString();
  363. var request = new SendLocalListRequest()
  364. {
  365. listVersion = Convert.ToInt32(DateTime.Now.ToUniversalTime().ToString("yyMMddHHmm")),
  366. updateType = isFull ? UpdateType.Full : UpdateType.Differential,
  367. localAuthorizationList = new List<AuthorizationData>()
  368. {
  369. new AuthorizationData()
  370. {
  371. idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.ToUniversalTime().AddDays(3), status= AuthorizationStatus.Accepted},
  372. idTag="123"
  373. }, new AuthorizationData()
  374. {
  375. idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.ToUniversalTime().AddDays(3), status= AuthorizationStatus.Expired},
  376. idTag="456"
  377. }
  378. }
  379. };
  380. WritetoDB(uuid, request);
  381. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  382. }
  383. catch (Exception ex)
  384. {
  385. MessageBox.Show(ex.ToString());
  386. }
  387. }
  388. #endregion
  389. private void SetRemoteTrigger(string action)
  390. {
  391. try
  392. {
  393. string type = action.Split('_').Last();
  394. var uuid = Guid.NewGuid().ToString();
  395. var request = new TriggerMessageRequest()
  396. {
  397. requestedMessage = (MessageTrigger)Enum.Parse(typeof(MessageTrigger), type, false)
  398. };
  399. WritetoDB(uuid, request);
  400. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  401. }
  402. catch (Exception ex)
  403. {
  404. MessageBox.Show(ex.ToString());
  405. }
  406. }
  407. private void SetGetConfiguration()
  408. {
  409. try
  410. {
  411. string uuid = Guid.NewGuid().ToString();
  412. var request = new GetConfigurationRequest()
  413. {
  414. key = uxConfigKeyTb.Text.Split('/').ToList()
  415. };
  416. WritetoDB(uuid, request);
  417. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  418. }
  419. catch (Exception ex)
  420. {
  421. MessageBox.Show(ex.ToString());
  422. }
  423. }
  424. private void UxCmdCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  425. {
  426. var item = uxCmdCb.SelectedValue as ComboBoxItem;
  427. action = item.Content as string;
  428. }
  429. private void SetRemoteStartTransaction(bool hasProfile = false)
  430. {
  431. try
  432. {
  433. var uuid = Guid.NewGuid().ToString();
  434. var request = new RemoteStartTransactionRequest()
  435. {
  436. connectorId = byte.Parse(uxConnectorIdTb.Text),
  437. idTag = uxIdTagTb.Text
  438. };
  439. if (hasProfile)
  440. {
  441. request.chargingProfile = new csChargingProfiles()
  442. {
  443. chargingProfileId = Convert.ToInt32(DateTime.Now.ToUniversalTime().ToString("yyMMddHHmm")),
  444. chargingProfileKind = ChargingProfileKindType.Absolute,
  445. chargingProfilePurpose = ChargingProfilePurposeType.TxProfile,
  446. recurrencyKind = RecurrencyKindType.Daily,
  447. stackLevel = 1,
  448. chargingSchedule = new ChargingSchedule()
  449. {
  450. chargingRateUnit = ChargingRateUnitType.A,
  451. duration = 300,
  452. minChargingRate = 0,
  453. startSchedule = DateTime.Now.ToUniversalTime().Date,
  454. chargingSchedulePeriod = new List<ChargingSchedulePeriod>()
  455. {
  456. new ChargingSchedulePeriod()
  457. { limit=10, startPeriod=0, numberPhases=3 },
  458. new ChargingSchedulePeriod()
  459. { limit=2, startPeriod=60, numberPhases=3 },
  460. new ChargingSchedulePeriod()
  461. { limit=8, startPeriod=120, numberPhases=3 }
  462. }
  463. }
  464. };
  465. }
  466. WritetoDB(uuid, request);
  467. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  468. }
  469. catch (Exception ex)
  470. {
  471. MessageBox.Show(ex.ToString());
  472. }
  473. }
  474. private void SetUnlockConnector()
  475. {
  476. try
  477. {
  478. var uuid = Guid.NewGuid().ToString();
  479. var request = new UnlockConnectorRequest()
  480. {
  481. connectorId = byte.Parse(uxConnectorIdTb.Text),
  482. };
  483. WritetoDB(uuid, request);
  484. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  485. }
  486. catch (Exception ex)
  487. {
  488. MessageBox.Show(ex.ToString());
  489. }
  490. }
  491. private void SetChangeAvailability_Inoperative()
  492. {
  493. try
  494. {
  495. var uuid = Guid.NewGuid().ToString();
  496. var request = new ChangeAvailabilityRequest()
  497. {
  498. connectorId = byte.Parse(uxConnectorIdTb.Text),
  499. type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Inoperative
  500. };
  501. WritetoDB(uuid, request);
  502. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  503. }
  504. catch (Exception ex)
  505. {
  506. MessageBox.Show(ex.ToString());
  507. }
  508. }
  509. private void SetChangeAvailability_Operative()
  510. {
  511. try
  512. {
  513. var uuid = Guid.NewGuid().ToString();
  514. var request = new ChangeAvailabilityRequest()
  515. {
  516. connectorId = byte.Parse(uxConnectorIdTb.Text),
  517. type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Operative
  518. };
  519. WritetoDB(uuid, request);
  520. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  521. }
  522. catch (Exception ex)
  523. {
  524. MessageBox.Show(ex.ToString());
  525. }
  526. }
  527. private void SetChangeConfiguration()
  528. {
  529. try
  530. {
  531. var uuid = Guid.NewGuid().ToString();
  532. var request = new ChangeConfigurationRequest()
  533. {
  534. key = uxConfigKeyTb.Text,
  535. value = uxConfigValueTb.Text,
  536. };
  537. WritetoDB(uuid, request);
  538. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  539. }
  540. catch (Exception ex)
  541. {
  542. MessageBox.Show(ex.ToString());
  543. }
  544. }
  545. private void SetClearCache()
  546. {
  547. try
  548. {
  549. var uuid = Guid.NewGuid().ToString();
  550. var request = new ClearCacheRequest()
  551. {
  552. };
  553. WritetoDB(uuid, request);
  554. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  555. }
  556. catch (Exception ex)
  557. {
  558. MessageBox.Show(ex.ToString());
  559. }
  560. }
  561. private void SetGetConfiguration_ALL()
  562. {
  563. try
  564. {
  565. var uuid = Guid.NewGuid().ToString();
  566. var request = new GetConfigurationRequest()
  567. {
  568. key = new List<string>() { }
  569. };
  570. WritetoDB(uuid, request);
  571. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  572. }
  573. catch (Exception ex)
  574. {
  575. MessageBox.Show(ex.ToString());
  576. }
  577. }
  578. private void SetHardReset()
  579. {
  580. try
  581. {
  582. var uuid = Guid.NewGuid().ToString();
  583. var request = new ResetRequest()
  584. {
  585. type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Hard
  586. };
  587. WritetoDB(uuid, request);
  588. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  589. }
  590. catch (Exception ex)
  591. {
  592. MessageBox.Show(ex.ToString());
  593. }
  594. }
  595. private void SetSoftReset()
  596. {
  597. try
  598. {
  599. var uuid = Guid.NewGuid().ToString();
  600. var request = new ResetRequest()
  601. {
  602. type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Soft
  603. };
  604. WritetoDB(uuid, request);
  605. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  606. }
  607. catch (Exception ex)
  608. {
  609. MessageBox.Show(ex.ToString());
  610. }
  611. }
  612. private void SetRemoteStopTransaction()
  613. {
  614. try
  615. {
  616. var uuid = Guid.NewGuid().ToString();
  617. var request = new RemoteStopTransactionRequest()
  618. {
  619. transactionId = int.Parse(uxTransactionIdTb.Text)
  620. };
  621. WritetoDB(uuid, request);
  622. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  623. }
  624. catch (Exception ex)
  625. {
  626. MessageBox.Show(ex.ToString());
  627. }
  628. }
  629. private void WritetoDB(string uuid, IRequest request)
  630. {
  631. using (var db = new MainDBContext())
  632. {
  633. db.MachineOperateRecord.Add(new MachineOperateRecord()
  634. {
  635. CreatedOn = DateTime.Now.ToUniversalTime(),
  636. ChargeBoxId = uxChargeBoxIdTb.Text,
  637. SerialNo = uuid,
  638. RequestContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  639. EVSE_Status = 0,
  640. Status = 0,
  641. RequestType = 1,
  642. Action = request.Action.ToString()
  643. });
  644. db.ServerMessage.Add(new ServerMessage()
  645. {
  646. ChargeBoxId = uxChargeBoxIdTb.Text,
  647. CreatedBy = "TestTool",
  648. CreatedOn = DateTime.Now.ToUniversalTime(),
  649. OutAction = request.Action.ToString(),
  650. OutRequest = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  651. SerialNo = uuid,
  652. InMessage = string.Empty
  653. });
  654. db.SaveChanges();
  655. }
  656. }
  657. private void uxFileSubmitBtn_Click(object sender, RoutedEventArgs e)
  658. {
  659. OpenFileDialog openFileDialog = new OpenFileDialog();
  660. openFileDialog.Title = "Select file";
  661. var result = openFileDialog.ShowDialog();
  662. if (result == true)
  663. {
  664. uxUploadFileTb.Text = openFileDialog.FileName;
  665. }
  666. }
  667. private void uxUploadBtn_Click(object sender, RoutedEventArgs e)
  668. {
  669. if (string.IsNullOrEmpty(uxUploadFileTb.Text))
  670. {
  671. uxMsgTb.Text = "Please select upload file!";
  672. return;
  673. }
  674. FileInfo f = new FileInfo(uxUploadFileTb.Text);
  675. int size = (int)f.Length;
  676. string md5 = "";
  677. string filePreUrl = ConfigurationManager.AppSettings["FilePreUrl"];
  678. using (WebClient client = new WebClient())
  679. {
  680. client.UseDefaultCredentials = false;
  681. client.Headers.Add("Content-Type", "application/octet-stream");
  682. using (Stream fileStream = File.OpenRead(uxUploadFileTb.Text))
  683. {
  684. var _md5 = MD5.Create();
  685. var hash = _md5.ComputeHash(fileStream);
  686. md5 = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();
  687. }
  688. }
  689. UploadFile ufObj = new UploadFile();
  690. ufObj.CreatedOn = DateTime.Now.ToUniversalTime();
  691. ufObj.FileExtensionName = System.IO.Path.GetExtension(uxUploadFileTb.Text);
  692. ufObj.Id = Guid.NewGuid();
  693. ufObj.FileName = md5 + ufObj.FileExtensionName;
  694. ufObj.FilePath = "~/UploadFiles/Fw/" + ufObj.FileName;
  695. ufObj.FileSize = size;
  696. ufObj.FileMD5 = md5;
  697. ufObj.CustomerId = new Guid("8456AED9-6DD9-4BF3-A94C-9F5DCB9506F7");
  698. ufObj.OriginName = System.IO.Path.GetFileName(uxUploadFileTb.Text);
  699. ufObj.FileUrl = new Uri(Properties.Settings.Default.FilePreUrl + ufObj.FilePath.Replace("~/", "")).ToString();
  700. ufObj.IsOnline = true;
  701. using (var db = new MainDBContext())
  702. {
  703. db.UploadFile.Add(ufObj);
  704. db.SaveChanges();
  705. var pvQry = db.PublishVersion.Where(x => x.CustomerMachineComponentId == 1).OrderByDescending(c => c.Version).FirstOrDefault();
  706. PublishVersion pv = new PublishVersion { CustomerMachineComponentId = 1, CreatedOn = DateTime.Now.ToUniversalTime(), Version = 1 };
  707. if (pvQry != null)
  708. {
  709. pv.Version = pvQry.Version + 1;
  710. }
  711. pv.PublishVersionFiles.Add(new PublishVersionFile() { UploadFileId = ufObj.Id });
  712. db.PublishVersion.Add(pv);
  713. db.SaveChanges();
  714. }
  715. FTPClient UploadClient = new FTPClient(@"ftp://test.evsocket.phihong.com.cn", "testocpp", "testocpp");
  716. bool uploadResult = UploadClient.FtpUploadBroken(uxUploadFileTb.Text, @"ftp://test.evsocket.phihong.com.cn/" + ufObj.FileName);
  717. uxMsgTb.Text = "Upload File Result :" + (uploadResult ? "Success" : "Fail");
  718. }
  719. private void uxFTPUploadBtn_Click(object sender, RoutedEventArgs e)
  720. {
  721. if (string.IsNullOrEmpty(uxUploadFileTb.Text))
  722. {
  723. uxMsgTb.Text = "Please select upload file!";
  724. return;
  725. }
  726. FileInfo f = new FileInfo(uxUploadFileTb.Text);
  727. int size = (int)f.Length;
  728. string md5 = "";
  729. using (WebClient client = new WebClient())
  730. {
  731. client.UseDefaultCredentials = false;
  732. client.Headers.Add("Content-Type", "application/octet-stream");
  733. using (Stream fileStream = File.OpenRead(uxUploadFileTb.Text))
  734. {
  735. var _md5 = MD5.Create();
  736. var hash = _md5.ComputeHash(fileStream);
  737. md5 = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();
  738. }
  739. }
  740. UploadFile ufObj = new UploadFile();
  741. ufObj.CreatedOn = DateTime.Now.ToUniversalTime();
  742. ufObj.FileExtensionName = System.IO.Path.GetExtension(uxUploadFileTb.Text);
  743. ufObj.Id = Guid.NewGuid();
  744. ufObj.FileName = md5 + ufObj.FileExtensionName;
  745. ufObj.FilePath = "~/UploadFiles/Fw/" + ufObj.FileName;
  746. ufObj.FileSize = size;
  747. ufObj.FileMD5 = md5;
  748. ufObj.CustomerId = new Guid("8456AED9-6DD9-4BF3-A94C-9F5DCB9506F7");
  749. ufObj.OriginName = System.IO.Path.GetFileName(uxUploadFileTb.Text);
  750. ufObj.FileUrl = new Uri(Properties.Settings.Default.FTPFilePreUrl + ufObj.FileName).ToString();
  751. ufObj.IsOnline = true;
  752. using (var db = new MainDBContext())
  753. {
  754. db.UploadFile.Add(ufObj);
  755. db.SaveChanges();
  756. var pvQry = db.PublishVersion.Where(x => x.CustomerMachineComponentId == 1).OrderByDescending(c => c.Version).FirstOrDefault();
  757. PublishVersion pv = new PublishVersion { CustomerMachineComponentId = 1, CreatedOn = DateTime.Now.ToUniversalTime(), Version = 1 };
  758. if (pvQry != null)
  759. {
  760. pv.Version = pvQry.Version + 1;
  761. }
  762. pv.PublishVersionFiles.Add(new PublishVersionFile() { UploadFileId = ufObj.Id });
  763. db.PublishVersion.Add(pv);
  764. db.SaveChanges();
  765. }
  766. FTPClient UploadClient = new FTPClient(@"ftp://test.evsocket.phihong.com.cn", "testocpp", "testocpp");
  767. bool uploadResult = UploadClient.FtpUploadBroken(uxUploadFileTb.Text, @"ftp://test.evsocket.phihong.com.cn/" + ufObj.FileName);
  768. uxMsgTb.Text = "Upload File Result :" + (uploadResult ? "Success" : "Fail");
  769. }
  770. private void uxRefreshBtn_Click(object sender, RoutedEventArgs e)
  771. {
  772. using (var db = new MainDBContext())
  773. {
  774. publishes = db.PublishVersion
  775. .Include(c => c.PublishVersionFiles)
  776. .Include(c => c.PublishVersionFiles.Select(z => z.UploadFile))
  777. .Include(c => c.CustomerMachineComponent)
  778. .Include(c => c.CustomerMachineComponent.MachineComponent)
  779. .Include(c => c.CustomerMachineComponent.MachineComponent.MachineModel)
  780. .Include(c => c.CustomerMachineComponent.MachineComponent.MachinePart)
  781. .Where(c => c.CustomerMachineComponent.CustomerId == new Guid("8456AED9-6DD9-4BF3-A94C-9F5DCB9506F7") && c.CustomerMachineComponent.Id == 1 && c.CustomerMachineComponent.Id == 1).OrderByDescending(c => c.Id).Take(20).ToList();
  782. StringBuilder sb = new StringBuilder();
  783. uxPublishCb.Items.Clear();
  784. foreach (var i in publishes)
  785. {
  786. string prefix = i.PublishVersionFiles.FirstOrDefault().UploadFile.FileUrl.StartsWith("ftp") ? "FTP" : "HTTP";
  787. sb.Clear();
  788. sb.Append(string.Format("[{0}]", prefix));
  789. sb.Append("_版本號:");
  790. sb.Append(i.Version);
  791. sb.Append("_上傳日期:");
  792. sb.Append(i.PublishVersionFiles.First().UploadFile.CreatedOn.ToString());
  793. sb.Append("_檔名:");
  794. foreach (var f in i.PublishVersionFiles)
  795. {
  796. sb.Append(f.UploadFile.OriginName);
  797. sb.Append("、");
  798. }
  799. uxPublishCb.Items.Add(sb.ToString());
  800. uxPublishCb.SelectedIndex = 0;
  801. }
  802. }
  803. }
  804. private void uxPublishBtn_Click(object sender, RoutedEventArgs e)
  805. {
  806. if (selectedPublish < 0)
  807. {
  808. uxMsgTb.Text = "Please choose publish version!";
  809. return;
  810. }
  811. using (var db = new MainDBContext())
  812. {
  813. var machine = db.Machine.Where(x => x.ChargeBoxId == uxChargeBoxIdTb.Text).FirstOrDefault();
  814. MachineVersion obj = new MachineVersion()
  815. {
  816. MachineId = machine.Id,
  817. PublishVersionId = publishes[selectedPublish].Id,
  818. CreatedOn = DateTime.Now.ToUniversalTime()
  819. };
  820. obj.MachineVersionFiles.Add(new MachineVersionFile()
  821. {
  822. UploadFileId = publishes[selectedPublish].PublishVersionFiles.First().UploadFileId,
  823. CreatedOn = DateTime.Now.ToUniversalTime()
  824. });
  825. db.MachineVersion.Add(obj);
  826. //將machine的軟體更新更新到這個值
  827. machine.FW_AssignedVersion = publishes[selectedPublish].Version;
  828. machine.FW_MachineVersion = obj;
  829. db.SaveChanges();
  830. }
  831. uxMsgTb.Text = "Publish Complete";
  832. }
  833. private void uxPublishCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  834. {
  835. selectedPublish = uxPublishCb.SelectedIndex;
  836. }
  837. private void uxClearPublishBtn_Click(object sender, RoutedEventArgs e)
  838. {
  839. using (var db = new MainDBContext())
  840. {
  841. var machine = db.Machine.Where(x => x.ChargeBoxId == uxChargeBoxIdTb.Text).FirstOrDefault();
  842. machine.FW_AssignedVersion = null;
  843. machine.FW_AssignedMachineVersionId = null;
  844. db.SaveChanges();
  845. }
  846. uxMsgTb.Text = "Clear Publish";
  847. }
  848. }
  849. }