--[[
|
编码: WMS-38-XX
|
名称:
|
作者:李帅帅
|
日期:2025-06-23
|
|
函数: Main
|
功能:
|
|
更改记录:
|
|
--]]
|
json = require("json")
|
mobox = require("OILua_JavelinExt")
|
m3 = require("oi_base_mobox")
|
wms_wh = require("wms_wh")
|
|
-- 从内存中获取设备信息
|
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
|
|
-- 写数据到PLC
|
local function WriteS7PLCCommsData(strLuaDEID, devicecode, commcode, comm_value)
|
local nRet, strRetInfo, canshu
|
|
-- comm_value为数组 {1}、{1,2} 可以写DInt
|
local canshu = {
|
device_code = devicecode,
|
comm_code = commcode,
|
value = comm_value
|
}
|
|
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.Debug(strLuaDEID, debug.getinfo(1), "strHeader:", strHeader)
|
local strurl = "http://192.168.1.205:5121/api/devctrl/writedata"
|
local strBody = lua.table2str(canshu)
|
lua.Debug(strLuaDEID, debug.getinfo(1), "接口调用前参数:", strBody)
|
nRet, strRetInfo = mobox.sendHttpRequest(strurl, strHeader, strBody)
|
if (nRet ~= 0 or strRetInfo == '') then
|
lua.Error(strLuaDEID, debug.getinfo(1),
|
"S7PLC 写入失败! device_code = " .. devicecode .. " comm_code = " .. commcode .. "错误码:" .. nRet ..
|
" " .. strRetInfo)
|
end
|
lua.Debug(strLuaDEID, debug.getinfo(1), "写PLC返回信息:", strRetInfo)
|
local api_ret = json.decode(strRetInfo)
|
if (api_ret.err_code ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1),
|
"S7PLC 写入失败! device_code = " .. devicecode .. " comm_code = " .. commcode .. "错误码:" ..
|
api_ret.err_code .. " --> " .. api_ret.err_msg)
|
end
|
end
|
|
-- 写连续数据到S7的通讯项
|
local function WriteMultiS7PLCCommsData(strLuaDEID, devicecode, commcode, comm_value)
|
local nRet, strRetInfo, canshu
|
|
-- comm_value为数组 {1}、{1,2} 可以写DInt
|
local canshu = {
|
device_code = devicecode,
|
comm_code = commcode,
|
value = comm_value
|
}
|
|
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 strurl = "http://192.168.1.205:5121/api/devctrl/WriteMultiData"
|
local strBody = lua.table2str(canshu)
|
lua.DebugEx(strLuaDEID, "接口调用前参数:", strBody)
|
nRet, strRetInfo = mobox.sendHttpRequest(strurl, strHeader, strBody)
|
if (nRet ~= 0 or strRetInfo == '') then
|
lua.Stop(strLuaDEID,
|
"S7PLC 写入失败! device_code = " .. devicecode .. " comm_code = " .. commcode .. "错误码:" .. nRet ..
|
" " .. strRetInfo)
|
return
|
end
|
lua.DebugEx(strLuaDEID, "写PLC返回信息:", strRetInfo)
|
local api_ret = json.decode(strRetInfo)
|
if (api_ret.err_code ~= 0) then
|
lua.Stop(strLuaDEID,
|
"S7PLC 写入失败! device_code = " .. devicecode .. " comm_code = " .. commcode .. "错误码:" ..
|
api_ret.err_code .. " --> " .. api_ret.err_msg)
|
end
|
|
return nRet
|
end
|
|
-- 读出线体任务号 转换为我们的任务号
|
local function ReadPlcTaskNo(strLuaDEID, taskno1, taskno2)
|
lua.Debug(strLuaDEID, debug.getinfo(1), "ReadPlcTaskNo:", "IN")
|
if (taskno1 == 0 or taskno2 == 0) then
|
return '0'
|
end
|
local no1 = tostring(taskno1) -- 确保输入是字符串
|
if #no1 < 4 then
|
no1 = string.rep("0", 4 - #no1) .. no1
|
end
|
|
local no2 = tostring(taskno2) -- 确保输入是字符串
|
if #no2 < 4 then
|
no2 = string.rep("0", 4 - #no2) .. no2
|
end
|
local shortYear = os.date("%y")
|
return "TA" .. shortYear .. no1 .. "-" .. no2
|
end
|
|
--出库任务堆垛机任务完成创建输送线任务
|
local function CreatPutOutTask(strLuaDEID, Wcstask)
|
-- region 创建出库任务
|
local AfterstrCode, nRet
|
|
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 WmsTask
|
local WmsTaskSelect = "S_CODE = '" .. Wcstask.op_code .. "'"
|
nRet, WmsTask = m3.GetDataObjByCondition(strLuaDEID, "Operation", WmsTaskSelect)
|
if (nRet ~= 0) then
|
lua.stop(strLuaDEID, 'WCS创建输送线任务=》出库:查询作业信息错误!' .. WmsTask)
|
return 1
|
end
|
|
local task = m3.AllocObject(strLuaDEID, "Task")
|
task.code = AfterstrCode
|
task.op_code = Wcstask.op_code -- 作业编码
|
task.op_name = Wcstask.op_name -- 作业名称
|
task.factory = Wcstask.factory -- 工厂
|
task.cntr_code = Wcstask.cntr_code -- 托盘
|
-- task.start_lan = Wcstask.start_lan -- 巷道
|
-- 起点
|
task.start_wh_code = Wcstask.end_wh_code
|
task.start_area_code = Wcstask.end_area_code
|
task.start_loc_code = Wcstask.end_loc_code
|
|
-- 终点
|
task.end_wh_code = WmsTask.end_wh_code
|
task.end_area_code = WmsTask.end_area_code
|
task.end_loc_code = WmsTask.end_loc_code
|
-- task.type = wms_base.WMS_nConst(strLuaDEID, "任务类型-输送线出库搬运") -- 任务类型
|
-- task.schedule_type = wms_base.WMS_nConst(strLuaDEID, "调度类型-输送线") -- 设置调度类型
|
task.type = 2
|
task.schedule_type = 4
|
|
nRet, task = m3.CreateDataObj(strLuaDEID, task)
|
if (nRet ~= 0) then
|
lua.stop(strLuaDEID, "创建堆垛机任务失败!" .. task)
|
return 1
|
end
|
end
|
-- 设置任务为完成状态
|
local function SetTaskComplete(strLuaDEID, task_no)
|
lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete", "IN")
|
local nRet, task_info, strRetInfo, updateTask
|
local strCondition = "S_CODE = '" .. task_no.code .. "' "
|
nRet, updateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task", strCondition,
|
"S_B_STATE = '完成',N_B_STATE = 3")
|
if (nRet ~= 0) then
|
lua.stop(strLuaDEID, debug.getinfo(1), " SetTaskComplete 设置任务信息失败! " .. updateTask)
|
return 1
|
end
|
lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete 设置任务" .. task_no.code .. "为完成状态成功!",
|
updateTask)
|
end
|
--任务状态处理
|
local function TaskStatus(strLuaDEID, WcsTask, Status)
|
local nRet, UpdateTask, strRetInfo
|
|
--取货完成信号处理
|
if Status == 4 then
|
--修改任务状态
|
nRet, UpdateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task",
|
"S_CODE = '" .. WcsTask.code .. "'", "S_B_STATE = '取货完成'")
|
if nRet ~= 0 then
|
lua.Stop(strLuaDEID, "更新前叉任务状态(取货完成)" .. UpdateTask .. "失败! ")
|
return 1
|
end
|
--出库任务需要解锁货位
|
if WcsTask.type == 6 then
|
nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, WcsTask.start_loc_code,
|
WcsTask.cntr_code, "绑定解绑方法-系统", WcsTask.code .. "搬运完成")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, '货位容器绑定失败!' .. strRetInfo)
|
return 1
|
end
|
end
|
return 0
|
end
|
|
--任务完成信号处理
|
if Status == 2 then
|
SetTaskComplete(strLuaDEID, WcsTask)
|
--出库任务需要创建二段输送线任务
|
if (WcsTask.type == 6) then
|
CreatPutOutTask(strLuaDEID, WcsTask)
|
end
|
|
--入库任务需要设置作业完成
|
if (WcsTask.type == 5) then
|
nRet, strRetInfo = wms.wms_TaskFinish(strLuaDEID, WcsTask.code)
|
if (nRet ~= 0 or nRet == '') then
|
lua.stop(strLuaDEID, "任务编码='" .. WcsTask.code .. "的任务设置完成失败!" .. strRetInfo)
|
return 1
|
end
|
end
|
return 0
|
end
|
--更新任务取消
|
if Status == 7 then
|
--更新任务取消
|
nRet, UpdateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task",
|
"S_CODE = '" .. WcsTask.code .. "'", "S_B_STATE = '取消'")
|
if nRet ~= 0 then
|
lua.Stop(strLuaDEID, "更新前叉任务状态(取货完成)" .. UpdateTask .. "失败! ")
|
return 1
|
end
|
|
--更新作业取消
|
end
|
end
|
|
function DeviceStatus(strLuaDEID)
|
--设备编码
|
local device_codes = { "TC1", "TC2" }
|
--通信项编码
|
local comm_code = { "18", "21", "22", "23", "24", "25", "26", "27", "28" }
|
for i = 1, #device_codes do
|
local DeviceResult = ReadS7PLCCommsData(strLuaDEID, device_codes[i], comm_code)
|
|
if (DeviceResult ~= nil and DeviceResult ~= "") then
|
local ShakeHands = DeviceResult[1].value[1] --握手信号
|
local BeforeCompleted = DeviceResult[2].value
|
[1] --前叉任务完成
|
local AfterCompleted = DeviceResult[3].value
|
[1] --后叉任务完成
|
local BeforeAnomaly = DeviceResult[4].value
|
[1] --?前叉异常
|
local AfterAnomaly = DeviceResult[5].value
|
[1] --?后叉异常
|
local BeforePickup = DeviceResult[6].value
|
[1] --前叉取货完成信号
|
local AfterPickup = DeviceResult[7].value
|
[1] --后叉取货完成信号
|
local BeforeTaskNo = ReadPlcTaskNo(strLuaDEID, DeviceResult[8].value[1], DeviceResult[8].value[2]) -- todo 前叉任务号
|
local AfterTaskNo = ReadPlcTaskNo(strLuaDEID, DeviceResult[9].value[1], DeviceResult[9].value[2]) -- todo 后叉任务号
|
--前叉后叉都没有任务之间返回
|
if (BeforeTaskNo == '0' and AfterTaskNo == '0') then
|
return 0
|
end
|
if (ShakeHands == 1) then
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "18_WRITE", { 2 })
|
elseif (ShakeHands == 2) then
|
--!握手交互完清除我方通道
|
local value = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
WriteMultiS7PLCCommsData(strLuaDEID, device_codes[i], "03_WRITE", value)
|
elseif (ShakeHands == 0) then
|
--根据任务号获取任务信息
|
local nRet, BeforeTask, AfterTask, result
|
|
|
if (BeforeTaskNo ~= '0') then
|
local TaskSelect = "S_CODE = '" .. BeforeTaskNo .. "'"
|
nRet, BeforeTask = m3.GetDataObjByCondition(strLuaDEID, "Task", TaskSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID,
|
"获取前叉任务信息有误! " .. BeforeTask .. " SQL条件: " .. TaskSelect)
|
return 1
|
end
|
lua.DebugEx(strLuaDEID, '堆垛机状态交互=》查询前叉任务', BeforeTask)
|
--异常处理
|
if (BeforeAnomaly == 1 or BeforeAnomaly == 3) then
|
--修改任务状态为错误,等待人工确认
|
end
|
--取货完成信号处理
|
if (BeforePickup == 1 and BeforeTask.s_state == "已推送") then
|
result = TaskStatus(strLuaDEID, BeforeTask, 4)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "25_WRITE", { 1 })
|
end
|
--任务完成信号处理
|
if (BeforeCompleted == 1 and BeforeTask.s_state == "取货完成") then
|
result = TaskStatus(strLuaDEID, BeforeTask, 2)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "21_WRITE", { 1 })
|
end
|
--任务取消
|
if (BeforeCompleted == 3) then
|
result = TaskStatus(strLuaDEID, BeforeTask, 7)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "21_WRITE", { 3 })
|
end
|
end
|
|
if (AfterTaskNo ~= '0') then
|
local TaskSelect = "S_CODE = '" .. AfterTaskNo .. "'"
|
nRet, AfterTask = m3.GetDataObjByCondition(strLuaDEID, "Task", TaskSelect)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID,
|
"获取后叉任务信息有误! " .. AfterTask .. " SQL条件: " .. TaskSelect)
|
return 1
|
end
|
lua.DebugEx(strLuaDEID, '堆垛机状态交互=》查询前叉任务', AfterTask)
|
--取货完成信号处理
|
if (AfterPickup == 1 and AfterTask.s_state == "已推送") then
|
result = TaskStatus(strLuaDEID, AfterTask, 4)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "26_WRITE", { 1 })
|
end
|
--任务完成信号处理
|
if (AfterCompleted == 1 and AfterTask.s_state == "取货完成") then
|
result = TaskStatus(strLuaDEID, AfterTask, 2)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "22_WRITE", { 1 })
|
end
|
|
--任务取消
|
if (AfterCompleted == 3) then
|
result = TaskStatus(strLuaDEID, BeforeTask, 7)
|
if result ~= 0 then
|
return result
|
end
|
WriteS7PLCCommsData(strLuaDEID, device_codes[i], "22_WRITE", { 3 })
|
end
|
end
|
end
|
end
|
end
|
return 0
|
end
|