--[[
|
编码: WMS-40-XX
|
名称:
|
作者:李帅帅
|
日期:2025-06-20
|
|
函数:WmsTaskRun
|
功能:wcs启动wsm作业
|
|
更改记录:
|
|
--]]
|
json = require("json")
|
mobox = require("OILua_JavelinExt")
|
m3 = require("oi_base_mobox")
|
ams_plc = require("amsplc_base")
|
-- 从内存中获取设备信息
|
local function ReadS7PLCCommsData(strLuaDEID, devicecode, commcode)
|
local nRet, strRetInfo, api_ret
|
-- 开始调用
|
local canshu = {
|
device_code = devicecode,
|
comm_code = commcode
|
}
|
|
local strKey = "OpenInfo"
|
local strSecret = "OpenInfoSecret"
|
local strHeader = ''
|
-- 获取Header
|
nRet, strHeader = mobox.genReqVerify(strKey, strSecret)
|
strHeader = string.gsub(strHeader, "ReqTime", "\nReqTime")
|
strHeader = string.gsub(strHeader, "ReqVerify", "\nReqVerify")
|
lua.DebugEx(strLuaDEID, "strHeader:", strHeader)
|
|
local strBody = lua.table2str(canshu)
|
local strurl = "http://192.168.1.205:5121/api/devctrl/GetCommData"
|
lua.DebugEx(strLuaDEID, "接口调用前参数:", strBody)
|
|
nRet, strRetInfo = mobox.sendHttpRequest(strurl, strHeader, strBody)
|
lua.DebugEx(strLuaDEID, "接口调用后返回信息:", strRetInfo)
|
if (nRet ~= 0 or strRetInfo == '') then
|
lua.Stop(strLuaDEID,
|
"调用OIDeviceCommS接口ReadData失败! device_code = " .. devicecode .. " comm_code = " .. commcode ..
|
"错误码:" .. nRet .. " " .. strRetInfo)
|
return 1
|
end
|
local api_ret = json.decode(strRetInfo)
|
if (api_ret.err_code ~= 0) then
|
lua.Stop(strLuaDEID,
|
"调用OIDeviceCommS接口ReadData失败! device_code = " .. devicecode .. " comm_code = " .. commcode ..
|
"错误码:" .. api_ret.err_code .. " " .. api_ret.err_msg)
|
return 1
|
end
|
|
local retinfo = api_ret.result
|
return retinfo
|
end
|
|
--[[
|
创建出库任务
|
Wcstask:需要移库的出库任务
|
StartLoc:起点货位信息
|
Endloc:分配的终点
|
grouby:分组,为后叉任务号
|
--]]
|
local function CreatPutOutTask(strLuaDEID, WMstask, Endloc, grouby, start_lan)
|
local AfterstrCode, nRet
|
--创建任务签查询一下作业状态是否已经改变
|
local WmstaskSelect = "S_CODE='" .. WMstask.code .. "'"
|
nRet, Task = m3.GetDataObjByCondition(strLuaDEID, "Operation", WmstaskSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, 'WCS作业启动=》出库:创建任务查询作业失败' .. "Sql" .. WmstaskSelect)
|
return 1
|
end
|
if Task.bs_state ~= 0 then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:创建任务查询作业:', "作业已经启动")
|
return 1
|
end
|
|
-- region 创建出库任务
|
local AfterstrHeader = 'TA' .. os.date("%y%m%d") .. '-'
|
nRet, AfterstrCode = mobox.getSerialNumber("任务", AfterstrHeader, 4)
|
if (nRet ~= 0)
|
then
|
lua.Stop(strLuaDEID, '申请【任务】编码失败!' .. AfterstrCode)
|
return 1
|
end
|
|
|
local task = m3.AllocObject(strLuaDEID, "Task")
|
task.code = AfterstrCode
|
task.op_code = WMstask.code -- 作业编码
|
task.op_name = WMstask.op_def_name -- 作业名称
|
task.factory = WMstask.factory -- 工厂
|
task.cntr_code = WMstask.cntr_code -- 托盘
|
task.start_lan = start_lan -- 巷道
|
-- 起点
|
task.start_wh_code = WMstask.start_wh_code
|
task.start_area_code = WMstask.start_area_code
|
task.start_loc_code = WMstask.start_loc_code
|
task.groupby = grouby
|
-- 终点
|
task.end_wh_code = WMstask.start_wh_code
|
task.end_area_code = WMstask.start_area_code
|
task.end_loc_code = Endloc
|
-- task.type = wms_base.WMS_nConst(strLuaDEID, "任务类型-堆垛机出库搬运") -- 任务类型
|
-- task.schedule_type = wms_base.WMS_nConst(strLuaDEID, "调度类型-堆垛机") -- 设置调度类型
|
task.type = 6
|
task.schedule_type = 5
|
|
nRet, task = m3.CreateDataObj(strLuaDEID, task)
|
if (nRet ~= 0) then
|
lua.stop(strLuaDEID, "创建堆垛机任务失败!" .. task)
|
return 1
|
end
|
|
local updatemst
|
local strCondition = "S_CODE = '" .. WMstask.code .. "' "
|
nRet, updatemst = mobox.updateDataAttrByCondition(strLuaDEID, "Operation", strCondition,
|
"N_B_STATE = 1,S_B_STATE = '执行'")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "修改作业状态失败!" .. updatemst)
|
return 1
|
end
|
end
|
|
|
|
-- 出库作业启动创建任务
|
local function PutOutCreateTask(strLuaDEID, wmstask)
|
-- wmstask = m3.KeyValueAttrsToObjAttr(wmstask.attrs)
|
--获取作业起点货位信息
|
local nRet, StartLoc
|
local StartLocSelect = "S_CODE = '" .. wmstask.start_loc_code .. "'"
|
nRet, StartLoc = m3.GetDataObjByCondition(strLuaDEID, "Location", StartLocSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID,
|
"获取起点货位信息有误! " .. StartLoc .. " SQL条件: " .. StartLocSelect)
|
return 1
|
-- return 1 2,"获取起点货位信息有误! " .. StartLoc .. " SQL条件: " .. StartLocSelect;
|
end
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点货位', StartLoc)
|
|
--判断起点前列是否存在未启动的作业出库作业,如果有未启动的出库作业,暂不启动
|
-- region 查询前叉货位 前叉作业
|
local BeforeWmsTask = "" --前叉任务
|
local BeforeStartLoc --前叉货位
|
|
--查询前叉货位
|
local BeforeStartLocSelect = "N_ROW = '" ..
|
StartLoc.row ..
|
"' and N_COL='" ..
|
StartLoc.col - 1 .. "' and N_ROADWAY='" .. StartLoc.roadway .. "' and N_LAYER='" .. StartLoc.layer .. "'"
|
nRet, BeforeStartLoc = m3.GetDataObjByCondition(strLuaDEID, "Location", BeforeStartLocSelect)
|
if (nRet == 1) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点前列货位', "起点前列货位不存在")
|
BeforeStartLoc = ""
|
end
|
if (nRet == 2) then
|
lua.Stop(strLuaDEID, 'WCS作业启动=》出库:查询起点前列货位错误' .. BeforeStartLoc .. " SQL条件: " .. BeforeStartLoc)
|
return 1
|
end
|
|
--前列起点不为空,查询前列是否存在作业
|
if (BeforeStartLoc ~= "") then
|
local BeforeWmsTaskSelect = "S_START_LOC = '" .. BeforeStartLoc.code .. "' and N_B_STATE='0'"
|
nRet, BeforeWmsTask = m3.GetDataObjByCondition(strLuaDEID, "Operation", BeforeWmsTaskSelect)
|
if (nRet == 1) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点前列作业', "起点前列作业不存在")
|
BeforeWmsTask = ""
|
end
|
if (nRet == 2) then
|
lua.Stop(strLuaDEID, 'WCS作业启动=》出库:查询起点前列货位错误' .. BeforeWmsTask .. " SQL条件: " .. BeforeWmsTaskSelect)
|
return 1
|
end
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点前列作业', BeforeWmsTask)
|
end
|
|
--endregion
|
--存在前列任务 不做处理 直接返回
|
if (BeforeWmsTask ~= "") then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:存在前列任务,暂不出库', BeforeWmsTask)
|
return 1
|
end
|
|
-- 若不存在前叉任务则 判断是否存在后叉任务,如果有后叉作业,则前叉后叉一起启动
|
-- region 查询后叉货位 后叉作业
|
local AfterWmsTask --后叉任务
|
local AfterStartLoc --后叉货位
|
|
--查询后叉货位
|
local AfterAfterStartLocSelect = "N_ROW = '" ..
|
StartLoc.row ..
|
"' and N_COL='" ..
|
StartLoc.col + 1 .. "' and N_ROADWAY='" .. StartLoc.roadway .. "'and N_LAYER='" .. StartLoc.layer .. "'"
|
nRet, AfterStartLoc = m3.GetDataObjByCondition(strLuaDEID, "Location", AfterAfterStartLocSelect)
|
if (nRet == 1) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点后列货位', "起点后列货位不存在")
|
AfterStartLoc = ""
|
end
|
if (nRet == 2) then
|
lua.Stop(strLuaDEID, 'WCS作业启动=》出库:查询起点后列货位错误' .. AfterStartLoc .. " SQL条件: " .. AfterAfterStartLocSelect)
|
return 1
|
end
|
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点后列货位', AfterStartLoc)
|
|
--后列起点不为空,查询后列是否存在作业
|
if (AfterStartLoc ~= "") then
|
local AfterWmsTaskSelect = "S_START_LOC = '" .. AfterStartLoc.code .. "' and N_B_STATE=0"
|
nRet, AfterWmsTask = m3.GetDataObjByCondition(strLuaDEID, "Operation", AfterWmsTaskSelect)
|
if (nRet == 1) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:查询起点后列作业', "起点后列作业不存在")
|
AfterWmsTask = ""
|
end
|
if (nRet == 2) then
|
lua.Stop(strLuaDEID, 'WCS作业启动=》出库:查询起点后列作业错误' .. AfterWmsTask .. " SQL条件: " .. AfterWmsTaskSelect)
|
return 1
|
end
|
end
|
|
-- endregion
|
-- region 如果作业起点属于内侧货位,判断外侧货位是否有出库锁,如果有锁,说明外侧存在作业,暂不启动,等待外侧任务外侧,推送内侧任务
|
if (StartLoc.pos == 2) then
|
local OutStartLoc --外侧货位
|
|
--获取外侧货位
|
local OutStartLocSelect = "N_COL='" .. StartLoc.col .. "' and N_LAYER='" .. StartLoc.layer .. "' and N_POS=1"
|
nRet, OutStartLoc = m3.GetDataObjByCondition(strLuaDEID, "Location", OutStartLocSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID,
|
"获取起点外侧货位信息有误! " .. OutStartLoc .. " SQL条件: " .. StartLocSelect)
|
return 1
|
end
|
|
--外侧起点不为空,并且数量不为0,且存在锁,直接返回
|
if (OutStartLoc ~= nil and OutStartLoc.cur_num ~= 0 and OutStartLoc.lock_state ~= 0) then
|
return 1;
|
end
|
end
|
--endregion
|
|
--后叉存在任务 生成双托任务
|
if (AfterWmsTask ~= "" and AfterWmsTask ~= nil) then
|
--如果任务属于内侧货位,判断后叉的外侧货位是否有出库锁,如果有锁,说明外侧存在作业,暂不启动,等待外侧任务外侧,推送内侧任务
|
if (StartLoc.pos == 2) then
|
local AfterOutStartLoc --后列外侧货位
|
|
--获取后列外侧货位
|
local AfterOutStartLocSelect = "N_ROW = '" ..
|
AfterStartLoc.row ..
|
"' and N_COL='" .. AfterStartLoc.col .. "' and N_LAYER='" .. AfterStartLoc.layer .. "' and N_POS=='2' "
|
nRet, AfterOutStartLoc = m3.GetDataObjByCondition(strLuaDEID, "Location", AfterOutStartLocSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID,
|
"获取起点后列外侧货位信息有误! " .. AfterOutStartLoc .. " SQL条件: " .. AfterOutStartLocSelect)
|
end
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:起点后列外侧货位', AfterOutStartLoc)
|
--后列外侧起点不为空,并且数量不为0,获取外侧是否存在任务
|
if (AfterOutStartLoc ~= nil and AfterOutStartLoc.cur_num ~= 0 and AfterOutStartLoc.lock_state ~= 0) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:后列外侧存在任务,暂不出库', AfterOutStartLoc)
|
return 1;
|
end
|
end
|
--有可以一起推送的双托任务,推送双托任务
|
--创建后叉任务
|
--根据巷道获取后叉接驳位货位信息
|
local Afterendcode = "1035"
|
if (wmstask.start_aisle == 2) then
|
Afterendcode = "1047"
|
end
|
|
local result = CreatPutOutTask(strLuaDEID, AfterWmsTask, Afterendcode, AfterWmsTask.code, AfterStartLoc.roadway)
|
if result ~= 0 then
|
return 1
|
end
|
-- 创建前叉任务
|
--根据巷道获取前叉接驳位货位信息
|
local endcode = "1033"
|
if (wmstask.start_aisle == 2) then
|
endcode = "1045"
|
end
|
|
local result = CreatPutOutTask(strLuaDEID, wmstask, endcode, AfterWmsTask.code, StartLoc.roadway)
|
if result ~= 0 then
|
return 1
|
end
|
else
|
--region 单托任务推送前 需要判断能不能和其他单托任务一起出,如果可以则推送不连续的双托任务
|
--先判断单托任务有没有标记,如果没有标记先不推送
|
if (wmstask.note == "" or wmstask.note == nil) then
|
local updatemst
|
local strCondition = "S_CODE = '" .. wmstask.code .. "' "
|
nRet, updatemst = mobox.updateDataAttrByCondition(strLuaDEID, "Operation", strCondition,
|
"S_NOTE = '单托'")
|
|
-- wmstask.note = "单托"
|
-- nRet, updatemst = mobox.updateDataObj(strLuaDEID, "Operation", lua.table2str(wmstask))
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "修改作业状态失败!" .. strCondition)
|
return 1
|
end
|
--标记未单托后结束
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:该任务未标记,切为单托任务,暂不推送', wmstask)
|
--return 1;
|
else
|
--如果已经标记为单托,则去寻找有没有其他可以一起出库的单托任务,如果有,推送双托任务
|
local AfterWmsTaskSelect = "S_NOTE = '单托' and N_B_STATE='0' and N_START_AISLE='" ..
|
StartLoc.roadway .. "' and S_CODE !='" .. wmstask.code .. "'"
|
nRet, AfterWmsTask = m3.GetDataObjByCondition(strLuaDEID, "Operation", AfterWmsTaskSelect)
|
if (nRet == 1) then
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:获取单托作业信息为空', AfterWmsTask)
|
AfterWmsTask = nil
|
end
|
if (nRet == 2) then
|
lua.Stop(strLuaDEID,
|
"获取单托作业信息有误! " .. StartLoc .. " SQL条件: " .. AfterWmsTaskSelect)
|
end
|
lua.DebugEx(strLuaDEID, 'WCS作业启动=》出库:获取单托作业信息为空', "")
|
end
|
-- endregion
|
--region 有可以一起推送的双托任务,推送双托任务,如果没有推送单托任务
|
if (AfterWmsTask ~= nil) then
|
--有可以一起推送的双托任务,推送双托任务
|
--创建后叉任务
|
|
--根据巷道获取后叉接驳位货位信息
|
local Afterendcode = "1035"
|
if (wmstask.start_aisle == 2) then
|
Afterendcode = "1047"
|
end
|
|
local result = CreatPutOutTask(strLuaDEID, AfterWmsTask, Afterendcode, AfterWmsTask.code,
|
AfterStartLoc.roadway)
|
if result ~= 0 then
|
return 1
|
end
|
|
-- 创建前叉任务
|
--根据巷道获取前叉接驳位货位信息
|
local endcode = "1033"
|
if (wmstask.start_aisle == 2) then
|
endcode = "1045"
|
end
|
|
result = CreatPutOutTask(strLuaDEID, wmstask, endcode, AfterWmsTask.code, StartLoc.roadway)
|
if result ~= 0 then
|
return 1
|
end
|
else
|
--后叉不存在任务,生成单托任务
|
local endcode = "1033"
|
if (wmstask.start_aisle == 2) then
|
endcode = "1045"
|
end
|
--创建出库任务
|
local result = CreatPutOutTask(strLuaDEID, wmstask, endcode, StartLoc.roadway)
|
if result ~= 0 then
|
return 1
|
end
|
end
|
-- endregion
|
end
|
end
|
|
--创建呼叫托盘任务
|
local function CreateEmptyTask(strLuaDEID, WMstask)
|
--根据作业终点货位,判断对应站台
|
local startLoc, endLocs, nRet, strCode, readValue, updatemst
|
if WMstask.end_loc_code == "1069" then
|
startLoc = "1027"
|
endLocs = { "1033", "1032", "1031", "1030", "1029" }
|
readValue = { "1033-WORK_MODE", "1032-WORK_MODE", "1031-WORK_MODE", "1030-WORK_MODE", "1029-WORK_MODE" }
|
elseif WMstask.end_loc_code == "1082" then
|
startLoc = "1117"
|
endLocs = { "1023", "1022", "1021", "1020", "1019" }
|
readValue = { "1023-WORK_MODE", "1022-WORK_MODE", "1021-WORK_MODE", "1020-WORK_MODE", "1019-WORK_MODE" }
|
elseif WMstask.end_loc_code == "1096" then
|
startLoc = "1117"
|
endLocs = { "1013", "1012", "1011", "1010", "1009" }
|
readValue = { "1013-WORK_MODE", "1012-WORK_MODE", "1011-WORK_MODE", "1010-WORK_MODE", "1009-WORK_MODE" }
|
end
|
|
--创建任务判断终点有多少托盘,如果呼叫托盘数量大于剩余空托数量,返回报错
|
local DeviceResult = ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", readValue)
|
local CntrNumber = 0
|
for i = 1, #DeviceResult do
|
if DeviceResult[i].value[1] == 1 then
|
CntrNumber = CntrNumber + 1
|
end
|
end
|
|
--判断剩余空位是否能够呼叫足够的托盘
|
if (5 - CntrNumber < WMstask.code_qty) then
|
local strCondition = "S_CODE = '" .. WMstask.code .. "' "
|
nRet, updatemst = mobox.updateDataAttrByCondition(strLuaDEID, "Operation", strCondition,
|
"N_B_STATE =4,S_NOTE = '对应空托位置无法呼叫对应数量的空托,请修改呼叫空托数量'")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "修改作业状态失败!" .. updatemst)
|
return 1
|
end
|
end
|
|
--根据托盘数量创建任务
|
for k = 1 + CntrNumber, WMstask.code_qty do
|
local strHeader = 'TA' .. os.date("%y%m%d") .. '-'
|
nRet, strCode = mobox.getSerialNumber("任务", strHeader, 4)
|
if (nRet ~= 0)
|
then
|
lua.Stop(strLuaDEID, '申请【任务】编码失败!' .. strCode)
|
return 1
|
end
|
local task = m3.AllocObject(strLuaDEID, "Task")
|
task.code = strCode
|
task.op_code = WMstask.code -- 作业编码
|
task.op_name = WMstask.op_def_name -- 作业名称
|
task.factory = WMstask.factory -- 工厂
|
task.cntr_code = WMstask.cntr_code -- 托盘
|
-- 起点
|
-- task.start_wh_code = WMstask.start_wh_code
|
--task.start_area_code = WMstask.start_area_code
|
task.start_loc_code = startLoc
|
-- 终点
|
|
task.end_loc_code = endLocs[k]
|
-- task.type = wms_base.WMS_nConst(strLuaDEID, "任务类型-输送线空托搬运") -- 任务类型
|
-- task.schedule_type = wms_base.WMS_nConst(strLuaDEID, "调度类型-输送线") -- 设置调度类型
|
task.type = 8
|
task.schedule_type = 4
|
-- local strCondition = "S_CODE = '" .. WMstask.code .. "' "
|
nRet, task = m3.CreateDataObj(strLuaDEID, task)
|
if (nRet ~= 0) then
|
lua.stop(strLuaDEID, "创建呼叫空托任务失败!" .. task)
|
return 1
|
end
|
end
|
--修改作业状态
|
local strCondition = "S_CODE = '" .. WMstask.code .. "' "
|
nRet, updatemst = mobox.updateDataAttrByCondition(strLuaDEID, "Operation", strCondition,
|
"N_B_STATE = 1,S_B_STATE = '执行'")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "修改作业状态失败!" .. updatemst)
|
return 1
|
end
|
end
|
|
function WmsTaskRun(strLuaDEID)
|
local nRet, WmsTask
|
m3.PrintLuaDEInfo(strLuaDEID)
|
-- step1: 获取需要推送的任务对象
|
nRet, WmsTask = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "m3.GetSysCurEditDataObj 失败!" .. WmsTask)
|
return
|
end
|
|
lua.DebugEx(strLuaDEID, '内存中WmsTask:', WmsTask)
|
|
-- 查数据库表
|
local wms_task = ams_plc.QueryOpbyopcode(strLuaDEID, WmsTask.code)
|
|
lua.DebugEx(strLuaDEID, '查询到的wms_task:', wms_task)
|
--遍历作业 根据作业类型创建任务
|
if (WmsTask ~= '') then
|
if (WmsTask.op_type == 2) then
|
--出库作业启动
|
PutOutCreateTask(strLuaDEID, WmsTask)
|
elseif (WmsTask.op_type == 3) then
|
CreateEmptyTask(strLuaDEID, WmsTask)
|
end
|
end
|
lua.DebugEx(strLuaDEID, 'WCS作业启动:', "结束")
|
|
end
|