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 ams_plc = { _version = "0.1.1" } -- 读出线体任务号 转换为我们的任务号 function ams_plc.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 ams_plc.WritePlcTaskNo(strLuaDEID, taskno) -- 2. 截取倒数第五位到倒数第八位(共4个字符) local no1 = string.sub(taskno, -9, -6) -- 1. 截取最后四位 local no2 = string.sub(taskno, -4) -- 转换为数字再转回字符串(自动去除前导零) local Numberno1 = tonumber(no1) -- 转换为数字再转回字符串(自动去除前导零) local Numberno2 = tonumber(no2) return Numberno1, Numberno2 end -- 写连续数据到S7的通讯项 function ams_plc.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 --- 从内存中获取设备信息 function ams_plc.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 function ams_plc.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 return 0 end -- 根据任务号或托盘号返回当前任务信息 function ams_plc.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 -- 根据任务终点或托盘 返回是否有执行中的任务 yes/no function ams_plc.IsTaskExec(strLuaDEID, end_loc, cntr_code) local nRet, task_info, isexist -- 获取任务信息 if (end_loc ~= "") then local sqlCondition = "S_END_LOC = '" .. end_loc .. "' AND N_B_STATE = 2" nRet, isexist = mobox.existThisData(strLuaDEID, "Task", sqlCondition) if (nRet ~= 0) then lua.Error("IsTaskExec 获取【任务表】信息失败! " .. isexist) end -- lua.Debug(strLuaDEID, debug.getinfo(1), " IsTaskExec 获取【终点】" .. end_loc .. "信息成功!", isexist) end if (cntr_code ~= "") then local sqlCondition = "S_CNTR_CODE = '" .. cntr_code .. "' AND N_B_STATE = 2" nRet, isexist = mobox.existThisData(strLuaDEID, "Task", sqlCondition) if (nRet ~= 0) then lua.Error("IsTaskExec 获取【任务表】信息失败! " .. isexist) end -- lua.Debug(strLuaDEID, debug.getinfo(1), "IsTaskExec 获取【任务】" .. cntr_code .. "信息成功!", isexist) end return isexist end -- 更新输送线任务终点 function ams_plc.UpdateEndLoc(strLuaDEID, task_no, end_loc) -- lua.Debug(strLuaDEID, debug.getinfo(1), "UpdateEndLoc", "IN") local nRet, updateTask -- 更新容器表 local strCondition = "S_CODE = '" .. task_no .. "' " nRet, updateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task", strCondition, "S_END_LOC = '" .. end_loc .. "'") if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), " UpdateEndLoc 修改【任务终点】信息失败! " .. updateTask) end lua.Debug(strLuaDEID, debug.getinfo(1), "UpdateEndLoc 修改【任务" .. task_no .. "终点】信息成功!", updateTask) return 0 end -- 输入任务号,设置任务为完成状态 function ams_plc.SetTaskComplete(strLuaDEID, task_no) -- lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete", "IN") local nRet, task_info, strRetInfo, updateTask local strCondition = "S_CODE = '" .. task_no .. "' " 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 end lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete 设置任务" .. task_no .. "为完成状态成功!", updateTask) return 0 end -- 根据作业编码判断是否存在已创建的堆垛机任务 yes/no function ams_plc.IsExistTask(strLuaDEID, op_code) -- lua.Debug(strLuaDEID, debug.getinfo(1), " IsExistTask", "IN") local nRet, isExist_Task local sqlCondition = "S_OP_CODE = '" .. op_code .. "' AND N_TYPE = 5 " nRet, isExist_Task = mobox.existThisData(strLuaDEID, "Task", sqlCondition) if (nRet ~= 0) then lua.stop(" GetTaskInfo 获取【任务表】信息失败! " .. isExist_Task) return end -- lua.DebugEx(strLuaDEID, " GetTaskInfo 获取【任务】" .. op_code .. "信息成功!", isExist_Task) return isExist_Task end -- 根据堆垛机任务起点 判断是否存在已创建的堆垛机任务 yes/no function ams_plc.IsExistTaskByStartLoc(strLuaDEID, start_loc) -- lua.Debug(strLuaDEID, debug.getinfo(1), " IsExistTaskByStartLoc", "IN") local nRet, isExist_Task local sqlCondition = "S_START_LOC = '" .. start_loc .. "' AND N_TYPE = 5 AND N_B_STATE = 0 " nRet, isExist_Task = mobox.existThisData(strLuaDEID, "Task", sqlCondition) if (nRet ~= 0) then lua.stop(" GetTaskInfo 获取【任务表】信息失败! " .. isExist_Task) return end return isExist_Task end -- 判断有无正在执行的输送线入库任务 (入库接驳位时用,判断是否推双托) yes/no function ams_plc.IsExistLineTask(strLuaDEID) -- lua.DebugEx(strLuaDEID, "IsExistLineTask 查询有无正在执行的输送线入库任务", "IN") local sqlCondition = "N_TYPE = 1 and N_SCHEDULE_TYPE = 4 and N_B_STATE = 2" local nRet, isExist_LineTask = mobox.existThisData(strLuaDEID, "Task", sqlCondition) if (nRet ~= 0) then lua.stop(strLuaDEID, "IsExistLineTask 获取【任务表】信息失败! " .. isExist_LineTask) return end -- lua.DebugEx(strLuaDEID, "IsExistLineTask 查询有无正在执行的输送线入库任务返回", isExist_LineTask) return isExist_LineTask end -- 根据任务号/ 托盘码获取当前任务信息 (入库接驳位时用) function ams_plc.GetTaskInfobyTaskno(strLuaDEID, task_no, cntr_code) -- lua.DebugEx(strLuaDEID, "GetTaskInfobyTaskno!", "IN") local nRet, strRetInfo -- 根据任务号获取 if (task_no ~= "") then nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CODE = '" .. task_no .. "'", "T_CREATE DESC") if (nRet ~= 0) then lua.Stop(strLuaDEID, "GetTaskInfobyTaskno 获取任务" .. task_no .. "信息失败! " .. strRetInfo) return end end -- 根据托盘码获取输送线入库执行中的任务 if (cntr_code ~= "") then nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CNTR_CODE = '" .. cntr_code .. "' AND N_TYPE = 1 AND N_B_STATE = 2", "T_CREATE DESC") if (nRet ~= 0) then lua.Stop(strLuaDEID, "GetTaskInfobyTaskno 获取任务" .. task_no .. "信息失败! " .. strRetInfo) return end end -- lua.DebugEx(strLuaDEID, "GetTaskInfobyTaskno返回的任务信息:", strRetInfo) return strRetInfo end -- 托盘号查作业信息 (1072、1085、1099 入库称重点时用) function ams_plc.QueryOpbyTray(strLuaDEID, cntr_code, op_code) local nRet, strRetInfo -- lua.DebugEx(strLuaDEID, "QueryOpbyTray", "IN") if (cntr_code ~= "") then 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 end if (op_code ~= "") then local strCondition = "S_OP_CODE = '" .. op_code .. "'" nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Task", strCondition, "T_CREATE DESC") if (nRet ~= 0) then lua.Stop(strLuaDEID, "QueryOpbyTray 根据作业" .. op_code .. "获取任务信息信息失败! " .. strRetInfo) end end return strRetInfo end -- 创建入库输送线任务 1072->1019 -- 入库作业启动创建任务 创建后,将作业、输送线任务改为执行中 function ams_plc.PutInCreateTask(strLuaDEID, op_info, start_loc) local nRet, updateOperation, updateTask -- 创建输送线任务 -- 生成【任务】编码 local strCode local strHeader = 'TA' .. os.date("%y%m%d") .. '-' nRet, strCode = mobox.getSerialNumber("任务", strHeader, 4) if (nRet ~= 0) then lua.Stop(strLuaDEID, '申请【任务】编码失败!' .. strCode) return end local task = m3.AllocObject(strLuaDEID, "Task") task.code = strCode task.op_code = op_info.code -- 作业编码 task.op_name = op_info.op_def_name -- 作业名称 task.factory = op_info.factory -- 工厂 task.cntr_code = op_info.cntr_code -- 托盘 task.start_lan = op_info.start_aisle -- 巷道 -- 起点 task.start_wh_code = op_info.start_wh_code task.start_area_code = op_info.start_area_code task.start_loc_code = start_loc -- 起点货位 -- 终点 task.end_wh_code = op_info.end_wh_code task.end_area_code = op_info.end_area_code task.end_loc_code = "1019" -- 终点 task.bs_state = 2 task.type = 1 task.schedule_type = 4 -- lua.Debug(strLuaDEID, debug.getinfo(1), 'WCS作业启动:', "生成作业数据" .. task) -- 创建任务 nRet, task = m3.CreateDataObj(strLuaDEID, task) if (nRet ~= 0) then lua.Stop(strLuaDEID, "创建输送线任务失败!" .. task) return end -- 更新作业状态为 1 执行 local strCondition = "S_CODE = '" .. op_info.code .. "' " nRet, updateOperation = mobox.updateDataAttrByCondition(strLuaDEID, "Operation", strCondition, "N_B_STATE = 1,S_B_STATE = '执行'") if (nRet ~= 0) then lua.Stop(strLuaDEID, " UpdateEndLoc 修改【任务终点】信息失败! " .. updateOperation) return end return strCode end -- 创建堆垛机入库任务 (ok) function ams_plc.CreateTask(strLuaDEID, start_loc, task_info) lua.DebugEx(strLuaDEID, "创建堆垛机任务方法CreateTask", "IN") -- 申请一个堆垛机任务号 -- 生成一个任务号 local nRet, task_no local strCode local strHeader = 'TA' .. os.date("%y%m%d") .. '-' nRet, strCode = mobox.getSerialNumber("任务", strHeader, 4) if (nRet ~= 0) then lua.Stop(strLuaDEID, '申请【任务】编码失败!' .. strCode) return end local task = m3.AllocObject(strLuaDEID, "Task") task.code = strCode -- 任务号 task.op_code = task_info.op_code -- 作业编码 task.op_name = task_info.op_name -- 作业名称 task.factory = task_info.factory -- 工厂 task.cntr_code = task_info.cntr_code -- 托盘 -- 起点 task.start_wh_code = task_info.start_wh_code task.start_area_code = task_info.start_area_code task.start_loc_code = start_loc -- 终点 task.end_wh_code = task_info.end_wh_code task.end_area_code = task_info.end_area_code task.end_loc_code = start_loc -- task.type = wms_base.WMS_nConst(strLuaDEID, "任务类型-堆垛机入库搬运") -- 任务类型 -- task.schedule_type = wms_base.WMS_nConst(strLuaDEID, "调度类型-堆垛机") -- 设置调度类型 task.type = 5 task.schedule_type = 5 -- 创建任务 nRet, task = m3.CreateDataObj(strLuaDEID, task) if (nRet ~= 0) then lua.Stop(strLuaDEID, "创建堆垛机入库搬运任务失败!" .. task) return end lua.DebugEx(strLuaDEID, "创建【线体编号为" .. start_loc .. "的输送线任务】信息成功!", task) return strCode end -- 查作业信息 (1072、1085、1099 入库称重点时用) function ams_plc.QueryOpbyopcode(strLuaDEID, op_code) local nRet, strRetInfo -- lua.DebugEx(strLuaDEID, "QueryOpbyTray", "IN") local strCondition = "S_CODE = '" .. op_code .. "'" nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", strCondition, "T_CREATE DESC") if (nRet ~= 0) then lua.Stop(strLuaDEID, "QueryOpbyTray 根据作业号" .. op_code .. "获取作业信息失败! " .. strRetInfo) return end return strRetInfo end -- 根据起点、状态获取任务信息 (获取wms堆垛机任务终点时用) function ams_plc.Gettaskinfoforend(strLuaDEID, start_loc, status) local nRet, strRetInfo local strCondition = "S_START_LOC = '" .. start_loc .. "' AND N_B_STATE = " .. status -- lua.DebugEx(strLuaDEID, "Gettaskinfoforend strCondition", strCondition) nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Task", strCondition, "T_CREATE DESC") if (nRet ~= 0) then lua.Stop(strLuaDEID, "Gettaskinfoforend 根据起点" .. start_loc .. "获取作业信息失败! " .. strRetInfo) return end -- lua.DebugEx(strLuaDEID, "Gettaskinfoforend 返回的信息", strCondition) return strRetInfo end -- 接驳口创建设备动作优化用 function ams_plc.GetddjTaskInfo(strLuaDEID, op_code) local nRet, strRetInfo local strCondition = "S_OP_CODE = '" .. op_code .. "' AND N_TYPE = 5 AND N_B_STATE = 1 " local nRet, isExistTask = mobox.existThisData(strLuaDEID, "Task", strCondition) if (nRet ~= 0) then lua.stop(strLuaDEID, "IsExistLineTask 获取【任务表】信息失败! " .. isExistTask) return end -- lua.DebugEx(strLuaDEID, "IsExistLineTask 查询有无正在执行的输送线入库任务返回", isExist_LineTask) return isExistTask end return ams_plc