private void DoStatus(object obj) { Thread.Sleep(2000);//这里休眠5秒是为了让程序先执行一次Dowork方法 string areaId = obj.ToString(); while (true) { GeelyPtlEntities context = new GeelyPtlEntities(); var dbContextTransaction = context.Database.BeginTransaction(); var model = context.Assemblings.Where(x => x.Status == 1 && x.AreaId == areaId).FirstOrDefault(); if (model == null) { //获取所有该区域所有装配指令,有匹配的物料则亮灯,亮灯后删除指令,没有则不亮灯 AssemblyLightOrder order = new AssemblyLightOrder(); try { //context.Session.BeginTransaction(); order = context.AssemblyLightOrders.OrderBy(p => p.Id).FirstOrDefault(); var item = context.Assemblings.Where(x => x.MaterialId == order.MaterialId).FirstOrDefault(); if (item != null) { item.Status = 1;//亮灯 //删除已使用指令 context.AssemblyLightOrders.Remove(order); } context.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception ex) { //if (context.Session.IsInTransaction) //{ // context.Session.RollbackTransaction(); //} dbContextTransaction.Rollback(); } finally { context.Dispose(); } Thread.Sleep(1000); } } }
void CallButton_Pressed(object sender, Ptl900UPressedEventArgs e) { var targetDevice = devices.FirstOrDefault(x => x.CallButton == sender); if (targetDevice != null) { targetDevice.CallButton.Clear(true);//灭灯 ThreadPool.QueueUserWorkItem(new WaitCallback(_ => { GeelyPtlEntities context = new GeelyPtlEntities(); try { //解绑 var item = context.Assemblings.Where(x => x.Id == targetDevice.Id).FirstOrDefault(); if (item != null) { item.Status = 0; item.MaterialId = null; } context.SaveChanges(); //解锁 var count = context.Assemblings.Where(x => x.AreaId == targetDevice.AreaId && x.MaterialId == null).Count(); if (count == 3) { //读取同缓存区内所有锁定的灯,执行解锁 var list = context.CacheRegions.Where(x => x.AreaId == targetDevice.AreaId && x.IsLocked == true).ToList(); foreach (var li in list) { li.IsLocked = false; } context.SaveChanges(); } } catch { } finally { context.Dispose(); } })); } }
private void DoStatus(object obj) { Thread.Sleep(2000);//这里休眠5秒是为了让程序先执行一次Dowork方法 string areaId = (string)obj; while (true) { if (dictBool[areaId] == false)//该区域的灯没有被拍下,执行操作 { //查找该区域下是否有亮灯,有则不执行,表示有任务 var device = devices.Where(x => x.AreaId == areaId && x.Status == 1).FirstOrDefault(); if (device != null) { //获取第一个指令,在缓存区匹配物料,判断指令是否无效,无效则删除 GeelyPtlEntities context = new GeelyPtlEntities(); try { CacheRegionLightOrder lightorder = context.CacheRegionLightOrders.Where(x => x.AreaId == areaId).FirstOrDefault(); if (lightorder != null) { var item = context.CacheRegions.Where(x => x.Id == device.Id).FirstOrDefault();//context.Query<CacheRegion>().Where(x => x.AreaId == areaId && (x.Material_A == lightorder.MaterialId || x.Material_B == lightorder.MaterialId || x.Material_C == lightorder.MaterialId)).FirstOrDefault(); if (item != null) { if (item.Material_A == lightorder.MaterialId || item.Material_B == lightorder.MaterialId || item.Material_C == lightorder.MaterialId) { //指令无效,删除 context.CacheRegionLightOrders.Remove(lightorder); } } } } catch { } finally { context.Dispose(); } Thread.Sleep(1000); continue; } //获取第一个指令,在装配区,判断指令是否无效,无效则删除 //读取指令表中的当前区域的第一个指令 CacheRegionLightOrder order = null; try { using (GeelyPtlEntities context = new GeelyPtlEntities()) { order = context.CacheRegionLightOrders.Where(x => x.AreaId == areaId).FirstOrDefault(); } } catch { } if (order != null) { GeelyPtlEntities context = new GeelyPtlEntities(); var dbContextTransaction = context.Database.BeginTransaction(); try { //判断该指令在装配区是否有匹配项,有为无效指令删除,没有则继续 var assembling = context.Assemblings.Where(x => x.AreaId == areaId && x.MaterialId == order.MaterialId).Count(); if (assembling > 0) { context.CacheRegionLightOrders.Remove(order); } else { //执行亮灯操作 //根据区域和物料找到匹配的第一个交互灯 var target = context.CacheRegions .Where(x => x.AreaId == areaId && (x.Material_A == order.MaterialId || x.Material_B == order.MaterialId || x.Material_C == order.MaterialId) && x.IsInteractive == true) .FirstOrDefault(); if (target != null)//如果找不到,则认定相关物料未绑定,不执行任何操作 { target.Status = 1;//点灯 //找到柱灯,亮灯,代码吴恺补充 var m3 = context.CacheRegions .Where(x => x.AreaId == areaId && (x.Material_A == order.MaterialId || x.Material_B == order.MaterialId || x.Material_C == order.MaterialId) && x.IsInteractive == false) .FirstOrDefault(); if (m3 != null) { m3.Status = 1; } context.SaveChanges(); //查找装配区物料不为null的个数,大于0则表示正在装配,需要锁定灯 var number = context.Assemblings.Where(x => x.AreaId == areaId && x.MaterialId != null).Count(); if (number > 0) { target.IsLocked = true;//锁灯 var targetDevice = this.devices.FirstOrDefault(x => x.Id == target.Id); if (targetDevice != null) { targetDevice.CallButton.Lock(); //这里锁一次 //targetDevice.IsLocked = true; } } // context.Update<CacheRegion>(target); context.SaveChanges(); } //删除当前指令 context.CacheRegionLightOrders.Remove(order); dbContextTransaction.Commit(); } } catch { dbContextTransaction.Rollback(); } finally { context.Dispose(); } } } //休眠 Thread.Sleep(1000); } }