CellPowerSupplier.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.RT.Routine;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.CommonData.PowerSupplier;
  8. using MECF.Framework.Common.Device.PowerSupplier;
  9. using MECF.Framework.Common.Equipment;
  10. using CyberX8_Core;
  11. using CyberX8_RT.Modules.Transporter;
  12. using System;
  13. using System.Collections.Concurrent;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Threading.Channels;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. namespace CyberX8_RT.Devices.PowerSupplier
  22. {
  23. public class CellPowerSupplier : BaseDevice, IDevice
  24. {
  25. private enum PowerSupplierOperation
  26. {
  27. None=0,
  28. StepOperation=1,
  29. SetCurrent=2,
  30. SetStartOperation=3
  31. }
  32. #region 常量
  33. private const string POWERSUPPLIER_DATA = "PowerSupplierData";
  34. private const string SET_POINT = "SetPoint";
  35. private const string CURRENT = "Current";
  36. private const string VOLTAGE = "Voltage";
  37. private const string ENABLED = "Enabled";
  38. private const string CONNECTED = "Connected";
  39. private const string POWER_STATUS = "PowerStatus";
  40. private const string POWER_CONTROL = "PowerControl";
  41. private const string POWER_RUN_MODEL = "PowerRunModel";
  42. private const int CURRENT_UNIT_SCALE = 10000;
  43. private const int CURRENT_DEVICE_UNIT_SCALE = 1000;
  44. private const int VOLTAGE_DEVICE_SCALE = 10000;
  45. private const int STEP_VOLTAGE_SCALE = 1000;
  46. #endregion
  47. #region 内部变量
  48. /// <summary>
  49. /// 数据
  50. /// </summary>
  51. private PowerSupplierData _powerSupplierData;
  52. /// <summary>
  53. /// 通道
  54. /// </summary>
  55. private byte _channel=0;
  56. /// <summary>
  57. /// 设备参数
  58. /// </summary>
  59. private PowerSupplierDevice _deviceParameter;
  60. /// <summary>
  61. /// 定时器
  62. /// </summary>
  63. private PeriodicJob _periodicJob;
  64. /// <summary>
  65. /// 指定读取指令集合
  66. /// </summary>
  67. private ConcurrentQueue<string> _readCommandList = new ConcurrentQueue<string>();
  68. /// <summary>
  69. /// 步阶Routine
  70. /// </summary>
  71. private PowerSupplierStepRoutine _powerSupplierStepRoutine;
  72. /// <summary>
  73. /// 设置电流Routine
  74. /// </summary>
  75. private PowerSupplierSetCurrentRoutine _powerSupplierSetCurrentRoutine;
  76. /// <summary>
  77. /// 设置步阶Routine
  78. /// </summary>
  79. private PowerSupplierStartStepRoutine _powerSupplierStartStepRoutine;
  80. /// <summary>
  81. /// 状态
  82. /// </summary>
  83. private RState _status;
  84. /// <summary>
  85. /// 当前操作
  86. /// </summary>
  87. private PowerSupplierOperation _currentOperation;
  88. /// <summary>
  89. /// <summary>
  90. /// 读取电流时间
  91. /// </summary>
  92. private DateTime _sendDateTime = DateTime.Now;
  93. /// <summary>
  94. /// 连接上升沿
  95. /// </summary>
  96. private R_TRIG _connectedTrig = new R_TRIG();
  97. /// <summary>
  98. /// 发送Remote上升沿
  99. /// </summary>
  100. private R_TRIG _sendRemoteTrig = new R_TRIG();
  101. /// <summary>
  102. /// 连接成功时间
  103. /// </summary>
  104. private DateTime _connectTime = DateTime.Now;
  105. #endregion
  106. #region 属性
  107. /// <summary>
  108. /// 数据
  109. /// </summary>
  110. public PowerSupplierData PowerSupplierData { get { return _powerSupplierData; } }
  111. /// <summary>
  112. /// 状态
  113. /// </summary>
  114. public RState Status { get { return _status; } }
  115. /// <summary>
  116. /// 连接状态
  117. /// </summary>
  118. public bool IsConnected { get { return PowerSupplierDeviceConfigManager.Instance.GetModuleConnected(Module); } }
  119. #endregion
  120. /// <summary>
  121. /// 构造函数
  122. /// </summary>
  123. /// <param name="moduleName"></param>
  124. public CellPowerSupplier(string moduleName) : base(moduleName, moduleName, moduleName, moduleName)
  125. {
  126. _powerSupplierData = new PowerSupplierData();
  127. _periodicJob = new PeriodicJob(100, OnTimer, $"{Module}.OnTimer", true);
  128. }
  129. /// <summary>
  130. /// 初始化
  131. /// </summary>
  132. /// <returns></returns>
  133. public bool Initialize()
  134. {
  135. InitializeOperation();
  136. InitializeData();
  137. InitializeRoutine();
  138. return true;
  139. }
  140. /// <summary>
  141. /// 初始化操作
  142. /// </summary>
  143. private void InitializeOperation()
  144. {
  145. OP.Subscribe($"{Module}.Enable", EnableOperation);
  146. OP.Subscribe($"{Module}.Disable", DisableOperation);
  147. OP.Subscribe($"{Module}.SetPoint", SetPointOperation);
  148. OP.Subscribe($"{Module}.StartStepPeriod", StartStepPeriod);
  149. OP.Subscribe($"{Module}.SetLocalControl", SetLocalOperation);
  150. OP.Subscribe($"{Module}.SetRemoteControl", SetRemoteOperation);
  151. OP.Subscribe($"{Module}.Abort", AbortOperation);
  152. }
  153. /// <summary>
  154. /// 初始化数据
  155. /// </summary>
  156. private void InitializeData()
  157. {
  158. _deviceParameter = PowerSupplierDeviceConfigManager.Instance.GetPowerSupplierDeviceByName(Module);
  159. if(_deviceParameter!=null)
  160. {
  161. if(!byte.TryParse(_deviceParameter.Address,out _channel))
  162. {
  163. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, $"{Module} channel is invalid");
  164. }
  165. }
  166. else
  167. {
  168. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, $"{Module} is not in config xml");
  169. }
  170. DATA.Subscribe($"{Module}.{POWERSUPPLIER_DATA}", () => _powerSupplierData, Aitex.Core.Util.SubscriptionAttribute.FLAG.IgnoreSaveDB);
  171. DATA.Subscribe($"{Module}.{CONNECTED}", () => PowerSupplierDeviceConfigManager.Instance.GetModuleConnected(Module), SubscriptionAttribute.FLAG.IgnoreSaveDB);
  172. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, ENABLED, UpdateVariableValue);
  173. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, SET_POINT, UpdateVariableValue);
  174. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, VOLTAGE, UpdateVariableValue);
  175. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, CURRENT, UpdateVariableValue);
  176. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, POWER_STATUS, UpdateVariableValue);
  177. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module,POWER_CONTROL, UpdateVariableValue);
  178. PowerSupplierDeviceConfigManager.Instance.SubscribeModuleVariable(Module, POWER_RUN_MODEL, UpdateVariableValue);
  179. }
  180. /// <summary>
  181. /// 初始化Routine
  182. /// </summary>
  183. private void InitializeRoutine()
  184. {
  185. _powerSupplierStepRoutine = new PowerSupplierStepRoutine(Module);
  186. _powerSupplierSetCurrentRoutine = new PowerSupplierSetCurrentRoutine(Module);
  187. _powerSupplierStartStepRoutine= new PowerSupplierStartStepRoutine(Module);
  188. }
  189. /// <summary>
  190. /// 更新变量数值
  191. /// </summary>
  192. /// <param name="variable"></param>
  193. /// <param name="value"></param>
  194. private void UpdateVariableValue(string variable, object value)
  195. {
  196. PropertyInfo property = PowerSupplierData.GetType().GetProperty(variable);
  197. if (property != null)
  198. {
  199. if (!PowerSupplierData.IsDataInitialized)
  200. {
  201. PowerSupplierData.IsDataInitialized = true;
  202. SetPowerCCStatus();
  203. }
  204. if(variable==SET_POINT)
  205. {
  206. if (double.TryParse(value.ToString(), out double doubleValue))
  207. {
  208. property.SetValue(PowerSupplierData, doubleValue / CURRENT_DEVICE_UNIT_SCALE);
  209. }
  210. else
  211. {
  212. property.SetValue(PowerSupplierData, value);
  213. }
  214. }
  215. else if(variable==CURRENT)
  216. {
  217. if (double.TryParse(value.ToString(), out double doubleValue))
  218. {
  219. property.SetValue(PowerSupplierData, doubleValue / CURRENT_UNIT_SCALE);
  220. }
  221. else
  222. {
  223. property.SetValue(PowerSupplierData, value);
  224. }
  225. }
  226. else if (variable == VOLTAGE)
  227. {
  228. if (double.TryParse(value.ToString(), out double doubleValue))
  229. {
  230. property.SetValue(PowerSupplierData, doubleValue / VOLTAGE_DEVICE_SCALE);
  231. }
  232. else
  233. {
  234. property.SetValue(PowerSupplierData, value);
  235. }
  236. }
  237. else
  238. {
  239. property.SetValue(PowerSupplierData, value);
  240. }
  241. }
  242. }
  243. /// <summary>
  244. /// 定时器
  245. /// </summary>
  246. /// <returns></returns>
  247. private bool OnTimer()
  248. {
  249. bool connected = PowerSupplierDeviceConfigManager.Instance.GetModuleConnected(Module);
  250. _connectedTrig.CLK = connected;
  251. if (_connectedTrig.Q)
  252. {
  253. _connectTime = DateTime.Now;
  254. return true;
  255. }
  256. if(!connected)
  257. {
  258. _connectedTrig.RST = true;
  259. PowerSupplierDeviceConfigManager.Instance.StartConnectDevice(Module);
  260. return true;
  261. }
  262. if (!_sendRemoteTrig.Q)
  263. {
  264. if (DateTime.Now.Subtract(_connectTime).TotalSeconds >= 1)
  265. {
  266. _sendRemoteTrig.CLK = SetRemoteOperation("", null);
  267. SetRemoteOperation("", null);
  268. }
  269. return true;
  270. }
  271. if (_readCommandList.Count != 0)
  272. {
  273. if (_readCommandList.TryDequeue(out string command))
  274. {
  275. switch (command)
  276. {
  277. case ENABLED:
  278. PowerSupplierDeviceConfigManager.Instance.GetChannelOutputSwitch(Module, _channel);
  279. break;
  280. case SET_POINT:
  281. PowerSupplierDeviceConfigManager.Instance.GetChannelCurrentSetting(Module, _channel);
  282. break;
  283. case POWER_STATUS:
  284. PowerSupplierDeviceConfigManager.Instance.GetChannelPowerStatus(Module, _channel);
  285. break;
  286. case POWER_CONTROL:
  287. PowerSupplierDeviceConfigManager.Instance.GetChannelPowerControl(Module, _channel);
  288. break;
  289. case POWER_RUN_MODEL:
  290. PowerSupplierDeviceConfigManager.Instance.GetPowerRunmodelControl(Module, _channel);
  291. break;
  292. }
  293. _sendDateTime = DateTime.Now;
  294. }
  295. }
  296. else
  297. {
  298. //控制模式为local
  299. if (PowerSupplierData.PowerControlContent != null && PowerSupplierData.PowerControlContent.ToLower() == "local")
  300. {
  301. return true;
  302. }
  303. if (DateTime.Now.Subtract(_sendDateTime).TotalMilliseconds >= 300)
  304. {
  305. ReadCurrentAndVoltage();
  306. }
  307. }
  308. if(_status==RState.Running)
  309. {
  310. IRoutine routine = GetCurrentRoutine();
  311. if (routine != null)
  312. {
  313. _status = routine.Monitor();
  314. if (_status == RState.Failed || _status == RState.Timeout)
  315. {
  316. _status = RState.Failed;
  317. if(_currentOperation==PowerSupplierOperation.SetCurrent)
  318. {
  319. DisableOperation("", null);
  320. }
  321. _currentOperation = PowerSupplierOperation.None;
  322. }
  323. else if (_status == RState.End)
  324. {
  325. _currentOperation = PowerSupplierOperation.None;
  326. }
  327. }
  328. }
  329. return true;
  330. }
  331. /// <summary>
  332. /// 读取电流和电压
  333. /// </summary>
  334. private void ReadCurrentAndVoltage()
  335. {
  336. _sendDateTime = DateTime.Now;
  337. PowerSupplierDeviceConfigManager.Instance.GetChannelCurrentAndVoltage(Module, _channel);
  338. }
  339. /// <summary>
  340. /// 获取比例后的数值
  341. /// </summary>
  342. /// <param name="value"></param>
  343. /// <returns></returns>
  344. private double GetAfterScaledValue(int value)
  345. {
  346. return (double)value / (CURRENT_UNIT_SCALE);
  347. }
  348. #region Operation
  349. /// <summary>
  350. /// Enable Operation
  351. /// </summary>
  352. /// <param name="cmd"></param>
  353. /// <param name="args"></param>
  354. /// <returns></returns>
  355. public bool EnableOperation(string cmd, object[] args)
  356. {
  357. if (PowerSupplierData == null)
  358. {
  359. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  360. return false;
  361. }
  362. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  363. {
  364. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  365. return false;
  366. }
  367. if (PowerSupplierData.SetPoint==0)
  368. {
  369. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Setpoint is 0,cannot enable output");
  370. return false;
  371. }
  372. EnableOutput();
  373. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Power Control enable output Success");
  374. return true;
  375. }
  376. /// <summary>
  377. /// Disable Operation
  378. /// </summary>
  379. /// <param name="cmd"></param>
  380. /// <param name="args"></param>
  381. /// <returns></returns>
  382. public bool DisableOperation(string cmd, object[] args)
  383. {
  384. if(_status==RState.Running)
  385. {
  386. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  387. return false;
  388. }
  389. DisableOutput();
  390. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Power Control disable output Success");
  391. return true;
  392. }
  393. /// <summary>
  394. /// 中止
  395. /// </summary>
  396. /// <param name="cmd"></param>
  397. /// <param name="args"></param>
  398. /// <returns></returns>
  399. private bool AbortOperation(string cmd, object[] args)
  400. {
  401. if(_status==RState.Running)
  402. {
  403. IRoutine routine = GetCurrentRoutine();
  404. if(routine!=null)
  405. {
  406. routine.Abort();
  407. }
  408. DisableOutput();
  409. PowerSupplierDeviceConfigManager.Instance.SetPowerRunmodelControl(Module, _channel, (byte)PowerRunModelEnum.Normal);
  410. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"abort {_currentOperation} operation");
  411. _status = RState.End;
  412. _currentOperation = PowerSupplierOperation.None;
  413. _readCommandList.Enqueue(POWER_RUN_MODEL);
  414. }
  415. return true;
  416. }
  417. /// <summary>
  418. /// 关闭输出
  419. /// </summary>
  420. /// <returns></returns>
  421. public bool DisableOutput()
  422. {
  423. if(PowerSupplierData==null)
  424. {
  425. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  426. return false;
  427. }
  428. if(PowerSupplierData.PowerControl==(short)PowerControlEnum.Local)
  429. {
  430. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  431. return false;
  432. }
  433. bool result= PowerSupplierDeviceConfigManager.Instance.SetChannelOutputSwitchControl(Module, _channel, false);
  434. _sendDateTime = DateTime.Now;
  435. _readCommandList.Enqueue(ENABLED);
  436. _readCommandList.Enqueue(POWER_STATUS);
  437. return result;
  438. }
  439. /// <summary>
  440. /// 启动输出
  441. /// </summary>
  442. /// <returns></returns>
  443. public bool EnableOutput()
  444. {
  445. if (PowerSupplierData == null)
  446. {
  447. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  448. return false;
  449. }
  450. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  451. {
  452. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  453. return false;
  454. }
  455. bool result= PowerSupplierDeviceConfigManager.Instance.SetChannelOutputSwitchControl(Module, _channel, true);
  456. _sendDateTime = DateTime.Now;
  457. _readCommandList.Enqueue(ENABLED);
  458. _readCommandList.Enqueue(POWER_STATUS);
  459. return result;
  460. }
  461. /// <summary>
  462. /// SetPoint
  463. /// </summary>
  464. /// <param name="cmd"></param>
  465. /// <param name="args"></param>
  466. /// <returns></returns>
  467. public bool SetPointOperation(string cmd, object[] args)
  468. {
  469. if (_status == RState.Running)
  470. {
  471. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  472. return false;
  473. }
  474. if (PowerSupplierData == null)
  475. {
  476. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  477. return false;
  478. }
  479. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  480. {
  481. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  482. return false;
  483. }
  484. if (args.Length<2)
  485. {
  486. return false;
  487. }
  488. if (double.TryParse(args[1].ToString(), out double value))
  489. {
  490. SetCurrent(value);
  491. }
  492. return true;
  493. }
  494. /// <summary>
  495. /// 设置电流
  496. /// </summary>
  497. /// <param name="current"></param>
  498. /// <returns></returns>
  499. public bool SetCurrent(double current)
  500. {
  501. PowerSupplierDeviceConfigManager.Instance.SetPowerRunmodelControl(Module, _channel, (byte)PowerRunModelEnum.Normal);
  502. int setPoint = (int)Math.Round(CURRENT_DEVICE_UNIT_SCALE * current, 0);
  503. bool result= PowerSupplierDeviceConfigManager.Instance.SetChannelCurrent(Module, _channel, setPoint);
  504. _sendDateTime = DateTime.Now;
  505. if (result)
  506. {
  507. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Current {current} Setting success");
  508. _readCommandList.Enqueue(SET_POINT);
  509. _readCommandList.Enqueue(POWER_RUN_MODEL);
  510. return true;
  511. }
  512. else
  513. {
  514. return false;
  515. }
  516. }
  517. /// <summary>
  518. /// 启动
  519. /// </summary>
  520. /// <param name="cmd"></param>
  521. /// <param name="args"></param>
  522. /// <returns></returns>
  523. private bool StartStepPeriod(string cmd, object[] args)
  524. {
  525. if (_status == RState.Running)
  526. {
  527. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  528. return false;
  529. }
  530. if (PowerSupplierData == null)
  531. {
  532. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  533. return false;
  534. }
  535. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  536. {
  537. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  538. return false;
  539. }
  540. bool result= _powerSupplierStepRoutine.Start(args)==RState.Running;
  541. if(result)
  542. {
  543. _status = RState.Running;
  544. _currentOperation = PowerSupplierOperation.StepOperation;
  545. }
  546. return result;
  547. }
  548. /// <summary>
  549. /// 启动设置步阶,
  550. /// </summary>
  551. /// <param name="cmd"></param>
  552. /// <param name="args"></param>
  553. /// <returns></returns>
  554. public bool StartSetStepPeriodNoWaitEnd(List<PowerSupplierStepPeriodData> datas)
  555. {
  556. if (_status == RState.Running)
  557. {
  558. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  559. return false;
  560. }
  561. if (PowerSupplierData == null)
  562. {
  563. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  564. return false;
  565. }
  566. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  567. {
  568. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  569. return false;
  570. }
  571. bool result = _powerSupplierStartStepRoutine.Start(datas) == RState.Running;
  572. if (result)
  573. {
  574. _status = RState.Running;
  575. _currentOperation = PowerSupplierOperation.SetStartOperation;
  576. }
  577. return result;
  578. }
  579. /// <summary>
  580. /// 设置步阶
  581. /// </summary>
  582. /// <param name="cmd"></param>
  583. /// <param name="args"></param>
  584. /// <returns></returns>
  585. public bool SetStepPeriod(string cmd, object[] args)
  586. {
  587. List<PowerSupplierStepPeriodData> powerSupplierStepPeriodDatas = (List<PowerSupplierStepPeriodData>)args[1];
  588. bool result=PowerSupplierDeviceConfigManager.Instance.SetStepPeriod(Module, _channel, powerSupplierStepPeriodDatas,STEP_VOLTAGE_SCALE,CURRENT_DEVICE_UNIT_SCALE);
  589. _sendDateTime = DateTime.Now;
  590. if (result)
  591. {
  592. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Step Period Set Success");
  593. return true;
  594. }
  595. else
  596. {
  597. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, $"Step Period Set Fail");
  598. return false;
  599. }
  600. }
  601. /// <summary>
  602. /// 启动步阶
  603. /// </summary>
  604. /// <param name="endStep"></param>
  605. /// <param name="cycle"></param>
  606. /// <returns></returns>
  607. public bool StartStepPeriod(ushort endStep,ushort cycle)
  608. {
  609. bool result = PowerSupplierDeviceConfigManager.Instance.StartStepPeriod(Module, _channel, 1, endStep, cycle);
  610. _sendDateTime= DateTime.Now;
  611. return result;
  612. }
  613. /// <summary>
  614. /// 设置电源运行模式
  615. /// </summary>
  616. /// <param name="runModel"></param>
  617. /// <returns></returns>
  618. public bool SwitchPowerRunModel(byte runModel)
  619. {
  620. bool result= PowerSupplierDeviceConfigManager.Instance.SetPowerRunmodelControl(Module,_channel, runModel);
  621. _sendDateTime = DateTime.Now;
  622. _readCommandList.Enqueue(POWER_RUN_MODEL);
  623. return result;
  624. }
  625. /// <summary>
  626. /// 设置本地模式
  627. /// </summary>
  628. /// <param name="cmd"></param>
  629. /// <param name="args"></param>
  630. /// <returns></returns>
  631. private bool SetLocalOperation(string cmd, object[] args)
  632. {
  633. if (_status == RState.Running)
  634. {
  635. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  636. return false;
  637. }
  638. bool result= PowerSupplierDeviceConfigManager.Instance.SetChannelPowerControl(Module, _channel, 1);
  639. _sendDateTime = DateTime.Now;
  640. if (result)
  641. {
  642. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Power Control Switch Local Control Success");
  643. _readCommandList.Enqueue(POWER_CONTROL);
  644. return true;
  645. }
  646. else
  647. {
  648. return false;
  649. }
  650. }
  651. /// <summary>
  652. /// 设置远程模式
  653. /// </summary>
  654. /// <param name="cmd"></param>
  655. /// <param name="args"></param>
  656. /// <returns></returns>
  657. private bool SetRemoteOperation(string cmd, object[] args)
  658. {
  659. if (_status == RState.Running)
  660. {
  661. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  662. return false;
  663. }
  664. bool result= PowerSupplierDeviceConfigManager.Instance.SetChannelPowerControl(Module, _channel, 2);
  665. _sendDateTime = DateTime.Now;
  666. if (result)
  667. {
  668. LOG.WriteLog(eEvent.INFO_POWERSUPPLIER, Module, $"Power Control Switch Remote Control Success");
  669. _readCommandList.Enqueue(POWER_CONTROL);
  670. return true;
  671. }
  672. else
  673. {
  674. return false;
  675. }
  676. }
  677. /// <summary>
  678. /// 设置电流并设置输出
  679. /// </summary>
  680. /// <param name="cmd"></param>
  681. /// <param name="args"></param>
  682. /// <returns></returns>
  683. public bool SetCurrentAndOutput(double current)
  684. {
  685. if (_status == RState.Running)
  686. {
  687. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is Busy");
  688. return false;
  689. }
  690. if (PowerSupplierData == null)
  691. {
  692. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Data is null");
  693. return false;
  694. }
  695. if (PowerSupplierData.PowerControl == (short)PowerControlEnum.Local)
  696. {
  697. LOG.WriteLog(eEvent.ERR_POWERSUPPLIER, Module, "Power Supplier is local control");
  698. return false;
  699. }
  700. bool result = _powerSupplierSetCurrentRoutine.Start(current) == RState.Running;
  701. if (result)
  702. {
  703. _status = RState.Running;
  704. _currentOperation = PowerSupplierOperation.SetCurrent;
  705. }
  706. return result;
  707. }
  708. /// <summary>
  709. /// 获取当前的routine
  710. /// </summary>
  711. /// <returns></returns>
  712. private IRoutine GetCurrentRoutine()
  713. {
  714. switch(_currentOperation)
  715. {
  716. case PowerSupplierOperation.StepOperation:
  717. return _powerSupplierStepRoutine;
  718. case PowerSupplierOperation.SetCurrent:
  719. return _powerSupplierSetCurrentRoutine;
  720. case PowerSupplierOperation.SetStartOperation:
  721. return _powerSupplierStartStepRoutine;
  722. default:
  723. return null;
  724. }
  725. }
  726. /// <summary>
  727. /// 设置CC电源状态
  728. /// </summary>
  729. private void SetPowerCCStatus()
  730. {
  731. _readCommandList.Enqueue(POWER_STATUS);
  732. _readCommandList.Enqueue(SET_POINT);
  733. _readCommandList.Enqueue(ENABLED);
  734. _readCommandList.Enqueue(POWER_CONTROL);
  735. _readCommandList.Enqueue(POWER_RUN_MODEL);
  736. }
  737. #endregion
  738. public void Monitor()
  739. {
  740. }
  741. public void Reset()
  742. {
  743. }
  744. public void Terminate()
  745. {
  746. DisableOutput();
  747. }
  748. }
  749. }