1
Jianw
2025-07-09 f6f5e6b632d6649386a380558d84003f3de7ec6c
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
--[[
   编码: WMS-38-XX
   名称:WCS任务启动
   作者:李帅帅
   日期:2025-06-18
 
   函数: WcsTaskRun
   功能:  推送任务给设备
 
   更改记录:
 
--]]
json    = require("json")
mobox   = require("OILua_JavelinExt")
m3      = require("oi_base_mobox")
ams_plc = require("amsplc_base")
 
 
--推送堆垛机任务
--taskType 任务类型0=无 1=入库 2=出库 3=移库
--deviceType 1=前叉 2=后叉 3=双叉
 
local function SendTask(strLuaDEID, beforeTask, afterTask, taskType, deviceType)
    --获取前叉数据
    local beforetask1 = 0
    local beforetask2 = 0
    local startRow1 = 0
    local startCol1 = 0
    local startLayer1 = 0
    local endRow1 = 0
    local endCol1 = 0
    local endLayer1 = 0
    local deviceCode = "TC1" --堆垛机编码
    --获取前叉任务数据
    if (beforeTask ~= nil and beforeTask ~= "") then
        beforetask1, beforetask2 = ams_plc.WritePlcTaskNo(strLuaDEID, beforeTask.code)
        --获取起点信息
        local nRet, StartLoc1
        local StartLocSelect1 = "S_CODE = '" .. beforeTask.start_loc_code .. "'"
        nRet, StartLoc1 = m3.GetDataObjByCondition(strLuaDEID, "Location", StartLocSelect1)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID,
                "获取起点货位信息有误! " .. StartLoc1 .. " SQL条件: " .. StartLocSelect1)
            return 1
        end
        startRow1 = StartLoc1.row
        startCol1 = StartLoc1.col
        startLayer1 = StartLoc1.layer
        if (StartLoc1.roadway == 2) then
            deviceCode = "TC2"
        end
        --判断对应巷道堆垛机有无任务
        local TaskNo = ams_plc.ReadS7PLCCommsData(strLuaDEID, deviceCode, { "27" })
        if (TaskNo[1].value[1] ~= 0 or TaskNo[1].value[2] ~= 0) then
            lua.DebugEx(strLuaDEID, '堆垛机任务推送:推送失败', "查询前叉存在任务")
            return 1
        end
        lua.DebugEx(strLuaDEID, '堆垛机任务推送:查询起点货位', StartLoc1)
 
        --获取终点信息
        local Endtoc1
        local EndLocSelect1 = "S_CODE = '" .. beforeTask.end_loc_code .. "'"
        nRet, Endtoc1 = m3.GetDataObjByCondition(strLuaDEID, "Location", EndLocSelect1)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID,
                "获取起点货位信息有误! " .. Endtoc1 .. " SQL条件: " .. EndLocSelect1)
            return 1
        end
        endRow1 = Endtoc1.row
        endCol1 = Endtoc1.col
        endLayer1 = Endtoc1.layer
        lua.DebugEx(strLuaDEID, '堆垛机任务推送:查询起点货位', Endtoc1)
    end
    local startRow2 = 0
    local startCol2 = 0
    local startLayer2 = 0
    local endRow2 = 0
    local endCol2 = 0
    local endLayer2 = 0
    local Aftertask1 = 0
    local Aftertask2 = 0
 
 
 
    --获取后叉任务数据
    if (afterTask ~= nil and afterTask ~= "") then
        Aftertask1, Aftertask2 = ams_plc.WritePlcTaskNo(strLuaDEID, afterTask.code)
        --获取起点信息
        local nRet, StartLoc2
        local StartLocSelect2 = "S_CODE = '" .. afterTask.start_loc_code .. "'"
        nRet, StartLoc2 = m3.GetDataObjByCondition(strLuaDEID, "Location", StartLocSelect2)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID,
                "获取起点货位信息有误! " .. StartLoc2 .. " SQL条件: " .. StartLocSelect2)
            return 1
        end
        if (StartLoc2.roadway == 2) then
            deviceCode = "TC1"
        end
         --判断对应巷道堆垛机有无任务
        local TaskNo = ams_plc.ReadS7PLCCommsData(strLuaDEID, deviceCode, { "27" })
        if (TaskNo[1].value[1] ~= 0 or TaskNo[1].value[2] ~= 0) then
            lua.DebugEx(strLuaDEID, '堆垛机任务推送:推送失败', "查询后叉存在任务")
            return 1
        end
        startRow2 = StartLoc2.row
        startRow2 = StartLoc2.col
        startRow2 = StartLoc2.layer
        lua.DebugEx(strLuaDEID, '堆垛机任务推送:查询起点货位', StartLoc2)
 
        --获取终点信息
        local Endtoc2
        local EndLocSelect2 = "S_CODE = '" .. afterTask.end_loc_code .. "'"
        nRet, Endtoc2 = m3.GetDataObjByCondition(strLuaDEID, "Location", EndLocSelect2)
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID,
                "获取起点货位信息有误! " .. Endtoc2 .. " SQL条件: " .. EndLocSelect2)
            return 1
        end
        endRow2 = Endtoc2.row
        endCol2 = Endtoc2.col
        endLayer2 = Endtoc2.layer
    end
    --拼接批量写入数据
    local value = { deviceType, startRow1, startCol1, startLayer1, endRow1, endCol1, endLayer1, taskType, startRow2,
        startCol2, startLayer2, endRow2, endCol2, endLayer2, taskType, 1, 0, 0, 0, 0, 0, 0, 0, 0, beforetask1,
        beforetask2, Aftertask1, Aftertask2 }
    local code = ams_plc.WriteMultiS7PLCCommsData(strLuaDEID, deviceCode, "03_WRITE", value)
    --下发成功修改任务状态
    if code == 0 then
        if (beforeTask ~= nil and beforeTask ~= "") then
            local nRet, updateTask
            local strCondition = "S_CODE = '" .. beforeTask.code .. "' "
            nRet, updateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task", strCondition,
                "S_B_STATE = '已推送',N_B_STATE = 1")
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, " 堆垛机推送=》 修改任务" .. beforeTask.code .. "状态失败! " .. updateTask)
                return 1
            end
        end
        if (afterTask ~= nil and afterTask ~= "") then
            local nRet, updateTask
            local strCondition = "S_CODE = '" .. afterTask.code .. "' "
            nRet, updateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task", strCondition,
                "S_B_STATE = '已推送',N_B_STATE = 1")
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, " 堆垛机推送=》 修改任务" .. afterTask.code .. "状态失败! " .. updateTask)
                return 1
            end
        end
    end
end
 
 
function WcsTaskRun(strLuaDEID)
    local nRet, WcsTask, taskType
    -- step1: 获取需要推送的任务对象
    nRet, WcsTask = m3.GetSysCurEditDataObj(strLuaDEID, "Task")
    if (nRet ~= 0) then
        lua.Stop(strLuaDEID, "m3.GetSysCurEditDataObj 失败!" .. WcsTask)
        return
    end
    if (WcsTask.schedule_type == 5 and WcsTask.type == 5) then     --堆垛机入库
        taskType = 1
    elseif (WcsTask.schedule_type == 5 and WcsTask.type == 6) then --堆垛机出库
        taskType = 2
    elseif (WcsTask.schedule_type == 5 and WcsTask.type == 7) then --堆垛机移库
        taskType = 3
    end
    --判断当前任务是单托任务还是双托任务
    if WcsTask.groupby ~= '' then
        if (WcsTask.groupby == WcsTask.op_code) then --作业号等于分组标识,说明是后叉任务 和前叉任务一起推送,不单个推送
            lua.DebugEx(strLuaDEID, 'WCS任务启动=》出库:该任务为后叉任务,和前叉一起推送,暂不推送', WcsTask)
            return
        else
            --前叉任务查询后叉任务一起推送
            local AfterTask
            local AfterTaskSelect = "S_OP_CODE = '" .. WcsTask.groupby .. "' and N_B_STATE='0' and N_SCHEDULE_TYPE=5"
            nRet, AfterTask = m3.GetDataObjByCondition(strLuaDEID, "Task", AfterTaskSelect)
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, "获取后叉任务信息失败! " .. AfterTask .. " SQL条件: " .. AfterTaskSelect)
                return
            end
            nRet = SendTask(strLuaDEID, WcsTask, AfterTask, taskType, 3)
        end
    else
        --单托任务直接推送
        if taskType == 1 then
            nRet = SendTask(strLuaDEID, nil, WcsTask, taskType, 1)
        else
            nRet = SendTask(strLuaDEID, WcsTask, nil, taskType, 1)
        end
    end
    if nRet ~= 0 then
        return 1
    end
end