This commit is contained in:
suyl
2021-07-21 18:19:14 +08:00
parent a6b1a43a86
commit 6d6fc074ad

View File

@@ -109,6 +109,9 @@ var (
regexpQrl = regexp.MustCompile(byteSignQrLeft + "(.*?)" + byteSignQrLeftE) regexpQrl = regexp.MustCompile(byteSignQrLeft + "(.*?)" + byteSignQrLeftE)
regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE) regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE)
regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE) regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE)
printMsgChan = make(chan *model.PrintMsg)
printMsgChanFail = make(chan *model.PrintMsg)
) )
type PrintInfo struct { type PrintInfo struct {
@@ -259,6 +262,17 @@ func printFail() (err error) {
) )
prints, _ := dao.GetPrintMsgs(db, []int{printMsgWait, printMsgFail, printMsgErr}, time.Now().Add(-time.Hour*3), time.Now(), 0, 999) prints, _ := dao.GetPrintMsgs(db, []int{printMsgWait, printMsgFail, printMsgErr}, time.Now().Add(-time.Hour*3), time.Now(), 0, 999)
for _, printMsg := range prints { for _, printMsg := range prints {
printMsgChanFail <- printMsg
}
return err
}
func doPrint(printMsgChanFail chan *model.PrintMsg) (err error) {
var (
db = dao.GetDB()
)
for {
printMsg := <-printMsgChanFail
var ( var (
data []byte data []byte
c net.Conn c net.Conn
@@ -289,7 +303,6 @@ func printFail() (err error) {
} }
} }
} }
return err
} }
func printStatus2JxStatus(printStatus string) (status int) { func printStatus2JxStatus(printStatus string) (status int) {
@@ -346,53 +359,62 @@ func HandleTcpMessages() {
) )
for { for {
//一直读? //一直读?
var err error
time.Sleep(time.Second / 2) time.Sleep(time.Second / 2)
prints, _ := dao.GetPrintMsgs(db, []int{printMsgWait}, time.Now().Add(-time.Hour*3), time.Now(), offset, pageSize) prints, _ := dao.GetPrintMsgs(db, []int{printMsgWait}, time.Now().Add(-time.Hour*3), time.Now(), offset, pageSize)
for _, printMsg := range prints { for _, printMsg := range prints {
var ( printMsgChan <- printMsg
data []byte }
c net.Conn }
) }
if printMsg != nil {
if err = checkPrintMsg(db, printMsg); err == nil { func doPrint2(printMsgChan chan *model.PrintMsg) (err error) {
tcpClient.s.RLock() var (
if tcpClient.Clients[printMsg.PrintNo] != nil { db = dao.GetDB()
if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline { )
if tcpClient.Clients[printMsg.PrintNo].C != nil { for {
c = tcpClient.Clients[printMsg.PrintNo].C printMsg := <-printMsgChan
data, err = buildMsg(printMsg) var (
} data []byte
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOffline { c net.Conn
err = fmt.Errorf("打印机离线!") )
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnlineWithoutPaper { if printMsg != nil {
err = fmt.Errorf("打印机缺纸!") if err = checkPrintMsg(db, printMsg); err == nil {
tcpClient.s.RLock()
if tcpClient.Clients[printMsg.PrintNo] != nil {
if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline {
if tcpClient.Clients[printMsg.PrintNo].C != nil {
c = tcpClient.Clients[printMsg.PrintNo].C
data, err = buildMsg(printMsg)
} }
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOffline {
err = fmt.Errorf("打印机离线!")
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnlineWithoutPaper {
err = fmt.Errorf("打印机缺纸!")
} }
tcpClient.s.RUnlock()
} }
} else { tcpClient.s.RUnlock()
err = fmt.Errorf("未查询到此printMsg")
} }
if err != nil { } else {
printMsg.Status = printMsgErr err = fmt.Errorf("未查询到此printMsg")
printMsg.Comment = err.Error() }
dao.UpdateEntity(db, printMsg, "Status", "Comment") if err != nil {
delete(tcpClient.Clients, printMsg.PrintNo) printMsg.Status = printMsgErr
if c != nil { printMsg.Comment = err.Error()
dao.UpdateEntity(db, printMsg, "Status", "Comment")
delete(tcpClient.Clients, printMsg.PrintNo)
if c != nil {
c.Close()
}
} else {
if c != nil {
if _, err = c.Write(data); err != nil {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
delete(tcpClient.Clients, printMsg.PrintNo)
c.Close() c.Close()
} } else {
} else { globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
if c != nil { printMsg.Status = printMsgAlreadySend
if _, err = c.Write(data); err != nil { dao.UpdateEntity(db, printMsg, "Status", "Comment")
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
delete(tcpClient.Clients, printMsg.PrintNo)
c.Close()
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
printMsg.Status = printMsgAlreadySend
dao.UpdateEntity(db, printMsg, "Status", "Comment")
}
} }
} }
} }