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
--[[
   编码: WMS-0605-0001
   名称: 
   作者:
   日期:2025-06-05
 
   函数: Main
   功能:
 
   更改记录:
 
--]]
 
json  = require ("json")
mobox = require ("OILua_JavelinExt")
m3 = require ("oi_base_mobox")
 
function Main( strLuaDEID )
    Test( strLuaDEID )
   -- local nRet, strRetInfo = mobox.setCacheValue("aaa", "111");
   -- lua.Debug(strLuaDEID, debug.getinfo(1), "setCacheValue!", strRetInfo)
   -- nRet, strRetInfo = mobox.getCacheValue("aaa");
   -- lua.Debug(strLuaDEID, debug.getinfo(1), "getCacheValue!", strRetInfo)
 
  WriteS7PLCCommsData( strLuaDEID, { device_code = "S7_LINE", comm_code = "1001-WORK_MODE", value = {3} } )
end
function Test( strLuaDEID )
    local strUserLogin, strUserName, nRet
    nRet, strUserLogin, strUserName = mobox.getCurUserInfo( strLuaDEID )
    
    lua.DebugEx( strLuaDEID, "strUserLogin", strUserLogin )
    
    if ( nRet ~= 0 ) then 
        return 2, "获取当前操作人员信息失败! "..strUserLogin 
    end
    -- 获取当前操作人员的单位编码,作为工厂标识
    nRet, strRetInfo = mobox.getUserSectionUnit( strLuaDEID,strUserLogin )
    if ( nRet ~= 0 ) then 
        return 2, "获取当前操作人员所属单位失败! "..strRetInfo 
    end
    
    lua.DebugEx( strLuaDEID, "strRetInfo", strRetInfo )
    
    local factory = ''
    if ( strRetInfo ~= '' ) then
       local orgInfo = json.decode( strRetInfo ) 
       factory = orgInfo.company_code
    end
    
        lua.DebugEx( strLuaDEID, "factory1", factory )
        
    if factort == '' then
        nRet, factory = wms_base.Get_sConst2( "WMS_Default_Factory")
        
        lua.DebugEx( strLuaDEID, "factory2", factory )
        
        if ( nRet ~= 0 ) then
            return 2, "系统无法获取常量'WMS_Default_Factory'"
        end  
    end
 
end
function WriteS7PLCCommsData( strLuaDEID, body )
    local strDeviceCommSvr, strKey, strSecret
    --strDeviceCommSvr, strKey, strSecret = GetDeviceCommSInfo( strLuaDEID )
 
    --local strurl =  strDeviceCommSvr..'/api/devctrl/writedata'
    strKey="OpenInfo"
    strSecret="OpenInfoSecret"
    local strurl =  'http://192.168.1.205:5121/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
function 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