public void Start() { this.Stop(); try { List <MarketZone> items = new List <MarketZone>(); using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.MarketZones.ToList(); } foreach (var item in items) { XGate xgate = this.xGates .FirstOrDefault(xg => xg.IPEndPoint.Address.ToString() == item.XGateIP); if (xgate == null) { xgate = new XGate(item.XGateIP); this.xGates.Add(xgate); } RS485Bus bus = xgate.Buses[(byte)item.Bus]; PtlMXP1O5 m3 = bus.Devices.FirstOrDefault(d => d.Address == (byte)item.Address) as PtlMXP1O5; if (m3 == null) { m3 = new PtlMXP1O5(); m3.Address = (byte)item.Address; bus.Devices.AddOrUpdate(m3); } Lighthouse redLighthouse = m3.Lighthouses[2]; Lighthouse blueLighthouse = m3.Lighthouses[0]; Lighthouse greenLighthouse = m3.Lighthouses[4]; MarketZoneDevice logicDevice = new MarketZoneDevice { XGate = xgate, Bus = bus, M3 = m3, RedLighthouse = redLighthouse, GreenLighthouse = greenLighthouse, BlueLighthouse = blueLighthouse, Id = item.Id, GroundId = item.GroundId, AreaId = item.AreaId }; this.devices.Add(logicDevice); } dict = this.devices.ToDictionary(x => x.Id); //启动 PTL 通讯 foreach (XGate xGate in this.xGates) { xGate.StartUnicastCommandQueue(); foreach (RS485Bus bus in xGate.Buses) { foreach (PtlDevice target in bus.Devices) { target.Initialize(); } } } thread = new Thread(new ParameterizedThreadStart(Dowork)); thread.IsBackground = true; thread.Start(); areaThread = new Thread(new ParameterizedThreadStart(DoStatus)); areaThread.IsBackground = true; areaThread.Start(); } catch { this.Stop(); } }
private void Dowork(object obj) { while (true) { List <MarketZone> items = new List <MarketZone>(); try { using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.MarketZones.ToList(); } } catch { } foreach (var item in items) { MarketZoneDevice device = null; dict.TryGetValue(item.Id, out device); if (device != null) { if (item.Status != device.Status || item.AreaStatus != device.AreaStatus) { Thread.Sleep(100); if (item.Status == 2) { //蓝闪 device.BlueLighthouse.Clear(); device.RedLighthouse.Display(LightOnOffPeriod.Period100, LightOnOffRatio.RatioP1V2); device.GreenLighthouse.Display(LightOnOffPeriod.Period100, LightOnOffRatio.RatioP1V2); } else if (item.AreaStatus == 1) { //黄=红+绿 device.BlueLighthouse.Clear(); device.RedLighthouse.Display(); device.GreenLighthouse.Display(); } else { if (item.Status == 1)//绿色 { device.GreenLighthouse.Display(); device.BlueLighthouse.Clear(); device.RedLighthouse.Clear(); } else if (item.Status == 0)//灭灯 { device.GreenLighthouse.Clear(); device.BlueLighthouse.Clear(); device.RedLighthouse.Clear(); } } device.Status = item.Status; device.AreaStatus = item.AreaStatus; } } } Thread.Sleep(100); } }