网络打印
网络打印主要通过服务器脚本实现,目前仅确定,可以打印标签,流转卡等简单的网络打印。
实现方法
实现网络打印实现前提条件:
1、打印模板准备
2、打印数据
3、网络打印服务器脚本
网络打印服务器脚本接收打印数据和打印版本进行组装,调用已封装好的打印接口文件print_comm.lua,将打印任务发送至配置好的网络打印机进行打印。
4、客户端程序、pda程序通过调用网络打印服务器接口创建打印任务实现网络打印
5、连接打印机的客户端程序需要常置【接收打印任务】的功能
实现案例
案例1
serv:use(luaid(),'print_comm')
function process()
local sCmd = inIt:get('cmd');
if sCmd == 'print_server_label' then --服务器打印标签
print_server_label();
return;
end
outIt:set('code','1');
outIt:set('error','');
end
--服务器打印标签
function print_server_label()
local helpPlanWo = CSelectHelp(); --工单信息
local helpReportUser = CSelectHelp(); --报工人员信息
inIt:getHelp('helpPlanWo',helpPlanWo);
inIt:getHelp('helpReportUser',helpReportUser);
--wo_codeplan_sp_codesale_order_idbatch_nomaterial_idmaterial_namematerial_specplan_numreport_numno_report_numeqpt_idprocess_route_idprocess_idprocess_namereq_timecreate_timeduty_user_idduty_user_nameproject_namewo_typewo_stateparent_wo_codecn_wo_statewo_start_timerecent_start_timerecent_pause_timewo_end_timetotal_timeW210225093525000001TS11-210200015TX32-210220002T510010008J制冷系统组件-T510010007J制冷系统组件-T510010007J_1082TX01TX01TX01线束工序2021-02-20 00:00:002021-02-25 09:35:2511011汪静娜3110加工0
--user_iduser_cn_name11011汪静娜
local sQty = inIt:get('report_qty'); --获取报工数量
local sLotNo = inIt:get('lot_no'); --获取报工批次
local sReportTime = cutil:getSys8Time (); --报工日期
--获取标签头部信息和打印机信息参数
local helpPrintParam = CSelectHelp();
query_print_param(helpPrintParam);
if helpPrintParam:size() == 0 then
outIt:set('code','-1');
outIt:set('error','标签打印参数(标签头部、打印机名称)未设置!');
return;
end
local sPrinterName = helpPrintParam:search('parameter_code','print_server_name','parameter_name')
local sLabelHead = helpPrintParam:search('parameter_code','label_head_name','parameter_name')
if #sPrinterName == 0 or sPrinterName =='' then
outIt:set('code','-1');
outIt:set('error','标签打印参数(打印机名称)未设置!');
return;
end
if #sLabelHead == 0 or sLabelHead =='' then
outIt:set('code','-1');
outIt:set('error','标签打印参数(标签头部)未设置!');
return;
end
--获取设备信息
local sEqptId = helpPlanWo:vs(0,'eqpt_id')
--打印模板code
local sTempCode = '';
--打印数据help
local helpData = CSelectHelp();
if sEqptId == 'TX01' then --线束加工一线
sTempCode = 'line';
helpData:addFieldWithData('label_head', sLabelHead);
helpData:addFieldWithData('sale_order_id', helpPlanWo:vs(0,'sale_order_id'));
helpData:addFieldWithData('plan_sp_code', helpPlanWo:vs(0,'plan_sp_code'));
helpData:addFieldWithData('project_name', helpPlanWo:vs(0,'project_name'));
helpData:addFieldWithData('material_name', helpPlanWo:vs(0,'material_name'));
helpData:addFieldWithData('material_spec', helpPlanWo:vs(0,'material_spec'));
helpData:addFieldWithData('qty', sQty);
helpData:addFieldWithData('create_time', sReportTime);
helpData:addFieldWithData('lot_no', sLotNo);
helpData:addFieldWithData('create_user', helpReportUser:vs(0,'user_cn_name'));
elseif sEqptId == 'TS02' or sEqptId == 'TS03' then --TS02:配电箱组装一线 --TS03:动环箱组装一线
sTempCode = 'box';
helpData:addFieldWithData('label_head', sLabelHead);
helpData:addFieldWithData('sale_order_id', helpPlanWo:vs(0,'sale_order_id'));
helpData:addFieldWithData('plan_sp_code', helpPlanWo:vs(0,'plan_sp_code'));
helpData:addFieldWithData('project_name', helpPlanWo:vs(0,'project_name'));
helpData:addFieldWithData('material_name', helpPlanWo:vs(0,'material_name'));
helpData:addFieldWithData('material_spec', helpPlanWo:vs(0,'material_spec'));
helpData:addFieldWithData('create_time', sReportTime);
helpData:addFieldWithData('lot_no', sLotNo);
else
outIt:set('code','-1');
outIt:set('error','非线束、配电箱、动环箱产品!');
return;
end
print('打印服务开始=============================================')
-- greport.setParam('','barcode', '123456777');
-- greport.setParam('','material_code', '24269554D10Z'); --设置固定字段数据
-- greport.setParam('','material_name', '1267输出轴_下料');
-- greport.setParam('','lot_no', 'LOTNO001');
-- greport.setParam('','lot_no_code', 'LOTNO001');
-- ui:use('print_comm');--引用print_comm后使用greport对象调用方法, 函数名与客户端保持一致
greport.setDetailGridHelp('', helpData, true);--表格数据设置 HELP字段名 对应模板表格列 关联参数。最后一个参数为是否嵌套打印
greport.setPrinterName(sPrinterName);--默认为空 使用默认打印机
local help = CSelectHelp();
local sPrintLogID = '';--表d_print_log的主键
local sReportValue = greport.toString();
local sReportValueCRC = sTempCode..'_'..cutil:getCRC(sReportValue);--根据模板code和模板数据CRC查询, 防止重复数据入库
if db:select(string.format("select print_log_id from d_print_log where print_param_crc='%s'", sReportValueCRC), '', help) < 0 then
set_error_return('查询打印记录失败');
return;
elseif help:size() > 0 then
sPrintLogID = help:vs(0, 'print_log_id');
else
sPrintLogID = cutil:getBusiUniqueID('P','');
sql = [[
insert into d_print_log (
print_log_id,site_id,user_id,temp_code,
print_param,print_param_crc,create_time,print_info
)
values (
'%s','%s','%s','%s',
'%s','%s','%s','%s'
)
]];
sql = string.format(sql, sPrintLogID, sEqptId, helpReportUser:vs(0,'user_id'), sTempCode,
sReportValue, sReportValueCRC, cutil:getSys14Time(),sLotNo
);
if db:execSQL(sql, '') == false then
set_error_return('保存数据失败'..db:error());
return;
end
end
--服务器执行(服务器连接网络打印机打印)
greport.remotePrint('127.0.0.1', 9001, sPrintLogID);
print('打印服务结束=============================================')
end
--获取标签头部信息和打印机信息参数
function query_print_param(helpPrintParam)
local sql = [[
select parameter_id,param_type_id,parameter_code,parameter_name
from sys_parameter
where param_type_id = 'yitiji_flag'
]]
if db:select (sql , '', helpPrintParam) < 0 then
return false;
end
return true;
end
process();
案例2
--print_comm.lua生成打印模板需要的数据
--项目名称: TouchNet-Mes
--Copyright (C) 杭州塔网科技有限公司 2015
--作者: liang
--更新日志
-- 2018-11-23 新建
local m_itPrint = CIntent();
greport = {};
greport.openFromStr = function(sPrintTemp)
if sPrintTemp == nil or type(sPrintTemp) ~= 'string' or #sPrintTemp <= 0 then
return false;
end
m_itPrint:set('openFromStr', sPrintTemp);
end
--以下4个函数的第一个参数sSubReportCode为子报表的cid, 如果不是子报表, 参数为空
--设置明细网格, 一个报表只有一个,多个使用子报表, 参数为是否嵌套打印
--bool setDetailGridHelp(CSelectHelp& help, bool bNest = false)
greport.setDetailGridHelp = function(sSubReportCode, help, bNest)
if help == nil then
return;
end
local itParam = CIntent();
itParam:set('cmd', 'setDetailGridHelp');
if sSubReportCode ~= nil and tostring(sSubReportCode) == 'string' then
itParam:set('sSubReportCode', sSubReportCode);
else
sSubReportCode = '';
end
itParam:setHelp('help', help);
if bNest == nil or bNest ~= true then
itParam:set('bNest', '0');
else
itParam:set('bNest', '1');
end
m_itPrint:setIntent('setDetailGridHelp'..sSubReportCode, itParam);
return true;
end
--设置自由表格数据, sCid为控件ID, bSetField是否加载help表头, iStartRow,iStartCol从自由表格哪一行哪一列为起始点加载数据
--bool setFreeGridHelp(const string& sCid, CSelectHelp& help, bool bSetField = true, int iStartRow = 0, int iStartCol = 0);
greport.setFreeGridHelp = function(sSubReportCode, sCid, help, bSetField, iStartRow, iStartCol)
if sCid == nil or type(sCid) ~= 'string' or help == nil or #sCid <= 0 or help:cols() <= 0 then
return false;
end
local itParam = CIntent();
itParam:set('cmd', 'setFreeGridHelp');
if sSubReportCode ~= nil and tostring(sSubReportCode) == 'string' then
itParam:set('sSubReportCode', sSubReportCode);
else
sSubReportCode = '';
end
itParam:set('sCid', sCid);
itParam:setHelp('help', help);
if bSetField == nil or bSetField ~= false then
itParam:set('bSetField', '1');
else
itParam:set('bSetField', '0');
end
if iStartRow == nil or type(iStartRow) ~= 'number' then
itParam:set('iStartRow', '0');
else
itParam:set('iStartRow', tostring(iStartRow));
end
if iStartCol == nil or type(iStartCol) ~= 'number' then
itParam:set('iStartCol', '0');
else
itParam:set('iStartCol', tostring(iStartCol));
end
m_itPrint:setIntent('setFreeGridHelp'..sSubReportCode..sCid, itParam);
return true;
end
--设置参数集合中的 某个参数的数据, sDataType为数据类型, 一般都为string
--bool setParam(const string& sParamName, const string& sValue, const string& sDataType = "string");
greport.setParam = function(sSubReportCode, sParamName, sValue, sDataType)
if sParamName == nil or type(sParamName) ~= 'string' or #sParamName <= 0 then
return false;
end
local itParam = CIntent();
itParam:set('cmd', 'setParam');
if sSubReportCode ~= nil and tostring(sSubReportCode) == 'string' then
itParam:set('sSubReportCode', sSubReportCode);
else
sSubReportCode = '';
end
itParam:set('sParamName', sParamName);
if sValue == nil then
sValue = '';
end
itParam:set('sValue', sValue);
if sDataType == nil then
sDataType = 'string';
end
itParam:set('sDataType', sDataType);
m_itPrint:setIntent('setParam'..sSubReportCode..sParamName, itParam);
return true;
end
--设置控件属性的方法, sCid为控件ID, sPropertyName为属性名称
--bool setProp(const string& sCid, const string& sPropertyName, const string& sValue);
greport.setProp = function(sSubReportCode, sCid, sPropertyName, sValue)
if sCid == nil or type(sCid) ~= 'string' or #sCid <= 0 or
sPropertyName == nil or type(sPropertyName)~='string' or #sPropertyName<= 0 then
return false;
end
local itParam = CIntent();
itParam:set('cmd', 'setProp');
if sSubReportCode ~= nil and tostring(sSubReportCode) == 'string' then
itParam:set('sSubReportCode', sSubReportCode);
else
sSubReportCode = '';
end
itParam:set('sCid', sCid);
itParam:set('sPropertyName', sPropertyName);
if sValue == nil then
sValue = '';
end
itParam:set('sValue', sValue);
m_itPrint:setIntent('setProp'..sSubReportCode..sCid..sPropertyName, itParam);
return true;
end
--设置打印机名称, windows可以使用ui:getPrinterList(CVector)获取打印机列表
--bool setPrinterName(const string& sPrinterName);
greport.setPrinterName = function(sPrinterName)
if sPrinterName == nil or type(sPrinterName) ~= 'string' or #sPrinterName <= 0 then
return false;
end
m_itPrint:set('setPrinterName', sPrinterName);
end
greport.toString = function()
return cutil:toBase64(m_itPrint:toString());
end
greport.remotePrint = function(sIP, iPort, sPrintLogID)
print('发送远程打印 print_log_id:'..sPrintLogID..' to IP'..sIP..':'..iPort);
local ice = LuaICE();
ice:setServer(sIP, iPort);
if ice:isLogin() == false then
if ice:login() == false then
return false;
end
end
if ice:isLogin() then
local outmsg = CTNString();--先打开打印服务所在的页面, 防止该页面没有打开
-- ice:commandStr('openDialog', 'tn_common_print_server', outmsg);
local itd = CIntent();
itd:set('dialogName', 'tn_common_print_server');
itd:set('cmd', 'process_print_task');
itd:set('param', sPrintLogID);
ice:commandStr('sendDialogCommand', itd:toString(), outmsg);
return true;
end
return false;
end
greport.print = function(bView)--客户端才能使用的函数
-- print('ttttt')
if string.len(m_itPrint:get('openFromStr')) <= 0 then
print('greport.print 未设置打印模板');
return false;
end
local g_report = CLuaGridReport();
g_report:openFromStr(m_itPrint:get('openFromStr'));
local line = CVector();
m_itPrint:getKeysByVector(line);
line:dump()
local sTmp = '';
local itOne = CIntent();
for i=0,line:size()-1 do
while true do
sKey = line:get(i);
if sKey == 'setPrinterName' then
g_report:setPrinterName(m_itPrint:get(sKey));
break;
end
itOne:clear();
m_itPrint:getIntent(sKey, itOne);
if itOne:get('cmd') == 'setDetailGridHelp' then
local help = CSelectHelp();
itOne:getHelp('help', help);
local bNest = false;
if itOne:get('bNest') == '1' then
bNest = true;
end
if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
g_report:setDetailGridHelp(help, bNest);
else
g_report:setSubDetailGridHelp(itOne:get('sSubReportCode'), help, bNest);
end
elseif itOne:get('cmd') == 'setFreeGridHelp' then
local help = CSelectHelp();
itOne:getHelp('help', help);
local bSetField = false;
if itOne:get('bSetField') == '1' then
bSetField = true;
end
if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
g_report:setFreeGridHelp(itOne:get('sCid'), help, bSetField, tonumber(itOne:get('iStartRow')), tonumber(itOne:get('iStartCol')));
else
g_report:setSubFreeGridHelp(itOne:get('sSubReportCode'), itOne:get('sCid'), help, bSetField, tonumber(itOne:get('iStartRow')), tonumber(itOne:get('iStartCol')));
end
elseif itOne:get('cmd') == 'setParam' then
if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
g_report:setParam(itOne:get('sParamName'), itOne:get('sValue'), itOne:get('sDataType'));
else
g_report:setSubParam(itOne:get('sSubReportCode'), itOne:get('sParamName'), itOne:get('sValue'), itOne:get('sDataType'));
end
elseif itOne:get('cmd') == 'setProp' then
if string.len(itOne:get('sSubReportCode')) <= 0 then--不是子报表
g_report:setProp(itOne:get('sCid'), itOne:get('sPropertyName'), itOne:get('sValue'));
else
g_report:setSubProp(itOne:get('sSubReportCode'), itOne:get('sCid'), itOne:get('sPropertyName'), itOne:get('sValue'));
end
end
break;
end
end
if bView == nil or bView then
g_report:printPreview(true);
else--不预览, 直接打印
g_report:print(false);
end
return true;
end
greport.print_log = function(sPrintLogID, bView)--根据print_log_id打印
-- print('gaolintao')
local sql = "select temp_code,print_param from d_print_log where print_log_id='%s'"
local help = CSelectHelp();
if db:select(string.format(sql, sPrintLogID), '', help) < 0 then
return ui:tip('查询失败');
elseif help:size() <= 0 then
return ui:tip('打印任务不存在');
end
local sTempCode = help:vs(0, 'temp_code');
local sPrintParam = help:vs(0, 'print_param');
if #sTempCode <= 0 then
return ui:tip('打印模板code不能为空');
end
sql = "select temp_value from d_print_temp where temp_code='%s'";
if db:select(string.format(sql, sTempCode), '', help) < 0 then
return ui:tip('查询模板失败');
elseif help:size() <= 0 then
return ui:tip('模板不存在');
end
local sTempValue = cutil:decodeSrc(help:vs(0, 'temp_value'));
if #sTempValue < 20 then
return ui:tip('模板为空');
end
m_itPrint:fromString(cutil:fromBase64(sPrintParam));
m_itPrint:set('openFromStr', sTempValue)
greport.print(bView);
end