qinchulong
2025-03-29 039a4a5433e7f80adc88b491b549e5d9486e4f9a
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
using WIDESEA_WCS.WCSClient;
 
namespace WIDESEA_WCS.Jobs
{
    public partial class EquipmentExecutor : SchedulerExecuteBase
    {
        private static int _readSignalso = 0;
        /// <summary>
        /// 涂布机下料工作逻辑
        /// </summary>
        /// <returns></returns>
        public static void Z_TBSB_DownTask() 
        {
            if (Interlocked.Exchange(ref _readSignalso, 1) == 0) 
            {
                WebResponseContent responseContent = new WebResponseContent();
                try
                {
                    VOLContext Context = new VOLContext();
                    Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(Context);
                    dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep);
                    Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
                    Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
                    Ibill_pda_groupdiskRepository pdaRepository = new bill_pda_groupdiskRepository(Context);
                    Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
                    Ibill_group_stockRepository groupRepository = new bill_group_stockRepository(Context);
                    List<string> TBSB = new List<string> { "正极1号涂布机", "正极2号涂布机", "正极3号涂布机" };
                    IequipmentRepository equipmentRepository = new equipmentRepository(Context);
                    PLCClient zagvplc = WCSService.Clients.Find(v => v.PLCName == "正极1号AGV");
                    foreach (var item in TBSB)
                    {
                        WriteLog.Info($"{item}上料").Write($"{item}开始,时间:" + DateTime.Now + "   ----------", $"{item}上料");
                        PLCClient plc = WCSService.Clients.Find(v => v.PLCName == item);
                        if (plc == null)
                            continue;
                        var task = agvRepository.FindFirst(f => f.agv_fromaddress == plc.PLCDownLoc);
                        if (task == null)
                        {
                            //获取下料请求
                            string isWork = plc.ReadValue(ConveyorLineInfoDBName.R_TBSB_DOWNrequest.ToString(), plc.PLCDescroption).ToString();
                            //获取工单号
                            string batchNo = plc.ReadValue(ConveyorLineInfoDBName.R_TBSB_DOWNbatchNo.ToString(), plc.PLCDescroption).ToString();
                            GetEquipmentInfo(plc.PLCDownLoc, isWork, batchNo, "", "");
                            WriteLog.Info($"{item}下料").Write($"isWork:{isWork},batchNo:{batchNo},时间:" + DateTime.Now + "   ----------", $"{item}下料");
                            if (bool.Parse(isWork))
                            {
                                //调用MES接口获取下料信息
                                MESback material = MESAPIInvoke.EquipUnloading(plc.PLCDownLoc, batchNo);
                                WriteLog.Info($"{item}下料").Write($"MES返回数据:{JsonConvert.SerializeObject(material)},时间:" + DateTime.Now + "", $"{item}下料");
                                if (material.Code == 0)
                                {
                                    //查询路由信息
                                    var routes = routingRepository.Find(v => v.route_began == plc.PLCDownLoc).ToList();
                                    foreach (var route in routes)
                                    {
                                        string ismaterial = zagvplc.ReadValue(ConveyorLineInfoDBName.R_Location_iswork.ToString(), route.route_end).ToString();
                                        GetEquipmentInfo(plc.PLCDownLoc, isWork, batchNo, "", route.route_end+"的货位状态是"+ismaterial);
                                        if (ismaterial == "2")
                                            continue;
                                        var location = locRepository.FindFirst(v => v.upper_code == route.route_end);
                                        if (location.location_state != LocationStateEnum.LocationState_Empty.ToString())
                                            continue;
                                        //条件全部通过,生成AGV任务
                                        dt_agvtask agvtask = new dt_agvtask
                                        {
                                            agv_materbarcode = "daiding",//下料任务预先没有条码,PDA在缓存架绑定时接收新的条码
                                            agv_barcode = "",
                                            agv_code = "正极1号AGV",
                                            agv_createtime = DateTime.Now,
                                            agv_fromaddress = route.route_began,
                                            agv_grade = 1,
                                            agv_materielid = material.MaterialType,
                                            agv_qty = 1,
                                            agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep),
                                            agv_taskstate = "Create",
                                            agv_tasktype = "TaskType_Outbound",
                                            agv_toaddress = route.route_end,
                                            agv_userid = "WCS",
                                            agv_worktype = 1
                                        };
                                        location.location_state = LocationStateEnum.LocationState_Lock.ToString();
                                        locRepository.Update(location, true);
                                        agvRepository.Add(agvtask, true);
                                        WriteLog.Info("Z_TBSB_DownTask").Write("Z_TBSB_DownTask" + agvtask.agv_tasknum + DateTime.Now, "Z_TBSB_DownTask");
                                        break;
                                    }
                                }
                                else
                                {
                                    WriteLog.Info("F_TBSB_DownTask").Write("调用MES接口EquipUnloading失败" + material.Message, "F_TBSB_DownTask");
                                }
                            }
                        }
                        WriteLog.Info($"{item}下料").Write($"{item}结束,时间:" + DateTime.Now + $"   ----------{Environment.NewLine}", $"{item}下料");
                    }
 
 
                }
                catch (Exception ex)
                {
                    WriteLog.Info("Z_TBSB_DownTask").Write("Z_TBSB_DownTask" + ex.Message + DateTime.Now, "Z_TBSB_DownTask");
                }
                finally 
                {
                    Interlocked.Exchange(ref _readSignalso, 0);
                }
            }
        }
    }
}