123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- using Message;
- using parkMonitor.LOG;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace parkspace_manager
- {
- public partial class FormParkSpaceStatus : Form
- {
- public static int spacesInRow = 15;
- public static int floors = 11;
- public static int rows = 1;
- public bool isClosing;
- private Dictionary<int, Parkspace_info> idPssMap;
- public FormParkSpaceStatus()
- {
- isClosing = false;
- InitializeComponent();
- Communicator.Instance.Init();
- Communicator.Instance.Bind("tcp://192.168.2.144:20000");
- Communicator.Instance.Connect("tcp://192.168.2.125:20000");
- // 库位显示
- Task.Factory.StartNew(() =>
- {
- UpdateParkingSpaceView();
- });
- }
- /// <summary>
- /// 更新立体停车位显示
- /// </summary>
- private void UpdateParkingSpaceView()
- {
- bool needUpdate = false;
- Parkspace_allocation_status_msg parkspace_status = new Parkspace_allocation_status_msg();
- while (!isClosing)
- {
- bool get_status_result = Communicator.Instance.Get_parkspace_status(ref parkspace_status);
- if (!get_status_result || parkspace_status.ParkspaceInfo == null)
- continue;
- List<Parkspace_info> psList = parkspace_status.ParkspaceInfo.ToList();
- if (psList == null || psList.Count <= 0)
- {
- Thread.Sleep(10000);
- continue;
- }
- //更新映射表
- try
- {
- if (idPssMap.Count != psList.Count)
- {
- idPssMap.Clear();
- for (int i = 0; i < psList.Count; i++)
- {
- idPssMap.Add(psList[i].ParkspaceId, psList[i]);
- }
- }
- else
- {
- for (int i = 0; i < psList.Count; i++)
- {
- idPssMap[psList[i].ParkspaceId] = psList[i];
- }
- }
- }
- catch { Log.WriteLog(LogType.process, LogFile.WARNING, "实时更新所有库位异常"); }
- TableLayoutPanel tlp = new TableLayoutPanel();
- tlp.Name = "tlp_ParkingSpace";
- tlp.ColumnCount = rows;
- tlp.RowCount = 1;
- tlp.Dock = DockStyle.Fill;
- tlp.AutoScroll = true;
- tlp.BackColor = SystemColors.GradientActiveCaption;
- this.Invoke(new Action(() =>
- {
- //List<TableLayoutPanel> tlpList = new List<TableLayoutPanel>();
- int blockSize = spacesInRow * floors * rows;
- int offset = (rows + 1 - ((blockSize / spacesInRow) % rows)) * spacesInRow;
- //创建控件
- if (ParkingSpaceMonitorPanel.Controls.Count == 0 || needUpdate)
- {
- for (int i = 0; i < rows; i++)
- {
- TableLayoutPanel temp = new TableLayoutPanel();
- temp.Name = "tlp_ParkingSpaceSection" + (i + 1).ToString();
- temp.RowCount = floors;
- temp.ColumnCount = spacesInRow;
- temp.Dock = DockStyle.Fill;
- temp.AutoSize = true;
- temp.BackColor = SystemColors.GradientActiveCaption;
- temp.Margin = new Padding(5, 2, 5, 2);
- for (int j = Math.Max(psList.Count, blockSize) - 1; j >= 0; j--)
- {
- Button btn = new Button();
- btn.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
- btn.AutoSize = true;
- btn.Location = new Point(10, 10);
- btn.Margin = new Padding(3, 10, 3, 10);
- btn.Padding = new Padding(5);
- btn.Name = "lb_parkingSpace" + j;
- btn.TextAlign = ContentAlignment.MiddleCenter;
- if (((j + offset) / spacesInRow) % rows == i)
- {
- if (j > psList.Count - 1)
- {
- btn.Text = "#####";
- temp.Controls.Add(btn);
- btn.BackColor = GetPSColor(Parkspace_status.EParkspaceError);
- }
- else
- {
- try
- {
- btn.Text = "车位" + psList[j].ParkspaceId;
- btn.BackColor = GetPSColor(psList[j].ParkspaceStatus);
- temp.Controls.Add(btn);
- }
- catch { Log.WriteLog(LogType.process, LogFile.WARNING, "实时更新单一库位异常"); }
- }
- }
- }
- tlp.Controls.Add(temp);
- //tlpList.Add(temp);
- }
- ParkingSpaceMonitorPanel.Controls.Clear();
- ParkingSpaceMonitorPanel.Controls.Add(tlp);
- needUpdate = false;
- }
- //更新控件
- else
- {
- Control.ControlCollection psCC = ParkingSpaceMonitorPanel.Controls;
- foreach (Control segment in psCC)
- {
- Control.ControlCollection segmentCC = segment.Controls;
- //Console.WriteLine("seg::"+segmentCC.Count);
- foreach (Control spaces in segmentCC)
- {
- Control.ControlCollection spaceCC = spaces.Controls;
- //Console.WriteLine("spaces::"+spaceCC.Count);
- foreach (Control space in spaceCC)
- {
- if (space.GetType() == typeof(Label))
- {
- int index = -1;
- bool result = int.TryParse(space.Text.Substring(2), out index);
- //Console.WriteLine("text is :" + space.Text+", index:"+index);
- if (idPssMap.ContainsKey(index))
- {
- //Console.WriteLine("index:" + index+", status:"+ idPssMap[index].spaceStatus);
- space.BackColor = GetPSColor(idPssMap[index].ParkspaceStatus);
- }
- else if (result)
- {
- needUpdate = true;
- //Console.WriteLine("need update:"+index);
- break;
- }
- }
- }
- }
- if (needUpdate)
- {
- break;
- }
- }
- }
- }));
- Thread.Sleep(5000);
- }
- }
- /// <summary>
- /// 按钮点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Btnvalue_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- //实例化窗口,选择待修改状态
-
- }
- /// <summary>
- /// 获取状态对应颜色
- /// </summary>
- /// <param name="status"></param>
- /// <returns></returns>
- private Color GetPSColor(Parkspace_status status)
- {
- switch (status)
- {
- case Parkspace_status.EParkspaceEmpty: return Color.White;
- case Parkspace_status.EParkspaceOccupied: return Color.Yellow;
- case Parkspace_status.EParkspaceReserved: return Color.SkyBlue;
- case Parkspace_status.EParkspaceLocked: return Color.Blue;
- case Parkspace_status.EParkspaceError: return Color.Red;
- default: return Color.Violet;
- }
- }
- private void FormParkSpaceStatus_FormClosed(object sender, FormClosedEventArgs e)
- {
- isClosing = true;
- Communicator.Instance.Uninit();
- }
- }
- }
|