wangxinhui
2024-12-26 78b99e5348592a29ca1393a5e13db619cc4eba56
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
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
    {
        private static int _readRKSB_Down = 0;
        /// <summary>
        /// 负极涂布下料工作逻辑
        /// </summary>
        /// <param name="client"></param>
        public static void RKSB_DownTask()
        {
            if (Interlocked.Exchange(ref _readRKSB_Down, 1) == 0)
            {
                Task.Run(() =>
                {
                    try
                    {
                        VOLContext Context = new VOLContext();
                        Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
                        Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
                        Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
                        List<string> RKSBS = new List<string> { "入壳机1", "入壳机2" };
                        //循环处理涂布设备下料请求
                        foreach (var RKSB in RKSBS)
                        {
                            if (WCSService.Clients == null)
                            {
                                return;
                            }
                            PLCClient plc = WCSService.Clients.Find(v => v.PLCName == RKSB);
                            if (plc == null)
                            {
                                continue;
                            }
                            string SB_Up_Location = "XL-RKSB" + RKSB.Replace("入壳机", "");//拼接入壳下料口
                            string equipType = "RKSB" + RKSB.Replace("入壳机", "");//设备
                            //获取当前入壳设备是否有下料AGV任务
                            var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
                            //有结束本次循环
                            if (SBtask != null) { continue; }
                            //获取当前设备的下料请求
                            int requestRKDown = plc.ModbusPLCClient.ModbusTcpNetClient.ReadInt16("5308").Content;
                            if (requestRKDown==4)
                            {
                                #region 记录下料请求信号
                                WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{requestRKDown}---{DateTime.Now}", $"入壳机下料请求");
                                #endregion
                                //找到对应缓存架货位,并确认空框
                                dt_stationinfo location = stationinfoRepository.Find(x => x.area == equipType && x.tpgd == false 
                                && x.wlgd == false && x.task == false).OrderBy(x => x.greenbuttontime).FirstOrDefault();
                                if (location != 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.stationCode,
                                            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.task = true;
                                        var entry = locRepository.DbContext.ChangeTracker.Entries<base_ware_location>().FirstOrDefault();
                                        if (entry != null)
                                        {
                                            entry.State = EntityState.Detached;
                                        }
                                        int x = locRepository.Update(location, d => new { d.task}, true);
                                        if (x == 1)
                                        {
                                            agvRepository.Add(agvtask, true);
                                            return content.OK();
                                        }
                                        else
                                        {
                                            return content.Error();
                                        }
 
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteLog.GetLog().Write($"入壳机下料:{DateTime.Now} --------------{ex}", $"入壳机下料调度Error");
                    }
                    finally
                    {
 
                        Interlocked.Exchange(ref _readRKSB_Down, 0);
                    }
                });
 
            }
        }
 
    }
}