@@ -5,10 +5,10 @@ import com.ZMPrinter.LSF.LSFDecoder;
import com.ZMPrinter.UsbUtility.UsbConnection ;
import com.zg.project.Inventory.AutoInventory.domain.entity.PrintRfid ;
import com.zg.project.Inventory.AutoInventory.mapper.PrintRfidMapper ;
import com.zg.project.information.domain.PcdeDetail ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.GetMapping ;
import or g.s pringframework.web.bind.annotation.Request Mapping ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.bind.annotation.* ;
import com.z g.project.information.mapper.PcdeDetail Mapper ;
import java.util.ArrayList ;
import java.util.List ;
@@ -19,7 +19,8 @@ import java.util.Map;
public class PrintController {
@Autowired
PrintRfidMapper rfidMapper ;
@Autowired
PcdeDetailMapper pcdeDetailMapper ;
/**
* 代码定义标签格式
* @return
@@ -257,12 +258,29 @@ public class PrintController {
// return AjaxResult.success(result);
// }
@PostMapping ( " /printaaa " )
public String printaaa ( @RequestBody Map < String , Object > params ) {
Integer printType = ( Integer ) params . get ( " printType " ) ;
//批量打印、单个打印
if ( printType = = 1 ) {
ArrayList ids = ( ArrayList ) params . get ( " ids " ) ;
List < PcdeDetail > rfidList = pcdeDetailMapper . selectPcdeDetailListIds ( ids ) ;
Long aa = 3333L ;
return " tttt " ;
} else if ( printType = = 2 ) { //手动输入信息打印
}
return " rrrr " ;
}
/**
* 模板定义标签格式
* @return
*/
@Ge tMapping ( " /printlsf " )
public String printlsf ( ) {
@Pos tMapping ( " /printlsf " )
public String printlsf ( @RequestBody Map < String , Object > params ) {
zmprinter thiszmprinter = new zmprinter ( ) ; //定义打印机对象
zmlabel thiszmlabel = new zmlabel ( ) ; //定义标签对象
List < zmlabelobject > thiszmlabelobjectlist = new ArrayList < > ( ) ; //定义标签上需要打印的对象列表
@@ -321,36 +339,52 @@ public class PrintController {
//endregion
}
//endregion
// 查print_rfid表, 打印标签
PrintRfid printRfid = new PrintRfid ( ) ;
printRfid . setIsprint ( 0L ) ;
List < P rintRfid > rfidList = new ArrayList < > ( ) ;
rfidList = rfidMapper . selectPrintRfidList ( printRfid ) ;
if ( rfidList . size ( ) > 0 ) {
//region 循环打印标签
for ( PrintRfid info : rfidList ) {
//批量打印、单个打印
Integer printType = ( Integer ) params . get ( " printType " ) ;
//批量打印、单个打印
if ( p rintType = = 1 ) {
ArrayList ids = ( ArrayList ) params . get ( " ids " ) ;
List < PcdeDetail > rfidList = pcdeDetailMapper . selectPcdeDetailListIds ( ids ) ;
i f( rfidList . size ( ) > 0 ) {
for ( PcdeDetail info : rfidList ) {
// System.out.println("打印第"+pnum+"张");
//region 处理读取标签数据(如果需要)
String tagTID = " " ;
if ( isRFIDread ) {
//region 读取UHF标签数据
UsbConnection usbConnection = new UsbConnection ( ) ; //建立USB连接
if ( ! usbConnection . Open ( ) ) {
System . out . println ( " 连接打印机失败,请检查。 " ) ;
return " 连接打印机失败,请检查 " ;
}
if ( isRFIDwrite )
tagTID = usbConnection . readUHFTag ( 0 , 3 ) ; //读TID, 后面需要写EPC操作
else
tagTID = usbConnection . readUHFTag ( 0 , 2 ) ; //读TID, 不需要写EPC操作
usbConnection . Close ( ) ; //关闭USB连接
//region 处理读取标签数据(如果需要)
String tagTID = " " ;
if ( isRFIDread ) {
//region 读取UHF标签数据
UsbConnection usbConnection = new UsbConnection ( ) ; //建立USB连接
if ( ! usbConnection . Open ( ) ) {
System . out . println ( " 连接打印机失败,请检查。 " ) ;
return " 连接打印机失败,请检查 " ;
}
if ( isRFIDwrite )
tagTID = usbConnection . readUHFTag ( 0 , 3 ) ; //读TID, 后面需要写EPC操作
else
tagTID = usbConnection . readUHFTag ( 0 , 2 ) ; //读TID, 不需要写EPC操作
usbConnection . Close ( ) ; //关闭USB连接
if ( tagTID . isEmpty ( ) | | tagTID . equals ( " X " ) ) //未读到数据
{
System . out . println ( " 未读到标签数据。 " ) ;
continue ; //退出当次循环
if ( tagTID . isEmpty ( ) | | tagTID . equals ( " X " ) ) //未读到数据
{
System . out . println ( " 未读到标签数据。 " ) ;
continue ; //退出当次循环
}
//更新读到的数据
if ( tagTID ! = null & & ! tagTID . isEmpty ( ) ) {
for ( zmlabelobject thisLabelObject : thiszmlabelobjectlist ) {
for ( int vi = 0 ; vi < thisLabelObject . Variables . size ( ) ; vi + + ) {
objectvariable thisVariable = thisLabelObject . Variables . get ( vi ) ;
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) { //LSF文件中有读取HF或UHF的ID对象
thisVariable . data = tagTID ;
thisLabelObject . Variables . set ( vi , thisVariable ) ; //更新数据
}
}
}
}
//endregion
}
//更新读到的数据
@@ -358,7 +392,7 @@ public class PrintController {
for ( zmlabelobject thisLabelObject : thiszmlabelobjectlist ) {
for ( int vi = 0 ; vi < thisLabelObject . Variables . size ( ) ; vi + + ) {
objectvariable thisVariable = thisLabelObject . Variables . get ( vi ) ;
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) { //LSF文件中有读取HF或UHF的ID对象
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) {
thisVariable . data = tagTID ;
thisLabelObject . Variables . set ( vi , thisVariable ) ; //更新数据
}
@@ -366,33 +400,18 @@ public class PrintController {
}
}
//endregion
}
//更新读到的数据
if ( tagTID ! = null & & ! tagTID . isEmpty ( ) ) {
for ( zmlabelobject thisLabelObject : thiszmlabelobjectlist ) {
for ( int vi = 0 ; vi < thisL abelO bject. Variables . size ( ) ; vi + + ) {
objectvariable thisVariable = thisL abelO bject. Variables . get ( vi ) ;
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) {
thisVariable . data = tagTID ;
thisLabelObject . Variables . set ( vi , thisVariable ) ; //更新数据
}
}
}
}
//endregion
//region 设置对象里变量的值(如果需要)
//注意: 参数varName是lsf文件里对象变量的“子字符串共享名称”, varValue是需要设置的新值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " position " , info . getPcde ( ) ) ; //设置变量的子字符串共享名称为“name”的值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " maposition " , info . getPcde ( ) ) ; //设置变量的子字符串共享名称为“model”的值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " rfid " , info . getRfid ( ) ) ; //设置变量的子字符串共享名称为“location”的值
//region 设置对象里变量的值(如果需要)
//注意: 参数varName是lsf文件里对象变量的“子字符串共享名称”, varValue是需要设置的新值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " position " , info . getPcode ( ) ) ; //设置变量的子字符串共享名称为“name”的值
zmprintoperate . setVarValue ( thiszml abelo bjectlist , " maposition " , info . getPcode ( ) ) ; //设置变量的子字符串共享名称为“model”的值
zmprintoperate . setVarValue ( thiszml abelo bjectlist , " rfid " , info . getEncodedId ( ) ) ; //设置变量的子字符串共享名称为“location”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"department","国产操作系统研发13组");//设置变量的子字符串共享名称为“department”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"purchasedate","2020-10-08");//设置变量的子字符串共享名称为“purchasedate”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"assetcode","2021092300090002"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“assetcode”的值
//endregion
//endregion
//region 预览标签(即生成标签图片)
//region 预览标签(即生成标签图片)
/* BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片
try {
if(labelimage!=null) {
@@ -402,43 +421,158 @@ public class PrintController {
} catch (IOException e) {
System.out.println("保存预览图片出错,错误信息:"+e.getMessage());
}*/
//endregion
//endregion
//region 将标签生成打印数据
byte [ ] zmpclecommand = zmprintoperate . CreateLabelCommand ( thiszmprinter , thiszmlabel , thiszmlabelobjectlist ) ; //生成标签数据
if ( zmpclecommand = = null ) {
System . out . println ( " 生成标签数据异常为空. " ) ;
return " 生成标签数据异常为空 " ;
//region 将标签生成打印数据
byte [ ] zmpclecommand = zmprintoperate . CreateLabelCommand ( thiszmprinter , thiszmlabel , thiszmlabelobjectlist ) ; //生成标签数据
if ( zmpclecommand = = null ) {
System . out . println ( " 生成标签数据异常为空. " ) ;
return " 生成标签数据异常为空 " ;
}
//endregion
//region 通过打印机USB端口发送打印数据
UsbConnection usbConnection = new UsbConnection ( ) ; //建立USB连接
if ( ! usbConnection . Open ( ) ) {
System . out . println ( " 连接打印机失败,请检查。 " ) ;
return " 连接打印机失败,请检查 " ;
}
try {
usbConnection . write ( zmpclecommand ) ; //发送数据给打印机
if ( thiszmprinter . printerinterface = = printerstyle . RFID_USB ) {
//如果是RFID打印机, 则通过打印机USB端口读回状态, 确定打印机收到数据并等待打印完成
String returnmsg = usbConnection . read ( 512 ) ; //读取打印机数据
if ( ! returnmsg . isEmpty ( ) )
System . out . println ( " 打印机返回的数据: " + returnmsg ) ;
zmprintoperate . WaitPrintProcess ( thiszmprinter , thiszmlabel ) ; //等待打印完成
}
}
finally {
usbConnection . Close ( ) ; //关闭USB连接
}
//endregion
//修改表字段,标记为已打印
// rfidMapper.updatePrintRfid(info.getId());
}
//endregion
//region 通过打印机USB端口发送打印数据
//end region
}
} else if ( printType = = 2 ) { //手动输入信息打印
String printStr = ( String ) params . get ( " printStr " ) ;
// 将 printStr 转换为十六进制字符串
StringBuilder hex = new StringBuilder ( ) ;
for ( int i = 0 ; i < printStr . length ( ) ; i + + ) {
char c = printStr . charAt ( i ) ;
String hexPart = Integer . toHexString ( c ) . toUpperCase ( ) ;
hex . append ( hexPart ) ;
}
String rfidStr = hex . toString ( ) ;
//region 处理读取标签数据(如果需要)
String tagTID = " " ;
if ( isRFIDread ) {
//region 读取UHF标签数据
UsbConnection usbConnection = new UsbConnection ( ) ; //建立USB连接
if ( ! usbConnection . Open ( ) ) {
System . out . println ( " 连接打印机失败,请检查。 " ) ;
return " 连接打印机失败,请检查 " ;
}
try {
usbConnection . write ( zmpclecommand ) ; //发送数据给打印机
if ( thiszmprinter . printerinterface = = printerstyle . RFID_USB ) {
//如果是RFID打印机, 则通过打印机USB端口读回状态, 确定打印机收到数据并等待打印完成
String returnmsg = usbConnection . read ( 512 ) ; //读取打印机数据
if ( ! returnmsg . isEmpty ( ) )
System . out . println ( " 打印机返回的数据: " + returnmsg ) ;
zmprintoperate . WaitPrintProcess ( thiszmprinter , thiszmlabel ) ; //等待打印完成
if ( isRFIDwrite )
tagTID = usbConnection. readUHFTag ( 0 , 3 ) ; //读TID, 后面需要写EPC操作
else
tagTID = usbConnection . readUHFTag ( 0 , 2 ) ; //读TID, 不需要写EPC操作
usbConnection . Close ( ) ; //关闭USB连接
if ( tagTID . isEmpty ( ) | | tagTID . equals ( " X " ) ) //未读到数据
{
System . out . println ( " 未读到标签数据。 " ) ;
return " 未读到标签数据 " ;
}
//更新读到的数据
if ( tagTID ! = null & & ! tagTID . isEmpty ( ) ) {
for ( zmlabelobject thisLabelObject : thiszmlabelobjectlist ) {
for ( int vi = 0 ; vi < thisLabelObject . Variables . size ( ) ; vi + + ) {
objectvariable thisVariable = thisLabelObject . Variables . get ( vi ) ;
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) { //LSF文件中有读取HF或UHF的ID对象
thisVariable . data = tagTID ;
thisLabelObject . Variables . set ( vi , thisVariable ) ; //更新数据
}
}
}
}
finally {
usbConnection . Close ( ) ; //关闭USB连接
}
//endregion
//修改表字段,标记为已打印
rfidMapper . updatePrintRfid ( info . getId ( ) ) ;
}
//更新读到的数据
if ( tagTID ! = null & & ! tagTID . isEmpty ( ) ) {
for ( zmlabelobject thisLabelObject : thiszmlabelobjectlist ) {
for ( int vi = 0 ; vi < thisLabelObject . Variables . size ( ) ; vi + + ) {
objectvariable thisVariable = thisLabelObject . Variables . get ( vi ) ;
if ( thisVariable . type = = 4 | | thisVariable . type = = 5 ) {
thisVariable . data = tagTID ;
thisLabelObject . Variables . set ( vi , thisVariable ) ; //更新数据
}
}
}
}
//endregion
//region 设置对象里变量的值(如果需要)
//注意: 参数varName是lsf文件里对象变量的“子字符串共享名称”, varValue是需要设置的新值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " position " , printStr ) ; //设置变量的子字符串共享名称为“name”的值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " maposition " , printStr ) ; //设置变量的子字符串共享名称为“model”的值
zmprintoperate . setVarValue ( thiszmlabelobjectlist , " rfid " , rfidStr ) ; //设置变量的子字符串共享名称为“location”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"department","国产操作系统研发13组");//设置变量的子字符串共享名称为“department”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"purchasedate","2020-10-08");//设置变量的子字符串共享名称为“purchasedate”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"assetcode","2021092300090002"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“assetcode”的值
//endregion
//region 预览标签(即生成标签图片)
/* BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片
try {
if(labelimage!=null) {
File imageoutputfile = new File("labelpreview.png");
ImageIO.write(labelimage, "png", imageoutputfile);//保存图片
}
} catch (IOException e) {
System.out.println("保存预览图片出错,错误信息:"+e.getMessage());
}*/
//endregion
//region 将标签生成打印数据
byte [ ] zmpclecommand = zmprintoperate . CreateLabelCommand ( thiszmprinter , thiszmlabel , thiszmlabelobjectlist ) ; //生成标签数据
if ( zmpclecommand = = null ) {
System . out . println ( " 生成标签数据异常为空. " ) ;
return " 生成标签数据异常为空 " ;
}
//endregion
//region 通过打印机USB端口发送打印数据
UsbConnection usbConnection = new UsbConnection ( ) ; //建立USB连接
if ( ! usbConnection . Open ( ) ) {
System . out . println ( " 连接打印机失败,请检查。 " ) ;
return " 连接打印机失败,请检查 " ;
}
try {
usbConnection . write ( zmpclecommand ) ; //发送数据给打印机
if ( thiszmprinter . printerinterface = = printerstyle . RFID_USB ) {
//如果是RFID打印机, 则通过打印机USB端口读回状态, 确定打印机收到数据并等待打印完成
String returnmsg = usbConnection . read ( 512 ) ; //读取打印机数据
if ( ! returnmsg . isEmpty ( ) )
System . out . println ( " 打印机返回的数据: " + returnmsg ) ;
zmprintoperate . WaitPrintProcess ( thiszmprinter , thiszmlabel ) ; //等待打印完成
}
}
finally {
usbConnection . Close ( ) ; //关闭USB连接
}
}
// 查print_rfid表, 打印标签
return " 打印成功 " ;
}
}