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
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.LoctionEnum;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS
{
    public partial class StackerExecutor
    {
        public static object requestZJTBDown = new object();//requestZJJK
        private static int _readZTBSignalsoZ_Down = 0;//_readZTBUPSignalso
        private static int lock_TBXL_Z = 1;
        /// <summary>
        /// 正极涂布下料工作逻辑
        /// </summary>
        /// <param name="client"></param>
        public static void Z_TBSB_DownTask()
        {
            if (Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 1) == 0)
            {
                Task.Run(() =>
                {
                    try
                    {
                        VOLContext Context = new VOLContext();
                        Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
                        Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
                        Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
                        List<string> TBSBS = new List<string> { "涂布机正极1", "涂布机正极2", "涂布机正极3", "涂布机正极4" };
 
                        if (WCSService.Clients == null)
                        {
                            return;
                        }
                        //循环处理涂布设备下料请求
                        foreach (var TBSB in TBSBS)
                        {
 
                            PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
                            if (plc == null)
                            {
                                continue;
                            }
 
                            string SB_Up_Location = "ZJXL-TBSB" + TBSB.Replace("涂布机正极", "");//拼接涂布下料口
                                                                                            //查看当前涂布机设备是否有AGV任务
                            var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
                            //有结束本次循环
                            if (SBtask != null) { continue; }
                            //当前设备有没有下料请求
                            bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11293.00").Content;
                            //Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
                            if (GYR_RFX)
                            {
                                #region 记录下料请求信号
                                WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{GYR_RFX}---{DateTime.Now}", $"正极涂布下料请求");
                                #endregion
                                //找到路由规则(以设备为起点的路由规则)
                                List<base_routing_table> Routing_Table = routingRepository.Find(d => d.route_began == SB_Up_Location);
                                base_ware_location location_TB = null;
 
                                if (Routing_Table.Count > 0)
                                {
                                    foreach (var item in Routing_Table)
                                    {
                                        //找先找涂布区域缓存架
                                        location_TB = locRepository.FindFirst(t => t.upper_code == item.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.tpgd == 0 && t.area == "ZJ-TBHCJ" && t.status == 1 && t.task == 2);
                                        if (location_TB != null)
                                        {
                                            //location = location_TB.FirstOrDefault();//如果找到,赋值后退出循环
                                            break;
                                        }
                                    }
                                    //以上循环未找到合适货位,再找箔材库三号位缓存架
                                    if (location_TB == null)
                                    {
 
                                        //定位铂材库路由,找到铂材库货位号
                                        base_routing_table Loc_BCK = Routing_Table.FirstOrDefault(t => t.route_end.Contains("ZJ-BCHCJ"));
                                        if (Loc_BCK != null)
                                        {
                                            //根据根据货位号找到铂材库位置并确认是否有料
                                            location_TB = locRepository.FindFirst(t => t.upper_code == Loc_BCK.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.status == 1 && t.task == 2);
                                            if (location_TB == null)
                                            {
 
                                                return;
                                            }
                                        }
 
                                    }
                                }
                                if (location_TB != null)
                                {
 
                                    WebResponseContent content = new WebResponseContent();
                                    locRepository.DbContextBeginTransaction(() =>
                                    {
                                        //生成涂布机下料任务
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
                                            agv_id = Guid.NewGuid(),
                                            agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
                                            agv_fromaddress = SB_Up_Location,//以设备未起点
                                            agv_toaddress = location_TB.upper_code,
                                            agv_code = "正极AGV调度",
                                            agv_remark = "涂布下料任务",
                                            agv_taskstate = AGVTaskStateEnum.Create.ToString(),
                                            agv_tasktype = AGVTaskTypeEnum.TaskType_Outbound.ToString(),
                                            agv_worktype = 1,//工作类型
                                            agv_materielid = "",
                                            agv_qty = 1,
                                            agv_createtime = DateTime.Now,
                                            agv_grade = 0,//任务优先级
                                            agv_userid = "WCS",
                                            agv_barcode = "",
                                        };
 
                                        location_TB.update_time = DateTime.Now;
                                        location_TB.task = 1;
                                        var entry = locRepository.DbContext.ChangeTracker.Entries<base_ware_location>().FirstOrDefault();
                                        if (entry != null)
                                        {
                                            entry.State = EntityState.Detached;
                                        }
                                        int x = locRepository.Update(location_TB, d => new { d.update_time, d.task }, true);
                                        if (x == 1)
                                        {
                                            agvRepository.Add(agvtask, true);
                                            //WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"生成任务:{agvtask.agv_tasknum},终点货位【{location_TB.upper_code}】状态:托盘光电检测:{location_TB.tpgd},状态:{location_TB.location_state},更新时间:{location_TB.wireless_time}", $"【{TBSB}】下料任务生成");
                                            //WriteDBLog.Info($"【{TBSB}】下料任务生成").Write($"----------------任务生成成功  {DateTime.Now} -------------- {Environment.NewLine}", $"【{TBSB}】下料任务生成");
                                            return content.OK();
                                        }
                                        else
                                        {
                                            return content.Error();
                                        }
                                    });
                                }
 
 
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog.GetLog().Write($"正极涂布下料:{DateTime.Now} --------------{ex}", $"正极涂布下料");
                    }
                    finally
                    {
 
                        Interlocked.Exchange(ref _readZTBSignalsoZ_Down, 0);
                    }
                });
                
            }
        }
 
    }
}