MatrixPanel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  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 IntelligentControlForsx.Service.WindturbineControl.Domain.Cmd;
  12. using IntelligentControlForsx.Model;
  13. using IntelligentControlForsx.Service;
  14. using EntityDataSet;
  15. using IntelligentControlForsx.ChildForms;
  16. using System.Threading;
  17. using IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc;
  18. using WisdomClient.data;
  19. using WisdomClient;
  20. namespace IntelligentControlForsx.MyControls
  21. {
  22. public partial class MatrixPanel : UserControl
  23. {
  24. public MatrixPanel()
  25. {
  26. InitializeComponent();
  27. toolTip1.SetToolTip(this.btnRecomm, "智能推荐");
  28. }
  29. private void btnRecomm_Click(object sender, EventArgs e)
  30. {
  31. //MessageBox.Show("aldskj" + this.controlType);
  32. Recommond();
  33. }
  34. public void Recommond()
  35. {
  36. var sp = PredictService.GetStationPredict(StationId);
  37. IList<WindturbineStatus> lstWs = new List<WindturbineStatus>();
  38. if (this.controlType == CmdType.Start)
  39. lstWs = sp.StartupList;
  40. else if (this.controlType == CmdType.Stop)
  41. lstWs = sp.StopList;
  42. else if (this.controlType == CmdType.Reset)
  43. lstWs = sp.ResetList;
  44. else if (this.controlType == CmdType.Maintain)
  45. lstWs = sp.MaintainList;
  46. else if (this.controlType == CmdType.UnMaintain)
  47. lstWs = sp.CancelMaintainList;
  48. // case 0: //停机状态
  49. // case 1: //上电状态
  50. // case 2: //待机状态
  51. // case 3: //启动状态
  52. // case 4: //并网状态
  53. // case 5: //故障状态
  54. // case 6: //维护状态
  55. // case 7: //离线状态
  56. this.tlpDevices.Controls.Clear();
  57. if (lstWs.Count > 0)
  58. {
  59. wtList = lstWs.Select(q => q.WindturbineId).ToList();
  60. UpdateTablePanel(this.tlpDevices, lstWs);
  61. }
  62. }
  63. private void btnRecomm_MouseHover(object sender, EventArgs e)
  64. {
  65. Button btn = sender as Button;
  66. btn.FlatAppearance.BorderSize = 1;
  67. }
  68. private void btnRecomm_MouseLeave(object sender, EventArgs e)
  69. {
  70. Button btn = sender as Button;
  71. btn.FlatAppearance.BorderSize = 0;
  72. }
  73. #region 属性定义
  74. string title;
  75. string stationId;
  76. CmdType controlType;
  77. List<String> wtList;
  78. public static List<string> lblList = new List<string>();
  79. [Browsable(true), Category("Data")]
  80. public string Title
  81. {
  82. get
  83. {
  84. return title;
  85. }
  86. set
  87. {
  88. title = value;
  89. lblTitle.Text = title;
  90. }
  91. }
  92. public string StationId
  93. {
  94. get
  95. {
  96. return stationId;
  97. }
  98. set
  99. {
  100. stationId = value;
  101. }
  102. }
  103. [Browsable(true), Category("Data")]
  104. public CmdType ControlType
  105. {
  106. get
  107. {
  108. return controlType;
  109. }
  110. set
  111. {
  112. controlType = value;
  113. }
  114. }
  115. public List<string> WtList
  116. {
  117. get
  118. {
  119. return wtList;
  120. }
  121. set
  122. {
  123. wtList = value;
  124. UpateWtList();
  125. }
  126. }
  127. public void UpateWtList()
  128. {
  129. IList<WindturbineStatus> lstWs = PredictService.GetWindturbineStatus(wtList, this.stationId);
  130. this.tlpDevices.Controls.Clear();
  131. this.dictPopupDevice2.Clear();
  132. UpdateTablePanel(this.tlpDevices, lstWs);
  133. }
  134. private void UpdateTablePanel(TableLayoutPanel tlp, IList<WindturbineStatus> lstWs)
  135. {
  136. int col = 4;
  137. int row = (int)Math.Ceiling((decimal)lstWs.Count / col);
  138. int arrIndex = 0;
  139. var arr = lstWs.ToArray();
  140. tlp.Height = row * 60;
  141. tlp.RowCount = row;
  142. for (int i = 0; i < row; i++)
  143. {
  144. tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 60));
  145. for (int j = 0; j < col; j++)
  146. {
  147. if (arrIndex >= lstWs.Count)
  148. return;
  149. PopupDevice2 pd = new PopupDevice2();
  150. WindturbineStatus ws = arr[arrIndex];
  151. pd.MatrixPanel1 = this;
  152. pd.DeviceId = ws.WindturbineId;
  153. int x = 50;
  154. int.TryParse(pd.DeviceId.Substring(2, 2), out x);
  155. pd.Rank = RandomRank(110 - x);
  156. pd.WindSpeed = ws["AI022"].doubleValue == null ? 0 : ws["AI022"].doubleValue.Value;
  157. pd.Status = ws.GetStatus();
  158. pd.Power = ws["AI130"].doubleValue == null ? 0 : ws["AI130"].doubleValue.Value;
  159. tlp.Controls.Add(pd);
  160. if (!dictPopupDevice2.ContainsKey(pd.DeviceId))
  161. dictPopupDevice2.Add(pd.DeviceId, pd);
  162. tlp.SetRow(pd, i);
  163. tlp.SetColumn(pd, j);
  164. arrIndex++;
  165. }
  166. }
  167. }
  168. #endregion
  169. #region 窗体事件处理
  170. private void MatrixPanel_DragDrop(object sender, DragEventArgs e)
  171. {
  172. if (e.Data.GetDataPresent(typeof(System.String)))
  173. {
  174. string item = (string)e.Data.GetData(typeof(System.String));
  175. if (e.Effect == DragDropEffects.Copy ||
  176. e.Effect == DragDropEffects.Move)
  177. {
  178. //if (this.wtList == null)
  179. // this.wtList = new List<string>();
  180. //if (wtList.Contains(item) == false)
  181. // wtList.Add(item);
  182. foreach (String tid in lblList)
  183. {
  184. if (wtList == null)
  185. {
  186. wtList = new List<string>();
  187. }
  188. if (wtList.Contains(tid) == false)
  189. wtList.Add(tid);
  190. }
  191. UpateWtList();
  192. //MessageBox.Show(this.title + "," +item.ToString());
  193. }
  194. }
  195. }
  196. private void MatrixPanel_DragEnter(object sender, DragEventArgs e)
  197. {
  198. e.Effect = DragDropEffects.Move;
  199. }
  200. private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
  201. {
  202. foreach (var ctrl in tlpDevices.Controls)
  203. {
  204. PopupDevice2 pd = ctrl as PopupDevice2;
  205. if (pd != null)
  206. {
  207. if (chkSelectAll.Checked)
  208. {
  209. pd.Checked1 = true;
  210. }
  211. else
  212. {
  213. pd.Checked1 = false;
  214. }
  215. }
  216. }
  217. }
  218. private void btnCancel_MouseHover(object sender, EventArgs e)
  219. {
  220. Button btn = sender as Button;
  221. btn.FlatAppearance.BorderSize = 1;
  222. }
  223. private void btnCancel_MouseLeave(object sender, EventArgs e)
  224. {
  225. Button btn = sender as Button;
  226. btn.FlatAppearance.BorderSize = 0;
  227. }
  228. private void btnCancel_Click(object sender, EventArgs e)
  229. {
  230. if (wtList != null && wtList.Count > 0)
  231. {
  232. wtList.Clear();
  233. UpateWtList();
  234. }
  235. this.chkSelectAll.Checked = false;
  236. }
  237. private void btnConfirm_Click(object sender, EventArgs e)
  238. {
  239. //bool ifSend = false;
  240. //foreach (var ctrl in tlpDevices.Controls)
  241. //{
  242. // PopupDevice2 pd = ctrl as PopupDevice2;
  243. // if (pd != null)
  244. // {
  245. // if (pd.Checked1 == true)
  246. // {
  247. // ifSend = true;
  248. // break;
  249. // }
  250. // }
  251. //}
  252. //if (ifSend == false)
  253. //{
  254. // MessageBox.Show("您没有勾选任何风机!");
  255. // return;
  256. //}
  257. //user u = null;
  258. //FingerCheck fc = new FingerCheck();
  259. //fc.ShowDialog();
  260. //u = FingerCheck.checkSuccessUser;
  261. //if (u != null)
  262. //{
  263. // //wtList.Clear();
  264. // IList<string> windturbineIdList = new List<string>();
  265. // foreach (var ctrl in tlpDevices.Controls)
  266. // {
  267. // PopupDevice2 pd = ctrl as PopupDevice2;
  268. // if (pd != null)
  269. // {
  270. // string windturbineId = "SG01_" + pd.DeviceId.Replace("SG", "");
  271. // windturbineIdList.Add(windturbineId);
  272. // if (pd.Checked1 == true)
  273. // {
  274. // wtList.Remove(pd.DeviceId);
  275. // }
  276. // }
  277. // }
  278. // UpateWtList();
  279. // string busyWindturbine = "";
  280. // for (int i = 0; i < windturbineIdList.Count; i++)
  281. // {
  282. // if (TaskQueueSvc.dicTask.ContainsKey(windturbineIdList[i]))
  283. // busyWindturbine = busyWindturbine + windturbineIdList[i] + ",";
  284. // }
  285. // if (!string.IsNullOrEmpty(busyWindturbine))
  286. // {
  287. // MessageBox.Show(busyWindturbine + "风机操作过频繁");
  288. // return;
  289. // }
  290. // TaskQueueSvc.QueueAdd(windturbineIdList, "SBQ_FDC", controlType, u);
  291. // //ExitPopup();
  292. //}
  293. wtList.Clear();
  294. UpateWtList();
  295. }
  296. private void btnConfirm_MouseHover(object sender, EventArgs e)
  297. {
  298. Button btn = sender as Button;
  299. btn.FlatAppearance.BorderSize = 1;
  300. }
  301. private void btnConfirm_MouseLeave(object sender, EventArgs e)
  302. {
  303. Button btn = sender as Button;
  304. btn.FlatAppearance.BorderSize = 0;
  305. }
  306. #endregion
  307. private Dictionary<string, PopupDevice2> dictPopupDevice2 = new Dictionary<string, PopupDevice2>();
  308. private void timer1_Tick(object sender, EventArgs e)
  309. {
  310. if (isLoadding == true)
  311. return;
  312. Thread ayscThread = new Thread(LoadData);
  313. ayscThread.IsBackground = true;
  314. ayscThread.Start();
  315. //timer1.Start();
  316. }
  317. public void DeActive()
  318. {
  319. timer1.Stop();
  320. }
  321. public void Active()
  322. {
  323. Thread ayscThread = new Thread(LoadData);
  324. ayscThread.IsBackground = true;
  325. ayscThread.Start();
  326. timer1.Start();
  327. }
  328. private bool isLoadding = false;
  329. private void LoadData()
  330. {
  331. if (isLoadding == true)
  332. return;
  333. isLoadding = true;
  334. try
  335. {
  336. UpdateWindturbineStates();
  337. }
  338. catch (Exception ex)
  339. {
  340. //logger.Info("读取实时数据失败!ex=" + ex.Message);
  341. }
  342. finally
  343. {
  344. isLoadding = false;
  345. }
  346. }
  347. private void UpdateWindturbineStates()
  348. {
  349. if (wtList == null || wtList.Count() < 1)
  350. return;
  351. var dict = CacheService.Instance.MatrixPointMap;
  352. if (dict == null)
  353. return;
  354. var pmms = dict.Values.Where(q => q.ThingType == PointType.Windturbine && wtList.Contains(q.ThingId)).ToArray();
  355. var points = pmms.Select(q => q.PointId).ToArray();
  356. Dictionary<string, TsData> dictResult = RestfulClient.findLatestByTagNames(points);
  357. lock (this)
  358. {
  359. this.BeginInvoke(
  360. (Action)delegate
  361. {
  362. for (int i = 0; i < points.Length; i++)
  363. {
  364. if (dictResult.ContainsKey(points[i]) == false)
  365. continue;
  366. double value = dictResult[points[i]].doubleValue.Value;
  367. pmms[i].Value = value;
  368. var wBlock = dictPopupDevice2[pmms[i].ThingId];
  369. if (wBlock == null)
  370. continue;
  371. switch (pmms[i].UniformCode)
  372. {
  373. #region Switch
  374. //"AI021", //3秒平均风速
  375. //"AI130", //功率
  376. //"AI010", //风向角度
  377. //"FJZT" //风机状态
  378. case "AI022":
  379. wBlock.WindSpeed = value;
  380. break;
  381. case "AI130":
  382. wBlock.Power = value;
  383. break;
  384. //case "AI010":
  385. // wBlock.WindAngle = values[i].ToString("f2") + " 度";
  386. // break;
  387. case "FJZT":
  388. wBlock.Status = value;
  389. break;
  390. #endregion
  391. }
  392. }
  393. });
  394. }
  395. }
  396. private string RandomRank(int intRank)
  397. {
  398. //Random random = new Random();
  399. int rank = intRank;// random.Next(0, 100);
  400. if (rank >= 90)
  401. return "AA";
  402. else if (rank > 80)
  403. return "A";
  404. else if (rank > 60)
  405. return "BBB";
  406. else if (rank > 40)
  407. return "BB";
  408. else if (rank > 20)
  409. return "B";
  410. else if (rank > 10)
  411. return "C";
  412. else
  413. return "C-";
  414. }
  415. }
  416. }