wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using HslCommunication;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.DBHelper;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_WCS.EquipBaseInfo;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.SchedulerExecute.AGV
{
    public partial class AGVSchedulerExecute
    {
        private static int _readSendAGVTaskSignalso = 0;
        private static bool isTrue1 = false;
        private static string name1 = "";
        public static void SendAGVTask1(PLCClient plcClient)
        {
 
            try
            {
                if (plcClient == null) { return; }
 
                VOLContext context = new VOLContext();
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                Ibase_ware_locationRepository locationRepository = new base_ware_locationRepository(context);
                int TaskInteractive = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveR.ToString()));//1收到
                int TaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString()));//0初始/1下发/2取消/3更改
                int resetTaskInteractiveW = Convert.ToInt32(plcClient.ReadValue(TaskDBName.resetTaskInteractiveW.ToString()));
 
                if (TaskInteractive == 0 && TaskInteractiveW == 0)
                {
                    dt_agvtask agvTask = agvtaskRepository.Find(r => r.agv_taskstate == "Create" && r.agv_code == plcClient.PLCName && r.agv_tasktype.Contains("TaskType_Outbound")).OrderBy(r => r.agv_createtime).OrderByDescending(r => r.agv_grade).FirstOrDefault();
                    if (null != agvTask)
                    {
                        //写入任务信息
                        string rel = AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum);
 
                        if (string.IsNullOrEmpty(rel))
                        {
                            Thread.Sleep(500);
                            for (int i = 0; i < 5; i++)
                            {
                                Thread.Sleep(200);
                                string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString();
                                if (taskId != agvTask.agv_tasknum)
                                {
                                    if (i < 4)
                                        AGVBaseInfo.TaskWrite(plcClient, agvTask, AGVJobEnum.newTaskEnum);
                                    else
                                        return;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            //写入任务确认1
                            bool taskInteractiveW = plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1);
                            for (int i = 0; i < 5; i++)
                            {
                                Thread.Sleep(200);
                                object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString());
                                if (readValue != null && readValue.ToString() == "1")
                                {
                                    break;
                                }
                                else
                                {
                                    plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 1);
                                }
                            }
                        }
                    }
                }
                else if (TaskInteractive == 11 && TaskInteractiveW == 1)
                {
                    plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                    for (int i = 0; i < 5; i++)
                    {
                        Thread.Sleep(200);
                        object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString());
                        if (readValue != null && readValue.ToString() == "0")
                        {
                            break;
                        }
                        else
                        {
                            plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                        }
                    }
                }
                else if (1 == TaskInteractive && TaskInteractiveW == 1)
                {
                    string taskId = plcClient.ReadValue(TaskDBName.taskID.ToString()).ToString();
                    var agvTask = agvtaskRepository.FindFirst(x => x.agv_tasknum == taskId);
                    if (agvTask != null)
                    {
                        if (agvTask.agv_taskstate != AGVTaskStateEnum.SendOut.ToString())
                            agvTask.agv_taskstate = AGVTaskStateEnum.SendOut.ToString();
                        agvTask.agv_realesstime = DateTime.Now;
                        agvtaskRepository.Update(agvTask, true);
                        plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                        for (int i = 0; i < 5; i++)
                        {
                            Thread.Sleep(200);
                            object readValue = plcClient.ReadValue(TaskDBName.taskInteractiveW.ToString());
                            if (readValue != null && readValue.ToString() == "0")
                            {
                                break;
                            }
                            else
                            {
                                plcClient.WriteValue(TaskDBName.taskInteractiveW.ToString(), 0);
                            }
                        }
                    }
                }
 
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
                WriteLog.Info($"{plcClient.PLCName}SendAGVTask").Write(ex.Message, $"{plcClient.PLCName}SendAGVTask");
            }
 
        }
 
        private static void updateloction(dt_agvtask aTask, Ibase_ware_locationRepository locationRepository, Idt_agvtaskRepository agvtaskRepository)
        {
            if (aTask != null)
            {
                if (aTask.agv_toaddress.Contains("HCJ") && aTask.agv_fromaddress.Contains("SB"))
                {
                    if (aTask.agv_remark.Contains("辊分下料任务"))
                    {
                        if (aTask.agv_toaddress.Contains("ZJ-GFHCJ10-"))
                        {
                            string NUM = aTask.agv_toaddress.Remove(11) + "4";
                            base_ware_location location = locationRepository.FindFirst(x => x.upper_code == NUM);
                            if (location != null)
                            {
                                string lotionCode = aTask.agv_toaddress.Remove(11);
                                dt_agvtask Agvtask = agvtaskRepository.FindFirst(d => d.agv_tasknum != aTask.agv_tasknum && d.agv_toaddress.Contains(lotionCode));
                                if (Agvtask == null)
                                {
                                    location.logic_col = 0;
                                    location.logic_row = "0";
                                    location.task = 2;
                                    locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true);
                                }
                            }
                        }
                        else
                        {
                            string NUM = aTask.agv_toaddress.Remove(10) + "4";
                            base_ware_location location = locationRepository.FindFirst(x => x.upper_code == NUM);
                            if (location != null)
                            {
                                string lotionCode = aTask.agv_toaddress.Remove(10);
                                dt_agvtask Agvtask = agvtaskRepository.FindFirst(d => d.agv_tasknum != aTask.agv_tasknum && d.agv_toaddress.Contains(lotionCode));
                                if (Agvtask == null)
                                {
                                    location.logic_col = 0;
                                    location.logic_row = "0";
                                    location.task = 2;
                                    locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true);
                                }
                            }
                        }
 
                    }
                    else
                    {
                        base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_toaddress);
                        location.logic_col = 0;
                        location.logic_row = "0";
                        location.task = 2;
                        locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true);
                    }
                }
                else if (aTask.agv_toaddress.Contains("SB") && aTask.agv_fromaddress.Contains("HCJ"))
                {
                    base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_fromaddress);
                    location.logic_col = 0;
                    location.logic_row = "0";
                    location.task = 2;
                    locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true);
                }
                else if (aTask.agv_toaddress.Contains("HCJ") && aTask.agv_fromaddress.Contains("HCJ"))
                {
                    base_ware_location location = locationRepository.FindFirst(x => x.upper_code == aTask.agv_toaddress);
                    location.logic_col = 0;
                    location.logic_row = "0";
                    location.task = 2;
                    locationRepository.Update(location, x => new { x.logic_col, x.logic_row, x.task }, true);
 
                    base_ware_location location1 = locationRepository.FindFirst(x => x.upper_code == aTask.agv_fromaddress);
                    location1.logic_col = 0;
                    location1.logic_row = "0";
                    location1.task = 2;
                    var entry = locationRepository.DbContext.ChangeTracker.Entries<base_ware_location>().FirstOrDefault();
                    if (entry != null)
                    {
                        entry.State = EntityState.Detached;
                    }
                    int a = locationRepository.Update(location1, x => new { x.logic_col, x.logic_row, x.task }, true);
                    if (a != 1)
                    {
                        ////WriteLog.Info("AGV任务反馈5").Write($"任务号:{aTask.agv_tasknum},起点地址:{aTask.agv_fromaddress},修改失败:{a},时间: {DateTime.Now}", "AGV任务反馈5");
                    }
                }
            }
 
 
        }
 
 
    }
}