private void Dowork(object obj) { while (true) { List <FeedZone> items = new List <FeedZone>(); try { using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.FeedZones.ToList(); } } catch { } foreach (var item in items) { FeedZoneDevice device = null; dict.TryGetValue(item.Id, out device); if (device != null) { if (item.Status != device.Status) { if (device.IsInteractive)//是交互灯 { if (item.Status == 1) { device.CallButton.Display(new Display900UItem(), LightColor.Green, true);//交互灯开启采集 } else { device.CallButton.Clear(true); } } else { if (item.Status == 1) { device.GreenLighthouse.Display(); } else { device.GreenLighthouse.Clear(); } } device.Status = item.Status; } } } Thread.Sleep(1000); } }
public void Start() { this.Stop(); try { List <FeedZone> items = new List <FeedZone>(); using (GeelyPtlEntities dbContext = new GeelyPtlEntities()) { items = dbContext.FeedZones.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]; if (item.IsInteractive == true) //交互灯是PTL900U { Ptl900U callButton = (Ptl900U)bus.Devices.FirstOrDefault(d => d.Address == (byte)item.Address); if (callButton == null) { callButton = new Ptl900U(); callButton.Address = (byte)item.Address; bus.Devices.AddOrUpdate(callButton); } FeedZoneDevice logicDevice = new FeedZoneDevice { XGate = xgate, Bus = bus, CallButton = callButton, IsM3 = false, IsInteractive = item.IsInteractive, Id = item.Id, RFID = item.RFID, GroundId = item.GroundId, AreaId = item.AreaId }; this.devices.Add(logicDevice); } else { 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[(byte)2]; Lighthouse orangeLighthouse = m3.Lighthouses[(byte)1]; Lighthouse greenLighthouse = m3.Lighthouses[(byte)4]; FeedZoneDevice logicDevice = new FeedZoneDevice { XGate = xgate, Bus = bus, M3 = m3, RedLighthouse = redLighthouse, GreenLighthouse = greenLighthouse, OrangeLighthouse = orangeLighthouse, IsM3 = item.IsM3, IsInteractive = false, Id = item.Id, RFID = item.RFID, GroundId = item.GroundId, AreaId = item.AreaId }; this.devices.Add(logicDevice); } } //启动 PTL 通讯 foreach (XGate xGate in this.xGates) { xGate.StartUnicastCommandQueue(); foreach (RS485Bus bus in xGate.Buses) { foreach (PtlDevice target in bus.Devices) { target.Initialize(); var targetDevice = devices.FirstOrDefault(x => x.CallButton == target && x.IsInteractive == true); if (targetDevice != null) { targetDevice.CallButton.Pressed += CallButton_Pressed; } } } } dict = this.devices.ToDictionary(x => x.Id); thread = new Thread(new ParameterizedThreadStart(Dowork)); thread.IsBackground = true; thread.Start(); //threadStatus = new Thread(new ParameterizedThreadStart(DoStatus)); //threadStatus.IsBackground = true; //threadStatus.Start(); var groups = this.devices.GroupBy(x => x.AreaId); foreach (var group in groups) { dictBool.AddOrUpdate(group.Key, false, (a, b) => b); Thread threadStatus = new Thread(new ParameterizedThreadStart(DoStatus)); threadStatus.IsBackground = true; threadStatus.Start(group.Key); dictThread.AddOrUpdate(group.Key, threadStatus, (a, b) => b); } } catch { this.Stop(); } }