--[[ 版本: Version 2.1 创建日期: 2023-6-16 创建人: HAN WMS-Basis-Model-Version: V15.5 功能: 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