PowerSupplierSocketSimulator.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using Aitex.Core.RT.DataCenter;
  2. using CyberX8_Core;
  3. using MECF.Framework.Common.CommonData;
  4. using MECF.Framework.Common.CommonData.PowerSupplier;
  5. using MECF.Framework.Common.Net;
  6. using MECF.Framework.Common.Utilities;
  7. using MECF.Framework.Simulator.Core.Driver;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Security.Cryptography.X509Certificates;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Timers;
  16. namespace CyberX8_Simulator.Devices
  17. {
  18. public class PowerSupplierSocketSimulator : SocketDeviceSimulator
  19. {
  20. private const short POWER_CONTROL_ADDRESS = 0x0113;
  21. private const short STEP_PERIOD_ADDRESS = 0x1400;
  22. private const short STEP_PERIOD_START_ADDRESS = 0x1640;
  23. /// <summary>
  24. /// 电源状态(00-cv输出,01-cc输出)
  25. /// </summary>
  26. private const short POWER_STATUS_ADDRESS = 0x0200;
  27. Dictionary<int, PowerSupplierData> _powerSupplierDic = new Dictionary<int, PowerSupplierData>();
  28. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  29. private List<PowerSupplierStepPeriodData> _powerSupplierDatas = new List<PowerSupplierStepPeriodData>();
  30. private byte _startStep = 1;
  31. private byte _endStep = 255;
  32. private byte _cycle = 1;
  33. private byte _currentStep = 0;
  34. private int _currentLength = 0;
  35. private DateTime _currentTime = DateTime.Now;
  36. private System.Timers.Timer _timer;
  37. public PowerSupplierSocketSimulator(int port):base(port)
  38. {
  39. PowerSupplierData powerSupplierData1 = new PowerSupplierData();
  40. powerSupplierData1.Current = 0;
  41. powerSupplierData1.Voltage = 56000;
  42. _powerSupplierDic[1] = powerSupplierData1;
  43. PowerSupplierData powerSupplierData2 = new PowerSupplierData();
  44. powerSupplierData2.Current = 0;
  45. powerSupplierData2.Voltage = 2000;
  46. _powerSupplierDic[2] = powerSupplierData2;
  47. PowerSupplierData powerSupplierData3 = new PowerSupplierData();
  48. powerSupplierData3.Current = 0;
  49. powerSupplierData3.Voltage = 3000;
  50. _powerSupplierDic[3] = powerSupplierData3;
  51. PowerSupplierData powerSupplierData4 = new PowerSupplierData();
  52. powerSupplierData4.Current = 0;
  53. powerSupplierData4.Voltage = 4000;
  54. _powerSupplierDic[4] = powerSupplierData4;
  55. _timer = new System.Timers.Timer();
  56. _timer.Interval = 1000;
  57. _timer.Elapsed += Timer_Elapsed;
  58. }
  59. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  60. {
  61. if(_currentStep>=_cycle*_endStep)
  62. {
  63. _timer.Stop();
  64. }
  65. if(DateTime.Now.Subtract(_currentTime).TotalSeconds>=_currentLength)
  66. {
  67. _currentStep++;
  68. if (_currentStep >= _cycle * _endStep)
  69. {
  70. _powerSupplierDic[1].Current = 0;
  71. _timer.Stop();
  72. return;
  73. }
  74. _currentTime = DateTime.Now;
  75. _currentLength = (int)(_powerSupplierDatas[_currentStep].Hour * 3600 + _powerSupplierDatas[_currentStep].Minute * 60 +
  76. _powerSupplierDatas[_currentStep].Second);
  77. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[_currentStep].Current*10);
  78. }
  79. }
  80. protected override void ProcessUnsplitMessage(byte[] data)
  81. {
  82. short flag = byteTransform.TransInt16(data, 0);
  83. byte channel = data[6];
  84. byte command = data[7];
  85. if(!_powerSupplierDic.ContainsKey(channel))
  86. {
  87. OnWriteMessage(CreateError(flag,channel, command, 0x02));
  88. return;
  89. }
  90. if (command == 0x03)//读取
  91. {
  92. short startAddress = byteTransform.TransInt16(data, 8);
  93. short registerCount = byteTransform.TransInt16(data, 10);
  94. if (startAddress == 0x201)
  95. {
  96. byte[] bytes = new byte[2*registerCount];
  97. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Voltage), 0, bytes, 0, 4);
  98. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].Current), 0, bytes, 4, 4);
  99. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  100. return;
  101. }
  102. else if (startAddress == 0x0110)
  103. {
  104. byte[] bytes = new byte[2];
  105. bytes[0] = 0;
  106. bytes[1] = _powerSupplierDic[channel].Enabled?(byte)1:(byte)0;
  107. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  108. return;
  109. }
  110. else if (startAddress == POWER_STATUS_ADDRESS)
  111. {
  112. byte[] bytes = new byte[2];
  113. bytes[0] = 0;
  114. bytes[1] = (byte)_powerSupplierDic[channel].PowerStatus;
  115. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  116. return;
  117. }
  118. else if (startAddress == 0x0101)
  119. {
  120. byte[] bytes = new byte[2];
  121. Array.Copy(byteTransform.GetBytes(_powerSupplierDic[channel].CurrentSetting), 0, bytes, 0, bytes.Length);
  122. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  123. return;
  124. }
  125. else if (startAddress == 0X80)
  126. {
  127. byte[] bytes = new byte[2];
  128. bytes[0] = 0;
  129. bytes[1] = _powerSupplierDic[channel].OutputSwitchControl;
  130. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  131. return;
  132. }
  133. else if (startAddress == 0x0111)
  134. {
  135. byte[] bytes = new byte[2];
  136. bytes[0] = 0;
  137. bytes[1] = (byte)_powerSupplierDic[channel].RunModel;
  138. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  139. return;
  140. }
  141. else if (startAddress == POWER_CONTROL_ADDRESS)
  142. {
  143. byte[] bytes = new byte[2];
  144. bytes[0] = 0;
  145. bytes[1] = (byte)_powerSupplierDic[channel].PowerControl;
  146. OnWriteMessage(CreateReadResponse(flag, channel, command, registerCount, bytes));
  147. return;
  148. }
  149. else
  150. {
  151. OnWriteMessage(CreateError(flag, channel, command, 03));
  152. }
  153. }
  154. else if (command == 0x06)//设置
  155. {
  156. short startAddress = byteTransform.TransInt16(data, 8);
  157. short value = byteTransform.TransInt16(data, 10);
  158. if(startAddress==0x0110)
  159. {
  160. UpdateChannelEnable(flag, channel, command, startAddress, data[11]);
  161. if (_powerSupplierDic[channel].Enabled)
  162. {
  163. if (_powerSupplierDic[channel].RunModel == 1)
  164. {
  165. _powerSupplierDic[channel].Current = _powerSupplierDic[channel].CurrentSetting * 10;
  166. }
  167. }
  168. else
  169. {
  170. _powerSupplierDic[channel].Current = 0;
  171. }
  172. }
  173. else if (startAddress == 0x0101)
  174. {
  175. UpdateChannelCurrent(flag,channel, command, startAddress, value);
  176. }
  177. else if (startAddress == POWER_CONTROL_ADDRESS)
  178. {
  179. UpdateChannelPowerControl(flag, channel, command, startAddress, data[11]);
  180. }
  181. else if(startAddress== 0x0111)
  182. {
  183. UpdateChannelRunModel(flag, channel, command, startAddress, data[11]);
  184. }
  185. else
  186. {
  187. byte[] errorByt = CreateError(flag, channel, command, 03);
  188. OnWriteMessage(errorByt);
  189. return;
  190. }
  191. }
  192. else if(command==0x10)
  193. {
  194. short startAddress = byteTransform.TransInt16(data, 8);
  195. short length = byteTransform.TransInt16(data, 10);
  196. if (startAddress == STEP_PERIOD_ADDRESS)
  197. {
  198. _powerSupplierDatas.Clear();
  199. int listCount = length / 6;
  200. for(int i=0;i<listCount;i++)
  201. {
  202. PowerSupplierStepPeriodData powerSupplierData = new PowerSupplierStepPeriodData();
  203. powerSupplierData.Voltage = byteTransform.TransInt16(data, 13 + i * 12);
  204. powerSupplierData.Current = byteTransform.TransInt16(data, 15 + i * 12);
  205. powerSupplierData.Hour = byteTransform.TransUInt16(data, 17 + i * 12);
  206. powerSupplierData.Minute = byteTransform.TransUInt16(data, 19 + i * 12);
  207. powerSupplierData.Second = byteTransform.TransUInt16(data, 21 + i * 12);
  208. powerSupplierData.Microsecond = byteTransform.TransUInt16(data, 23 + i * 12);
  209. _powerSupplierDatas.Add(powerSupplierData);
  210. }
  211. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  212. }
  213. else if(startAddress==STEP_PERIOD_START_ADDRESS)
  214. {
  215. _startStep = (byte)byteTransform.TransInt16(data,13);
  216. _endStep = (byte)byteTransform.TransInt16(data, 15);
  217. _cycle = (byte)byteTransform.TransInt16(data, 17);
  218. _currentLength = (_powerSupplierDatas[0].Hour * 3600 + _powerSupplierDatas[0].Minute * 60 +
  219. _powerSupplierDatas[0].Second);
  220. _powerSupplierDic[1].Current = (int)(_powerSupplierDatas[0].Current*10);
  221. _currentTime = DateTime.Now;
  222. _currentStep = 0;
  223. _timer.Start();
  224. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, length));
  225. }
  226. }
  227. else
  228. {
  229. OnWriteMessage(CreateError(flag, channel, command, 0x01));
  230. }
  231. }
  232. private byte[] CreateReadResponse(short flag,byte channel,byte command,short registerCount, byte[] values)
  233. {
  234. byte[] bytes = new byte[3 + values.Length + 6];
  235. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  236. bytes[2] = 0x00;
  237. bytes[3] = 0x00;
  238. short dataLength=(short)(3+ values.Length);
  239. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  240. bytes[6] = channel;
  241. bytes[7] = command;
  242. bytes[8] = (byte)(2 * registerCount);
  243. Array.Copy(values,0,bytes, 9, values.Length);
  244. return bytes;
  245. }
  246. private void UpdateChannelCurrent(short flag,byte channel, byte command, short startAddress, short value)
  247. {
  248. _powerSupplierDic[channel].CurrentSetting = value;
  249. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  250. }
  251. private void UpdateChannelEnabled(short flag,byte channel, byte command, short startAddress, short value)
  252. {
  253. _powerSupplierDic[channel].OutputSwitchControl = (byte)value;
  254. OnWriteMessage(CreateWriteResponse(flag,channel, command, startAddress, value));
  255. }
  256. private void UpdateChannelPowerControl(short flag, byte channel, byte command, short startAddress, short value)
  257. {
  258. _powerSupplierDic[channel].PowerControl = (byte)value;
  259. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  260. }
  261. private void UpdateChannelRunModel(short flag, byte channel, byte command, short startAddress, short value)
  262. {
  263. _powerSupplierDic[channel].RunModel = (byte)value;
  264. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  265. }
  266. private void UpdateChannelEnable(short flag, byte channel, byte command, short startAddress, short value)
  267. {
  268. _powerSupplierDic[channel].Enabled = value == 1;
  269. OnWriteMessage(CreateWriteResponse(flag, channel, command, startAddress, value));
  270. }
  271. private byte[] CreateWriteResponse(short flag,byte channel,byte command, short startAddress,short value)
  272. {
  273. byte[] byt = new byte[12];
  274. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  275. byt[2] = 0x00;
  276. byt[3] = 0x00;
  277. byt[4] = 0x00;
  278. byt[5] = 0x06;
  279. byt[6] = channel;
  280. byt[7] = command;
  281. byte[] addressByt= byteTransform.GetBytes(startAddress);
  282. Array.Copy(addressByt, 0, byt, 8, 2);
  283. byte[] valueByt=byteTransform.GetBytes(value);
  284. Array.Copy(valueByt, 0, byt, 10, 2);
  285. return byt;
  286. }
  287. private byte[] CreateMultiWriteResponse(short flag, byte channel, byte command, short startAddress, short count)
  288. {
  289. byte[] byt = new byte[12];
  290. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  291. byt[2] = 0x00;
  292. byt[3] = 0x00;
  293. byt[4] = 0x00;
  294. byt[5] = 0x06;
  295. byt[6] = channel;
  296. byt[7] = command;
  297. byte[] addressByt = byteTransform.GetBytes(startAddress);
  298. Array.Copy(addressByt, 0, byt, 8, 2);
  299. byte[] valueByt = byteTransform.GetBytes(count);
  300. Array.Copy(valueByt, 0, byt, 10, 2);
  301. return byt;
  302. }
  303. private byte[] CreateError(short flag,byte channel,byte command,byte error)
  304. {
  305. byte[] byt = new byte[9];
  306. Array.Copy(byteTransform.GetBytes(flag), 0, byt, 0, 2);
  307. byt[2] = 0x00;
  308. byt[3] = 0x00;
  309. byt[4] = 0x00;
  310. byt[5] = 0x03;
  311. byt[6]=channel;
  312. byt[7]=(byte)(command|0x80);
  313. byt[8] = error;
  314. return byt;
  315. }
  316. }
  317. internal class PowerSupplierData
  318. {
  319. private short _voltageSetting;
  320. private short _currentSetting;
  321. private byte _outputSwitchControl;
  322. private int _voltage;
  323. private int _current;
  324. private short _powerStatus = (int)PowerStatusEnum.CC;
  325. private short _powerControl=(int)PowerControlEnum.Remote;
  326. private short _runModel = (int)PowerRunModelEnum.Normal;
  327. /// <summary>
  328. /// 可用性
  329. /// </summary>
  330. private bool _enabled;
  331. public short VoltageSetting { get { return _voltageSetting; } set { _voltageSetting = value; } }
  332. public short CurrentSetting { get { return _currentSetting; } set { _currentSetting = value; } }
  333. public byte OutputSwitchControl { get { return _outputSwitchControl; } set { _outputSwitchControl = value; } }
  334. public int Voltage { get { return _voltage; } set { _voltage = value; } }
  335. public int Current { get { return _current; } set { _current = value; } }
  336. public short PowerStatus { get { return _powerStatus; } set { _powerStatus = value; } }
  337. public short PowerControl { get { return _powerControl; } set { _powerControl = value; } }
  338. public short RunModel { get { return _runModel; } set { _runModel = value; } }
  339. public bool Enabled { get { return _enabled; } set { _enabled = value; } }
  340. }
  341. public class PowerSupplierStepPeriodData
  342. {
  343. #region 内部变量
  344. private double _voltage;
  345. private double _current;
  346. private ushort _hour;
  347. private ushort _minute;
  348. private ushort _second;
  349. private ushort _microsecond;
  350. #endregion
  351. #region 属性
  352. public double Voltage { get { return _voltage; } set { _voltage = value; } }
  353. public double Current { get { return _current; } set { _current = value; } }
  354. public ushort Hour { get { return _hour; } set { _hour = value; } }
  355. public ushort Minute { get { return _minute; } set { _minute = value; } }
  356. public ushort Second { get { return _second; } set { _second = value; } }
  357. public ushort Microsecond { get { return _microsecond; } set { _microsecond = value; } }
  358. #endregion
  359. }
  360. }