lzh
2025-06-19 3a6436e0c88042c6ce8dca2fe8adb0109f0ad9e4
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
--[[
    版本:    Version 1.0
    创建日期: 2023-6-16
    创建人:   HAN
 
    功能:
        wms_dev Lua程序包整合了一些和设备通讯项相关的一些函数
 
        -- ReadS7PLCCommsData   从S7的通讯项里读取数据
        -- WriteS7PLCCommsData 写数据到S7的通讯项
        -- GetDeviceCommSInfo 获取 OIDeviceComms 服务的IP地址和访问这个服务需要的 Key,Secret
        -- GetDeviceCommSExtInfo 获取设备通讯项相关的扩展信息,比如和该设备通讯项相关的货位
    更改说明:
--]]
 
wms_base = require ("wms_base")
 
local wms_dev = {_version = "0.1.1"}
--//////////////////////////////////////// 设备通讯相关 //////////////////////////////////////////////////////////////
-- 获取 OIDeviceComms 服务的IP地址和访问这个服务需要的 Key,Secret
function wms_dev.GetDeviceCommSInfo( strLuaDEID )
    local strDeviceCommSvr, strKey, strSecret
    local nRet
 
    nRet, strDeviceCommSvr= mobox.getParameter(strLuaDEID, '9100')
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "系统无法获取9100参数(OIDeviceCommS服务器配置)! "..strDeviceCommSvr ) end 
    if ( strDeviceCommSvr == '' or strDeviceCommSvr == nil ) then lua.Error( strLuaDEID, debug.getinfo(1), "9100参数值为空, 请到系统参数进行设置! " ) end
 
    nRet, strKey = mobox.getParameter(strLuaDEID, '9101')
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "系统无法获取9101参数(OIDeviceCommS服务器Key设置)! "..strKey ) end 
    if ( strKey == '' or strKey == nil ) then lua.Error( strLuaDEID, debug.getinfo(1), "9101参数值为空, 请到系统参数进行设置! " ) end
 
    nRet, strSecret = mobox.getParameter(strLuaDEID, '9102')
    if ( nRet ~= 0 ) then lua.Error( strLuaDEID, debug.getinfo(1), "系统无法获取9102参数(OIDeviceCommS服务器Key设置)! "..strSecret ) end 
    if ( strSecret == '' or strSecret == nil ) then lua.Error( strLuaDEID, debug.getinfo(1), "9102参数值为空, 请到系统参数进行设置! " ) end
 
    return strDeviceCommSvr, strKey, strSecret
end
 
-- 获取设备通讯项相关的扩展信息,比如和该设备通讯项相关的货位
function wms_dev.GetDeviceCommSExtInfo( device_code, comm_code )
    local nRet, strRetInfo
 
    nRet, strRetInfo = wms.wms_GetDevCommExt( device_code, comm_code )
    if ( nRet ~= 0 or  strRetInfo == '' ) then return 2, 'wms_GetDevCommExt 失败!'..strRetInfo end
    local success
    local dev_comms_ext
    success, dev_comms_ext = pcall( json.decode, strRetInfo)
    if ( success == false ) then return 2, "wms_GetDevCommExt 返回的结果不正确! 非法的JSON格式!"..dev_comms_ext  end
    return 0, dev_comms_ext
end
 
-- 从S7的通讯项里读取数据
--
function wms_dev.ReadS7PLCCommsData( strLuaDEID, device_code, comm_code )
    if (device_code == nil or device_code == '' ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "ReadS7PLCCommsData 函数参数错误,设备编码不能为空! "  ) 
    end
    if (comm_code == nil or comm_code == '' ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "ReadS7PLCCommsData 函数参数错误,通讯项编码不能为空! "  ) 
    end
 
    local strDeviceCommSvr, strKey, strSecret
    local nRet, strRetInfo
 
    strDeviceCommSvr, strKey, strSecret = wms_dev.GetDeviceCommSInfo( strLuaDEID )
 
    local strurl =  strDeviceCommSvr..'/api/devctrl/readdata'
    local body = {}
    local strHeader = ''
 
    body.device_code = device_code
    body.comm_code = comm_code
    -- 获取Header
    nRet, strHeader = mobox.genReqVerify( strKey, strSecret )
    if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "获取PLC服务访问授权失败! "..strHeader  ) end
    nRet, strRetInfo = mobox.sendHttpRequest( strurl, strHeader, lua.table2str(body) )
 
    if ( nRet ~= 0 or strRetInfo == '' ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "调用OIDeviceCommS接口ReadData失败! device_code = "..device_code.." comm_code = "..comm_code.."错误码:"..nRet.."  "..strRetInfo ) 
    end
    local api_ret = json.decode(strRetInfo)
    if ( api_ret.err_code ~= 0 ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "调用OIDeviceCommS接口ReadData失败!  device_code = "..device_code.." comm_code = "..comm_code.."错误码:"..api_ret.err_code.."  "..api_ret.err_msg ) 
    end
    return api_ret.result.value
end
 
-- 写数据到S7的通讯项
function wms_dev.WriteS7PLCCommsData( strLuaDEID, body )
    local strDeviceCommSvr, strKey, strSecret
    strDeviceCommSvr, strKey, strSecret = wms_dev.GetDeviceCommSInfo( strLuaDEID )
 
    local strurl =  strDeviceCommSvr..'/api/devctrl/writedata'
    local strHeader = ''
 
    -- 获取Header
    nRet, strHeader = mobox.genReqVerify( strKey, strSecret )
    if ( nRet ~= 0 ) then  lua.Error( strLuaDEID, debug.getinfo(1), "获取PLC服务访问授权失败! "..strHeader  ) end
    -- 写PLC地址
    nRet, strRetInfo = mobox.sendHttpRequest( strurl, strHeader, lua.table2str(body) )
 
    if ( nRet ~= 0 or strRetInfo == '' ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "S7PLC 写入失败!  device_code = "..body.device_code.." comm_code = "..body.comm_code.."错误码:"..nRet.."  "..strRetInfo ) 
    end
    local api_ret = json.decode(strRetInfo)
    if ( api_ret.err_code ~= 0 ) then 
        lua.Error( strLuaDEID, debug.getinfo(1), "S7PLC 写入失败! device_code = "..body.device_code.." comm_code = "..body.comm_code.."错误码:"..api_ret.err_code.." --> "..api_ret.err_msg )
    end
end
 
return wms_dev