m3 = require("oi_base_mobox")
|
lua = require("oi_base_func")
|
json = require("json")
|
mobox = require("OILua_JavelinExt")
|
wms_base = require("wms_base")
|
wms_cntr = require("wms_container")
|
|
--- 主动获取线体通讯项信
|
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
|
|
-- 托盘号查作业信息 (1072、1085、1099 入库称重点时用)
|
local function QueryOpbyTray(strLuaDEID, cntr_code)
|
local nRet, strRetInfo
|
|
lua.DebugEx(strLuaDEID, "QueryOpbyTray", "IN")
|
local strCondition = "S_CNTR_CODE = '" .. cntr_code .. "'"
|
nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", strCondition, "T_CREATE DESC")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "QueryOpbyTray 根据托盘" .. cntr_code .. "获取作业信息失败! " .. strRetInfo)
|
return
|
end
|
return strRetInfo
|
end
|
|
-- 根据任务号返回当前任务
|
local function ReturnTaskEnd(strLuaDEID, task_no, cntr_code)
|
lua.Debug(strLuaDEID, debug.getinfo(1), "ReturnTaskEnd:", "IN")
|
|
local nRet, task_info, end_loc
|
if (task_no ~= "") then
|
nRet, task_info = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CODE = '" .. task_no .. "'", "T_CREATE DESC")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, " ReturnTaskEnd 获取属性失败! " .. task_info)
|
end
|
lua.DebugEx(strLuaDEID, "task_info:", task_info)
|
|
-- end_loc = task_info.end_loc_code -- 当前任务终点
|
end
|
|
if (cntr_code ~= "") then
|
nRet, task_info = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CNTR_CODE = '" .. cntr_code .. "'",
|
"T_CREATE DESC")
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, " ReturnTaskEnd 获取属性失败! " .. task_info)
|
end
|
-- end_loc = task_info.end_loc_code -- 当前任务终点
|
end
|
lua.DebugEx(strLuaDEID, "ReturnTaskEnd end_loc:", end_loc)
|
|
return task_info
|
end
|
|
-- 读出线体任务号 转换为我们的任务号
|
local function ReadPlcTaskNo(strLuaDEID, taskno1, taskno2)
|
lua.Debug(strLuaDEID, debug.getinfo(1), "ReadPlcTaskNo:", "IN")
|
|
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
|
|
-- 主函数
|
function PLCStatusChange(strLuaDEID)
|
local nRet, strRetInfo
|
local input_datajson
|
|
-- 设备编码, 线体编号, 信号,订阅名称
|
local device_code, unit_code, value, task_info, condition_flag, isexit
|
-- 获取接口中的Data
|
nRet, input_datajson = m3.GetSysDataJson(strLuaDEID)
|
if (nRet ~= 0) then
|
lua.Stop(strLuaDEID, "PLCStateChange无法获取数据包!" .. input_datajson)
|
return 1
|
end
|
-- 打印获取的设备信息
|
lua.DebugEx(strLuaDEID, "data_objs-->", input_datajson)
|
-- 解析数据
|
device_code = lua.Get_StrAttrValue(input_datajson.device_code) -- 设备编码
|
unit_code = lua.Get_StrAttrValue(input_datajson.unit_code) -- 线体编号
|
-- condition_flag = lua.Get_StrAttrValue(input_datajson.condition_flag) --订阅名称
|
value = input_datajson.value[1]
|
if (value ~= 3) then
|
lua.DebugEx(strLuaDEID, "工作模式不为3,直接返回", value)
|
return 0
|
end
|
|
lua.DebugEx(strLuaDEID, "unit_code", unit_code)
|
lua.DebugEx(strLuaDEID, "value", value)
|
|
-- 扫码位
|
if (unit_code == "1072" or unit_code == "1085" or unit_code == "1099") then
|
-- 读取托盘码
|
local cntr_info = ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", {unit_code .. "-S_CNTR_NO"})
|
lua.DebugEx(strLuaDEID, "cntr_info", cntr_info)
|
cntr_code = cntr_info[1].value
|
lua.DebugEx(strLuaDEID, "cntr_code", cntr_code)
|
-- 查询作业号
|
local op_info = QueryOpbyTray(strLuaDEID, cntr_code)
|
|
lua.DebugEx(strLuaDEID, "作业信息", op_info)
|
local op_code = op_info.code
|
|
local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_OP_CODE = '" .. op_code .. "' "
|
nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
|
return
|
end
|
|
if (isexit == 'yes') then
|
lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
|
return 0
|
end
|
-- 判断当前车辆的动作码是否已经在队列中, 如果已经存在则不做处理
|
local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
|
mq_eq_action.op_code = op_code
|
mq_eq_action.eq_code = unit_code
|
mq_eq_action.action_code = 1
|
mq_eq_action.cntr_code = cntr_code
|
-- 把车辆动作写入队列
|
mq_eq_action.factory = "0001"
|
mq_eq_action.eq_type = 4
|
mq_eq_action.eq_type_name = "输送线"
|
|
lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
|
end
|
|
lua.DebugEx(strLuaDEID, "成功", "成功")
|
end
|
|
-- 分支点
|
if (unit_code == "1019" or unit_code == "1026") then
|
local is_task_running
|
local front_line_code -- 前叉线体编号
|
local back_line_code -- 后叉线体编号
|
local current_task_no_key -- :当前任务号通讯项
|
local front_fork_task_key -- :前叉任务号通讯项 (用于校验终点)
|
local front_fork_mode_key -- :前叉工作模式通讯项 (用于判断有载状态)
|
local back_fork_mode_key -- :后叉工作模式通讯项 (用于判断有载状态)
|
local next_branch_code -- :下个分支点编号
|
|
-- 前叉后叉 线体编号
|
if (unit_code == "1019") then
|
front_line_code = "1035" -- 前叉编号
|
back_line_code = "1033" -- 后叉编号
|
current_task_no_key = "1019-S_TASK_NO"
|
front_fork_task_key = "1035-S_TASK_NO"
|
front_fork_mode_key = "1035-WORK_MODE"
|
back_fork_mode_key = "1033-WORK_MODE"
|
next_branch_code = "1026"
|
elseif (unit_code == "1026") then
|
front_line_code = "1047"
|
back_line_code = "1045"
|
current_task_no_key = "1026-S_TASK_NO"
|
front_fork_task_key = "1047-S_TASK_NO"
|
front_fork_mode_key = "1047-WORK_MODE"
|
back_fork_mode_key = "1045-WORK_MODE"
|
next_branch_code = "1019"
|
end
|
|
-- 读当前任务号校验终点
|
local current_task_no = ReadS7PLCCommsData(strLuaDEID, device_code, {current_task_no_key})
|
if current_task_no then
|
-- 如果是数组类型(如[612,10]),需要根据业务需求处理:
|
-- 方案1:取第一个元素
|
local task_number = current_task_no[1].value[1]
|
local task_number2 = current_task_no[1].value[2]
|
if (task_number == 0 or task_number2 == 0) then
|
lua.DebugEx(strLuaDEID, "任务号为空已退出", "")
|
return
|
end
|
lua.DebugEx(strLuaDEID, "读到的任务号", current_task_no)
|
-- 将读到的任务号转换为我们的任务号
|
local us_task_no = ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
|
|
-- 通过任务号当前任务的终点
|
local mst = ReturnTaskEnd(strLuaDEID, us_task_no, "")
|
lua.DebugEx(strLuaDEID, "通过任务号查到的终点", mst.end_loc_code)
|
|
-- 终点为当前线体时
|
if (mst.end_loc_code == unit_code) then
|
-- 判断当前车辆的动作码是否已经在队列中, 如果已经存在则不做处理
|
|
local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_TASK_CODE = '" .. us_task_no .. "'"
|
nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
|
return
|
end
|
|
if (isexit == 'yes') then
|
lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
|
return 0
|
end
|
|
local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
|
mq_eq_action.eq_code = unit_code
|
mq_eq_action.action_code = 2
|
mq_eq_action.task_code = us_task_no
|
-- 把车辆动作写入队列
|
mq_eq_action.factory = "0001"
|
mq_eq_action.eq_type = 4
|
mq_eq_action.eq_type_name = "输送线"
|
|
lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
|
end
|
else
|
WriteS7PLCCommsData(strLuaDEID, "S7_LINE_01", "1019-TARGRT_ADDRESS", {tonumber(mst.end_loc_code)})
|
end
|
end
|
end
|
|
-- 接驳位
|
if (unit_code == "1035" or unit_code == "1033" or unit_code == "1047" or unit_code == "1045") then
|
|
-- 获取当前位置的任务号
|
local current_task_no = ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", {unit_code .. "-S_TASK_NO"}); -- 当前任务号
|
lua.DebugEx(strLuaDEID, "PlcConnection当前位置" .. unit_code .. "返回的任务号", "IN")
|
|
if current_task_no then
|
-- 如果是数组类型(如[612,10]),需要根据业务需求处理:
|
-- 方案1:取第一个元素
|
local task_number = current_task_no[1].value[1]
|
local task_number2 = current_task_no[1].value[2]
|
if (task_number == 0 or task_number2 == 0) then
|
lua.DebugEx(strLuaDEID, "读到的任务号为0,不执行", task_number)
|
return
|
end
|
|
lua.DebugEx(strLuaDEID, "读到的任务号", current_task_no)
|
-- 将读到的任务号转换为我们的任务号
|
local us_task_no = ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
|
-- 通过任务号当前任务的终点
|
local mst = ReturnTaskEnd(strLuaDEID, us_task_no, "")
|
lua.DebugEx(strLuaDEID, "通过任务号得到的终点", mst.end_loc_code)
|
|
-- 终点为当前线体时
|
if (mst.end_loc_code == unit_code) then
|
|
local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_TASK_CODE = '" .. us_task_no .. "'"
|
nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
|
return
|
end
|
|
if (isexit == 'yes') then
|
lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
|
return 0
|
end
|
|
local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
|
mq_eq_action.eq_code = unit_code
|
mq_eq_action.action_code = 3
|
mq_eq_action.task_code = us_task_no
|
mq_eq_action.op_code = mst.op_code
|
-- 把车辆动作写入队列
|
mq_eq_action.factory = "0001"
|
mq_eq_action.eq_type = 4
|
mq_eq_action.eq_type_name = "输送线"
|
|
lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
|
nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
|
if (nRet ~= 0) then
|
lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
|
end
|
end
|
end
|
end
|
end
|