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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
--[[
   编码: WMS-38-XX
   名称:
   作者:李帅帅
   日期:2025-06-23
 
   函数: Main
   功能:
 
   更改记录:
 
--]]
json   = require("json")
mobox  = require("OILua_JavelinExt")
m3     = require("oi_base_mobox")
wms_wh = require("wms_wh")
 
-- 从内存中获取设备信息
local function 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
local function 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
end
 
-- 写连续数据到S7的通讯项
local function 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
 
-- 读出线体任务号 转换为我们的任务号
local function ReadPlcTaskNo(strLuaDEID, taskno1, taskno2)
    lua.Debug(strLuaDEID, debug.getinfo(1), "ReadPlcTaskNo:", "IN")
    if (taskno1 == 0 or taskno2 == 0) then
        return '0'
    end
    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
 
--出库任务堆垛机任务完成创建输送线任务
local function CreatPutOutTask(strLuaDEID, Wcstask)
    -- region 创建出库任务
    local AfterstrCode, nRet
 
    local AfterstrHeader = 'TA' .. os.date("%y%m%d") .. '-'
    nRet, AfterstrCode = mobox.getSerialNumber("任务", AfterstrHeader, 4)
    if (nRet ~= 0)
    then
        lua.Stop(strLuaDEID, '申请【任务】编码失败!' .. AfterstrCode)
        return 1
    end
    local WmsTask
    local WmsTaskSelect = "S_CODE = '" .. Wcstask.op_code .. "'"
    nRet, WmsTask = m3.GetDataObjByCondition(strLuaDEID, "Operation", WmsTaskSelect)
    if (nRet ~= 0) then
        lua.stop(strLuaDEID, 'WCS创建输送线任务=》出库:查询作业信息错误!' .. WmsTask)
        return 1
    end
 
    local task = m3.AllocObject(strLuaDEID, "Task")
    task.code = AfterstrCode
    task.op_code = Wcstask.op_code     -- 作业编码
    task.op_name = Wcstask.op_name     -- 作业名称
    task.factory = Wcstask.factory     -- 工厂
    task.cntr_code = Wcstask.cntr_code -- 托盘
    -- task.start_lan = Wcstask.start_lan         -- 巷道
    -- 起点
    task.start_wh_code = Wcstask.end_wh_code
    task.start_area_code = Wcstask.end_area_code
    task.start_loc_code = Wcstask.end_loc_code
 
    -- 终点
    task.end_wh_code = WmsTask.end_wh_code
    task.end_area_code = WmsTask.end_area_code
    task.end_loc_code = WmsTask.end_loc_code
    -- task.type = wms_base.WMS_nConst(strLuaDEID, "任务类型-输送线出库搬运") -- 任务类型
    -- task.schedule_type = wms_base.WMS_nConst(strLuaDEID, "调度类型-输送线") -- 设置调度类型
    task.type = 2
    task.schedule_type = 4
 
    nRet, task = m3.CreateDataObj(strLuaDEID, task)
    if (nRet ~= 0) then
        lua.stop(strLuaDEID, "创建堆垛机任务失败!" .. task)
        return 1
    end
end
-- 设置任务为完成状态
local function SetTaskComplete(strLuaDEID, task_no)
    lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete", "IN")
    local nRet, task_info, strRetInfo, updateTask
    local strCondition = "S_CODE = '" .. task_no.code .. "' "
    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 1
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), " SetTaskComplete 设置任务" .. task_no.code .. "为完成状态成功!",
        updateTask)
end
--任务状态处理
local function TaskStatus(strLuaDEID, WcsTask, Status)
    local nRet, UpdateTask, strRetInfo
 
    --取货完成信号处理
    if Status == 4 then
        --修改任务状态
        nRet, UpdateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task",
            "S_CODE = '" .. WcsTask.code .. "'", "S_B_STATE = '取货完成'")
        if nRet ~= 0 then
            lua.Stop(strLuaDEID, "更新前叉任务状态(取货完成)" .. UpdateTask .. "失败! ")
            return 1
        end
        --出库任务需要解锁货位
        if WcsTask.type == 6 then
            nRet, strRetInfo = wms_wh.Loc_Container_Unbinding(strLuaDEID, WcsTask.start_loc_code,
                WcsTask.cntr_code, "绑定解绑方法-系统", WcsTask.code .. "搬运完成")
            if (nRet ~= 0) then
                lua.Stop(strLuaDEID, '货位容器绑定失败!' .. strRetInfo)
                return 1
            end
        end
        return 0
    end
 
    --任务完成信号处理
    if Status == 2 then
        SetTaskComplete(strLuaDEID, WcsTask)
        --出库任务需要创建二段输送线任务
        if (WcsTask.type == 6) then
            CreatPutOutTask(strLuaDEID, WcsTask)
        end
 
        --入库任务需要设置作业完成
        if (WcsTask.type == 5) then
            nRet, strRetInfo = wms.wms_TaskFinish(strLuaDEID, WcsTask.code)
            if (nRet ~= 0 or nRet == '') then
                lua.stop(strLuaDEID, "任务编码='" .. WcsTask.code .. "的任务设置完成失败!" .. strRetInfo)
                return 1
            end
        end
        return 0
    end
    --更新任务取消
    if Status == 7 then
        --更新任务取消
        nRet, UpdateTask = mobox.updateDataAttrByCondition(strLuaDEID, "Task",
            "S_CODE = '" .. WcsTask.code .. "'", "S_B_STATE = '取消'")
        if nRet ~= 0 then
            lua.Stop(strLuaDEID, "更新前叉任务状态(取货完成)" .. UpdateTask .. "失败! ")
            return 1
        end
 
        --更新作业取消
    end
end
 
function DeviceStatus(strLuaDEID)
    --设备编码
    local device_codes = { "TC1", "TC2" }
    --通信项编码
    local comm_code = { "18", "21", "22", "23", "24", "25", "26", "27", "28" }
    for i = 1, #device_codes do
        local DeviceResult = ReadS7PLCCommsData(strLuaDEID, device_codes[i], comm_code)
 
        if (DeviceResult ~= nil and DeviceResult ~= "") then
            local ShakeHands      = DeviceResult[1].value[1]                                                      --握手信号
            local BeforeCompleted = DeviceResult[2].value
                [1]                                                                                               --前叉任务完成
            local AfterCompleted  = DeviceResult[3].value
                [1]                                                                                               --后叉任务完成
            local BeforeAnomaly   = DeviceResult[4].value
                [1]                                                                                               --?前叉异常
            local AfterAnomaly    = DeviceResult[5].value
                [1]                                                                                               --?后叉异常
            local BeforePickup    = DeviceResult[6].value
                [1]                                                                                               --前叉取货完成信号
            local AfterPickup     = DeviceResult[7].value
                [1]                                                                                               --后叉取货完成信号
            local BeforeTaskNo    = ReadPlcTaskNo(strLuaDEID, DeviceResult[8].value[1], DeviceResult[8].value[2]) -- todo 前叉任务号
            local AfterTaskNo     = ReadPlcTaskNo(strLuaDEID, DeviceResult[9].value[1], DeviceResult[9].value[2]) -- todo 后叉任务号
            --前叉后叉都没有任务之间返回
            if (BeforeTaskNo == '0' and AfterTaskNo == '0') then
                return 0
            end
            if (ShakeHands == 1) then
                WriteS7PLCCommsData(strLuaDEID, device_codes[i], "18_WRITE", { 2 })
            elseif (ShakeHands == 2) then
                --!握手交互完清除我方通道
                local value = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
                WriteMultiS7PLCCommsData(strLuaDEID, device_codes[i], "03_WRITE", value)
            elseif (ShakeHands == 0) then
                --根据任务号获取任务信息
                local nRet, BeforeTask, AfterTask, result
 
 
                if (BeforeTaskNo ~= '0') then
                    local TaskSelect = "S_CODE = '" .. BeforeTaskNo .. "'"
                    nRet, BeforeTask = m3.GetDataObjByCondition(strLuaDEID, "Task", TaskSelect)
                    if (nRet ~= 0) then
                        lua.Stop(strLuaDEID,
                            "获取前叉任务信息有误! " .. BeforeTask .. " SQL条件: " .. TaskSelect)
                        return 1
                    end
                    lua.DebugEx(strLuaDEID, '堆垛机状态交互=》查询前叉任务', BeforeTask)
                    --异常处理
                    if (BeforeAnomaly == 1 or BeforeAnomaly == 3) then
                        --修改任务状态为错误,等待人工确认
                    end
                    --取货完成信号处理
                    if (BeforePickup == 1 and BeforeTask.s_state == "已推送") then
                        result = TaskStatus(strLuaDEID, BeforeTask, 4)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "25_WRITE", { 1 })
                    end
                    --任务完成信号处理
                    if (BeforeCompleted == 1 and BeforeTask.s_state == "取货完成") then
                        result = TaskStatus(strLuaDEID, BeforeTask, 2)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "21_WRITE", { 1 })
                    end
                    --任务取消
                    if (BeforeCompleted == 3) then
                        result = TaskStatus(strLuaDEID, BeforeTask, 7)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "21_WRITE", { 3 })
                    end
                end
 
                if (AfterTaskNo ~= '0') then
                    local TaskSelect = "S_CODE = '" .. AfterTaskNo .. "'"
                    nRet, AfterTask = m3.GetDataObjByCondition(strLuaDEID, "Task", TaskSelect)
                    if (nRet ~= 0) then
                        lua.Stop(strLuaDEID,
                            "获取后叉任务信息有误! " .. AfterTask .. " SQL条件: " .. TaskSelect)
                        return 1
                    end
                    lua.DebugEx(strLuaDEID, '堆垛机状态交互=》查询前叉任务', AfterTask)
                    --取货完成信号处理
                    if (AfterPickup == 1 and AfterTask.s_state == "已推送") then
                        result = TaskStatus(strLuaDEID, AfterTask, 4)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "26_WRITE", { 1 })
                    end
                    --任务完成信号处理
                    if (AfterCompleted == 1 and AfterTask.s_state == "取货完成") then
                        result = TaskStatus(strLuaDEID, AfterTask, 2)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "22_WRITE", { 1 })
                    end
 
                    --任务取消
                    if (AfterCompleted == 3) then
                        result = TaskStatus(strLuaDEID, BeforeTask, 7)
                        if result ~= 0 then
                            return result
                        end
                        WriteS7PLCCommsData(strLuaDEID, device_codes[i], "22_WRITE", { 3 })
                    end
                end
            end
        end
    end
    return 0
end