All checks were successful
Create Release & Upload Assets / Upload Assets To Gitea w/ goreleaser (push) Successful in 2m48s
135 lines
3.4 KiB
Go
135 lines
3.4 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/hex"
|
|
fifo "github.com/foize/go.fifo"
|
|
"github.com/justinmichaelvieira/escpos"
|
|
"github.com/liamg/tml"
|
|
"image"
|
|
"net"
|
|
"net/http"
|
|
"uberbringer/config"
|
|
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
)
|
|
|
|
func newPrintJob(input DataStruct, queue *fifo.Queue) {
|
|
var byteLength int
|
|
|
|
for _, item := range input.Data {
|
|
byteLength += len(item.Text)
|
|
}
|
|
|
|
if byteLength > 4096 {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " buffer limit reached</red>\n")
|
|
return
|
|
} else {
|
|
tml.Printf("<blue>[" + config.Project + "]: Job #" + input.JobID + " buffer size used " + string(byteLength) + " bytes </blue>\n")
|
|
}
|
|
|
|
printJobCreate(input, queue)
|
|
}
|
|
|
|
func printJobCreate(input DataStruct, queue *fifo.Queue) {
|
|
|
|
tml.Printf("<yellow>[" + config.Project + "]: Adding job #" + input.JobID + " to print queue</yellow>\n")
|
|
|
|
socket, err := net.Dial("tcp", config.PrinterAddr+":9100")
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
defer func(socket net.Conn) {
|
|
err := socket.Close()
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
}(socket)
|
|
|
|
p := escpos.New(socket)
|
|
p.SetConfig(escpos.ConfigEpsonTMT20II)
|
|
|
|
p.Initialize()
|
|
p.DefaultLineSpacing()
|
|
|
|
for _, item := range input.Data {
|
|
if item.Type <= 0 || item.Type > 7 {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " bad type</red>\n")
|
|
return
|
|
} else if item.Size <= 0 || item.Size > 16 {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " bad size</red>\n")
|
|
return
|
|
} else {
|
|
switch item.Type {
|
|
case 1:
|
|
// Generate Regular Text
|
|
p.Bold(false).Size(uint8(item.Size), uint8(item.Size)).Write(item.Text)
|
|
case 2:
|
|
// Generate Bold Text
|
|
p.Bold(true).Size(uint8(item.Size), uint8(item.Size)).Write(item.Text)
|
|
case 3:
|
|
// Generate QR Code
|
|
p.QRCode(item.Text, true, uint8(item.Size), escpos.QRCodeErrorCorrectionLevelH)
|
|
case 4:
|
|
// New Line
|
|
p.LineFeed()
|
|
case 5:
|
|
// New Line X amount of times
|
|
p.LineFeedD(uint8(item.Size))
|
|
case 6:
|
|
// Decode Base64Raw Input
|
|
/*
|
|
base64_decoded, err := base64.StdEncoding.DecodeString(item.Text)
|
|
if err != nil {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " base64 decode failed</red>\n")
|
|
return
|
|
}
|
|
*/
|
|
hex_decoded, err := hex.DecodeString(item.Text)
|
|
if err != nil {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " hex decode failed</red>\n")
|
|
return
|
|
}
|
|
|
|
p.WriteRaw([]byte(hex_decoded))
|
|
case 7:
|
|
// Print Image from URL
|
|
response, err := http.Get(item.Text)
|
|
if err != nil {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " image load failed</red>\n")
|
|
return
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != 200 {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " image load failed - received non 200 response code</red>\n")
|
|
return
|
|
}
|
|
|
|
img, _, err := image.Decode(response.Body)
|
|
if err != nil {
|
|
tml.Printf("<red>[" + config.Project + "]: Job #" + input.JobID + " image decode failed</red>\n")
|
|
return
|
|
}
|
|
|
|
p.PrintImage(img)
|
|
|
|
response.Body.Close()
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
tml.Printf("<yellow>[" + config.Project + "]: Printing job #" + input.JobID + "</yellow>\n")
|
|
|
|
p.LineFeed()
|
|
err = p.PrintAndCut()
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
|
|
tml.Printf("<green>[" + config.Project + "]: Done printing #" + input.JobID + "</green>\n")
|
|
|
|
socket.Close()
|
|
}
|