RFCalibrationViewModel.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. using Aitex.Core.RT.Log;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Mime;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using System.Xml;
  12. using System.Xml.Serialization;
  13. using Aitex.Common.Util;
  14. using Aitex.Core.Common.DeviceData;
  15. using Aitex.Core.RT.Event;
  16. using Aitex.Core.UI.MVVM;
  17. using Aitex.Core.Util;
  18. using DocumentFormat.OpenXml;
  19. using DocumentFormat.OpenXml.Packaging;
  20. using DocumentFormat.OpenXml.Spreadsheet;
  21. using MECF.Framework.Common.CommonData;
  22. using MECF.Framework.Common.DataCenter;
  23. using MECF.Framework.Common.Equipment;
  24. using MECF.Framework.Common.OperationCenter;
  25. using OpenSEMI.ClientBase;
  26. using SciChart.Charting.Visuals.RenderableSeries;
  27. using SciChart.Core.Extensions;
  28. using Color = System.Drawing.Color;
  29. using VirgoUI.Client.Models.Sys;
  30. namespace VirgoUI.Client.Models.PMs
  31. {
  32. public class RFCalibrationViewModel : ModuleUiViewModelBase, ISupportMultipleSystem
  33. {
  34. public bool SystemInManual { get; set; }
  35. public bool IsNextEnable { get; set; }
  36. public bool IsSaveEnable { get; set; } = true;
  37. private AITRfData _currentSelectionRF;
  38. public AITRfData CurrentSelectionRF
  39. {
  40. get { return _currentSelectionRF; }
  41. set
  42. {
  43. _currentSelectionRF = value;
  44. NotifyOfPropertyChange("CurrentSelectionRF");
  45. }
  46. }
  47. #region RF1
  48. private AITRfData _rf1;
  49. [Subscription("Rf.DeviceData")]
  50. public AITRfData Rf1
  51. {
  52. get
  53. {
  54. return _rf1;
  55. }
  56. set
  57. {
  58. _rf1 = value;
  59. }
  60. }
  61. private AITRfData _rfBias1;
  62. [Subscription("BiasRf.DeviceData")]
  63. public AITRfData RfBias1
  64. {
  65. get
  66. {
  67. return _rfBias1;
  68. }
  69. set
  70. {
  71. _rfBias1 = value;
  72. }
  73. }
  74. #endregion
  75. public ObservableCollection<IRenderableSeries> RenderableSeries { get; set; }
  76. public ICalibrationTableViewModelParameter CustomParameter { get; set; }
  77. public class NotifiableCalibrationTableItem : NotifiableItem
  78. {
  79. public string DisplayName { get; set; }
  80. public bool IsSelected { get; set; }
  81. [XmlIgnore]
  82. public bool IsSelectedEnable { get; set; } = true;
  83. public float _setPoint;
  84. public float SetPoint
  85. {
  86. get
  87. {
  88. return _setPoint;
  89. }
  90. set
  91. {
  92. _setPoint = value;
  93. InvokePropertyChanged("SetPoint");
  94. }
  95. }
  96. [XmlIgnore]
  97. public bool SetPointEnable { get; set; } = true;
  98. public float _forwardPowerUI;
  99. public float ForwardPowerUI
  100. {
  101. get
  102. {
  103. return _forwardPowerUI;
  104. }
  105. set
  106. {
  107. _forwardPowerUI = value;
  108. InvokePropertyChanged("ForwardPowerUI");
  109. }
  110. }
  111. private float _forwardPowerMeter;
  112. public float ForwardPowerMeter
  113. {
  114. get
  115. {
  116. return _forwardPowerMeter;
  117. }
  118. set
  119. {
  120. _forwardPowerMeter = value;
  121. InvokePropertyChanged("ForwardPowerMeter");
  122. InvokePropertyChanged("Difference");
  123. InvokePropertyChanged("DifferenceBackground");
  124. }
  125. }
  126. public float ReflectedPower { get; set; }
  127. public float Difference
  128. {
  129. get
  130. {
  131. if (SetPoint == 0)
  132. return 0;
  133. return (ForwardPowerMeter - SetPoint) * 100 / SetPoint;
  134. }
  135. }
  136. public string DifferenceBackground
  137. {
  138. get
  139. {
  140. return Math.Abs(Difference) <= 3 ? "LightGreen" : "Red";
  141. }
  142. }
  143. }
  144. public ObservableCollection<CalibrationTableItem> CalibrationItems { get; set; }
  145. private List<int> selectedIndexs = new List<int>();
  146. public ObservableCollection<NotifiableCalibrationTableItem> TableData { get; set; }
  147. private int _tableDataSelectedIndex;
  148. public int TableDataSelectedIndex
  149. {
  150. get { return _tableDataSelectedIndex; }
  151. set
  152. {
  153. _tableDataSelectedIndex = value;
  154. NotifyOfPropertyChange("TableDataSelectedIndex");
  155. }
  156. }
  157. private CalibrationTableItem _currentSelection;
  158. public CalibrationTableItem CurrentSelection
  159. {
  160. get { return _currentSelection; }
  161. set
  162. {
  163. _currentSelection = value;
  164. ChangeSelection(_currentSelection);
  165. NotifyOfPropertyChange(nameof(CurrentSelection));
  166. }
  167. }
  168. private string _GeneratorSerialNumber;
  169. public string GeneratorSerialNumber
  170. {
  171. get { return _GeneratorSerialNumber; }
  172. set
  173. {
  174. _GeneratorSerialNumber = value;
  175. NotifyOfPropertyChange(nameof(GeneratorSerialNumber));
  176. }
  177. }
  178. private string _SensorSerialNumber;
  179. public string SensorSerialNumber
  180. {
  181. get { return _SensorSerialNumber; }
  182. set
  183. {
  184. _SensorSerialNumber = value;
  185. NotifyOfPropertyChange(nameof(SensorSerialNumber));
  186. }
  187. }
  188. private double _RFPhysicalMaxPower;
  189. public double RFPhysicalMaxPower
  190. {
  191. get { return _RFPhysicalMaxPower; }
  192. set
  193. {
  194. _RFPhysicalMaxPower = value;
  195. NotifyOfPropertyChange(nameof(RFPhysicalMaxPower));
  196. }
  197. }
  198. private double _CurrentRFMaxPower;
  199. public double CurrentRFMaxPower
  200. {
  201. get { return _CurrentRFMaxPower; }
  202. set
  203. {
  204. _CurrentRFMaxPower = value;
  205. NotifyOfPropertyChange(nameof(CurrentRFMaxPower));
  206. }
  207. }
  208. private double _RFCalibratedMaxPower;
  209. public double RFCalibratedMaxPower
  210. {
  211. get { return _RFCalibratedMaxPower; }
  212. set
  213. {
  214. _RFCalibratedMaxPower = value;
  215. NotifyOfPropertyChange(nameof(RFCalibratedMaxPower));
  216. }
  217. }
  218. private bool _IsSelectedAllEnable;
  219. public bool IsSelectedAllEnable
  220. {
  221. get { return _IsSelectedAllEnable; }
  222. set
  223. {
  224. _IsSelectedAllEnable = value;
  225. NotifyOfPropertyChange(nameof(IsSelectedAllEnable));
  226. }
  227. }
  228. public RFCalibrationViewModel()
  229. {
  230. DisplayName = "Calibration Table ";
  231. CalibrationItems = new ObservableCollection<CalibrationTableItem>();
  232. TableData = new ObservableCollection<NotifiableCalibrationTableItem>();
  233. RenderableSeries = new ObservableCollection<IRenderableSeries>();
  234. IsSelectedAllEnable = true;
  235. }
  236. protected override void OnInitialize()
  237. {
  238. base.OnInitialize();
  239. if (CustomParameter != null && CustomParameter.Items != null && CalibrationItems.IsEmpty())
  240. {
  241. foreach (var item in CustomParameter.Items)
  242. {
  243. if (GetRFEnablebyDisplayName(item.DisplayName))
  244. CalibrationItems.Add(new CalibrationTableItem()
  245. {
  246. DisplayName = item.DisplayName,
  247. ItemEnableScName = item.ItemEnableScName,
  248. ItemTableScName = item.ItemTableScName,
  249. });
  250. }
  251. }
  252. }
  253. protected override void OnActivate()
  254. {
  255. base.OnActivate();
  256. }
  257. private AITRfData GetRFbyDisplayName(string displayName)
  258. {
  259. switch (displayName)
  260. {
  261. case "Source RF":
  262. return Rf1;
  263. case "Bias RF":
  264. return RfBias1;
  265. }
  266. return null;
  267. }
  268. private bool GetRFEnablebyDisplayName(string displayName)
  269. {
  270. switch (displayName)
  271. {
  272. case "Bias RF":
  273. return (bool)QueryDataClient.Instance.Service.GetConfig($"{SystemName}.BiasRf.EnableBiasRF");
  274. }
  275. return true;
  276. }
  277. protected void ChangeSelection(CalibrationTableItem item)
  278. {
  279. if (item == null)
  280. {
  281. TableData.Clear();
  282. return;
  283. }
  284. var rf = GetRFbyDisplayName(item.DisplayName);
  285. if (rf == null) return;
  286. GeneratorSerialNumber = QueryDataClient.Instance.Service.GetConfig($"{rf.Module}.{rf.DeviceName}.GeneratorSerialNumber").ToString();
  287. SensorSerialNumber = QueryDataClient.Instance.Service.GetConfig($"{rf.Module}.{rf.DeviceName}.SensorSerialNumber").ToString();
  288. RFPhysicalMaxPower = (double)QueryDataClient.Instance.Service.GetConfig($"{rf.Module}.{rf.DeviceName}.RFPhysicalMaxPower");
  289. CurrentRFMaxPower = (double)QueryDataClient.Instance.Service.GetConfig($"{rf.Module}.{rf.DeviceName}.CurrentRFMaxPower");
  290. RFCalibratedMaxPower = (double)QueryDataClient.Instance.Service.GetConfig($"{rf.Module}.{rf.DeviceName}.RFCalibratedMaxPower");
  291. var tableValues = QueryDataClient.Instance.Service.GetConfig(item.ItemTableDetailScName);
  292. if (tableValues == null)
  293. return;
  294. var scValue = (string)tableValues;
  295. if (string.IsNullOrEmpty(scValue))
  296. {
  297. InitTableData(item);
  298. return;
  299. }
  300. try
  301. {
  302. var tableData = CustomXmlSerializer.Deserialize<List<NotifiableCalibrationTableItem>>(scValue);
  303. //tableData = tableData.OrderBy(x => x.SetPoint).ToList();
  304. TableData = new ObservableCollection<NotifiableCalibrationTableItem>(tableData);
  305. NotifyOfPropertyChange(nameof(TableData));
  306. }
  307. catch (Exception ex)
  308. {
  309. InitTableData(item);
  310. }
  311. }
  312. private void InitTableData(CalibrationTableItem item)
  313. {
  314. var tableData = new List<NotifiableCalibrationTableItem>();
  315. for (int i = 0; i < 20; i++)
  316. {
  317. tableData.Add(new NotifiableCalibrationTableItem() { DisplayName = item.DisplayName, SetPoint = (i + 1) * 100, ForwardPowerUI = (i + 1) * 100, ForwardPowerMeter = (i + 1) * 100 });
  318. }
  319. TableData = new ObservableCollection<NotifiableCalibrationTableItem>(tableData);
  320. NotifyOfPropertyChange(nameof(TableData));
  321. }
  322. public void Save()
  323. {
  324. if (CurrentSelection == null)
  325. return;
  326. string detailData = CustomXmlSerializer.Serialize(TableData);
  327. string data = "";
  328. foreach (var item in TableData)
  329. {
  330. data += $"{item.SetPoint}#{item.ForwardPowerMeter};";
  331. }
  332. var rf = GetRFbyDisplayName(CurrentSelection.DisplayName);
  333. if (rf == null)
  334. return;
  335. InvokeClient.Instance.Service.DoOperation("System.SetConfig", $"{rf.Module}.{rf.DeviceName}.GeneratorSerialNumber", GeneratorSerialNumber);
  336. InvokeClient.Instance.Service.DoOperation("System.SetConfig", $"{rf.Module}.{rf.DeviceName}.SensorSerialNumber", SensorSerialNumber);
  337. InvokeClient.Instance.Service.DoOperation("System.SetConfig", $"{rf.Module}.{rf.DeviceName}.RFPhysicalMaxPower", RFPhysicalMaxPower);
  338. InvokeClient.Instance.Service.DoOperation("System.SetConfig", $"{rf.Module}.{rf.DeviceName}.CurrentRFMaxPower", CurrentRFMaxPower);
  339. InvokeClient.Instance.Service.DoOperation("System.SetConfig", $"{rf.Module}.{rf.DeviceName}.RFCalibratedMaxPower", RFCalibratedMaxPower);
  340. InvokeClient.Instance.Service.DoOperation("System.SetConfig", CurrentSelection.ItemTableScName, data);
  341. InvokeClient.Instance.Service.DoOperation("System.SetConfig", CurrentSelection.ItemTableDetailScName, detailData);
  342. Reload();
  343. MessageBox.Show("Save Successful !");
  344. }
  345. public void Reload()
  346. {
  347. ChangeSelection(CurrentSelection);
  348. }
  349. public void Start()
  350. {
  351. if (CurrentSelection == null)
  352. return;
  353. ////CurrentCalibrationTableItems = new List<NotifiableCalibrationTableItem>();
  354. //foreach (var item in TableData)
  355. //{
  356. // if (item.IsSelected)
  357. // {
  358. // item.ForwardPowerUI = 0;
  359. // item.ForwardPowerMeter = 0;
  360. // item.ReflectedPower = 0;
  361. // //CurrentCalibrationTableItems.Add(item);
  362. // }
  363. //}
  364. selectedIndexs = new List<int>();
  365. for (int i = 0; i < TableData.Count; i++)
  366. {
  367. if (TableData[i].IsSelected)
  368. {
  369. TableData[i].ForwardPowerUI = 0;
  370. TableData[i].ForwardPowerMeter = 0;
  371. TableData[i].ReflectedPower = 0;
  372. selectedIndexs.Add(i);
  373. }
  374. IsSelectedAllEnable = false;
  375. TableData[i].SetPointEnable = false;
  376. TableData[i].IsSelectedEnable = false;
  377. TableData[i].InvokePropertyChanged("SetPointEnable");
  378. TableData[i].InvokePropertyChanged("IsSelectedEnable");
  379. }
  380. IsNextEnable = true;
  381. IsSaveEnable = false;
  382. Next();
  383. }
  384. public void Next()
  385. {
  386. if (selectedIndexs.Count > 0)
  387. {
  388. TableDataSelectedIndex = selectedIndexs[0];
  389. RFCalibration(TableData[TableDataSelectedIndex]);
  390. selectedIndexs.RemoveAt(0);
  391. }
  392. else
  393. {
  394. CalcResult();
  395. }
  396. }
  397. public void End()
  398. {
  399. var rf = GetRFbyDisplayName(CurrentSelection.DisplayName);
  400. if (rf == null)
  401. return;
  402. InvokeClient.Instance.Service.DoOperation($"{rf.Module}.{rf.DeviceName}.{AITRfOperation.SetPower}", 0);
  403. InvokeClient.Instance.Service.DoOperation($"{rf.Module}.{rf.DeviceName}.{AITRfOperation.SetPowerOnOff}", "false");
  404. selectedIndexs = new List<int>();
  405. TableDataSelectedIndex = 0;
  406. CalcResult();
  407. }
  408. public void SelectedAll()
  409. {
  410. if (TableData != null && TableData.Count > 0) { }
  411. TableData.ForEachDo(x =>
  412. {
  413. x.IsSelected = true;
  414. x.InvokePropertyChanged("IsSelected");
  415. });
  416. }
  417. public void UnselectedAll()
  418. {
  419. if (TableData != null && TableData.Count > 0)
  420. TableData.ForEachDo(x =>
  421. {
  422. x.IsSelected = false;
  423. x.InvokePropertyChanged("IsSelected");
  424. });
  425. }
  426. public void Export()
  427. {
  428. bool? result;
  429. string savePath;
  430. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  431. dlg.DefaultExt = ".xlsx"; // Default file extension
  432. dlg.FileName = $"RFCalibration{DateTime.Now.ToString("yyyyMMddhhmmss")}";
  433. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  434. result = dlg.ShowDialog(); // Show open file dialog box
  435. savePath = dlg.FileName;
  436. if (result != true) // Process open file dialog box results
  437. return;
  438. if (File.Exists(dlg.FileName))
  439. {
  440. File.Delete(dlg.FileName);
  441. }
  442. if (result == true)
  443. {
  444. using (MemoryStream mem = new MemoryStream())
  445. {
  446. using (var temp = File.OpenRead(PathManager.GetCfgDir() + "RFCalibration导出模板.xlsx"))
  447. {
  448. temp.CopyTo(mem);
  449. }
  450. using (SpreadsheetDocument doc = SpreadsheetDocument.Open(mem, true))
  451. {
  452. WorkbookPart wbPart = doc.WorkbookPart;
  453. Worksheet worksheet = wbPart.WorksheetParts.First().Worksheet;
  454. //statement to get the sheetdata which contains the rows and cell in table
  455. SheetData sheetData = worksheet.GetFirstChild<SheetData>();
  456. SharedStringTablePart shareStringPart;
  457. if (wbPart.GetPartsOfType<SharedStringTablePart>().Any())
  458. shareStringPart = wbPart.GetPartsOfType<SharedStringTablePart>().First();
  459. else
  460. shareStringPart = wbPart.AddNewPart<SharedStringTablePart>();
  461. uint currentRowIndex = 2;
  462. var currentRow = sheetData.GetRow(currentRowIndex);
  463. uint[] lineStyles = currentRow.Elements<Cell>().Select(c => c.StyleIndex.Value).ToArray();
  464. currentRow.RemoveAllChildren();
  465. currentRow.AppendChild(new Cell().SetValue(GeneratorSerialNumber, shareStringPart,
  466. styleIndex: lineStyles[0]));
  467. currentRow.AppendChild(new Cell().SetValue(SensorSerialNumber, shareStringPart,
  468. styleIndex: lineStyles[1]));
  469. currentRow.AppendChild(new Cell().SetValue(RFPhysicalMaxPower, shareStringPart,
  470. styleIndex: lineStyles[2]));
  471. currentRow.AppendChild(new Cell().SetValue(CurrentRFMaxPower, shareStringPart,
  472. styleIndex: lineStyles[3]));
  473. currentRow.AppendChild(new Cell().SetValue(RFCalibratedMaxPower, shareStringPart,
  474. styleIndex: lineStyles[4]));
  475. currentRowIndex = 4;
  476. for (int i = 0; i < TableData.Count; i++)
  477. {
  478. Row row = new Row() { Height = 30 };
  479. row.RowIndex = currentRowIndex; //设置行号
  480. row.AppendChild(new Cell().SetValue(TableData[i].SetPoint, shareStringPart,
  481. styleIndex: lineStyles[0]));
  482. row.AppendChild(new Cell().SetValue(TableData[i].IsSelected.ToString(), shareStringPart,
  483. styleIndex: lineStyles[1]));
  484. row.AppendChild(new Cell().SetValue(TableData[i].ForwardPowerUI, shareStringPart,
  485. styleIndex: lineStyles[2]));
  486. row.AppendChild(new Cell().SetValue(TableData[i].ForwardPowerMeter, shareStringPart,
  487. styleIndex: lineStyles[3]));
  488. row.AppendChild(new Cell().SetValue(TableData[i].ReflectedPower, shareStringPart,
  489. styleIndex: lineStyles[4]));
  490. row.AppendChild(new Cell().SetValue(TableData[i].Difference, shareStringPart,
  491. styleIndex: lineStyles[5]));
  492. sheetData.AppendChild(row);
  493. currentRowIndex++;
  494. }
  495. wbPart.Workbook.Save();
  496. doc.SaveAs(savePath);
  497. doc.Close();
  498. }
  499. mem.Close();
  500. }
  501. }
  502. }
  503. private void CalcResult()
  504. {
  505. IsNextEnable = false;
  506. IsSaveEnable = true;
  507. RenderableSeries = new ObservableCollection<IRenderableSeries>();
  508. var ForwardPowerMeterLine = new ChartDataLine(CurrentSelection.DisplayName + " ForwardPowerMeter/SetPoint", CurrentSelection.DisplayName + " ForwardPowerMeter/SetPoint", System.Windows.Media.Color.FromRgb(Color.Red.R, Color.Red.G, Color.Red.B));
  509. var SetPointLine = new ChartDataLine(CurrentSelection.DisplayName + " SetPoint/SetPoint", CurrentSelection.DisplayName + " SetPoint/SetPoint", System.Windows.Media.Color.FromRgb(Color.Blue.R, Color.Blue.G, Color.Blue.B));
  510. for (int i = 0; i < TableData.Count; i++)
  511. {
  512. if (TableData[i].IsSelected)
  513. {
  514. ForwardPowerMeterLine.Append(TableData[i].SetPoint, TableData[i].ForwardPowerMeter);
  515. SetPointLine.Append(TableData[i].SetPoint, TableData[i].SetPoint);
  516. }
  517. IsSelectedAllEnable = true;
  518. TableData[i].SetPointEnable = true;
  519. TableData[i].IsSelectedEnable = true;
  520. TableData[i].InvokePropertyChanged("SetPointEnable");
  521. TableData[i].InvokePropertyChanged("IsSelectedEnable");
  522. }
  523. RenderableSeries.Add(ForwardPowerMeterLine);
  524. RenderableSeries.Add(SetPointLine);
  525. if (TableData.Count(x => x.IsSelected) == 0)
  526. return;
  527. RFCalibratedMaxPower = CurrentRFMaxPower * 100 / (100 - TableData.Where(x => x.IsSelected).Average(x => x.Difference));
  528. }
  529. public void RFCalibration(NotifiableCalibrationTableItem tableItem)
  530. {
  531. var rf = GetRFbyDisplayName(tableItem.DisplayName);
  532. if (rf == null)
  533. return;
  534. IsNextEnable = false;
  535. InvokeClient.Instance.Service.DoOperation($"{rf.Module}.{rf.DeviceName}.{AITRfOperation.SetPower}", tableItem.SetPoint);
  536. if (!rf.IsRfOn)
  537. InvokeClient.Instance.Service.DoOperation($"{rf.Module}.{rf.DeviceName}.{AITRfOperation.SetPowerOnOff}", "true");
  538. Task.Delay(10000).ContinueWith(x =>
  539. {
  540. UpdateForwardPower(tableItem);
  541. IsNextEnable = true;
  542. });
  543. }
  544. private void UpdateForwardPower(NotifiableCalibrationTableItem tableItem)
  545. {
  546. var tableDataItem = TableData.FirstOrDefault(x => x.SetPoint == tableItem.SetPoint);
  547. if (tableDataItem != null)
  548. tableDataItem.ForwardPowerUI = GetRFbyDisplayName(tableItem.DisplayName).ForwardPower;
  549. }
  550. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  551. {
  552. SystemInManual = (bool)QueryDataClient.Instance.Service.GetData("System.IsIdle");
  553. if (CurrentSelection != null)
  554. CurrentSelectionRF = GetRFbyDisplayName(CurrentSelection.DisplayName);
  555. if (CurrentSelectionRF == null)
  556. CurrentSelectionRF = new AITRfData();
  557. }
  558. }
  559. public static class OpenXmlExcelExtentions
  560. {
  561. public static Sheet GetSheet(this WorkbookPart workbookPart, string sheetName)
  562. {
  563. return workbookPart.Workbook
  564. .GetFirstChild<Sheets>()
  565. .Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
  566. }
  567. /// <summary>
  568. /// Given a worksheet and a row index, return the row.
  569. /// </summary>
  570. /// <param name="sheetData"></param>
  571. /// <param name="rowIndex"></param>
  572. /// <returns></returns>
  573. public static Row GetRow(this SheetData sheetData, uint rowIndex)
  574. {
  575. return sheetData.
  576. Elements<Row>().Where(r => r.RowIndex == rowIndex).FirstOrDefault();
  577. }
  578. public static Cell GetCell(this SheetData sheetData, string columnName, uint rowIndex)
  579. {
  580. Row row = GetRow(sheetData, rowIndex);
  581. if (row == null)
  582. return null;
  583. return row.Elements<Cell>().Where(c => string.Compare
  584. (c.CellReference.Value, columnName +
  585. rowIndex, true) == 0).FirstOrDefault();
  586. }
  587. // https://msdn.microsoft.com/en-us/library/office/cc861607.aspx
  588. // Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet.
  589. // If the cell already exists, returns it.
  590. public static Cell GetOrCreateCell(this SheetData sheetData, string columnName, uint rowIndex)
  591. {
  592. string cellReference = columnName + rowIndex;
  593. // If the worksheet does not contain a row with the specified row index, insert one.
  594. Row row;
  595. if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
  596. {
  597. row = sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
  598. }
  599. else
  600. {
  601. row = new Row() { RowIndex = rowIndex };
  602. sheetData.Append(row);
  603. }
  604. return row.GetOrCreateCell(cellReference);
  605. }
  606. public static Cell GetOrCreateCell(this Row row, string cellReference)
  607. {
  608. // If there is not a cell with the specified column name, insert one.
  609. if (row.Elements<Cell>().Where(c => c?.CellReference?.Value == cellReference).Count() > 0)
  610. {
  611. return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
  612. }
  613. else
  614. {
  615. // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
  616. Cell refCell = null;
  617. foreach (Cell cell in row.Elements<Cell>())
  618. {
  619. if (cell.CellReference.Value.Length == cellReference.Length)
  620. {
  621. if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
  622. {
  623. refCell = cell;
  624. break;
  625. }
  626. }
  627. }
  628. Cell newCell = new Cell() { CellReference = cellReference };
  629. row.InsertBefore(newCell, refCell);
  630. return newCell;
  631. }
  632. }
  633. public static string GetValue(this Cell cell, SharedStringTablePart shareStringPart)
  634. {
  635. if (cell == null)
  636. return null;
  637. string cellvalue = cell.InnerText;
  638. if (cell.DataType != null)
  639. {
  640. if (cell.DataType == CellValues.SharedString)
  641. {
  642. int id = -1;
  643. if (Int32.TryParse(cellvalue, out id))
  644. {
  645. SharedStringItem item = GetItem(shareStringPart, id);
  646. if (item.Text != null)
  647. {
  648. //code to take the string value
  649. cellvalue = item.Text.Text;
  650. }
  651. else if (item.InnerText != null)
  652. {
  653. cellvalue = item.InnerText;
  654. }
  655. else if (item.InnerXml != null)
  656. {
  657. cellvalue = item.InnerXml;
  658. }
  659. }
  660. }
  661. }
  662. return cellvalue;
  663. }
  664. public static string GetValue(this Cell cell, string[] shareStringPartValues)
  665. {
  666. if (cell == null)
  667. return null;
  668. string cellvalue = cell.InnerText;
  669. if (cell.DataType != null)
  670. {
  671. if (cell.DataType == CellValues.SharedString)
  672. {
  673. int id = -1;
  674. if (Int32.TryParse(cellvalue, out id))
  675. {
  676. cellvalue = shareStringPartValues[id];
  677. }
  678. }
  679. }
  680. return cellvalue;
  681. }
  682. public static Cell SetValue(this Cell cell, object value = null, SharedStringTablePart shareStringPart = null, int shareStringItemIndex = -1, uint styleIndex = 0)
  683. {
  684. if (value == null)
  685. {
  686. cell.CellValue = new CellValue();
  687. if (shareStringItemIndex != -1)
  688. {
  689. cell.CellValue = new CellValue(shareStringItemIndex.ToString());
  690. cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
  691. }
  692. }
  693. else if (value is string str)
  694. {
  695. if (shareStringPart == null)
  696. {
  697. cell.CellValue = new CellValue(str);
  698. cell.DataType = new EnumValue<CellValues>(CellValues.String);
  699. }
  700. else
  701. {
  702. // Insert the text into the SharedStringTablePart.
  703. int index = shareStringPart.GetOrInsertItem(str, false);
  704. // Set the value of cell
  705. cell.CellValue = new CellValue(index.ToString());
  706. cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
  707. }
  708. }
  709. else if (value is int || value is short || value is long ||
  710. value is float || value is double || value is uint ||
  711. value is ulong || value is ushort || value is decimal)
  712. {
  713. cell.CellValue = new CellValue(value.ToString());
  714. cell.DataType = new EnumValue<CellValues>(CellValues.Number);
  715. }
  716. else if (value is DateTime date)
  717. {
  718. cell.CellValue = new CellValue(date.ToString("yyyy-MM-dd")); // ISO 861
  719. cell.DataType = new EnumValue<CellValues>(CellValues.Date);
  720. }
  721. else if (value is XmlDocument xd)
  722. {
  723. if (shareStringPart == null)
  724. {
  725. throw new Exception("Param [shareStringPart] can't be null when value type is XmlDocument.");
  726. }
  727. else
  728. {
  729. int index = shareStringPart.GetOrInsertItem(xd.OuterXml, true);
  730. // Set the value of cell
  731. cell.CellValue = new CellValue(index.ToString());
  732. cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
  733. }
  734. }
  735. if (styleIndex != 0)
  736. cell.StyleIndex = styleIndex;
  737. return cell;
  738. }
  739. // https://msdn.microsoft.com/en-us/library/office/gg278314.aspx
  740. // Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text
  741. // and inserts it into the SharedStringTablePart. If the item already exists, returns its index.
  742. public static int GetOrInsertItem(this SharedStringTablePart shareStringPart, string content, bool isXml)
  743. {
  744. // If the part does not contain a SharedStringTable, create one.
  745. if (shareStringPart.SharedStringTable == null)
  746. {
  747. shareStringPart.SharedStringTable = new SharedStringTable();
  748. }
  749. int i = 0;
  750. // Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
  751. foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
  752. {
  753. if ((!isXml && item.InnerText == content) || (isXml && item.OuterXml == content))
  754. {
  755. return i;
  756. }
  757. i++;
  758. }
  759. // The text does not exist in the part. Create the SharedStringItem and return its index.
  760. if (isXml)
  761. shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(content));
  762. else
  763. shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new Text(content)));
  764. shareStringPart.SharedStringTable.Save();
  765. return i;
  766. }
  767. private static SharedStringItem GetItem(this SharedStringTablePart shareStringPart, int id)
  768. {
  769. return shareStringPart.SharedStringTable.Elements<SharedStringItem>().ElementAt(id);
  770. }
  771. /// <summary>
  772. /// https://docs.microsoft.com/en-us/office/open-xml/how-to-merge-two-adjacent-cells-in-a-spreadsheet
  773. /// </summary>
  774. /// <param name="worksheet"></param>
  775. /// <returns></returns>
  776. public static MergeCells GetOrCreateMergeCells(this Worksheet worksheet)
  777. {
  778. MergeCells mergeCells;
  779. if (worksheet.Elements<MergeCells>().Count() > 0)
  780. {
  781. mergeCells = worksheet.Elements<MergeCells>().First();
  782. }
  783. else
  784. {
  785. mergeCells = new MergeCells();
  786. // Insert a MergeCells object into the specified position.
  787. if (worksheet.Elements<CustomSheetView>().Count() > 0)
  788. {
  789. worksheet.InsertAfter(mergeCells, worksheet.Elements<CustomSheetView>().First());
  790. }
  791. else if (worksheet.Elements<DataConsolidate>().Count() > 0)
  792. {
  793. worksheet.InsertAfter(mergeCells, worksheet.Elements<DataConsolidate>().First());
  794. }
  795. else if (worksheet.Elements<SortState>().Count() > 0)
  796. {
  797. worksheet.InsertAfter(mergeCells, worksheet.Elements<SortState>().First());
  798. }
  799. else if (worksheet.Elements<AutoFilter>().Count() > 0)
  800. {
  801. worksheet.InsertAfter(mergeCells, worksheet.Elements<AutoFilter>().First());
  802. }
  803. else if (worksheet.Elements<Scenarios>().Count() > 0)
  804. {
  805. worksheet.InsertAfter(mergeCells, worksheet.Elements<Scenarios>().First());
  806. }
  807. else if (worksheet.Elements<ProtectedRanges>().Count() > 0)
  808. {
  809. worksheet.InsertAfter(mergeCells, worksheet.Elements<ProtectedRanges>().First());
  810. }
  811. else if (worksheet.Elements<SheetProtection>().Count() > 0)
  812. {
  813. worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetProtection>().First());
  814. }
  815. else if (worksheet.Elements<SheetCalculationProperties>().Count() > 0)
  816. {
  817. worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetCalculationProperties>().First());
  818. }
  819. else
  820. {
  821. worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetData>().First());
  822. }
  823. worksheet.Save();
  824. }
  825. return mergeCells;
  826. }
  827. /// <summary>
  828. /// Given the names of two adjacent cells, merges the two cells.
  829. /// Create the merged cell and append it to the MergeCells collection.
  830. /// When two cells are merged, only the content from one cell is preserved:
  831. /// the upper-left cell for left-to-right languages or the upper-right cell for right-to-left languages.
  832. /// </summary>
  833. /// <param name="mergeCells"></param>
  834. /// <param name="cell1Name"></param>
  835. /// <param name="cell2Name"></param>
  836. public static void MergeTwoCells(this MergeCells mergeCells, string cell1Name, string cell2Name)
  837. {
  838. MergeCell mergeCell = new MergeCell() { Reference = new StringValue(cell1Name + ":" + cell2Name) };
  839. mergeCells.Append(mergeCell);
  840. }
  841. public static IEnumerable<string> GetItemValues(this SharedStringTablePart shareStringPart)
  842. {
  843. foreach (var item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
  844. {
  845. if (item.Text != null)
  846. {
  847. //code to take the string value
  848. yield return item.Text.Text;
  849. }
  850. else if (item.InnerText != null)
  851. {
  852. yield return item.InnerText;
  853. }
  854. else if (item.InnerXml != null)
  855. {
  856. yield return item.InnerXml;
  857. }
  858. else
  859. {
  860. yield return null;
  861. }
  862. };
  863. }
  864. public static XmlDocument GetCellAssociatedSharedStringItemXmlDocument(this SheetData sheetData, string columnName, uint rowIndex, SharedStringTablePart shareStringPart)
  865. {
  866. Cell cell = GetCell(sheetData, columnName, rowIndex);
  867. if (cell == null)
  868. return null;
  869. if (cell.DataType == CellValues.SharedString)
  870. {
  871. int id = -1;
  872. if (Int32.TryParse(cell.InnerText, out id))
  873. {
  874. SharedStringItem ssi = shareStringPart.GetItem(id);
  875. var doc = new XmlDocument();
  876. doc.LoadXml(ssi.OuterXml);
  877. return doc;
  878. }
  879. }
  880. return null;
  881. }
  882. }
  883. }