|
|
|
@@ -105,34 +105,29 @@ open class PlasticBagPrinterService( |
|
|
|
} |
|
|
|
|
|
|
|
fun sendDataFlexJob(request: PrintRequest) { |
|
|
|
try { |
|
|
|
// 1. Establish Socket Connection |
|
|
|
val socket = Socket(request.printerIp, request.printerPort) |
|
|
|
socket.soTimeout = 5000 // 5 seconds timeout |
|
|
|
|
|
|
|
val writer = PrintWriter(socket.getOutputStream(), true) |
|
|
|
|
|
|
|
// 2. Format the command for DataFlex 6330 |
|
|
|
// Note: This format depends on your specific label design in CLARiSOFT. |
|
|
|
// Many DataFlex printers use the "Job Select" or "Set Variable" protocol. |
|
|
|
|
|
|
|
val command = """ |
|
|
|
^SVAR1|${request.itemCode} |
|
|
|
^SVAR2|${request.itemName} |
|
|
|
^SVAR3|${request.lotNo} |
|
|
|
^SVAR4|${request.expiryDate} |
|
|
|
^PRNT1 |
|
|
|
""".trimIndent() |
|
|
|
|
|
|
|
// 3. Send and Close |
|
|
|
writer.print(command) |
|
|
|
writer.flush() |
|
|
|
|
|
|
|
socket.close() |
|
|
|
println("Successfully sent command to DataFlex at ${request.printerIp}") |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
throw RuntimeException("Failed to communicate with DataFlex printer: ${e.message}") |
|
|
|
Socket().use { socket -> |
|
|
|
try { |
|
|
|
socket.connect(InetSocketAddress(request.printerIp, request.printerPort), 3000) |
|
|
|
val writer = PrintWriter(socket.getOutputStream(), true) |
|
|
|
|
|
|
|
// Videojet/DataFlex ASCII protocol |
|
|
|
// We assign the Lot number to both the text variable and the barcode variable |
|
|
|
val command = """ |
|
|
|
^SVAR1|${request.itemCode} |
|
|
|
^SVAR2|${request.itemName} |
|
|
|
^SVAR3|${request.lotNo} |
|
|
|
^SVAR4|${request.expiryDate} |
|
|
|
^SVAR_QR|${request.lotNo} |
|
|
|
^PRNT1 |
|
|
|
""".trimIndent() |
|
|
|
|
|
|
|
writer.println(command) |
|
|
|
writer.flush() |
|
|
|
|
|
|
|
println("DataFlex: Print job with QR data sent to ${request.printerIp}") |
|
|
|
} catch (e: Exception) { |
|
|
|
throw RuntimeException("DataFlex Error: ${e.message}") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -191,28 +186,38 @@ open class PlasticBagPrinterService( |
|
|
|
socket.connect(InetSocketAddress(request.printerIp, request.printerPort), 3000) |
|
|
|
val out = DataOutputStream(socket.getOutputStream()) |
|
|
|
|
|
|
|
// Construct TSPL commands |
|
|
|
// Note: Coordinates (x,y) are in dots. |
|
|
|
// For 203 DPI: 8 dots = 1mm. |
|
|
|
// 203 DPI: 8 dots = 1mm |
|
|
|
val tspl = StringBuilder() |
|
|
|
.append("SIZE 100 mm, 50 mm\n") // Adjust to your label size |
|
|
|
.append("SIZE 100 mm, 50 mm\n") |
|
|
|
.append("GAP 3 mm, 0 mm\n") |
|
|
|
.append("DIRECTION 1\n") |
|
|
|
.append("CLS\n") // Clear buffer |
|
|
|
// Text commands: TEXT x, y, "font", rotation, x-multi, y-multi, "content" |
|
|
|
.append("TEXT 50,50,\"ROMAN.TTF\",0,1,1,\"ITEM: ${request.itemCode}\"\n") |
|
|
|
.append("TEXT 50,100,\"ROMAN.TTF\",0,1,1,\"NAME: ${request.itemName}\"\n") |
|
|
|
.append("TEXT 50,150,\"ROMAN.TTF\",0,1,1,\"LOT: ${request.lotNo}\"\n") |
|
|
|
.append("TEXT 50,200,\"ROMAN.TTF\",0,1,1,\"EXP: ${request.expiryDate}\"\n") |
|
|
|
.append("CLS\n") |
|
|
|
|
|
|
|
// --- Text Content --- |
|
|
|
.append("TEXT 50,40,\"ROMAN.TTF\",0,1,1,\"ITEM: ${request.itemCode}\"\n") |
|
|
|
.append("TEXT 50,80,\"ROMAN.TTF\",0,1,1,\"NAME: ${request.itemName}\"\n") |
|
|
|
.append("TEXT 50,120,\"ROMAN.TTF\",0,1,1,\"EXP: ${request.expiryDate}\"\n") |
|
|
|
.append("TEXT 50,160,\"ROMAN.TTF\",0,1,1,\"LOT: ${request.lotNo}\"\n") |
|
|
|
|
|
|
|
// --- QR Code for Lot Number --- |
|
|
|
// Syntax: QRCODE x, y, ECC, cell_width, mode, rotation, "content" |
|
|
|
// ECC Level M (Standard), Cell width 4 (size), Auto mode, 0 rotation |
|
|
|
.append("QRCODE 450,40,M,4,A,0,\"${request.lotNo}\"\n") |
|
|
|
|
|
|
|
// Optional: Label for the QR Code |
|
|
|
.append("TEXT 450,180,\"ROMAN.TTF\",0,1,1,\"SCAN LOT\"\n") |
|
|
|
|
|
|
|
.append("PRINT 1,1\n") |
|
|
|
.toString() |
|
|
|
|
|
|
|
// TSC printers usually expect encoding in Windows-1252 or Thai (TIS-620) |
|
|
|
// Use TIS-620 for Thai support or Windows-1252 for English |
|
|
|
val bytes = tspl.toByteArray(Charset.forName("TIS-620")) |
|
|
|
out.write(bytes) |
|
|
|
out.flush() |
|
|
|
|
|
|
|
println("TSC: Print job with QR code sent to ${request.printerIp}") |
|
|
|
} catch (e: Exception) { |
|
|
|
println("TSC Error: ${e.message}") |
|
|
|
throw RuntimeException("TSC Printer Error: ${e.message}") |
|
|
|
} |
|
|
|
} |
|
|
|
|