lzh
2025-06-19 3a6436e0c88042c6ce8dca2fe8adb0109f0ad9e4
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
--[[
 编码: GT-40-34
 名称: 作业启动
 作者: LZH
 入口函数:OperationStart
 功能说明:
    创建一段国自任务,从作业起点到立库并调用国自的任务下发接口
 变更历史:
    V1.1 LZH 20241223 当站点为左侧 空载具/半料回库口 时,终点只能入5、6巷道
 --]]
require("WMS-BASE")
wms_op = require("wms_operation")
wms_wh = require("wms_wh")
wms_task = require("wms_task")
require("GT_InAndOutboundPolicies")
require("GT-Base")
function OperationStart(strLuaDEID)
    local nRet, strRetInfo
    -- 获取作业对象, 从作业对象中获取{扩展数据}, 从{扩展数据}获取线体设备编码
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "获取作业对象属性失败!" .. operation)
    end
 
    -- 解析作业中的扩展参数
    local ext_data, success
    success, ext_data = pcall(json.decode, operation.ext_data)
    if (success == false) then
        lua.Error(strLuaDEID, debug.getinfo(1), "operation_obj.ext_data 中内容JSON格式不合法!")
    end
    local rowday = ext_data.roadway_inventory
    lua.Debug(strLuaDEID, debug.getinfo(1), '胶料入库作业扩展参数!', ext_data)
 
    -- 计算回库口终点,如果回库口没有可入库货位则进入等待状态
    local loc_code
    -- V1.1 LZH 当站点为左侧 空载具/半料回库口 和三楼回库口时,终点只能入3\4巷道
    if (operation.start_loc_code == wms_base.Get_sConst(strLuaDEID, "KL_ReturnRgvStation1") or
            operation.start_loc_code == wms_base.Get_sConst(strLuaDEID, "F3L_InWeight1") or
            operation.start_loc_code == wms_base.Get_sConst(strLuaDEID, "F3R_InWeight1")) then
        local roadway_list = lua.split(rowday, ",")
        local str = ''  -- 用来拼接巷道查询条件字符串
        for i = 1, #roadway_list do
            if (roadway_list[i] == '3' or roadway_list[i] == '4') then
                str = str .. roadway_list[i] .. ","
            end
        end
        if (str == '') then lua.Error(strLuaDEID, debug.getinfo(1), "没有可入库巷道!") end
        -- 去除最后一个,
        str = lua.trim_laster_char(str)
        -- 可用巷道里面只能入3、4巷道
        nRet, loc_code = Empty_StorageTactics(strLuaDEID, operation.cntr_code, str)
        lua.Debug(strLuaDEID, debug.getinfo(1), 'loc_code', loc_code)
    else
        nRet, loc_code = Empty_StorageTactics(strLuaDEID, operation.cntr_code, rowday)
        lua.Debug(strLuaDEID, debug.getinfo(1), 'loc_code', loc_code)
    end
 
    lua.Debug(strLuaDEID, debug.getinfo(1), 'loc_code', loc_code)
 
    if (nRet ~= 0) then
        local msg
        msg = "作业编码为=" .. operation.code .. " 创建任务失败, 获取终点失败!" .. loc_code
        lua.Warning(strLuaDEID, debug.getinfo(1), msg)
        lua.Wait(strLuaDEID, msg)
        return
    end
 
    local loc_end
    nRet, loc_end = wms_wh.Location_GetInfo(strLuaDEID, loc_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败! " .. loc_end) end
 
    -- 更新作业终点货位信息
    nRet, strRetInfo = wms_op.SetEndLoc(strLuaDEID, operation.code, loc_end.code, loc_end.area_code,
        loc_end.wh_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "更新作业终点失败! " .. strRetInfo) end
 
    local strHeader = 'TA' .. os.date("%y%m%d") .. '-'
    local strCode
    nRet, strCode = mobox.getSerialNumber("任务", strHeader, 5)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '申请【任务】编码失败!' .. strCode) end
 
    -- 创建 国自 搬运任务
    local task = m3.AllocObject(strLuaDEID, "Task")
    task.code = strCode               -- 任务编码
    task.op_code = operation.code     -- 作业编码
    task.op_name = operation.op_def_name
    task.factory = operation.factory  -- 工厂
    task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-立库入库搬运")
    task.cntr_code = operation.cntr_code
    -- 起点
    task.start_wh_code = operation.start_wh_code
    task.start_area_code = operation.start_area_code
    task.start_loc_code = operation.start_loc_code
    -- 终点
    task.end_wh_code = loc_end.wh_code
    task.end_area_code = loc_end.area_code
    task.end_loc_code = loc_end.code
    task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-国自") -- 设置调度类型
    task.roadway = loc_end.roadway
    nRet, task = m3.CreateDataObj(strLuaDEID, task)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "创建AGV入库任务失败!" .. task)
    end
    -- 终点加锁操作(起点为站点有WCS管控)
    nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, task.end_loc_code, wms_base.Get_nConst(strLuaDEID, "锁类型-入库锁"),
        task.code,
        task.op_code, task.op_name)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1),
            "wms_LockLocation 失败! 货位='" .. task.end_loc_code .. "'  " .. strRetInfo)
    end
 
    -- 出库不指定具体终点,有WCS反馈终点,获取出库口库区绑定的WCS站点
    local condition = "S_VALUE = '" .. task.start_loc_code .. "' AND S_NOTE LIKE '%入库站台%'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo) end
    local loc_code_zd = strRetInfo.name
 
    -- 下发任务给国自
    local data = {}
    local strGUID = lua.guid()
    data.req_no = strGUID
    data.task_type = 3
    data.task_no = task.code
    data.tunnel_no = loc_end.roadway
    data.from_pos = loc_code_zd
    data.to_pos = task.end_loc_code
    data.mat_code = task.cntr_code
    data.req_time = os.date("%Y-%m-%d %H:%M:%S")
    data.mat_size = 1  -- 空托返回1
    lua.Debug(strLuaDEID, debug.getinfo(1), 'data', data)
 
    -- 调用国自的任务下发接口
    local url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
    local strurl = url .. "/create"
    local strHeader = ""
    local strBody = {}
    strBody[1] = data
    lua.Debug(strLuaDEID, debug.getinfo(1), 'strBody', strBody)
    nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "WCS", "任务创建")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
    end
 
    -- 设置状态未推送
    wms_task.SetStateByCode(strLuaDEID, task.code, "任务状态-已推送")
end