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
--[[
编码:
名称: PLCStatusChange
作者: xsy
入口函数: PLCStatusChange
功能说明:
变更历史:
--]] 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")
ams_plc = require("amsplc_base")
 
-- --- 主动获取线体通讯项信
-- local function ReadS7PLCCommsData(strLuaDEID, devicecode, commcode)
 
-- -- 写数据到PLC
-- local function WriteS7PLCCommsData(strLuaDEID, devicecode, commcode, comm_value)
 
-- -- 托盘号查作业信息 (1072、1085、1099 入库称重点时用)
 
-- -- 根据任务号返回当前任务
-- local function ReturnTaskEnd(strLuaDEID, task_no, cntr_code)
 
-- -- 读出线体任务号 转换为我们的任务号
-- local function ReadPlcTaskNo(strLuaDEID, taskno1, taskno2)
 
-- 主函数
function PLCStatusChange(strLuaDEID)
    local nRet, strRetInfo
    local input_datajson
     m3.PrintLuaDEInfo(strLuaDEID)
 
    -- 设备编码, 线体编号, 信号,订阅名称
    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) -- 线体编号
    value = input_datajson.value[1]
 
    --   lua.DebugEx(strLuaDEID, "unit_code", unit_code)
    --   lua.DebugEx(strLuaDEID, "value", value)
    if (value ~= 3) then
        return 0
    end
 
    -- 扫码位
    if (unit_code == "1072" or unit_code == "1085" or unit_code == "1099") then
        -- 读取托盘码
        local cntr_info = ams_plc.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 = ams_plc.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 .. "' "
        local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_OP_CODE = '" .. op_code .. "'" -- AND N_B_STATE != 1
        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 = ams_plc.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 = ams_plc.ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
 
            -- 通过任务号当前任务的终点
            local mst = ams_plc.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 .. "'"
                local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND N_B_STATE != 1"
                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
                ams_plc.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 = ams_plc.ReadS7PLCCommsData(strLuaDEID, "S7_LINE_01", {unit_code .. "-S_TASK_NO"}); -- 当前任务号
        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 = ams_plc.ReadPlcTaskNo(strLuaDEID, task_number, task_number2)
            -- 通过任务号当前任务的终点
            local mst = ams_plc.ReturnTaskEnd(strLuaDEID, us_task_no, "")
            -- lua.DebugEx(strLuaDEID, "通过任务号得到的终点", mst.end_loc_code)
            -- 查询当前作业信息
            local isPush = ams_plc.GetddjTaskInfo(strLuaDEID, mst.op_code)
            if (isPush ~= 'yes') then
                -- 终点为当前线体时
                if (mst.end_loc_code == unit_code) then
                    -- local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND S_TASK_CODE = '" .. us_task_no .. "'"
                    local strCondition = "S_EQ_CODE = '" .. unit_code .. "' AND N_B_STATE != 1"
                    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
            else
                lua.DebugEx(strLuaDEID,
                    "当前作业" .. mst.op_code .. "的堆垛机任务已推送,不再创建动作队列", "return")
            end
        end
    end
end