FormParkSpaceStatus.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using Message;
  2. using parkMonitor.LOG;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace parkspace_manager
  14. {
  15. public partial class FormParkSpaceStatus : Form
  16. {
  17. public static int spacesInRow = 15;
  18. public static int floors = 11;
  19. public static int rows = 1;
  20. public bool isClosing;
  21. private Dictionary<int, Parkspace_info> idPssMap;
  22. public FormParkSpaceStatus()
  23. {
  24. isClosing = false;
  25. InitializeComponent();
  26. Communicator.Instance.Init();
  27. Communicator.Instance.Bind("tcp://192.168.2.144:20000");
  28. Communicator.Instance.Connect("tcp://192.168.2.125:20000");
  29. // 库位显示
  30. Task.Factory.StartNew(() =>
  31. {
  32. UpdateParkingSpaceView();
  33. });
  34. }
  35. /// <summary>
  36. /// 更新立体停车位显示
  37. /// </summary>
  38. private void UpdateParkingSpaceView()
  39. {
  40. bool needUpdate = false;
  41. Parkspace_allocation_status_msg parkspace_status = new Parkspace_allocation_status_msg();
  42. while (!isClosing)
  43. {
  44. bool get_status_result = Communicator.Instance.Get_parkspace_status(ref parkspace_status);
  45. if (!get_status_result || parkspace_status.ParkspaceInfo == null)
  46. continue;
  47. List<Parkspace_info> psList = parkspace_status.ParkspaceInfo.ToList();
  48. if (psList == null || psList.Count <= 0)
  49. {
  50. Thread.Sleep(10000);
  51. continue;
  52. }
  53. //更新映射表
  54. try
  55. {
  56. if (idPssMap.Count != psList.Count)
  57. {
  58. idPssMap.Clear();
  59. for (int i = 0; i < psList.Count; i++)
  60. {
  61. idPssMap.Add(psList[i].ParkspaceId, psList[i]);
  62. }
  63. }
  64. else
  65. {
  66. for (int i = 0; i < psList.Count; i++)
  67. {
  68. idPssMap[psList[i].ParkspaceId] = psList[i];
  69. }
  70. }
  71. }
  72. catch { Log.WriteLog(LogType.process, LogFile.WARNING, "实时更新所有库位异常"); }
  73. TableLayoutPanel tlp = new TableLayoutPanel();
  74. tlp.Name = "tlp_ParkingSpace";
  75. tlp.ColumnCount = rows;
  76. tlp.RowCount = 1;
  77. tlp.Dock = DockStyle.Fill;
  78. tlp.AutoScroll = true;
  79. tlp.BackColor = SystemColors.GradientActiveCaption;
  80. this.Invoke(new Action(() =>
  81. {
  82. //List<TableLayoutPanel> tlpList = new List<TableLayoutPanel>();
  83. int blockSize = spacesInRow * floors * rows;
  84. int offset = (rows + 1 - ((blockSize / spacesInRow) % rows)) * spacesInRow;
  85. //创建控件
  86. if (ParkingSpaceMonitorPanel.Controls.Count == 0 || needUpdate)
  87. {
  88. for (int i = 0; i < rows; i++)
  89. {
  90. TableLayoutPanel temp = new TableLayoutPanel();
  91. temp.Name = "tlp_ParkingSpaceSection" + (i + 1).ToString();
  92. temp.RowCount = floors;
  93. temp.ColumnCount = spacesInRow;
  94. temp.Dock = DockStyle.Fill;
  95. temp.AutoSize = true;
  96. temp.BackColor = SystemColors.GradientActiveCaption;
  97. temp.Margin = new Padding(5, 2, 5, 2);
  98. for (int j = Math.Max(psList.Count, blockSize) - 1; j >= 0; j--)
  99. {
  100. Button btn = new Button();
  101. btn.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
  102. btn.AutoSize = true;
  103. btn.Location = new Point(10, 10);
  104. btn.Margin = new Padding(3, 10, 3, 10);
  105. btn.Padding = new Padding(5);
  106. btn.Name = "lb_parkingSpace" + j;
  107. btn.TextAlign = ContentAlignment.MiddleCenter;
  108. if (((j + offset) / spacesInRow) % rows == i)
  109. {
  110. if (j > psList.Count - 1)
  111. {
  112. btn.Text = "#####";
  113. temp.Controls.Add(btn);
  114. btn.BackColor = GetPSColor(Parkspace_status.EParkspaceError);
  115. }
  116. else
  117. {
  118. try
  119. {
  120. btn.Text = "车位" + psList[j].ParkspaceId;
  121. btn.BackColor = GetPSColor(psList[j].ParkspaceStatus);
  122. temp.Controls.Add(btn);
  123. }
  124. catch { Log.WriteLog(LogType.process, LogFile.WARNING, "实时更新单一库位异常"); }
  125. }
  126. }
  127. }
  128. tlp.Controls.Add(temp);
  129. //tlpList.Add(temp);
  130. }
  131. ParkingSpaceMonitorPanel.Controls.Clear();
  132. ParkingSpaceMonitorPanel.Controls.Add(tlp);
  133. needUpdate = false;
  134. }
  135. //更新控件
  136. else
  137. {
  138. Control.ControlCollection psCC = ParkingSpaceMonitorPanel.Controls;
  139. foreach (Control segment in psCC)
  140. {
  141. Control.ControlCollection segmentCC = segment.Controls;
  142. //Console.WriteLine("seg::"+segmentCC.Count);
  143. foreach (Control spaces in segmentCC)
  144. {
  145. Control.ControlCollection spaceCC = spaces.Controls;
  146. //Console.WriteLine("spaces::"+spaceCC.Count);
  147. foreach (Control space in spaceCC)
  148. {
  149. if (space.GetType() == typeof(Label))
  150. {
  151. int index = -1;
  152. bool result = int.TryParse(space.Text.Substring(2), out index);
  153. //Console.WriteLine("text is :" + space.Text+", index:"+index);
  154. if (idPssMap.ContainsKey(index))
  155. {
  156. //Console.WriteLine("index:" + index+", status:"+ idPssMap[index].spaceStatus);
  157. space.BackColor = GetPSColor(idPssMap[index].ParkspaceStatus);
  158. }
  159. else if (result)
  160. {
  161. needUpdate = true;
  162. //Console.WriteLine("need update:"+index);
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. if (needUpdate)
  169. {
  170. break;
  171. }
  172. }
  173. }
  174. }));
  175. Thread.Sleep(5000);
  176. }
  177. }
  178. /// <summary>
  179. /// 按钮点击事件
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void Btnvalue_Click(object sender, EventArgs e)
  184. {
  185. Button btn = (Button)sender;
  186. //实例化窗口,选择待修改状态
  187. }
  188. /// <summary>
  189. /// 获取状态对应颜色
  190. /// </summary>
  191. /// <param name="status"></param>
  192. /// <returns></returns>
  193. private Color GetPSColor(Parkspace_status status)
  194. {
  195. switch (status)
  196. {
  197. case Parkspace_status.EParkspaceEmpty: return Color.White;
  198. case Parkspace_status.EParkspaceOccupied: return Color.Yellow;
  199. case Parkspace_status.EParkspaceReserved: return Color.SkyBlue;
  200. case Parkspace_status.EParkspaceLocked: return Color.Blue;
  201. case Parkspace_status.EParkspaceError: return Color.Red;
  202. default: return Color.Violet;
  203. }
  204. }
  205. private void FormParkSpaceStatus_FormClosed(object sender, FormClosedEventArgs e)
  206. {
  207. isClosing = true;
  208. Communicator.Instance.Uninit();
  209. }
  210. }
  211. }