MatrixStation2.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 MatrixStation2 : UserControl
  22. {
  23. private ILog logger = LogManager.GetLogger("AppInfoLog");
  24. private string[] deviceArray;
  25. private Dictionary<string, MatrixBlock> dictMatrixDevice2;
  26. private ContextMenuStrip contextMenuStrip;
  27. public MatrixStation2()
  28. {
  29. InitializeComponent();
  30. //this.mpnlCancelMaintain.Recommond();
  31. //this.mpnlMaintain.Recommond();
  32. //this.mpnlReset.Recommond();
  33. //this.mpnlStartup.Recommond();
  34. //this.mpnlStop.Recommond();
  35. }
  36. string stationName;
  37. string stationId;
  38. int cntAll;
  39. int cntBW;
  40. int cntSD;
  41. int cntTJ;
  42. int cntDJ;
  43. int cntGZ;
  44. int cntWH;
  45. int cntLX;
  46. int cntQD;
  47. #region 基本属性
  48. [Browsable(true), Category("Data")]
  49. public string StationName
  50. {
  51. get
  52. {
  53. return stationName;
  54. }
  55. set
  56. {
  57. stationName = value;
  58. lblStationName.Text = value;
  59. }
  60. }
  61. [Browsable(true), Category("Data")]
  62. public string StationId
  63. {
  64. get
  65. {
  66. return stationId;
  67. }
  68. set
  69. {
  70. stationId = value;
  71. this.mpnlStartup.StationId = value;
  72. this.mpnlStop.StationId = value;
  73. this.mpnlReset.StationId = value;
  74. this.mpnlMaintain.StationId = value;
  75. this.mpnlCancelMaintain.StationId = value;
  76. }
  77. }
  78. public ContextMenuStrip ContextMenuStrip1
  79. {
  80. get
  81. {
  82. return contextMenuStrip;
  83. }
  84. set
  85. {
  86. contextMenuStrip = value;
  87. }
  88. }
  89. #endregion
  90. #region 状态台数
  91. [Browsable(true), Category("Data")]
  92. public int CntAll
  93. {
  94. get
  95. {
  96. return cntAll;
  97. }
  98. set
  99. {
  100. cntAll = value;
  101. lblCntAll.Text = value.ToString();
  102. }
  103. }
  104. [Browsable(true), Category("Data")]
  105. public int CntBW
  106. {
  107. get
  108. {
  109. return cntBW;
  110. }
  111. set
  112. {
  113. cntBW = value;
  114. lblCntAll.Text = value.ToString();
  115. }
  116. }
  117. [Browsable(true), Category("Data")]
  118. public int CntSD
  119. {
  120. get
  121. {
  122. return cntSD;
  123. }
  124. set
  125. {
  126. cntSD = value;
  127. lblCntSD.Text = value.ToString();
  128. }
  129. }
  130. [Browsable(true), Category("Data")]
  131. public int CntTJ
  132. {
  133. get
  134. {
  135. return cntTJ;
  136. }
  137. set
  138. {
  139. cntTJ = value;
  140. lblCntTJ.Text = value.ToString();
  141. }
  142. }
  143. [Browsable(true), Category("Data")]
  144. public int CntDJ
  145. {
  146. get
  147. {
  148. return cntDJ;
  149. }
  150. set
  151. {
  152. cntDJ = value;
  153. lblCntDJ.Text = value.ToString();
  154. }
  155. }
  156. [Browsable(true), Category("Data")]
  157. public int CntGZ
  158. {
  159. get
  160. {
  161. return cntGZ;
  162. }
  163. set
  164. {
  165. cntGZ = value;
  166. lblCntGZ.Text = value.ToString();
  167. }
  168. }
  169. [Browsable(true), Category("Data")]
  170. public int CntWH
  171. {
  172. get
  173. {
  174. return cntWH;
  175. }
  176. set
  177. {
  178. cntWH = value;
  179. lblCntWH.Text = value.ToString();
  180. }
  181. }
  182. [Browsable(true), Category("Data")]
  183. public int CntLX
  184. {
  185. get
  186. {
  187. return cntLX;
  188. }
  189. set
  190. {
  191. cntLX = value;
  192. lblCntLX.Text = value.ToString();
  193. }
  194. }
  195. [Browsable(true), Category("Data")]
  196. public int CntQD
  197. {
  198. get
  199. {
  200. return cntQD;
  201. }
  202. set
  203. {
  204. cntQD = value;
  205. lblCntQD.Text = value.ToString();
  206. }
  207. }
  208. #endregion
  209. public void InitTablePanel()
  210. {
  211. if (String.IsNullOrWhiteSpace(stationId))
  212. return;
  213. deviceArray = MatrixFormService.GetDeviceIdArrayByStationId(stationId);
  214. var wtArray = CacheService.Instance.GetWindturbineListByStationId(stationId).ToArray();
  215. if (wtArray == null || wtArray.Length < 1)
  216. return;
  217. dictMatrixDevice2 = new Dictionary<string, MatrixBlock>();
  218. int col = 20;
  219. int row = (int)Math.Ceiling((decimal)wtArray.Length / 20);
  220. int arrIndex = 0;
  221. pnlTable.Height = row * 42 + 10;
  222. //this.Height = pnlTable.Height + 33;
  223. pnlTable.RowCount = row;
  224. for (int i = 0; i < row; i++)
  225. {
  226. pnlTable.RowStyles.Add(new RowStyle(SizeType.Absolute, 42));
  227. for (int j = 0; j < col; j++)
  228. {
  229. if (arrIndex >= wtArray.Length)
  230. return;
  231. MatrixBlock device = new MatrixBlock();
  232. device.ContextMenuStrip = contextMenuStrip;
  233. device.DeviceId = CommonMethod.GetShortWindturbineId(wtArray[arrIndex].ID);
  234. device.DeviceModel = wtArray[arrIndex].MODELID;
  235. device.StationId = wtArray[arrIndex].WINDPOWERSTATIONID;
  236. device.Status = 7;
  237. device.Ts = 0;
  238. device.MyMouseDown += MyMouseDownHandler;
  239. device.MyMouseUp += MyMouseUpHandler;
  240. pnlTable.Controls.Add(device);
  241. if (!dictMatrixDevice2.ContainsKey(device.DeviceId))
  242. dictMatrixDevice2.Add(device.DeviceId, device);
  243. pnlTable.SetRow(device, i);
  244. pnlTable.SetColumn(device, j);
  245. arrIndex++;
  246. }
  247. }
  248. }
  249. public void DeActive()
  250. {
  251. this.Hide();
  252. timer1.Stop();
  253. this.mpnlCancelMaintain.DeActive();
  254. this.mpnlMaintain.DeActive();
  255. this.mpnlReset.DeActive();
  256. this.mpnlStartup.DeActive();
  257. this.mpnlStop.DeActive();
  258. }
  259. public void Active()
  260. {
  261. this.Show();
  262. Thread ayscThread = new Thread(LoadData);
  263. ayscThread.IsBackground = true;
  264. ayscThread.Start();
  265. timer1.Start();
  266. this.mpnlCancelMaintain.Active();
  267. this.mpnlMaintain.Active();
  268. this.mpnlReset.Active();
  269. this.mpnlStartup.Active();
  270. this.mpnlStop.Active();
  271. }
  272. #region 异步加载数据
  273. private void timer1_Tick(object sender, EventArgs e)
  274. {
  275. if (isLoadding) return;
  276. Thread ayscThread = new Thread(LoadData);
  277. ayscThread.IsBackground = true;
  278. ayscThread.Start();
  279. }
  280. private bool isLoadding = true;
  281. private void LoadData()
  282. {
  283. isLoadding = true;
  284. try
  285. {
  286. UpdateStationStates();
  287. UpdateWindturbineStates();
  288. }
  289. catch (Exception ex)
  290. {
  291. logger.Info("读取实时数据失败!ex=" + ex.Message);
  292. }
  293. finally
  294. {
  295. isLoadding = false;
  296. }
  297. }
  298. private void UpdateStationStates()
  299. {
  300. var dict = CacheService.Instance.MatrixPointMap;
  301. if (dict == null)
  302. return;
  303. var pmms = dict.Values.Where(q => q.ThingType == PointType.Station && q.ThingId == stationId).ToArray();
  304. var points = pmms.Select(q => q.PointId).ToArray();
  305. Dictionary<string, TsData> dictResult = RestfulClient.findLatestByTagNames(points);
  306. lock (this)
  307. {
  308. this.BeginInvoke(
  309. (Action)delegate
  310. {
  311. for (int i = 0; i < points.Length; i++)
  312. {
  313. if (dictResult.ContainsKey(points[i]) == false)
  314. continue;
  315. double value = dictResult[points[i]].doubleValue.Value;
  316. pmms[i].Value = value;
  317. switch (pmms[i].UniformCode)
  318. {
  319. #region Switch
  320. case "BWTS8":
  321. lblCntBW.Text = value.ToString("f0");
  322. break;
  323. case "LXTS8":
  324. lblCntLX.Text = value.ToString("f0");
  325. break;
  326. case "DJTS8":
  327. lblCntDJ.Text = value.ToString("f0");
  328. break;
  329. case "WHTS8":
  330. lblCntWH.Text = value.ToString("f0");
  331. break;
  332. case "GZTS8":
  333. lblCntGZ.Text = value.ToString("f0");
  334. break;
  335. case "TJTS8":
  336. lblCntTJ.Text = value.ToString("f0");
  337. break;
  338. case "QDTS8":
  339. lblCntQD.Text = value.ToString("f0");
  340. break;
  341. case "SDTS8":
  342. lblCntSD.Text = value.ToString("f0");
  343. break;
  344. #endregion
  345. }
  346. }
  347. });
  348. }
  349. }
  350. //private void UpdateStationStates() { }
  351. private void UpdateWindturbineStates()
  352. {
  353. if (deviceArray == null || deviceArray.Length < 1)
  354. return;
  355. var dict = CacheService.Instance.MatrixPointMap;
  356. if (dict == null)
  357. return;
  358. var pmms = dict.Values.Where(q => q.ThingType == PointType.Windturbine && deviceArray.Contains(q.ThingId)).ToArray();
  359. var points = pmms.Select(q => q.PointId).ToArray();
  360. Dictionary<string, TsData> dictResult = RestfulClient.findLatestByTagNames(points);
  361. lock (this)
  362. {
  363. this.BeginInvoke(
  364. (Action)delegate
  365. {
  366. for (int i = 0; i < points.Length; i++)
  367. {
  368. if (dictResult.ContainsKey(points[i]) == false)
  369. continue;
  370. double value = dictResult[points[i]].doubleValue.Value;
  371. long ts = dictResult[points[i]].ts;
  372. pmms[i].Value = value;
  373. var wBlock = dictMatrixDevice2[pmms[i].ThingId];
  374. if (wBlock == null)
  375. continue;
  376. string windturbineId = CommonMethod.GetLongWindturbineId(pmms[i].ThingId);
  377. switch (pmms[i].UniformCode)
  378. {
  379. #region Switch
  380. //"AI021", //3秒平均风速
  381. //"AI130", //功率
  382. //"AI010", //风向角度
  383. //"FJZT" //风机状态
  384. case "AI022":
  385. wBlock.WindSpeed = value;
  386. break;
  387. case "AI130":
  388. wBlock.Power = value;
  389. break;
  390. //case "AI010":
  391. // wBlock.WindAngle = values[i].ToString("f2") + " 度";
  392. // break;
  393. case "FJZT8":
  394. wBlock.Status = value;
  395. wBlock.Ts = ts;
  396. break;
  397. case "XDSL":
  398. if (value != 0)
  399. wBlock.IsLock = true;
  400. else
  401. wBlock.IsLock = false;
  402. wBlock.LockValue = value;
  403. break;
  404. #endregion
  405. }
  406. }
  407. refreshDevices();
  408. });
  409. }
  410. }
  411. #endregion
  412. #region 数据筛选
  413. private double selectedStatus = 9;
  414. private bool autoFilter = false;
  415. private void pnlAll_Click(object sender, EventArgs e)
  416. {
  417. selectedStatus = 9;
  418. refreshDevices();
  419. }
  420. private void refreshDevices()
  421. {
  422. foreach (var device in dictMatrixDevice2.Values)
  423. {
  424. double minSpeed, maxSpeed, minPower, maxPower;
  425. Double.TryParse(txtMinSpeed.Text, out minSpeed);
  426. Double.TryParse(txtMaxSpeed.Text, out maxSpeed);
  427. Double.TryParse(txtMinPower.Text, out minPower);
  428. Double.TryParse(txtMaxPower.Text, out maxPower);
  429. if (device.testShow(selectedStatus, autoFilter, minSpeed, maxSpeed, minPower, maxPower))
  430. {
  431. device.Show();
  432. }
  433. else
  434. {
  435. device.Hide();
  436. }
  437. }
  438. }
  439. //风机六种状态: 限电5》运行1》待机0》维护4》故障2》离线3
  440. private void pnlBW_Click(object sender, EventArgs e)
  441. {
  442. selectedStatus = 1;
  443. refreshDevices();
  444. }
  445. private void pnlDJ_Click(object sender, EventArgs e)
  446. {
  447. selectedStatus = 0;
  448. refreshDevices();
  449. }
  450. private void pnlGZ_Click(object sender, EventArgs e)
  451. {
  452. selectedStatus = 2;
  453. refreshDevices();
  454. }
  455. private void pnlWH_Click(object sender, EventArgs e)
  456. {
  457. selectedStatus = 4;
  458. refreshDevices();
  459. }
  460. private void pnlLX_Click(object sender, EventArgs e)
  461. {
  462. selectedStatus = 3;
  463. refreshDevices();
  464. }
  465. private void pnlQD_Click(object sender, EventArgs e)
  466. {
  467. selectedStatus = 11;
  468. refreshDevices();
  469. }
  470. private void pnlTJ_Click(object sender, EventArgs e)
  471. {
  472. selectedStatus = 12;
  473. refreshDevices();
  474. }
  475. private void pnlSD_Click(object sender, EventArgs e)
  476. {
  477. selectedStatus = 13;
  478. refreshDevices();
  479. }
  480. private void chkAutoFilter_CheckedChanged(object sender, EventArgs e)
  481. {
  482. autoFilter = chkAutoFilter.Checked;
  483. if (autoFilter)
  484. refreshDevices();
  485. }
  486. #endregion
  487. // public event EventHandler MyMouseDown;
  488. private void MyMouseDownHandler(object sender, EventArgs e)
  489. {
  490. PointGroup obj = (PointGroup)sender;
  491. if (obj != null)
  492. {
  493. // MessageBox.Show(obj.ToString());
  494. }
  495. }
  496. private void MyMouseUpHandler(object sender, EventArgs e)
  497. {
  498. PointGroup obj = (PointGroup)sender;
  499. if (obj != null)
  500. {
  501. //MessageBox.Show(obj.ToString());
  502. }
  503. }
  504. }
  505. }