MainWindow.xaml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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.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.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. try
  280. {
  281. var uuid = Guid.NewGuid().ToString();
  282. var request = new ReserveNowRequest()
  283. {
  284. connectorId = Convert.ToInt32(uxConnectorIdTb.Text),
  285. expiryDate = DateTime.Now.AddMinutes(1),
  286. idTag = uxIdTagTb.Text,
  287. parentIdTag = "PTAG",
  288. reservationId = Convert.ToInt32(DateTime.Now.ToString("yyMMddHHmm")),
  289. };
  290. WritetoDB(uuid, request);
  291. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  292. }
  293. catch (Exception ex)
  294. {
  295. MessageBox.Show(ex.ToString());
  296. }
  297. }
  298. private void GetDiagnostics()
  299. {
  300. try
  301. {
  302. var uuid = Guid.NewGuid().ToString();
  303. var request = new GetDiagnosticsRequest()
  304. {
  305. location = new Uri("ftp://phihong:y42j%2f4cj84@test.evsocket.phihong.com.cn/"),
  306. retries = 1,
  307. retryInterval = 30,
  308. startTime = DateTime.Now.AddHours(-1),
  309. stopTime = DateTime.Now
  310. };
  311. WritetoDB(uuid, request);
  312. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  313. }
  314. catch (Exception ex)
  315. {
  316. MessageBox.Show(ex.ToString());
  317. }
  318. }
  319. #region Local Authorization
  320. private void GetLocalListVersion()
  321. {
  322. try
  323. {
  324. var uuid = Guid.NewGuid().ToString();
  325. var request = new GetLocalListVersionRequest()
  326. {
  327. };
  328. WritetoDB(uuid, request);
  329. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  330. }
  331. catch (Exception ex)
  332. {
  333. MessageBox.Show(ex.ToString());
  334. }
  335. }
  336. private void SendLocalAuthorizationList(bool isFull)
  337. {
  338. try
  339. {
  340. var uuid = Guid.NewGuid().ToString();
  341. var request = new SendLocalListRequest()
  342. {
  343. listVersion = Convert.ToInt32(DateTime.Now.ToString("yyMMddHHmm")),
  344. updateType = isFull ? UpdateType.Full : UpdateType.Differential,
  345. localAuthorizationList = new List<AuthorizationData>()
  346. {
  347. new AuthorizationData()
  348. {
  349. idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.AddDays(3), status= AuthorizationStatus.Accepted},
  350. idTag="123"
  351. }, new AuthorizationData()
  352. {
  353. idTagInfo=new IdTagInfo(){ expiryDate=DateTime.Now.AddDays(3), status= AuthorizationStatus.Expired},
  354. idTag="456"
  355. }
  356. }
  357. };
  358. WritetoDB(uuid, request);
  359. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  360. }
  361. catch (Exception ex)
  362. {
  363. MessageBox.Show(ex.ToString());
  364. }
  365. }
  366. #endregion
  367. private void SetRemoteTrigger(string action)
  368. {
  369. try
  370. {
  371. string type = action.Split('_').Last();
  372. var uuid = Guid.NewGuid().ToString();
  373. var request = new TriggerMessageRequest()
  374. {
  375. requestedMessage = (MessageTrigger)Enum.Parse(typeof(MessageTrigger), type, false)
  376. };
  377. WritetoDB(uuid, request);
  378. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  379. }
  380. catch (Exception ex)
  381. {
  382. MessageBox.Show(ex.ToString());
  383. }
  384. }
  385. private void SetGetConfiguration()
  386. {
  387. try
  388. {
  389. var uuid = Guid.NewGuid().ToString();
  390. var request = new GetConfigurationRequest()
  391. {
  392. key = new List<string>() { uxConfigKeyTb.Text }
  393. };
  394. WritetoDB(uuid, request);
  395. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  396. }
  397. catch (Exception ex)
  398. {
  399. MessageBox.Show(ex.ToString());
  400. }
  401. }
  402. private void UxCmdCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  403. {
  404. var item = uxCmdCb.SelectedValue as ComboBoxItem;
  405. action = item.Content as string;
  406. }
  407. private void SetRemoteStartTransaction(bool hasProfile = false)
  408. {
  409. try
  410. {
  411. var uuid = Guid.NewGuid().ToString();
  412. var request = new RemoteStartTransactionRequest()
  413. {
  414. connectorId = byte.Parse(uxConnectorIdTb.Text),
  415. idTag = uxIdTagTb.Text
  416. };
  417. if (hasProfile)
  418. {
  419. request.chargingProfile = new csChargingProfiles()
  420. {
  421. chargingProfileId = Convert.ToInt32(DateTime.Now.ToString("yyMMddHHmm")),
  422. chargingProfileKind = ChargingProfileKindType.Absolute,
  423. chargingProfilePurpose = ChargingProfilePurposeType.TxProfile,
  424. recurrencyKind = RecurrencyKindType.Daily,
  425. stackLevel = 1,
  426. chargingSchedule = new ChargingSchedule()
  427. {
  428. chargingRateUnit = ChargingRateUnitType.A,
  429. duration = 300,
  430. minChargingRate = 0,
  431. startSchedule = DateTime.Now.Date,
  432. chargingSchedulePeriod = new List<ChargingSchedulePeriod>()
  433. {
  434. new ChargingSchedulePeriod()
  435. { limit=10, startPeriod=0, numberPhases=3 },
  436. new ChargingSchedulePeriod()
  437. { limit=2, startPeriod=60, numberPhases=3 },
  438. new ChargingSchedulePeriod()
  439. { limit=8, startPeriod=120, numberPhases=3 }
  440. }
  441. }
  442. };
  443. }
  444. WritetoDB(uuid, request);
  445. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  446. }
  447. catch (Exception ex)
  448. {
  449. MessageBox.Show(ex.ToString());
  450. }
  451. }
  452. private void SetUnlockConnector()
  453. {
  454. try
  455. {
  456. var uuid = Guid.NewGuid().ToString();
  457. var request = new UnlockConnectorRequest()
  458. {
  459. connectorId = byte.Parse(uxConnectorIdTb.Text),
  460. };
  461. WritetoDB(uuid, request);
  462. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  463. }
  464. catch (Exception ex)
  465. {
  466. MessageBox.Show(ex.ToString());
  467. }
  468. }
  469. private void SetChangeAvailability_Inoperative()
  470. {
  471. try
  472. {
  473. var uuid = Guid.NewGuid().ToString();
  474. var request = new ChangeAvailabilityRequest()
  475. {
  476. connectorId = byte.Parse(uxConnectorIdTb.Text),
  477. type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Inoperative
  478. };
  479. WritetoDB(uuid, request);
  480. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  481. }
  482. catch (Exception ex)
  483. {
  484. MessageBox.Show(ex.ToString());
  485. }
  486. }
  487. private void SetChangeAvailability_Operative()
  488. {
  489. try
  490. {
  491. var uuid = Guid.NewGuid().ToString();
  492. var request = new ChangeAvailabilityRequest()
  493. {
  494. connectorId = byte.Parse(uxConnectorIdTb.Text),
  495. type = EVCB_OCPP.Packet.Messages.SubTypes.AvailabilityType.Operative
  496. };
  497. WritetoDB(uuid, request);
  498. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  499. }
  500. catch (Exception ex)
  501. {
  502. MessageBox.Show(ex.ToString());
  503. }
  504. }
  505. private void SetChangeConfiguration()
  506. {
  507. try
  508. {
  509. var uuid = Guid.NewGuid().ToString();
  510. var request = new ChangeConfigurationRequest()
  511. {
  512. key = uxConfigKeyTb.Text,
  513. value = uxConfigValueTb.Text,
  514. };
  515. WritetoDB(uuid, request);
  516. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  517. }
  518. catch (Exception ex)
  519. {
  520. MessageBox.Show(ex.ToString());
  521. }
  522. }
  523. private void SetClearCache()
  524. {
  525. try
  526. {
  527. var uuid = Guid.NewGuid().ToString();
  528. var request = new ClearCacheRequest()
  529. {
  530. };
  531. WritetoDB(uuid, request);
  532. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  533. }
  534. catch (Exception ex)
  535. {
  536. MessageBox.Show(ex.ToString());
  537. }
  538. }
  539. private void SetGetConfiguration_ALL()
  540. {
  541. try
  542. {
  543. var uuid = Guid.NewGuid().ToString();
  544. var request = new GetConfigurationRequest()
  545. {
  546. key = new List<string>() { }
  547. };
  548. WritetoDB(uuid, request);
  549. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  550. }
  551. catch (Exception ex)
  552. {
  553. MessageBox.Show(ex.ToString());
  554. }
  555. }
  556. private void SetHardReset()
  557. {
  558. try
  559. {
  560. var uuid = Guid.NewGuid().ToString();
  561. var request = new ResetRequest()
  562. {
  563. type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Hard
  564. };
  565. WritetoDB(uuid, request);
  566. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  567. }
  568. catch (Exception ex)
  569. {
  570. MessageBox.Show(ex.ToString());
  571. }
  572. }
  573. private void SetSoftReset()
  574. {
  575. try
  576. {
  577. var uuid = Guid.NewGuid().ToString();
  578. var request = new ResetRequest()
  579. {
  580. type = EVCB_OCPP.Packet.Messages.SubTypes.ResetType.Soft
  581. };
  582. WritetoDB(uuid, request);
  583. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  584. }
  585. catch (Exception ex)
  586. {
  587. MessageBox.Show(ex.ToString());
  588. }
  589. }
  590. private void SetRemoteStopTransaction()
  591. {
  592. try
  593. {
  594. var uuid = Guid.NewGuid().ToString();
  595. var request = new RemoteStopTransactionRequest()
  596. {
  597. transactionId = int.Parse(uxTransactionIdTb.Text)
  598. };
  599. WritetoDB(uuid, request);
  600. uxMsgTb.Text = string.Format("UUID:{0}", uuid);
  601. }
  602. catch (Exception ex)
  603. {
  604. MessageBox.Show(ex.ToString());
  605. }
  606. }
  607. private void WritetoDB(string uuid, IRequest request)
  608. {
  609. using (var db = new MainDBContext())
  610. {
  611. db.MachineOperateRecord.Add(new MachineOperateRecord()
  612. {
  613. CreatedOn = DateTime.Now,
  614. ChargeBoxId = uxChargeBoxIdTb.Text,
  615. SerialNo = uuid,
  616. RequestContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  617. EVSE_Status = 0,
  618. Status = 0,
  619. RequestType = 1,
  620. });
  621. db.ServerMessage.Add(new ServerMessage()
  622. {
  623. ChargeBoxId = uxChargeBoxIdTb.Text,
  624. CreatedBy = "TestTool",
  625. CreatedOn = DateTime.Now,
  626. OutAction = request.Action.ToString(),
  627. OutRequest = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None }),
  628. SerialNo = uuid,
  629. InMessage = string.Empty
  630. });
  631. db.SaveChanges();
  632. }
  633. }
  634. private void uxFileSubmitBtn_Click(object sender, RoutedEventArgs e)
  635. {
  636. OpenFileDialog openFileDialog = new OpenFileDialog();
  637. openFileDialog.Title = "Select file";
  638. var result = openFileDialog.ShowDialog();
  639. if (result == true)
  640. {
  641. uxUploadFileTb.Text = openFileDialog.FileName;
  642. }
  643. }
  644. private void uxUploadBtn_Click(object sender, RoutedEventArgs e)
  645. {
  646. if (string.IsNullOrEmpty(uxUploadFileTb.Text))
  647. {
  648. uxMsgTb.Text = "Please select upload file!";
  649. return;
  650. }
  651. FileInfo f = new FileInfo(uxUploadFileTb.Text);
  652. int size = (int)f.Length;
  653. string md5 = "";
  654. string filePreUrl = ConfigurationManager.AppSettings["FilePreUrl"];
  655. using (WebClient client = new WebClient())
  656. {
  657. client.UseDefaultCredentials = false;
  658. client.Headers.Add("Content-Type", "application/octet-stream");
  659. using (Stream fileStream = File.OpenRead(uxUploadFileTb.Text))
  660. {
  661. var _md5 = MD5.Create();
  662. var hash = _md5.ComputeHash(fileStream);
  663. md5 = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();
  664. //using (Stream requestStream = client.OpenWrite(new Uri(@filePreUrl + "UploadFiles/Fw/" + ufObj.FileName), "POST"))
  665. //{
  666. // fileStream.CopyTo(requestStream);
  667. //}
  668. }
  669. }
  670. UploadFile ufObj = new UploadFile();
  671. ufObj.CreatedOn = DateTime.Now;
  672. ufObj.FileExtensionName = System.IO.Path.GetExtension(uxUploadFileTb.Text);
  673. ufObj.Id = Guid.NewGuid();
  674. ufObj.FileName = md5 + ufObj.FileExtensionName;
  675. ufObj.FilePath = "~/UploadFiles/Fw/" + ufObj.FileName;
  676. ufObj.FileSize = size;
  677. ufObj.FileMD5 = md5;
  678. ufObj.CustomerId = new Guid("8456AED9-6DD9-4BF3-A94C-9F5DCB9506F7");
  679. ufObj.OriginName = System.IO.Path.GetFileName(uxUploadFileTb.Text);
  680. ufObj.FileUrl = new Uri(Properties.Settings.Default.FilePreUrl + ufObj.FilePath.Replace("~/","")).ToString();
  681. ufObj.IsOnline = true;
  682. using (var db = new MainDBContext())
  683. {
  684. db.UploadFile.Add(ufObj);
  685. db.SaveChanges();
  686. var pvQry = db.PublishVersion.Where(x => x.CustomerMachineComponentId == 1).OrderByDescending(c => c.Version).FirstOrDefault();
  687. PublishVersion pv = new PublishVersion { CustomerMachineComponentId = 1, CreatedOn = DateTime.Now, Version = 1 };
  688. if (pvQry != null)
  689. {
  690. pv.Version = pvQry.Version + 1;
  691. }
  692. pv.PublishVersionFiles.Add(new PublishVersionFile() { UploadFileId = ufObj.Id });
  693. db.PublishVersion.Add(pv);
  694. db.SaveChanges();
  695. }
  696. uxMsgTb.Text = "write to DB ,but you must manaul upload file to server and filename replace with " + ufObj.FileName;
  697. }
  698. private void uxRefreshBtn_Click(object sender, RoutedEventArgs e)
  699. {
  700. using (var db = new MainDBContext())
  701. {
  702. publishes = db.PublishVersion
  703. .Include(c => c.PublishVersionFiles)
  704. .Include(c => c.PublishVersionFiles.Select(z => z.UploadFile))
  705. .Include(c => c.CustomerMachineComponent)
  706. .Include(c => c.CustomerMachineComponent.MachineComponent)
  707. .Include(c => c.CustomerMachineComponent.MachineComponent.MachineModel)
  708. .Include(c => c.CustomerMachineComponent.MachineComponent.MachinePart)
  709. .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();
  710. StringBuilder sb = new StringBuilder();
  711. uxPublishCb.Items.Clear();
  712. foreach (var i in publishes)
  713. {
  714. sb.Clear();
  715. sb.Append("_版本號:");
  716. sb.Append(i.Version);
  717. sb.Append("_上傳日期:");
  718. sb.Append(i.PublishVersionFiles.First().UploadFile.CreatedOn.ToString());
  719. sb.Append("_檔名:");
  720. foreach (var f in i.PublishVersionFiles)
  721. {
  722. sb.Append(f.UploadFile.OriginName);
  723. sb.Append("、");
  724. }
  725. uxPublishCb.Items.Add(sb.ToString());
  726. uxPublishCb.SelectedIndex = 0;
  727. }
  728. }
  729. }
  730. private void uxPublishBtn_Click(object sender, RoutedEventArgs e)
  731. {
  732. if (selectedPublish < 0)
  733. {
  734. uxMsgTb.Text = "Please choose publish version!";
  735. return;
  736. }
  737. using (var db = new MainDBContext())
  738. {
  739. var machine = db.Machine.Where(x => x.ChargeBoxId == uxChargeBoxIdTb.Text).FirstOrDefault();
  740. MachineVersion obj = new MachineVersion() { MachineId = machine.Id, PublishVersionId = publishes[selectedPublish].Id, CreatedOn = DateTime.Now };
  741. obj.MachineVersionFiles.Add(new MachineVersionFile() { UploadFileId = publishes[selectedPublish].PublishVersionFiles.First().UploadFileId, CreatedOn = DateTime.Now });
  742. db.MachineVersion.Add(obj);
  743. //將machine的軟體更新更新到這個值
  744. machine.FW_AssignedVersion = publishes[selectedPublish].Version;
  745. machine.FW_MachineVersion = obj;
  746. db.SaveChanges();
  747. }
  748. uxMsgTb.Text = "Publish Complete";
  749. }
  750. private void uxPublishCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  751. {
  752. selectedPublish = uxPublishCb.SelectedIndex;
  753. }
  754. }
  755. }