MatrixStation.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using EntityDataSet;
  12. using log4net;
  13. using IntelligentControlForsx.Service;
  14. using IntelligentControlForsx.Model;
  15. using WisdomClient.data;
  16. using WisdomClient;
  17. using System.Threading;
  18. using IntelligentControlForsx.Common;
  19. namespace IntelligentControlForsx.MyControls
  20. {
  21. public partial class MatrixStation : UserControl
  22. {
  23. private ILog logger = LogManager.GetLogger("AppInfoLog");
  24. private string[] deviceArray;
  25. private Dictionary<string, MatrixBlock> dictMatrixDevice;
  26. private ContextMenuStrip contextMenuStrip;
  27. public MatrixStation()
  28. {
  29. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);//开启双缓冲
  30. InitializeComponent();
  31. }
  32. string stationName;
  33. string stationId;
  34. double zfdl;
  35. double nfdl;
  36. double yfdl;
  37. double rfdl;
  38. double power;
  39. double windSpeed;
  40. double fhl;
  41. int cntAll;
  42. int cntBW;
  43. int cntSD;
  44. int cntTJ;
  45. int cntDJ;
  46. int cntGZ;
  47. int cntWH;
  48. int cntLX;
  49. int cntQD;
  50. #region 基本属性
  51. [Browsable(true), Category("Data")]
  52. public string StationName
  53. {
  54. get
  55. {
  56. return stationName;
  57. }
  58. set
  59. {
  60. stationName = value;
  61. lblStationName.Text = value;
  62. }
  63. }
  64. [Browsable(true), Category("Data")]
  65. public string StationId
  66. {
  67. get
  68. {
  69. return stationId;
  70. }
  71. set
  72. {
  73. stationId = value;
  74. }
  75. }
  76. public ContextMenuStrip ContextMenuStrip1
  77. {
  78. get
  79. {
  80. return contextMenuStrip;
  81. }
  82. set
  83. {
  84. contextMenuStrip = value;
  85. }
  86. }
  87. #endregion
  88. #region 指标属性
  89. [Browsable(true), Category("Data")]
  90. public double WindSpeed
  91. {
  92. get
  93. {
  94. return windSpeed;
  95. }
  96. set
  97. {
  98. windSpeed = value;
  99. lblWindSpeed.Text = Convert.ToDouble(value).ToString("0.00");
  100. }
  101. }
  102. [Browsable(true), Category("Data")]
  103. public double Power
  104. {
  105. get
  106. {
  107. return power;
  108. }
  109. set
  110. {
  111. power = value;
  112. lblPower.Text = Convert.ToDouble(value).ToString("0.00");
  113. }
  114. }
  115. [Browsable(true), Category("Data")]
  116. public double ZFDL
  117. {
  118. get
  119. {
  120. return zfdl;
  121. }
  122. set
  123. {
  124. zfdl = value;
  125. //lblZFDL.Text = Convert.ToDouble(value).ToString("0.00");
  126. }
  127. }
  128. [Browsable(true), Category("Data")]
  129. public double NFDL
  130. {
  131. get
  132. {
  133. return nfdl;
  134. }
  135. set
  136. {
  137. nfdl = value;
  138. lblNFDL.Text = Convert.ToDouble(value).ToString("0.00");
  139. }
  140. }
  141. [Browsable(true), Category("Data")]
  142. public double YFDL
  143. {
  144. get
  145. {
  146. return yfdl;
  147. }
  148. set
  149. {
  150. yfdl = value;
  151. lblYFDL.Text = Convert.ToDouble(value).ToString("0.00");
  152. }
  153. }
  154. [Browsable(true), Category("Data")]
  155. public double RFDL
  156. {
  157. get
  158. {
  159. return rfdl;
  160. }
  161. set
  162. {
  163. rfdl = value;
  164. lblRFDL.Text = Convert.ToDouble(value).ToString("0.00");
  165. }
  166. }
  167. [Browsable(true), Category("Data")]
  168. public double FHL
  169. {
  170. get
  171. {
  172. return fhl;
  173. }
  174. set
  175. {
  176. fhl = value;
  177. lblFHL.Text = Convert.ToDouble(value).ToString("0.00");
  178. }
  179. }
  180. #endregion
  181. #region 状态台数
  182. [Browsable(true), Category("Data")]
  183. public int CntAll
  184. {
  185. get
  186. {
  187. return cntAll;
  188. }
  189. set
  190. {
  191. cntAll = value;
  192. lblCntAll.Text = value.ToString();
  193. }
  194. }
  195. [Browsable(true), Category("Data")]
  196. public int CntBW
  197. {
  198. get
  199. {
  200. return cntBW;
  201. }
  202. set
  203. {
  204. cntBW = value;
  205. lblCntAll.Text = value.ToString();
  206. }
  207. }
  208. [Browsable(true), Category("Data")]
  209. public int CntSD
  210. {
  211. get
  212. {
  213. return cntSD;
  214. }
  215. set
  216. {
  217. cntSD = value;
  218. lblCntSD.Text = value.ToString();
  219. }
  220. }
  221. [Browsable(true), Category("Data")]
  222. public int CntTJ
  223. {
  224. get
  225. {
  226. return cntTJ;
  227. }
  228. set
  229. {
  230. cntTJ = value;
  231. lblCntTJ.Text = value.ToString();
  232. }
  233. }
  234. [Browsable(true), Category("Data")]
  235. public int CntDJ
  236. {
  237. get
  238. {
  239. return cntDJ;
  240. }
  241. set
  242. {
  243. cntDJ = value;
  244. lblCntDJ.Text = value.ToString();
  245. }
  246. }
  247. [Browsable(true), Category("Data")]
  248. public int CntGZ
  249. {
  250. get
  251. {
  252. return cntGZ;
  253. }
  254. set
  255. {
  256. cntGZ = value;
  257. lblCntGZ.Text = value.ToString();
  258. }
  259. }
  260. [Browsable(true), Category("Data")]
  261. public int CntWH
  262. {
  263. get
  264. {
  265. return cntWH;
  266. }
  267. set
  268. {
  269. cntWH = value;
  270. lblCntWH.Text = value.ToString();
  271. }
  272. }
  273. [Browsable(true), Category("Data")]
  274. public int CntLX
  275. {
  276. get
  277. {
  278. return cntLX;
  279. }
  280. set
  281. {
  282. cntLX = value;
  283. lblCntLX.Text = value.ToString();
  284. }
  285. }
  286. [Browsable(true), Category("Data")]
  287. public int CntQD
  288. {
  289. get
  290. {
  291. return cntQD;
  292. }
  293. set
  294. {
  295. cntQD = value;
  296. lblCntQD.Text = value.ToString();
  297. }
  298. }
  299. #endregion
  300. public void InitTablePanel()
  301. {
  302. if (String.IsNullOrWhiteSpace(stationId))
  303. return;
  304. deviceArray = MatrixFormService.GetDeviceIdArrayByStationId(stationId);
  305. var wtArray = CacheService.Instance.GetWindturbineListByStationId(stationId).ToArray();
  306. if (wtArray == null || wtArray.Length < 1)
  307. return;
  308. dictMatrixDevice = new Dictionary<string, MatrixBlock>();
  309. int col = 22;
  310. int row = (int)Math.Ceiling((decimal)wtArray.Length / 22);
  311. int arrIndex = 0;
  312. pnlTable.Height = row * 42;
  313. this.Height = pnlTable.Height + 24;
  314. pnlTable.RowCount = row;
  315. this.SuspendLayout();
  316. for (int i = 0; i < row; i++)
  317. {
  318. pnlTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 42));
  319. for (int j = 0; j < col; j++)
  320. {
  321. if (arrIndex >= wtArray.Length)
  322. return;
  323. MatrixBlock device = new MatrixBlock();
  324. device.ContextMenuStrip = contextMenuStrip;
  325. device.DeviceId = CommonMethod.GetShortWindturbineId(wtArray[arrIndex].ID);
  326. device.DeviceModel = wtArray[arrIndex].MODELID;
  327. device.StationId = wtArray[arrIndex].WINDPOWERSTATIONID;
  328. device.Status = 3;
  329. pnlTable.Controls.Add(device);
  330. if (!dictMatrixDevice.ContainsKey(device.DeviceId))
  331. dictMatrixDevice.Add(device.DeviceId, device);
  332. pnlTable.SetRow(device, i);
  333. pnlTable.SetColumn(device, j);
  334. arrIndex++;
  335. }
  336. }
  337. this.Refresh();
  338. this.ResumeLayout();
  339. timer1.Enabled = true;
  340. }
  341. public void DeActive()
  342. {
  343. this.Hide();
  344. timer1.Stop();
  345. }
  346. public void Active()
  347. {
  348. this.Show();
  349. Thread ayscThread = new Thread(LoadData);
  350. ayscThread.IsBackground = true;
  351. ayscThread.Start();
  352. timer1.Start();
  353. }
  354. #region 异步加载数据
  355. private void timer1_Tick(object sender, EventArgs e)
  356. {
  357. if (isLoadding) return;
  358. LoadData();
  359. }
  360. private bool isLoadding = true;
  361. private object loadDataLocker = new object();
  362. private async void LoadData()
  363. {
  364. isLoadding = true;
  365. try
  366. {
  367. foreach (var kv in await SearchStationData())
  368. {
  369. if (CacheService.Instance.MatrixPointMap.ContainsKey(kv.Key))
  370. {
  371. CacheService.Instance.MatrixPointMap[kv.Key].Value = kv.Value.doubleValue.Value;
  372. switch (CacheService.Instance.MatrixPointMap[kv.Key].UniformCode)
  373. {
  374. #region Switch
  375. //"YXTS", //运行台数
  376. //"TXZD", //通讯中断
  377. //"DJTS", //待机台数
  378. //"WHTJ", //维护台数
  379. //"GZTJ", //故障台数
  380. //"XDTS", //限电台数
  381. //"SSFS", //实时平均风速
  382. //"SSZGL", //实时总功率
  383. //"RFDL", //日发电量
  384. //"YFDL", //月发电量
  385. //"NFDL", //年发电量
  386. //"ZFDL" //总发电量
  387. case "BWTS8":
  388. lblCntBW.Text = kv.Value.doubleValue.Value.ToString("f0");
  389. break;
  390. case "LXTS8":
  391. lblCntLX.Text = kv.Value.doubleValue.Value.ToString("f0");
  392. break;
  393. case "DJTS8":
  394. lblCntDJ.Text = kv.Value.doubleValue.Value.ToString("f0");
  395. break;
  396. case "WHTS8":
  397. lblCntWH.Text = kv.Value.doubleValue.Value.ToString("f0");
  398. break;
  399. case "GZTS8":
  400. lblCntGZ.Text = kv.Value.doubleValue.Value.ToString("f0");
  401. break;
  402. case "QDTS8":
  403. lblCntQD.Text = kv.Value.doubleValue.Value.ToString("f0");
  404. break;
  405. case "TJTS8":
  406. lblCntTJ.Text = kv.Value.doubleValue.Value.ToString("f0");
  407. break;
  408. case "SDTS8":
  409. lblCntSD.Text = kv.Value.doubleValue.Value.ToString("f0");
  410. break;
  411. case "RFDL":
  412. lblRFDL.Text = kv.Value.doubleValue.Value.ToString("f2");
  413. break;
  414. case "YFDL":
  415. lblYFDL.Text = kv.Value.doubleValue.Value.ToString("f2");
  416. break;
  417. case "NFDL":
  418. lblNFDL.Text = kv.Value.doubleValue.Value.ToString("f2");
  419. break;
  420. case "ZFDL":
  421. // lblZFDL.Text = value.ToString("f2");
  422. break;
  423. case "SSFS":
  424. lblWindSpeed.Text = kv.Value.doubleValue.Value.ToString("f2");
  425. break;
  426. case "SSZGL":
  427. lblPower.Text = kv.Value.doubleValue.Value.ToString("f2");
  428. double fh = 0.0;
  429. double fhl = 0.0;
  430. if (stationId == "SBQ_FDC")
  431. {
  432. fh = kv.Value.doubleValue.Value / 198.5;
  433. fhl = fh * 100;
  434. }
  435. if (stationId == "MHS_FDC")
  436. {
  437. fh = kv.Value.doubleValue.Value / 69.5;
  438. fhl = fh * 100;
  439. }
  440. if (stationId == "XS_FDC")
  441. {
  442. fh = kv.Value.doubleValue.Value / 99;
  443. fhl = fh * 100;
  444. }
  445. if (stationId == "QS_FDC")
  446. {
  447. fh = kv.Value.doubleValue.Value / 99;
  448. fhl = fh * 100;
  449. }
  450. if (stationId == "NSS_FDC")
  451. {
  452. fh = kv.Value.doubleValue.Value / 148.5;
  453. fhl = fh * 100;
  454. }
  455. lblFHL.Text = fhl.ToString("f2");
  456. break;
  457. #endregion
  458. }
  459. }
  460. }
  461. foreach (var kv in await SearchWindturbineData())
  462. {
  463. if (CacheService.Instance.MatrixPointMap.ContainsKey(kv.Key))
  464. {
  465. CacheService.Instance.MatrixPointMap[kv.Key].Value = kv.Value.doubleValue.Value;
  466. MatrixBlock wBlock = dictMatrixDevice[CacheService.Instance.MatrixPointMap[kv.Key].ThingId];
  467. switch (CacheService.Instance.MatrixPointMap[kv.Key].UniformCode)
  468. {
  469. #region Switch
  470. //"AI021", //3秒平均风速
  471. //"AI130", //功率
  472. //"AI010", //风向角度
  473. //"FJZT" //风机状态
  474. case "AI022":
  475. wBlock.WindSpeed = kv.Value.doubleValue.Value;
  476. break;
  477. case "AI130":
  478. wBlock.Power = kv.Value.doubleValue.Value;
  479. break;
  480. //case "AI010":
  481. // wBlock.WindAngle = values[i].ToString("f2") + " 度";
  482. // break;
  483. case "FJZT8":
  484. wBlock.Status = kv.Value.doubleValue.Value;
  485. wBlock.Ts = kv.Value.ts;
  486. break;
  487. case "XDSL":
  488. if (kv.Value.doubleValue.Value != 0)
  489. wBlock.IsLock = true;
  490. else
  491. wBlock.IsLock = false;
  492. wBlock.LockValue = kv.Value.doubleValue.Value;
  493. break;
  494. #endregion
  495. }
  496. }
  497. }
  498. }
  499. catch (Exception ex)
  500. {
  501. logger.Info("读取实时数据失败!ex=" + ex.Message);
  502. }
  503. finally
  504. {
  505. isLoadding = false;
  506. }
  507. }
  508. private Task<Dictionary<string, TsData>> SearchStationData()
  509. {
  510. return Task.Run(
  511. () =>
  512. {
  513. var dict = CacheService.Instance.MatrixPointMap;
  514. if (dict == null)
  515. return new Dictionary<string, TsData>();
  516. var pmms = dict.Values.Where(q => q.ThingType == PointType.Station && q.ThingId == stationId).ToArray();
  517. var points = pmms.Select(q => q.PointId).ToArray();
  518. return RestfulClient.findLatestByTagNames(points);
  519. });
  520. }
  521. private Task<Dictionary<string, TsData>> SearchWindturbineData()
  522. {
  523. return Task.Run(
  524. () =>
  525. {
  526. if (deviceArray == null || deviceArray.Length < 1)
  527. return new Dictionary<string, TsData>();
  528. var dict = CacheService.Instance.MatrixPointMap;
  529. if (dict == null)
  530. return new Dictionary<string, TsData>();
  531. var pmms = dict.Values.Where(q => q.ThingType == PointType.Windturbine && deviceArray.Contains(q.ThingId)).ToArray();
  532. var points = pmms.Select(q => q.PointId).ToArray();
  533. return RestfulClient.findLatestByTagNames(points);
  534. });
  535. }
  536. #endregion
  537. }
  538. }