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
--[[
 编码: GT-40-46
 名称: 作业启动
 作者: LZH
 入口函数:OperationStart
 功能说明:
    创建一段AGV任务,从作业起点到回库接驳位并调用AGV的任务下发接口
 变更历史:
 --]]
require("WMS-BASE")
wms_op = require("wms_operation")
wms_wh = require("wms_wh")
wms_task = require("wms_task")
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 loc_start
    nRet, loc_start = wms_wh.Location_GetInfo(strLuaDEID, operation.start_loc_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "WMS_Location_GetInfo失败! " .. loc_start) end
 
    -- 获取起点库区信息
    local area
    local strCondition = "S_CODE = '" .. loc_start.area_code .. "'"
    nRet, area = m3.GetDataObjByCondition(strLuaDEID, "Area", strCondition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. area) end
 
    -- 通过楼层 + 库区位置 计算AGV的终点为哪个回库口库区
    -- 库区的备注为 半钢/全钢
    strCondition = "N_FLOOR = '" .. area.floor .. "' AND S_NAME LIKE '%" .. area.note .. "回库口%'"
    nRet, area = m3.GetDataObjByCondition(strLuaDEID, "Area", strCondition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "m3.GetDataObjByCondition 失败!" .. area) end
 
    -- 获取终点接驳位
    local end_loc_list
    local end_loc_code = ''
    strCondition = "N_LOCK_STATE = 0 AND S_AREA_CODE = '" .. area.code .. "'"
    nRet, end_loc_list = m3.QueryDataObject(strLuaDEID, "Location", strCondition)
    if (nRet ~= 0 or end_loc_list == '') then lua.Error(strLuaDEID, debug.getinfo(1), '查询货位失败!' .. end_loc_list) end
    for i = 1, #end_loc_list do
        local attrs = end_loc_list[i].attrs
        attrs = m3.KeyValueAttrsToObjAttr(attrs)
        if (attrs == nil) then goto coroutine end
        end_loc_code = attrs.S_CODE
        ::coroutine::
    end
 
    if (end_loc_code == '') then
        local msg
        msg = "作业编码为=" .. operation.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, end_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
 
    -- 创建 AGV 搬运任务
    local task = m3.AllocObject(strLuaDEID, "Task")
    task.op_code = operation.code    -- 作业编码
    task.op_name = operation.op_def_name
    task.factory = operation.factory -- 工厂
    task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-AGV入库搬运")
    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, "调度类型-AGV") -- 设置调度类型
    task.roadway = loc_end.roadway
    nRet, task = m3.CreateDataObj(strLuaDEID, task)
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "创建AGV入库任务失败!" .. task)
    end
 
    -- 如果是二楼回库口的话,需要给回库口货位上锁,在国自返回 放货完成允许离开 状态时解锁货位
    if (tonumber(area.floor) == 2) then
        nRet, strRetInfo = wms.wms_LockLocation(strLuaDEID, loc_end.code,
            wms_base.Get_nConst(strLuaDEID, "锁类型-入库锁"),
            operation.cntr_code, operation.cntr_code, operation.op_def_name)
        if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "wms_LockLocation 失败!" .. strRetInfo) end
    end
 
    -- 设置状态未推送
    wms_task.SetStateByCode(strLuaDEID, task.code, "任务状态-已推送")
 
    local data = {
        taskData = {
            taskNum = task.code,
            pickStation = task.start_loc_code,
            dropStation = task.end_loc_code,
            taskType = 2,
            carrierType = 1,
            priority = 1,
            wmsTaskNo = "",                 -- 出库才给
            grade = "",                     -- 入库等级是默认的
            produceTime = "",
            tyreType = "",
            supplier = "",
            receiveLot = "",
            subpool = ""
        },
        partData = {
            rfid = task.cntr_code,
            lotNumber = nil,
            partNumber = "KT",
            partDesc = "空托",
            partType = nil,
            weight = nil,
            unit = nil,
            maturityTime = nil,
            productionTime = nil,
            stewingTime = nil,
            overdueTime = nil
        }
    }
 
    local url = wms_base.Get_sConst(strLuaDEID, "AGV-url")
    local strurl = url .. "/CreateTask"
    local strBody = data
    local strHeader = ""
    nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "AGV", "任务创建")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "调用接口失败!" .. strRetInfo)
    end
end