| | |
| | | using System.Diagnostics; |
| | | using System.Text; |
| | | using Microsoft.Data.SqlClient; |
| | | using Newtonsoft.Json; |
| | | using WIDESEA_Core.Enums; |
| | | using WIDESEAWCS_Common.AGVEnum; |
| | | using WIDESEAWCS_Common.TaskEnum; |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_Core.Tool; |
| | | using WIDESEAWCS_DTO.TaskInfo; |
| | | using WIDESEAWCS_ISystemRepository; |
| | | using WIDESEAWCS_ITaskInfoRepository; |
| | | using WIDESEAWCS_Model.Models; |
| | | using WIDESEAWCS_QuartzJob; |
| | | |
| | | namespace WIDESEAWCS_Tasks.AGVJob |
| | | namespace WIDESEAWCS_Tasks |
| | | { |
| | | public class SendTaskAGV |
| | | { |
| | | private static int _readSendAGVTaskSignalso = 0; |
| | | private static bool isTrue1 = false; |
| | | private static string name1 = ""; |
| | | |
| | | public static void SendAGVTask1(AGV agv, ITaskRepository _taskRepository) |
| | | public static string url = AppSettings.Configuration["AGVTaskGeneration"]; |
| | | public static void SendAGVTask1(ITaskRepository _taskRepository, IDt_StationinfoRepository _stationinfoRepository) |
| | | { |
| | | //TODO: Implement sending task to AGV |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | if (agv == null) |
| | | Dt_Task task = _taskRepository.QueryData(x => x.TaskState == (int)TaskInStatusEnum.InNew).OrderBy(r => r.CreateDate).FirstOrDefault(); //.ThenByDescending(r => r.Grade) |
| | | if (task != null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var taskInteractiveR = agv.DeviceProDTOs.Where(r => r.DeviceProParamName == TaskDBName.taskInteractiveR.ToString()).FirstOrDefault().DeviceProAddress; |
| | | var taskInteractiveW = agv.DeviceProDTOs.Where(r => r.DeviceProParamName == TaskDBName.taskInteractiveW.ToString()).FirstOrDefault().DeviceProAddress; |
| | | var resetTaskW = agv.DeviceProDTOs.Where(r => r.DeviceProParamName == TaskDBName.resetTaskInteractiveW.ToString()).FirstOrDefault().DeviceProAddress; |
| | | var resetTaskInteractiveR = agv.DeviceProDTOs.Where(r => r.DeviceProParamName == TaskDBName.resetTaskInteractiveR.ToString()).FirstOrDefault().DeviceProAddress; |
| | | var taskID = agv.DeviceProDTOs.Where(r => r.DeviceProParamName == TaskDBName.taskID.ToString()).FirstOrDefault().DeviceProAddress; |
| | | |
| | | int TaskInteractive = agv.Communicator.Read<int>(taskInteractiveR);//1æ¶å° |
| | | int TaskInteractiveW = agv.Communicator.Read<int>(taskInteractiveW);//0åå§/1ä¸å/2åæ¶/3æ´æ¹ |
| | | int resetTaskInteractiveW = agv.Communicator.Read<int>(resetTaskW); |
| | | int resetTaskInteractiver = agv.Communicator.Read<int>(resetTaskInteractiveR); |
| | | if (isTrue1 && name1 == agv.DeviceName) |
| | | { |
| | | if (TaskInteractiveW != 0 || resetTaskInteractiveW != 0 || TaskInteractive != 0) |
| | | TaskGenerationDto taskGenerationDto = new TaskGenerationDto(); |
| | | { |
| | | agv.Communicator.Write(resetTaskW, 1); |
| | | Task.Delay(2000).Wait(); |
| | | resetTaskInteractiveW = agv.Communicator.Read<int>(resetTaskW); |
| | | if (resetTaskInteractiveW == 1) |
| | | { |
| | | agv.Communicator.Write(resetTaskW, 0); |
| | | agv.Communicator.Write(taskInteractiveW, 0); |
| | | Task.Delay(2000).Wait(); |
| | | } |
| | | return; |
| | | taskGenerationDto.taskid = task.TaskNum.ToString(); |
| | | taskGenerationDto.taskType = task.TaskType.ToString(); |
| | | taskGenerationDto.floor = task.Floor.ToString(); |
| | | taskGenerationDto.param1 = task.SourceAddress.ToString(); |
| | | taskGenerationDto.param2 = task.TargetAddress.ToString(); |
| | | taskGenerationDto.param3 = ""; |
| | | taskGenerationDto.param4 = ""; |
| | | taskGenerationDto.param5 = ""; |
| | | taskGenerationDto.param6 = ""; |
| | | taskGenerationDto.param7 = ""; |
| | | taskGenerationDto.param8 = ""; |
| | | taskGenerationDto.param9 = ""; |
| | | } |
| | | var ResultData = HttpHelper.Post(url, taskGenerationDto.ToJson(), headers: new Dictionary<string, string>()); |
| | | var res = JsonConvert.DeserializeObject<WebResponseContent>(ResultData); |
| | | if (res.code == 200) |
| | | { |
| | | WriteLog.Info("ä»»å¡ä¸åæå").Write($"ä»»å¡å·{task.TaskNum}", "ä»»å¡ä¸åæå"); |
| | | int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskInStatusEnum>(); |
| | | task.TaskState = nextStatus; |
| | | task.Dispatchertime = DateTime.Now; |
| | | _taskRepository.UpdateData(task); |
| | | } |
| | | else |
| | | { |
| | | isTrue1 = false; |
| | | name1 = ""; |
| | | WriteLog.Info("ä»»å¡ä¸å失败").Write($"ä»»å¡å·{task.TaskNum}ï¼ä¿¡æ¯{res.Message}", "ä»»å¡ä¸å失败"); |
| | | //_unitOfWorkManage.RollbackTran(); |
| | | content = WebResponseContent.Instance.Error($"{res.Message}"); |
| | | } |
| | | } |
| | | if (resetTaskInteractiver == 1) |
| | | Thread.Sleep(100); |
| | | Dt_Task dt_Task = _taskRepository.QueryData(x => x.TaskState == (int)TaskInStatusEnum.AGV_Queue).OrderBy(r => r.CreateDate).FirstOrDefault(); |
| | | if (dt_Task != null) |
| | | { |
| | | agv.Communicator.Write(taskInteractiveW, 0); |
| | | for (int i = 0; i < 5; i++) |
| | | var station = _stationinfoRepository.QueryFirst(x => x.StationCode == dt_Task.SourceAddress); |
| | | // æ¥æ¾ä¸¤ä¸ªå¯ç¨ç空ç¼åæ¶ |
| | | var freeStations = _stationinfoRepository |
| | | .QueryData(x => x.Location_state == LocationStatusEnum.Free.ObjToInt() && x.Enable && x.Remark == station.Remark && x.Area == station.Area)// && x.Stationtype == station.Stationtype |
| | | .Take(2) // åªåå两个 |
| | | .ToList(); |
| | | |
| | | int floor = 0; |
| | | if (station.Area.Contains("Aåº")) |
| | | { |
| | | Thread.Sleep(300); |
| | | var agvnumber = Convert.ToInt32(agv.Communicator.Read<int>(taskInteractiveW)); |
| | | if (agvnumber != 0) |
| | | { |
| | | agv.Communicator.Write(taskInteractiveW, 0); |
| | | } |
| | | else |
| | | { |
| | | break; |
| | | } |
| | | floor = 32; |
| | | } |
| | | } |
| | | if (0 == TaskInteractive && TaskInteractiveW == 0) |
| | | { |
| | | Dt_Task agvTask = _taskRepository.QueryData(r => r.TaskState == (int)TaskInStatusEnum.InNew && r.Roadway == agv.DeviceName && r.TaskType == (int)TaskOutboundTypeEnum.Outbound).OrderBy(r => r.CreateDate).OrderByDescending(r => r.Grade).FirstOrDefault(); |
| | | if (null != agvTask) |
| | | else |
| | | { |
| | | //åå
¥ä»»å¡ä¿¡æ¯ |
| | | string rel = TaskWrite(agv, agvTask, AGVJobEnum.newTaskEnum); |
| | | Thread.Sleep(1000); |
| | | string taskId = agv.Communicator.Read<string>(taskID).ToString(); |
| | | if (taskId != agvTask.TaskNum.ToString()) |
| | | { |
| | | isTrue1 = true; |
| | | name1 = agv.DeviceName; |
| | | //WriteLog.Info("SendAGVTask").Write("ç»AGVåå
¥ä»»å¡å¤±è´¥," + agvTask.agv_tasknum + DateTime.Now, "SendAGVTask"); |
| | | throw new Exception(string.Format("ç»AGVåå
¥ä»»å¡å¤±è´¥,ä»»å¡å·{0}", agvTask.TaskNum)); |
| | | } |
| | | if (string.IsNullOrEmpty(rel)) |
| | | { |
| | | //åå
¥ä»»å¡ç¡®è®¤1 |
| | | agv.Communicator.Write(taskInteractiveW, 1); |
| | | Thread.Sleep(1000); |
| | | //读åAGVä¿¡å· |
| | | int ReadTask_1_OK = agv.Communicator.Read<int>(taskInteractiveR); |
| | | if (ReadTask_1_OK == 1) |
| | | { |
| | | //åå
¥WCS确认信å·0 |
| | | agv.Communicator.Write(taskInteractiveW, 0); |
| | | Thread.Sleep(2000); |
| | | //读åAGVä¿¡å·æ¯å¦ä¸º0 |
| | | int ReadTask_0_OK = agv.Communicator.Read<int>(taskInteractiveR); |
| | | if (ReadTask_0_OK == 0) |
| | | { |
| | | int nextStatus = agvTask.TaskState.GetNextNotCompletedStatus<TaskInStatusEnum>(); |
| | | agvTask.TaskState = nextStatus; |
| | | agvTask.Dispatchertime = DateTime.Now; |
| | | _taskRepository.UpdateData(agvTask); |
| | | // ä¸ä¼ AGVè¿è¡æ°æ® by xiaoyang |
| | | //SendMESTask.SendMesTask(agvTask, 0); |
| | | } |
| | | else |
| | | { |
| | | isTrue1 = true; |
| | | name1 = agv.DeviceName; |
| | | throw new Exception(string.Format("ç»AGVåå
¥ä»»å¡ç¡®è®¤0失败,ä»»å¡å·{0}", agvTask.TaskNum)); |
| | | } |
| | | } |
| | | else if (ReadTask_1_OK == 11) |
| | | { |
| | | agv.Communicator.Write(taskInteractiveW, 0); |
| | | Thread.Sleep(2000); |
| | | int ReadTask_0_Error = agv.Communicator.Read<int>(taskInteractiveR); |
| | | if (ReadTask_0_Error == 0) |
| | | { |
| | | int nextStatus = agvTask.TaskState.GetNextNotCompletedStatus<TaskOutStatusEnum>(); |
| | | agvTask.TaskState = nextStatus; |
| | | agvTask.Dispatchertime = DateTime.Now; |
| | | _taskRepository.UpdateData(agvTask); |
| | | } |
| | | else |
| | | { |
| | | isTrue1 = true; |
| | | name1 = agv.DeviceName; |
| | | throw new Exception(string.Format("ç»AGVåå
¥ä»»å¡å¼å¸¸ç¡®è®¤0失败,ä»»å¡å·{0}", agvTask.TaskNum)); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | isTrue1 = true; |
| | | name1 = agv.DeviceName; |
| | | throw new Exception(string.Format("ç»AGVåå
¥ä»»å¡ç¡®è®¤1失败,ä»»å¡å·{0}", agvTask.TaskNum)); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | isTrue1 = true; |
| | | name1 = agv.DeviceName; |
| | | throw new Exception(string.Format("ç»AGVåå
¥ä»»å¡å¤±è´¥,ä»»å¡å·{0}" + rel, agvTask.TaskNum)); |
| | | } |
| | | floor = 128; |
| | | } |
| | | if (freeStations.Count < 2) |
| | | { |
| | | throw new Exception("æªæ¾å°ä¸¤ä¸ªå¯ç¨ç空ç¼åæ¶"); |
| | | } |
| | | // æ£æ¥è¿ä¸¤ä¸ªç¼åæ¶æ¯å¦è¢«ä»»å¡å ç¨ |
| | | var stationCodes = freeStations.Select(s => s.StationCode).ToList(); |
| | | freeStations[0].Location_state = LocationStatusEnum.Lock.ObjToInt(); |
| | | _stationinfoRepository.UpdateData(freeStations[0]); |
| | | freeStations[1].Location_state = LocationStatusEnum.Lock.ObjToInt(); |
| | | _stationinfoRepository.UpdateData(freeStations[1]); |
| | | //stationCodes. |
| | | var tasks = _taskRepository.QueryData(x => stationCodes.Contains(x.SourceAddress) || stationCodes.Contains(x.TargetAddress)).ToList(); |
| | | if (tasks.Any()) |
| | | { |
| | | throw new Exception("ç¼åæ¶å·²æä»»å¡"); |
| | | } |
| | | dt_Task.TargetAddress = string.Join("||", freeStations.Select(s => s.StationCode)); |
| | | dt_Task.NextAddress = string.Join("||", freeStations.Select(s => s.StationCode)); |
| | | dt_Task.TaskState = (int)TaskInStatusEnum.InNew; |
| | | dt_Task.TaskType = floor; |
| | | _taskRepository.UpdateData(dt_Task); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | StackTrace sta = new StackTrace(ex, true); |
| | | StackTrace st = new StackTrace(new StackFrame(true)); |
| | | StackFrame sf = sta.GetFrame(0); |
| | | //WriteLog.Info("SendAGVTask").Write(ex.Message + "è¡å·" + sf.GetFileLineNumber(), "SendAGVTask"); |
| | | |
| | | } |
| | | finally |
| | | { |
| | | // WriteLog.Info("SendAGVTask").Write(agv.PLCName+"\t"+DateTime.Now, "SendAGVTask"); |
| | | Interlocked.Exchange(ref _readSendAGVTaskSignalso, 0); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void SendAGVTask(AGV agv, ITaskRepository _taskRepository) |
| | | { |
| | | |
| | | // è¿æ¥å符串 - æ ¹æ®ä½ çæå¡å¨ä¿¡æ¯ä¿®æ¹ |
| | | string connectionString = "Data Source=.;Initial Catalog=WIDESEAWCS_GanFengLiYeNew;User ID=sa;Password=sa123456;Integrated Security=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; |
| | | List<Dt_Task> dt_Tasks = _taskRepository.QueryData(x => x.TaskState == (int)TaskInStatusEnum.InNew); |
| | | foreach (Dt_Task task in dt_Tasks) |
| | | { |
| | | // è¦æå
¥çSQLè¯å¥ |
| | | string insertSql = "INSERT INTO Dt_Stationinfo (StationCode, Location_state,Area,Enable,LastUpdateTime,Remark) VALUES (@StationCode, @Location_state,@Area,@Enable,@LastUpdateTime,@Remark)"; |
| | | |
| | | try |
| | | { |
| | | using (SqlConnection connection = new SqlConnection(connectionString)) |
| | | { |
| | | connection.Open(); |
| | | |
| | | using (SqlCommand command = new SqlCommand(insertSql, connection)) |
| | | { |
| | | // æ·»å 忰鲿¢SQL注å
¥ |
| | | command.Parameters.AddWithValue("StationCode", task.TaskNum); |
| | | command.Parameters.AddWithValue("Location_state", task.TaskNum); |
| | | command.Parameters.AddWithValue("Area", task.Roadway); |
| | | command.Parameters.AddWithValue("Enable", task.TaskNum); |
| | | command.Parameters.AddWithValue("LastUpdateTime", DateTime.Now); |
| | | command.Parameters.AddWithValue("Remark", task.Roadway); |
| | | command.Parameters.AddWithValue("Creater", "WCS"); |
| | | command.Parameters.AddWithValue("CreateDate", DateTime.Now); |
| | | |
| | | int rowsAffected = command.ExecuteNonQuery(); |
| | | Console.WriteLine($"æåæå
¥ {rowsAffected} è¡æ°æ®"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"åçé误: {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | // æ£æ¥AGVæ¯å¦ä¸ºç©º |
| | | if (agv == null) |
| | | { |
| | |
| | | try |
| | | { |
| | | // 读åä»»å¡äº¤äºåé置交äºçç¶æ |
| | | int taskInteractive = agv.Communicator.Read<int>(taskInteractiveR); // 1表示æ¶å° |
| | | int taskInteractiveW = agv.Communicator.Read<int>(taskInteractiveW1); // 0åå§/1ä¸å/2åæ¶/3æ´æ¹ |
| | | int resetTaskInteractiveW = agv.Communicator.Read<int>(resetTaskW); |
| | | int resetTaskInteractiver = agv.Communicator.Read<int>(resetTaskInteractiveR); |
| | | int taskInteractive = agv.Communicator.Read<Int16>(taskInteractiveR); // 1表示æ¶å° |
| | | int taskInteractiveW = agv.Communicator.Read<Int16>(taskInteractiveW1); // 0åå§/1ä¸å/2åæ¶/3æ´æ¹ |
| | | int resetTaskInteractiveW = agv.Communicator.Read<Int16>(resetTaskW); |
| | | int resetTaskInteractiver = agv.Communicator.Read<Int16>(resetTaskInteractiveR); |
| | | |
| | | // å¤çä»»å¡äº¤äºWåé置交äºWä¸ä¸º0çæ
åµ |
| | | if (isTrue1 && name1 == agv.DeviceName) |
| | | { |
| | | if (taskInteractiveW != 0 || resetTaskInteractiveW != 0 || taskInteractive != 0) |
| | | { |
| | | agv.Communicator.Write(resetTaskW, 1); |
| | | agv.Communicator.Write<UInt16>(resetTaskW, 1); |
| | | Task.Delay(2000).Wait(); |
| | | resetTaskInteractiveW = agv.Communicator.Read<int>(resetTaskW); |
| | | resetTaskInteractiveW = agv.Communicator.Read<Int16>(resetTaskW); |
| | | |
| | | if (resetTaskInteractiveW == 1) |
| | | { |
| | | agv.Communicator.Write(resetTaskW, 0); |
| | | agv.Communicator.Write(taskInteractiveW1, 0); |
| | | agv.Communicator.Write<UInt16>(resetTaskW, 0); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 0); |
| | | Task.Delay(2000).Wait(); |
| | | } |
| | | return; |
| | |
| | | // 妿é置交äºR为1ï¼åé置任å¡äº¤äºW |
| | | if (resetTaskInteractiver == 1) |
| | | { |
| | | agv.Communicator.Write(taskInteractiveW1, 0); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 0); |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | Task.Delay(300).Wait(); // ç¨Task.Delay代æ¿Thread.Sleepï¼ä»¥é¿å
线ç¨é»å¡ |
| | | int agvNumber = agv.Communicator.Read<int>(taskInteractiveW1); |
| | | int agvNumber = agv.Communicator.Read<Int16>(taskInteractiveW1); |
| | | if (agvNumber != 0) |
| | | { |
| | | agv.Communicator.Write(taskInteractiveW1, 0); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 0); |
| | | } |
| | | else |
| | | { |
| | |
| | | |
| | | // æ£æ¥ä»»å¡IDæ¯å¦åå
¥æ£ç¡® |
| | | string taskId = agv.Communicator.Read<string>(taskID); |
| | | if (taskId != agvTask.TaskNum.ToString()) |
| | | string numberPart = taskId.Split('-')[1]; |
| | | if (numberPart != agvTask.TaskNum.ToString()) |
| | | { |
| | | throw new Exception($"ç»AGVåå
¥ä»»å¡å¤±è´¥,ä»»å¡å·{agvTask.TaskNum}"); |
| | | } |
| | |
| | | if (string.IsNullOrEmpty(rel)) |
| | | { |
| | | // åå
¥ä»»å¡ç¡®è®¤ä¿¡å·1 |
| | | agv.Communicator.Write(taskInteractiveW1, 1); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 1); |
| | | Task.Delay(1000).Wait(); |
| | | |
| | | // 读åAGVçç¡®è®¤ä¿¡å· |
| | | int readTask1Ok = agv.Communicator.Read<int>(taskInteractiveR); |
| | | int readTask1Ok = agv.Communicator.Read<Int16>(taskInteractiveR); |
| | | if (readTask1Ok == 1) |
| | | { |
| | | // åå
¥WCSç确认信å·0 |
| | | agv.Communicator.Write(taskInteractiveW1, 0); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 0); |
| | | Task.Delay(2000).Wait(); |
| | | |
| | | // 忬¡è¯»åAGVçç¡®è®¤ä¿¡å· |
| | | int readTask0Ok = agv.Communicator.Read<int>(taskInteractiveR); |
| | | int readTask0Ok = agv.Communicator.Read<Int16>(taskInteractiveR); |
| | | if (readTask0Ok == 0) |
| | | { |
| | | // æ´æ°ä»»å¡ç¶æå¹¶ä¿å |
| | |
| | | else if (readTask1Ok == 11) |
| | | { |
| | | // å¤çä»»å¡å¼å¸¸æ
åµ |
| | | agv.Communicator.Write(taskInteractiveW1, 0); |
| | | agv.Communicator.Write<UInt16>(taskInteractiveW1, 0); |
| | | Task.Delay(2000).Wait(); |
| | | int readTask0Error = agv.Communicator.Read<int>(taskInteractiveR); |
| | | |
| | |
| | | public static string TaskWrite(AGV client, Dt_Task agvTask, AGVJobEnum jobEnum) |
| | | { |
| | | List<byte> sendData = new List<byte>(); |
| | | string tmp = agvTask.TaskNum.ToString(); |
| | | string tmp = "KH-" + agvTask.TaskNum.ToString(); |
| | | List<byte> taskID = Encoding.Default.GetBytes(tmp).ToList();//ä»»å¡ID |
| | | taskID.Insert(0, 30); |
| | | taskID.Insert(1, Convert.ToByte(tmp.Length)); |
| | |
| | | Array.Reverse(taskType); |
| | | sendData.AddRange(taskType); |
| | | |
| | | List<byte> containerType = Encoding.Default.GetBytes(agvTask.PalletCode).ToList(); |
| | | List<byte> containerType = Encoding.Default.GetBytes(agvTask.Barcode).ToList(); |
| | | containerType.Insert(0, 30); |
| | | containerType.Insert(1, Convert.ToByte(agvTask.PalletCode.Length)); |
| | | containerType.Insert(1, Convert.ToByte(agvTask.Barcode.Length)); |
| | | |
| | | for (int i = 0; i < 30 - agvTask.PalletCode.Length; i++) |
| | | for (int i = 0; i < 30 - agvTask.Barcode.Length; i++) |
| | | { |
| | | containerType.Add(0); |
| | | } |