1
Jianw
9 天以前 70f29da38121b9a467841253e3268feb5df02902
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
json = require("json")
mobox = require("OILua_JavelinExt")
m3 = require("oi_base_mobox")
wms_wh = require("wms_wh")
ams_plc = require("amsplc_base")
plc_base = require("plc_base")
function Main(strLuaDEID)
    -- -- 初始化 startList 表
    -- local startList = {{
    --     Loc = "JB-1-1-1",
    --     Cntr = '', -- 假设 ContainerHelper 已实现
    --     Weight = 500
    -- }, {
    --     Loc = "JB-1-1-2",
    --     Cntr = '',
    --     Weight = 300
    -- }}
    -- local endList = {}
    -- -- 查询 count1
    -- local strCondition1 = "N_ROW_GROUP = 1 AND N_CURRENT_NUM = 0 AND N_LAYER <= 3"
    -- local _, retInfo1 = mobox.queryDataObjAttr2(strLuaDEID, "Location", strCondition1, "", 0, "S_CODE") -- 获取总数量
    -- lua.Debug(strLuaDEID, debug.getinfo(1), "retInfo1!", retInfo1)
    -- local count1 = tonumber(json.decode(retInfo1).recordCount)
 
    -- -- 查询 count2
    -- local strCondition2 = "N_ROW_GROUP = 2 AND N_CURRENT_NUM = 0 AND N_LAYER <= 3"
    -- local _, retInfo2 = mobox.queryDataObjAttr2(strLuaDEID, "Location", strCondition2, "", 0, "S_CODE")
    -- lua.Debug(strLuaDEID, debug.getinfo(1), "retInfo2!", retInfo2)
    -- local count2 = tonumber(json.decode(retInfo2).recordCount)
 
    -- -- 确定排组
    -- local rowGroup = (count1 >= count2) and 1 or 2
    -- -- 获取算法模型列表(需实现 GetAlgorithmModelList 的 Lua 版本)
    -- local listAlgorithm = GetAlgorithmModelList(1, {rowGroup}, {2, 1}, {1, 2, 3})
    -- --lua.Debug(strLuaDEID, debug.getinfo(1), "listAlgorithm!", listAlgorithm)
    -- for i = 1, #listAlgorithm do
    --     -- 动态构建查询条件
    --     local algo = listAlgorithm[i]
    --     local data = queryLocationData(strLuaDEID, algo)
    --     lua.Debug(strLuaDEID, debug.getinfo(1), "data!", data)
    --     local needCount = #startList - #endList
    --     processLocations(strLuaDEID,data, endList, needCount)
    --     if #endList >= #startList then
    --         break
    --     end
 
    -- end
    -- if #endList > 0 then
    --     lua.Debug(strLuaDEID, debug.getinfo(1), "endList!", endList)
    -- end
    -- nRet, strRetInfo = wms_wh.Loc_Container_Unbinding( strLuaDEID, "S-B","FB-0010", "绑定解绑方法-系统",  "搬运完成" )
    -- if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '货位容器绑定失败!'..strRetInfo ) end 
    -- nRet, strRetInfo = wms_wh.Loc_Container_Unbinding( strLuaDEID, "S-E","FB-0045", "绑定解绑方法-系统",  "搬运完成" )
    -- if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '货位容器绑定失败!'..strRetInfo ) end     
    -- nRet, strRetInfo = wms_wh.Loc_Container_Unbinding( strLuaDEID, "S-F","FB-0042", "绑定解绑方法-系统",  "搬运完成" )
    -- if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '货位容器绑定失败!'..strRetInfo ) end     
    -- nRet, strRetInfo = wms_wh.Loc_Container_Unbinding( strLuaDEID, "S-G","FB-0044", "绑定解绑方法-系统",  "搬运完成" )
    -- if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), '货位容器绑定失败!'..strRetInfo ) end 
    local nRet, system_status = plc_base.ReadS7PLCCommsData(strLuaDEID, '192.168.100.128', "1001-N_CURRENT_NUM")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "读取设备通讯项失败!" .. system_status)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), 'system_status', system_status.value[1])
    
end
 
local function CheckInsideFree(strLuaDEID, loc)
    -- 构建查询条件
    local strCondition = string.format("S_AREA_CODE = '%s' AND N_ROW_GROUP = %d AND N_COL = %d " ..
                                           "AND N_POS = 1 AND N_CURRENT_NUM = 0 AND N_LOCK_STATE = 0", loc.S_AREA_CODE,
        loc.N_ROW_GROUP, loc.N_COL)
 
    -- 执行查询(假设 mobox.queryDataObjAttr2 接口与用户提供的示例一致)
    local nRet, strRetInfo = mobox.queryDataObjAttr2(strLuaDEID, "Location", strCondition, "", 1, "S_CODE")
    lua.Debug(strLuaDEID, debug.getinfo(1), "strRetInfo!", strRetInfo)
    local queryInfo = json.decode(strRetInfo)
 
    -- 处理结果
    if queryInfo.dataSet and #queryInfo.dataSet > 0 then
        return true
    else
        return false
    end
end
local function CheckCanIn(strLuaDEID, loc)
    lua.Debug(strLuaDEID, debug.getinfo(1), "loc!", loc)
    if loc.N_POS == 1 then
        return true
    else
        return CheckInsideFree(strLuaDEID,loc)
    end
end
 
function processLocations(strLuaDEID, data, endList, needCount)
    lua.Debug(strLuaDEID, debug.getinfo(1), "#data!", #data)
    lua.Debug(strLuaDEID, debug.getinfo(1), "needCount!", needCount)
    if #data > 0 then
        -- 按列号排序
        table.sort(data, function(a, b)
            return a.N_COL < b.N_COL
        end)
        -- 判断缺失数量
 
        if needCount == 2 and #data > 1 then
            -- 查找连续空位
            for j = 1, #data - 1 do
                if data[j + 1].N_COL == data[j].N_COL + 1 then
                    lua.Debug(strLuaDEID, debug.getinfo(1), "连续空位", data[j].S_CODE)
                    if CheckCanIn(strLuaDEID, data[j]) and CheckCanIn(strLuaDEID, data[j + 1]) then
                        table.insert(endList, data[j])
                        table.insert(endList, data[j + 1])
                    end
                    break
                end
            end
 
            -- 未找到连续则取前两个
            if #endList == 0 then
                local candidates = {}
                if data[1] then
                    table.insert(candidates, data[1])
                end
                if data[2] then
                    table.insert(candidates, data[2])
                end
                for _, candidate in ipairs(candidates) do
                    if candidate and CheckCanIn(strLuaDEID, candidate) then
                        table.insert(endList, candidate)
                    end
                end
            end
        else
            -- 单货位处理
            for j = 1, #data do
                if CheckCanIn(strLuaDEID, data[j]) then
                    table.insert(endList, data[j])
                    break
                end
            end
        end
    end
end
 
function queryLocationData(strLuaDEID, algo)
    local strCondition = string.format("N_LOCK_STATE = 0 AND N_CURRENT_NUM = 0 AND C_ENABLE = 'Y' " ..
                                           "AND N_AISLE = %d AND N_ROW_GROUP = %d AND N_POS = %d AND N_LAYER = %d",
        algo.Aisle, algo.RowGroup, algo.Pos, algo.Layer)
 
    -- 执行分页查询[6,7](@ref)
    local nRet, strRetInfo = mobox.queryDataObjAttr2(strLuaDEID, "Location", strCondition, " N_COL ", 100, "S_CODE", "N_COL",
        "S_AREA_CODE", "N_ROW_GROUP", "N_POS", "N_LAYER")
    local queryInfo = json.decode(strRetInfo)
    local data = {}
 
    -- 处理分页数据
    if queryInfo.dataSet then
        for _, item in ipairs(queryInfo.dataSet) do
            -- table.insert(data, {
            --     S_CODE = item.S_CODE,
            --     N_COL = tonumber(item.N_COL),
            --     S_AREA_CODE = item.S_AREA_CODE,
            --     N_ROW_GROUP = tonumber(item.N_ROW_GROUP),
            --     N_POS = tonumber(item.N_POS),
            --     N_LAYER = tonumber(item.N_LAYER)
            --     -- 其他字段按需提取...
            -- })
            if item.attrs then
                local newItem = {}
                for _, attr in ipairs(item.attrs) do
                    --lua.Debug(strLuaDEID, debug.getinfo(1), "attrs!", attr)
                    -- 根据属性名称映射字段[4,6](@ref)
                    if attr.attr == "S_CODE" then
                        newItem.S_CODE = attr.value
                    elseif attr.attr == "N_COL" then
                        newItem.N_COL = tonumber(attr.value)
                    elseif attr.attr == "S_AREA_CODE" then
                        newItem.S_AREA_CODE = attr.value
                    elseif attr.attr == "N_ROW_GROUP" then
                        newItem.N_ROW_GROUP = tonumber(attr.value)
                    elseif attr.attr == "N_POS" then
                        newItem.N_POS = tonumber(attr.value)
                    elseif attr.attr == "N_LAYER" then
                        newItem.N_LAYER = tonumber(attr.value)
                        -- 添加其他字段的映射...
                    end
                end
                table.insert(data, newItem)
            end
        end
    end
    
    return data
end
function GetAlgorithmModelList(v, rowGroup, pos, layer)
    local list = {} -- 初始化结果表
    -- 三重嵌套循环(顺序:rowGroup → pos → layer)
    for _, group in ipairs(rowGroup) do
        for _, p in ipairs(pos) do
            for _, l in ipairs(layer) do
                -- 生成算法模型对象并插入列表
                table.insert(list, {
                    Aisle = v,
                    RowGroup = group,
                    Pos = p,
                    Layer = l
                })
            end
        end
    end
 
    return list
end
function GetAlgorithmModelList1(v, rowGroup, pos, layer)
    local list = {} -- 初始化结果表
    -- 三重嵌套循环(顺序:rowGroup → pos → layer)
    for _, group in ipairs(rowGroup) do
        for _, p in ipairs(layer) do
            for _, l in ipairs(pos) do
                -- 生成算法模型对象并插入列表
                table.insert(list, {
                    Aisle = v,
                    RowGroup = group,
                    Pos = p,
                    Layer = l
                })
            end
        end
    end
 
    return list
end