1
Jianw
2025-07-09 f6f5e6b632d6649386a380558d84003f3de7ec6c
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
m3 = require("oi_base_mobox")
lua = require("oi_base_func")
json = require("json")
mobox = require("OILua_JavelinExt")
wms_base = require("wms_base")
wms_cntr = require("wms_container")
 
--- 主动获取线体通讯项信
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
 
-- 托盘号查作业信息 (1072、1085、1099 入库称重点时用)
local function QueryOpbyTray(strLuaDEID, cntr_code)
    local nRet, strRetInfo
 
    lua.DebugEx(strLuaDEID, "QueryOpbyTray", "IN")
    local strCondition = "S_CNTR_CODE = '" .. cntr_code .. "'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "Operation", strCondition, "T_CREATE DESC")
    if (nRet ~= 0) then
        lua.Stop(strLuaDEID, "QueryOpbyTray 根据托盘" .. cntr_code .. "获取作业信息失败! " .. strRetInfo)
        return
    end
    return strRetInfo
end
 
-- 根据任务号返回当前任务
local function ReturnTaskEnd(strLuaDEID, task_no, cntr_code)
    lua.Debug(strLuaDEID, debug.getinfo(1), "ReturnTaskEnd:", "IN")
 
    local nRet, task_info, end_loc
    if (task_no ~= "") then
        nRet, task_info = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CODE = '" .. task_no .. "'", "T_CREATE DESC")
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, " ReturnTaskEnd 获取属性失败! " .. task_info)
        end
        lua.DebugEx(strLuaDEID, "task_info:", task_info)
 
        -- end_loc = task_info.end_loc_code -- 当前任务终点
    end
 
    if (cntr_code ~= "") then
        nRet, task_info = m3.GetDataObjByCondition(strLuaDEID, "Task", "S_CNTR_CODE = '" .. cntr_code .. "'",
            "T_CREATE DESC")
        if (nRet ~= 0) then
            lua.Stop(strLuaDEID, " ReturnTaskEnd 获取属性失败! " .. task_info)
        end
        -- end_loc = task_info.end_loc_code -- 当前任务终点
    end
    lua.DebugEx(strLuaDEID, "ReturnTaskEnd end_loc:", end_loc)
 
    return task_info
end
 
-- 读出线体任务号 转换为我们的任务号
local function ReadPlcTaskNo(strLuaDEID, taskno1, taskno2)
    lua.Debug(strLuaDEID, debug.getinfo(1), "ReadPlcTaskNo:", "IN")
 
    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
 
-- 主函数
function PLCStatusChange(strLuaDEID)
    local nRet, strRetInfo
    local input_datajson
 
    -- 设备编码, 线体编号, 信号,订阅名称
    local device_code, unit_code, value, task_info, condition_flag, isexit
    -- 获取接口中的Data
    nRet, input_datajson = m3.GetSysDataJson(strLuaDEID)
    if (nRet ~= 0) then
        lua.Stop(strLuaDEID, "PLCStateChange无法获取数据包!" .. input_datajson)
        return 1
    end
    -- 打印获取的设备信息
    lua.DebugEx(strLuaDEID, "data_objs-->", input_datajson)
    -- 解析数据
    device_code = lua.Get_StrAttrValue(input_datajson.device_code) -- 设备编码
    unit_code = lua.Get_StrAttrValue(input_datajson.unit_code) -- 线体编号
    -- condition_flag = lua.Get_StrAttrValue(input_datajson.condition_flag) --订阅名称
    value = input_datajson.value[1]
    if (value ~= 3) then
        lua.DebugEx(strLuaDEID, "工作模式不为3,直接返回", value)
        return 0
    end
 
    lua.DebugEx(strLuaDEID, "unit_code", unit_code)
    lua.DebugEx(strLuaDEID, "value", value)
 
    -- 扫码位
    if (unit_code == "1072" or unit_code == "1085" or unit_code == "1099") then
        -- 读取托盘码
        local cntr_info = ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", {unit_code .. "-S_CNTR_NO"})
        lua.DebugEx(strLuaDEID, "cntr_info", cntr_info)
        cntr_code = cntr_info[1].value
        lua.DebugEx(strLuaDEID, "cntr_code", cntr_code)
        -- 查询作业号
        local op_info = QueryOpbyTray(strLuaDEID, cntr_code)
 
        lua.DebugEx(strLuaDEID, "作业信息", op_info)
        local op_code = op_info.code
 
        local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_OP_CODE = '" .. op_code .. "' "
        nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
            return
        end
 
        if (isexit == 'yes') then
            lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
            return 0
        end
        -- 判断当前车辆的动作码是否已经在队列中, 如果已经存在则不做处理
        local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
        mq_eq_action.op_code = op_code
        mq_eq_action.eq_code = unit_code
        mq_eq_action.action_code = 1
        mq_eq_action.cntr_code = cntr_code
        -- 把车辆动作写入队列
        mq_eq_action.factory = "0001"
        mq_eq_action.eq_type = 4
        mq_eq_action.eq_type_name = "输送线"
 
        lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
        nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
        if (nRet ~= 0) then
            lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
        end
 
        lua.DebugEx(strLuaDEID, "成功", "成功")
    end
 
    -- 分支点
    if (unit_code == "1019" or unit_code == "1026") then
        local is_task_running
        local front_line_code -- 前叉线体编号
        local back_line_code -- 后叉线体编号
        local current_task_no_key -- :当前任务号通讯项
        local front_fork_task_key -- :前叉任务号通讯项 (用于校验终点)
        local front_fork_mode_key -- :前叉工作模式通讯项 (用于判断有载状态)
        local back_fork_mode_key -- :后叉工作模式通讯项 (用于判断有载状态)
        local next_branch_code -- :下个分支点编号
 
        -- 前叉后叉 线体编号
        if (unit_code == "1019") then
            front_line_code = "1035" -- 前叉编号
            back_line_code = "1033" -- 后叉编号
            current_task_no_key = "1019-S_TASK_NO"
            front_fork_task_key = "1035-S_TASK_NO"
            front_fork_mode_key = "1035-WORK_MODE"
            back_fork_mode_key = "1033-WORK_MODE"
            next_branch_code = "1026"
        elseif (unit_code == "1026") then
            front_line_code = "1047"
            back_line_code = "1045"
            current_task_no_key = "1026-S_TASK_NO"
            front_fork_task_key = "1047-S_TASK_NO"
            front_fork_mode_key = "1047-WORK_MODE"
            back_fork_mode_key = "1045-WORK_MODE"
            next_branch_code = "1019"
        end
 
        -- 读当前任务号校验终点
        local current_task_no = ReadS7PLCCommsData(strLuaDEID, device_code, {current_task_no_key})
        if current_task_no then
            -- 如果是数组类型(如[612,10]),需要根据业务需求处理:
            -- 方案1:取第一个元素
            local task_number = current_task_no[1].value[1]
            local task_number2 = current_task_no[1].value[2]
            if (task_number == 0 or task_number2 == 0) then
                lua.DebugEx(strLuaDEID, "任务号为空已退出", "")
                return
            end
            lua.DebugEx(strLuaDEID, "读到的任务号", current_task_no)
            -- 将读到的任务号转换为我们的任务号
            local us_task_no = ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
 
            -- 通过任务号当前任务的终点
            local mst = ReturnTaskEnd(strLuaDEID, us_task_no, "")
            lua.DebugEx(strLuaDEID, "通过任务号查到的终点", mst.end_loc_code)
 
            -- 终点为当前线体时
            if (mst.end_loc_code == unit_code) then
                -- 判断当前车辆的动作码是否已经在队列中, 如果已经存在则不做处理
 
                local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_TASK_CODE = '" .. us_task_no .. "'"
                nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
                    return
                end
 
                if (isexit == 'yes') then
                    lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
                    return 0
                end
 
                local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
                mq_eq_action.eq_code = unit_code
                mq_eq_action.action_code = 2
                mq_eq_action.task_code = us_task_no
                -- 把车辆动作写入队列
                mq_eq_action.factory = "0001"
                mq_eq_action.eq_type = 4
                mq_eq_action.eq_type_name = "输送线"
 
                lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
                nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
                end
            else
                WriteS7PLCCommsData(strLuaDEID, "S7_LINE_01", "1019-TARGRT_ADDRESS", {tonumber(mst.end_loc_code)})
            end
        end
    end
 
    -- 接驳位
    if (unit_code == "1035" or unit_code == "1033" or unit_code == "1047" or unit_code == "1045") then
 
        -- 获取当前位置的任务号
        local current_task_no = ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", {unit_code .. "-S_TASK_NO"}); -- 当前任务号
        lua.DebugEx(strLuaDEID, "PlcConnection当前位置" .. unit_code .. "返回的任务号", "IN")
 
        if current_task_no then
            -- 如果是数组类型(如[612,10]),需要根据业务需求处理:
            -- 方案1:取第一个元素
            local task_number = current_task_no[1].value[1]
            local task_number2 = current_task_no[1].value[2]
            if (task_number == 0 or task_number2 == 0) then
                lua.DebugEx(strLuaDEID, "读到的任务号为0,不执行", task_number)
                return
            end
 
            lua.DebugEx(strLuaDEID, "读到的任务号", current_task_no)
            -- 将读到的任务号转换为我们的任务号
            local us_task_no = ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
            -- 通过任务号当前任务的终点
            local mst = ReturnTaskEnd(strLuaDEID, us_task_no, "")
            lua.DebugEx(strLuaDEID, "通过任务号得到的终点", mst.end_loc_code)
 
            -- 终点为当前线体时
            if (mst.end_loc_code == unit_code) then
 
                local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_TASK_CODE = '" .. us_task_no .. "'"
                nRet, isexit = mobox.existThisData(strLuaDEID, "MQ_EQAction", strCondition)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), "调用方法existThisData出错" .. isexit)
                    return
                end
 
                if (isexit == 'yes') then
                    lua.DebugEx(strLuaDEID, "设备队列中已存在,直接返回", isexit)
                    return 0
                end
 
                local mq_eq_action = m3.AllocObject(strLuaDEID, "MQ_EQAction")
                mq_eq_action.eq_code = unit_code
                mq_eq_action.action_code = 3
                mq_eq_action.task_code = us_task_no
                mq_eq_action.op_code = mst.op_code
                -- 把车辆动作写入队列
                mq_eq_action.factory = "0001"
                mq_eq_action.eq_type = 4
                mq_eq_action.eq_type_name = "输送线"
 
                lua.DebugEx(strLuaDEID, "创建设备动作队列前", mq_eq_action)
                nRet, strRetInfo = m3.CreateDataObj(strLuaDEID, mq_eq_action)
                if (nRet ~= 0) then
                    lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo)
                end
            end
        end
    end
end