wsz
2025-06-20 19898bd66dec87b500b200d5d50961d0fb538ce5
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
--[[
    编码: WMS-40-20#1
    名称: 作业-成品入库-启动
    作者:HAN  
    日期:2025-1-29
 
    版本: V2.1
    
    说明:该脚本和项目关联度较高,会根据项目不同而不同
    函数: OperationStart
 
    输入条件:
        1)  globa_attr 全局属性
            {
                factory:"0000"
 
                -- 作业起始货位信息
                start_wh:"W001"                 
                start_area:"AL1-01"
                start_loc:"LL1-1001"
                start_site_layer:"0"
                start_site:"1001"
                
                -- 作业信息
                op_id:"{5F0C57D6-85BB-4578-AF13-89C0BB7A3BC9}"
                op_type:"1"
                op_code:"OP230926-00672"
 
                cntr_code:"TP20000001"
            }
        2) dataJson -- 后台匹配到的 作业定义属性
            {
                factory:"0000"                
                start_wh:"W001"
                start_area:"AL1-01"   
                end_wh:"W001"
                end_area:"xxxx"
 
                enable:1            -- 是否启用
                type:1              -- 作业类型
 
                name:"成品入库"      -- 作业定义名称
                code:"OP011"        -- 作业定义编码
                priority:1
                work_mode:0
            }
            
        3) dataobj  -- Operation 对象
 
    功能:
        1)根据入库规则计算出入库位置
        2)创建2个任务
            1 -- 线体把货品搬运到 1,3,4,5 楼的线体入库口,任务明确起止点即可,通知PLC
            2 -- NDC 从线体出口搬运到 指定货位
    更改说明:
 
--]]
 
wms_op = require( "wms_operation" )
wms_wh = require( "wms_wh" )
 
function OperationStart ( strLuaDEID ) 
    local nRet, strRetInfo
 
    lua.Debug( strLuaDEID, debug.getinfo(1), "OperationStart", 1 )
 
    -- 通过全局参数 获取触发【作业】启动事件时服务端设置的一些输入参数
    -- start_loc,op_code,start_area, cntr_code 详细见mobox-WMS 编程指南
    local global_attrs
    nRet, global_attrs = m3.GetSysGloablAttr( strLuaDEID )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), global_attrs ) end
 
    -- 获取当前的【作业定义】 信息
    -- 根据【作业定义】中定义的终点仓库、库区、货位信息确定入库的目标货位
    local op_def
    nRet, op_def = m3.GetSysDataJson( strLuaDEID )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), op_def ) end
    local end_wh = ''
    local end_area = ''
    local end_loc = ''
 
    -- 获取作业对象, 从作业对象中获取{扩展数据}, 从{扩展数据}获取线体设备编码
    local operation_obj = {}
    nRet, operation_obj = m3.GetSysCurEditDataObj( strLuaDEID, "Operation" )
    if (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1), "获取作业对象属性失败!"..operation_obj ) end
 
    local str_ext_attr = operation_obj.ext_data
    if ( str_ext_attr == nil or str_ext_attr == '' ) then lua.Error( strLuaDEID, debug.getinfo(1), "作业对象中扩展数据属性不能为空或nil!" ) end
 
    if ( op_def.type == wms_base.Get_nConst(strLuaDEID, "作业类型-入库") ) then
        -- 判断作业定义里是否 确定了入库仓库
        end_wh = lua.Get_StrAttrValue( op_def.end_wh )
        if ( end_wh == '' ) then lua.Error( strLuaDEID, debug.getinfo(1), "编号:"..op_def.code.." 的作业定义不完整,必须要有入库终点仓库定义!" ) end 
        end_area = lua.Get_StrAttrValue( op_def.end_area )
 
        local store_loc = nil
        local docking_loc = nil
 
        if ( end_area == '' ) then
            -- 启动入库规则计算货位
            local data_json = {}
            local ret_loc
            data_json.cntr_code = global_attrs.cntr_code
 
            -- 返回格式:{"wh_code":"W001","area_code":"AF3-01","code":"LF3-1082","agv_site":"172","agv_site_layer":0}
            ret_loc = wms_wh.GetWHAvaliableLocByRule( strLuaDEID, end_wh, "成品入库", lua.table2str(data_json) )
            if (ret_loc == '') then
                -- 没有满足条件的货位可以分配
                -- ?? 需要进一步处理 是否有反馈到前端
                lua.Debug( strLuaDEID, debug.getinfo(1), "wms_GetWHAvaliableLocByRule", "没有满足条件的货位可以分配" ) 
                return
            end
            nRet, store_loc = wms_wh.GetLocInfo( ret_loc.code )
            f (nRet ~= 0) then lua.Error( strLuaDEID, debug.getinfo(1),"获取货位信息失败!"..store_loc) end
    
            -- 从货位所在的库区获取接驳位
            nRet, docking_loc = wms_wh.Get_Area_OneFuncLoc( strLuaDEID, ret_loc.area_code, "功能区类型-入库接驳")
            if (nRet ~= 0) then
                lua.Error( strLuaDEID, debug.getinfo(1), "获取库区的入库接驳位失败!"..docking_loc) 
            end
            
        else
            lua.Error( strLuaDEID, debug.getinfo(1), "成品入库不支持由作业定义中就指定库区!") 
        end
 
        -- 检查一下数据完整性
        if ( store_loc == nil or store_loc.loc_code == '') then lua.Error( strLuaDEID, debug.getinfo(1), "wms_GetWHAvaliableLocByRule返回值中不存在货位信息!") end
        if ( docking_loc == nil or docking_loc.loc_code == '') then 
            lua.Error( strLuaDEID, debug.getinfo(1), "wms_GetWHAvaliableLocByRule返回值中不存在接驳信息, 请检查相关逻辑库区的接驳定义!") 
        end
 
        -- 设置作业对象的终点货位信息
        nRet, strRetInfo = wms_op.SetEndLoc( strLuaDEID, global_attrs.op_code, store_loc.code, store_loc.area_code, store_loc.wh_code )
        if ( nRet ~= 0 ) then
            strErr = "设置作业编码 = '"..global_attrs.op_code.."' 的终点货位信息失败! "..strRetInfo
            lua.Error( strLuaDEID, debug.getinfo(1), strErr )
        end        
  
        -- AGV搬运任务需要 货位的 AGV站点 属性,因此需要根据货位去获取一下站点信息
        -- docking_loc.loc_code,store_loc.loc_code
        -- 获取货位信息
        local loc_from, loc_to
        nRet, loc_from = wms_wh.Location_GetInfo( strLuaDEID, docking_loc.loc_code )
        if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1),"wms_wh.Location_GetInfo 失败!"..loc_from) end
        nRet, loc_to = wms_wh.Location_GetInfo( strLuaDEID, store_loc.loc_code )
        if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1),"wms_wh.Location_GetInfo 失败!"..loc_from) end
 
        -- 创建2个任务,一个输送机搬运一个AGV搬运
        local task = m3.AllocObject(strLuaDEID,"Task")
 
        -- 创建输送机搬运任务
        task.op_code = global_attrs.op_code                         -- 作业编码
        task.sourc_sys = operation_obj.source_sys                   -- 来源系统
        task.op_name = operation_obj.op_def_name
        task.factory = global_attrs.factory                         -- 工厂
        task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-输送机入库搬运")     -- 任务类型 = 输送带搬运
        task.eq_code = str_ext_attr                                  -- 作业中的扩展属性保存了输送线设备编码
        task.cntr_code = operation_obj.cntr_code
        -- 起点
        task.start_wh_code = global_attrs.start_wh
        task.start_area_code  = global_attrs.start_area
        task.start_loc_code  = global_attrs.start_loc
        -- 终点
        task.end_wh_code  = docking_loc.wh_code
        task.end_area_code  = docking_loc.area_code
        task.end_loc_code  = docking_loc.code
        task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-输送机") -- 设置调度类型
        
        nRet, task = m3.CreateDataObj(strLuaDEID, task)
 
        if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1),"创建输送机任务失败!"..task) end
        -- 接驳位加入库锁
        -- 货位锁定 +入库锁
        
        lua.Debug( strLuaDEID, debug.getinfo(1), "wms_LockLocation1", task.end_loc_code..' '..task.code.." "..task.op_code.." "..task.op_name )
        -- 把目的货位加锁
        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 失败!"..strRetInfo ) end   
 
        -- 创建AGV搬运任务
        task = m3.AllocObject(strLuaDEID,"Task")
        task.op_code = global_attrs.op_code                         -- 作业编码
        task.op_name = operation_obj.op_def_name
        task.factory = global_attrs.factory                         -- 工厂
        task.type = wms_base.Get_nConst(strLuaDEID,"任务类型-AGV入库搬运")     
        task.cntr_code = operation_obj.cntr_code
 
        -- 起点
        task.start_wh_code = docking_loc.wh_code
        task.start_area_code  =  docking_loc.area_code
        task.start_loc_code  = docking_loc.code
        task.start_site = loc_from.agv_site
        -- 终点
        task.end_wh_code  = store_loc.wh_code
        task.end_area_code  = store_loc.area_code
        task.end_loc_code  = store_loc.code
        task.end_site = loc_to.agv_site
 
        task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-NDC")   -- 设置调度类型
    
        nRet, task = m3.CreateDataObj(strLuaDEID, task)
        if (nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1),"创建AGV任务失败!"..task) end     
    
        lua.Debug( strLuaDEID, debug.getinfo(1), "wms_LockLocation2", task.end_loc_code..' '..task.code.." "..task.op_code.." "..task.op_name )
        -- 货位+入库锁
        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 失败!"..strRetInfo ) end              
    else
        lua.Warning( strLuaDEID, debug.getinfo(1), "作业定义的类型不是入库类型!" ) 
    end
 
end