|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- //#define ModelSwitch
-
- using BPA.Helper;
- using BPASmartClient.SmallBatchingSystem;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.CustomResource.UserControls;
-
- namespace BPASmartClient.SmallBatchingSystem
- {
- public class Control
- {
- private volatile static Control _Instance;
- public static Control GetInstance => _Instance ?? (_Instance = new Control());
- private Control() { }
- public ConcurrentQueue<RecipeInfo> MakeOrderQueue { get; set; } = new ConcurrentQueue<RecipeInfo>();
-
- public bool IsCancel { get; set; }
-
- public void Init()
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- PlcServer.GetInstance.Connect();
- }), "设备初始化");
-
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- while (MakeOrderQueue.Count > 0)
- {
- if (MakeOrderQueue.TryDequeue(out RecipeInfo recipeInfo))
- {
- ProcessOne(recipeInfo);
- //ProcessTwo(recipeInfo);
- }
- }
- Thread.Sleep(10);
- }), "配方流程控制");
- }
-
- private void ProcessOne(RecipeInfo recipeInfo)
- {
- IsCancel = false;
- List<int> OutletInfo = new List<int>();
- recipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
- {
- var res = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(item.SiloName)) != null);
- if (res != null) if (!OutletInfo.Contains(res.OutletLoc)) OutletInfo.Add(res.OutletLoc);
- });
- foreach (var temp in OutletInfo)
- {
- PlcServer.GetInstance.WriteData("VW302", (ushort)temp);//设置出料口位置
- PlcServer.GetInstance.WriteData("M10.0", true);//定位启动
- RunLog($"启动定位,出料口位置:{temp}");
- int index = temp - 1;
- if (index >= 0)
- {
- RunLog("等待定位完成");
- while (!PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);//等待定位反馈
- if (IsCancel) break;
- RunLog("定位完成");
- PlcServer.GetInstance.WriteData("M20.0", false);
- while (PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);
- if (IsCancel) break;
- RunLog("定位完成,复位成功");
- foreach (var temp1 in recipeInfo.SiloInfoModels)
- {
- var rest = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(temp1.SiloName)) != null);
- if (rest != null && rest.OutletLoc == temp)
- {
- var res = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloName == temp1.SiloName);
- if (res != null)
- {
- StatusNotify(res.SiloName, Status.正在配料);
- RunLog($"写重量地址:{GetWeightAdd(res.SiloLoc)},重量:{temp1.SiloWeight * 10}");
- PlcServer.GetInstance.WriteData(GetWeightAdd(res.SiloLoc), (ushort)(temp1.SiloWeight * 10));
- RunLog($"写启动信号地址:{GetStartSingleAdd(res.SiloLoc)}");
- PlcServer.GetInstance.WriteData(GetStartSingleAdd(res.SiloLoc), true);
- while (!PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成
- if (IsCancel) break;
- RunLog("出料完成,复位出料完成状态");
- PlcServer.GetInstance.WriteData("M4.0", false);//复位出料完成信号
- while (PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成信号复位成功
- if (IsCancel) break;
- RunLog("出料完成复位成功");
- StatusNotify(res.SiloName, Status.配料完成);
- }
- }
- }
- if (IsCancel) return;
- }
- }
-
- if (!IsCancel)
- {
- RunLog($"写入配方执行完成信号");
- PlcServer.GetInstance.WriteData("M10.4", true);
- RunLog("等待配方执行完成");
- while (!PlcDataModel.RecipeBatchingComplete && !IsCancel) Thread.Sleep(1);
- if (IsCancel) return;
- RunLog($"【{recipeInfo.RecipeName}】配方执行完成");
- ActionManage.GetInstance.Send("GrindArenaceousCancel");
- }
-
-
- // IsCancel = false;
- // Dictionary<int, List<int>> DeviceSoilInfo = new Dictionary<int, List<int>>();
- // List<int> OutletInfo = new List<int>();
- // recipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
- // {
- // var res = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s == item.SiloName) != null);
- // if (res != null)
- // {
- // var soliInfo = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloName == item.SiloName);
- // if (soliInfo != null)
- // {
- // if (!DeviceSoilInfo.ContainsKey(res.OutletLoc))
- // {
- // DeviceSoilInfo.Add(res.OutletLoc, new List<int>());
- // DeviceSoilInfo[res.OutletLoc].Add(soliInfo.SiloLoc);
- // }
- // else
- // {
- // DeviceSoilInfo[res.OutletLoc].Add(soliInfo.SiloLoc);
- // }
- // }
-
- // }
- // });
-
- // foreach (var temp in DeviceSoilInfo)
- // {
- // PlcServer.GetInstance.WriteData("VW302", (ushort)temp.Key);//设置出料口位置
- //#if ModelSwitch
- // PlcServer.GetInstance.WriteData("M10.4", true);//定位启动
- //#else
- // PlcServer.GetInstance.WriteData("M10.0", true);//定位启动
- //#endif
-
- // RunLog($"启动定位,出料口位置:{temp.Key}");
- // int index = temp.Key - 1;
- // if (index >= 0)
- // {
- // RunLog("等待定位完成");
- // while (!PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);//等待定位反馈
- // if (IsCancel) break;
- // RunLog("定位完成");
- // PlcServer.GetInstance.WriteData("M20.0", false);
- // while (PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);
- // if (IsCancel) break;
- // RunLog("定位完成,复位成功");
- // foreach (var temp1 in temp.Value)
- // {
- // //var rest = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(temp1.SiloName)) != null);
- // //if (rest != null && rest.OutletLoc == temp)
- // //{
-
- // var res = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloLoc == temp1);
- // if (res != null)
- // {
- // var tempRecipe = recipeInfo.SiloInfoModels.FirstOrDefault(p => p.SiloName == res.SiloName);
- // if (tempRecipe != null)
- // {
- // StatusNotify(res.SiloName, Status.正在配料);
- // RunLog($"写重量地址:{GetWeightAdd(res.SiloLoc)},重量:{tempRecipe.SiloWeight}");
- // PlcServer.GetInstance.WriteData(GetWeightAdd(res.SiloLoc), (ushort)(tempRecipe.SiloWeight * 10));
- // RunLog($"写启动信号地址:{GetStartSingleAdd(res.SiloLoc)}");
- // PlcServer.GetInstance.WriteData(GetStartSingleAdd(res.SiloLoc), true);
- // while (!PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成
- // if (IsCancel) break;
- // RunLog("出料完成,复位出料完成状态");
- //#if ModelSwitch
- // PlcServer.GetInstance.WriteData("M10.0", false);//复位出料完成信号
- //#else
- // PlcServer.GetInstance.WriteData("M4.0", false);//复位出料完成信号
- //#endif
- // while (PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成信号复位成功
- // if (IsCancel) break;
- // RunLog("出料完成复位成功");
- // StatusNotify(res.SiloName, Status.配料完成);
- // }
- // }
- // //}
- // }
- // if (IsCancel) return;
- // }
- // }
-
-
-
-
-
-
-
- }
-
- /// <summary>
- /// 计时配料
- /// </summary>
- /// <param name="recipeInfo"></param>
- private void ProcessTwo(RecipeInfo recipeInfo)
- {
- IsCancel = false;
- Dictionary<int, List<int>> DeviceSoilInfo = new Dictionary<int, List<int>>();
- List<int> OutletInfo = new List<int>();
- recipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
- {
- var res = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s == item.SiloName) != null);
- if (res != null)
- {
- var soliInfo = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloName == item.SiloName);
- if (soliInfo != null)
- {
- if (!DeviceSoilInfo.ContainsKey(res.OutletLoc))
- {
- DeviceSoilInfo.Add(res.OutletLoc, new List<int>());
- DeviceSoilInfo[res.OutletLoc].Add(soliInfo.SiloLoc);
- }
- else
- {
- DeviceSoilInfo[res.OutletLoc].Add(soliInfo.SiloLoc);
- }
- }
-
- }
- });
-
- foreach (var temp in DeviceSoilInfo)
- {
- PlcServer.GetInstance.WriteData("VW302", (ushort)temp.Key);//设置出料口位置
- //PlcServer.GetInstance.WriteData("M10.0", true);//定位启动
- PlcServer.GetInstance.WriteData("M10.4", true);//定位启动
- RunLog($"启动定位,出料口位置:{temp}");
- int index = temp.Key - 1;
- if (index >= 0)
- {
- RunLog("等待定位完成");
- while (!PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);//等待定位反馈
- if (IsCancel) break;
- RunLog("定位完成");
- PlcServer.GetInstance.WriteData("M20.0", false);
- while (PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);
- if (IsCancel) break;
- RunLog("定位完成,复位成功");
- foreach (var temp1 in temp.Value)
- {
- //var rest = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(temp1.SiloName)) != null);
- //if (rest != null && rest.OutletLoc == temp)
- //{
-
- var res = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloLoc == temp1);
- if (res != null)
- {
- var tempRecipe = recipeInfo.SiloInfoModels.FirstOrDefault(p => p.SiloName == res.SiloName);
- if (tempRecipe != null)
- {
- StatusNotify(res.SiloName, Status.正在配料);
- RunLog($"写重量地址:{GetWeightAdd(res.SiloLoc)},重量:{tempRecipe.SiloWeight}");
- PlcServer.GetInstance.WriteData(GetWeightAdd(res.SiloLoc), (ushort)(tempRecipe.SiloWeight * 10));
- RunLog($"写启动信号地址:{GetStartSingleAdd(res.SiloLoc)}");
- PlcServer.GetInstance.WriteData(GetStartSingleAdd(res.SiloLoc), true);
- while (!PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成
- if (IsCancel) break;
- RunLog("出料完成,复位出料完成状态");
- #if ModelSwitch
- PlcServer.GetInstance.WriteData("M10.0", false);//复位出料完成信号
- #else
- PlcServer.GetInstance.WriteData("M4.0", false);//复位出料完成信号
- #endif
-
- while (PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成信号复位成功
- if (IsCancel) break;
- RunLog("出料完成复位成功");
- StatusNotify(res.SiloName, Status.配料完成);
- }
- }
- //}
- }
- if (IsCancel) return;
- }
- }
-
- //IsCancel = false;
- //List<int> OutletInfo = new List<int>();
- //recipeInfo.SiloInfoModels.ToList()?.ForEach(item =>
- //{
- // var res = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(item.SiloName)) != null);
- // if (res != null) if (!OutletInfo.Contains(res.OutletLoc)) OutletInfo.Add(res.OutletLoc);
- //});
- //foreach (var temp in OutletInfo)
- //{
- // PlcServer.GetInstance.WriteData("VW302", (ushort)temp);//设置出料口位置
- // //PlcServer.GetInstance.WriteData("M10.0", true);//定位启动
- // PlcServer.GetInstance.WriteData("M10.4", true);//定位启动
- // RunLog($"启动定位,出料口位置:{temp}");
- // int index = temp - 1;
- // if (index >= 0)
- // {
- // RunLog("等待定位完成");
- // while (!PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);//等待定位反馈
- // if (IsCancel) break;
- // RunLog("定位完成");
- // PlcServer.GetInstance.WriteData("M20.0", false);
- // while (PlcDataModel.TargetLocComplete && !IsCancel) Thread.Sleep(1);
- // if (IsCancel) break;
- // RunLog("定位完成,复位成功");
- // foreach (var temp1 in recipeInfo.SiloInfoModels)
- // {
- // var rest = Json<ConfigInfoModel>.Data.OutletInfoModels.FirstOrDefault(p => p.SiloInfos.FirstOrDefault(s => s.Contains(temp1.SiloName)) != null);
- // if (rest != null && rest.OutletLoc == temp)
- // {
- // var res = Json<ConfigInfoModel>.Data.SiloInfoModels.FirstOrDefault(p => p.SiloName == temp1.SiloName);
- // if (res != null)
- // {
- // StatusNotify(res.SiloName, Status.正在配料);
- // RunLog($"写重量地址:{GetWeightAdd(res.SiloLoc)},重量:{temp1.SiloWeight}");
- // PlcServer.GetInstance.WriteData(GetWeightAdd(res.SiloLoc), (ushort)(temp1.SiloWeight * 10));
- // RunLog($"写启动信号地址:{GetStartSingleAdd(res.SiloLoc)}");
- // PlcServer.GetInstance.WriteData(GetStartSingleAdd(res.SiloLoc), true);
- // while (!PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成
- // if (IsCancel) break;
- // RunLog("出料完成,复位出料完成状态");
- //#if test
- // PlcServer.GetInstance.WriteData("M10.0", false);//复位出料完成信号
- //#else
- //PlcServer.GetInstance.WriteData("M4.0", false);//复位出料完成信号
- //#endif
-
-
- // while (PlcDataModel.BatchingCompleted && !IsCancel) Thread.Sleep(1);//等待出料完成信号复位成功
- // if (IsCancel) break;
- // RunLog("出料完成复位成功");
- // StatusNotify(res.SiloName, Status.配料完成);
- // }
- // }
- // }
- // if (IsCancel) return;
- // }
- //}
-
- if (!IsCancel)
- {
- RunLog($"写入配方执行完成信号");
- //PlcServer.GetInstance.WriteData("M10.4", true);
- PlcServer.GetInstance.WriteData("M10.5", true);
- RunLog("等待配方执行完成");
- while (!PlcDataModel.RecipeBatchingComplete && !IsCancel) Thread.Sleep(1);
- if (IsCancel) return;
- RunLog($"【{recipeInfo.RecipeName}】配方执行完成");
- ActionManage.GetInstance.Send("GrindArenaceousCancel");
- }
- }
-
- /// <summary>
- /// 获取重量设置地址
- /// </summary>
- /// <param name="num"></param>
- /// <returns></returns>
- private string GetWeightAdd(int num)
- {
- #if ModelSwitch
- if (num > 0)
- {
- int add = 0;
- if (num >= 1 && num <= 18)
- {
- add = 200 + (num - 1) * 2;
- }
- return $"VW{add}";
- }
- return default;
- #else
- if (num > 0)
- {
- int add = 0;
- if (num >= 1 && num <= 8)
- {
- add = 100 + (num - 1) * 2;
- }
- else if (num >= 9 && num <= 18)
- {
- add = 102 + (num - 1) * 2;
- }
- return $"VW{add}";
- }
- return default;
- #endif
-
- }
-
- private void StatusNotify(string SiloName, Status Status)
- {
- ActionManage.GetInstance.Send("StatusNotify", new RecipeStatus() { RawMaterialName = SiloName, Status = Status.ToString() });
- }
-
- public void RunLog(string info)
- {
- BPA.Helper.MessageLog.GetInstance.Show(info);
- BPASmartClient.CustomResource.Pages.Model.MessageNotify.GetInstance.ShowRunLog(info);
- Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
- }
-
- public void OperationLog(string info)
- {
- BPA.Helper.MessageLog.GetInstance.Show(info);
- BPASmartClient.CustomResource.Pages.Model.MessageNotify.GetInstance.ShowUserLog(info);
- Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
- }
-
- public void DebugLog(string info)
- {
- BPASmartClient.Message.MessageLog.GetInstance.DebugLog(info);
- }
-
- public void NotifyPrompt(string info)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", info);
- });
- }
-
- /// <summary>
- /// 获取启动信号地址
- /// </summary>
- /// <param name="num"></param>
- /// <returns></returns>
- private string GetStartSingleAdd(int num)
- {
- #if ModelSwitch
- if (num > 0)
- {
- string Add = string.Empty;
- var t = num / 8;
- var c = (num % 8);
- if (c == 0)
- {
- t--;
- c = 7;
- }
- else c--;
- Add = $"M{t + 4}.{c}";
- return Add;
- }
- return default;
- #else
- if (num > 0)
- {
- string Add = string.Empty;
- var t = num / 8;
- var c = (num % 8);
- if (c == 0)
- {
- t--;
- c = 7;
- }
- else c--;
- Add = $"M{t}.{c}";
- return Add;
- }
- return default;
- #endif
-
- }
-
- }
- }
|