This commit is contained in:
suyl
2021-07-22 10:10:42 +08:00
parent 8c1bbd795a
commit 74b84bd21f

View File

@@ -111,7 +111,7 @@ var (
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) printMsgChan = make(chan interface{})
printMsgChanFail = make(chan *model.PrintMsg) printMsgChanFail = make(chan *model.PrintMsg)
) )
@@ -227,7 +227,8 @@ func handleConn(c net.Conn) {
} }
} else if strings.Contains(data, printText) { } else if strings.Contains(data, printText) {
globals.SugarLogger.Debugf("handleConn print callback: %v", data) globals.SugarLogger.Debugf("handleConn print callback: %v", data)
changePrintMsg(data) printMsgChan <- data
//changePrintMsg(data)
} }
} }
} }
@@ -375,57 +376,64 @@ func HandleTcpMessages() {
} }
} }
func doPrint2(printMsgChan chan *model.PrintMsg) (err error) { func doPrint2(printMsgChan chan interface{}) (err error) {
var ( var (
db = dao.GetDB() db = dao.GetDB()
) )
for { for {
select { select {
case printMsg, _ := <-printMsgChan: case obj, _ := <-printMsgChan:
var ( printMsg, ok := obj.(*model.PrintMsg)
data []byte if ok {
c net.Conn var (
) data []byte
if printMsg != nil { c net.Conn
if err = checkPrintMsg(db, printMsg); err == nil { )
tcpClient.s.RLock() if printMsg != nil {
if tcpClient.Clients[printMsg.PrintNo] != nil { if err = checkPrintMsg(db, printMsg); err == nil {
if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline { tcpClient.s.RLock()
if tcpClient.Clients[printMsg.PrintNo].C != nil { if tcpClient.Clients[printMsg.PrintNo] != nil {
c = tcpClient.Clients[printMsg.PrintNo].C if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline {
data, err = buildMsg(printMsg) 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("打印机缺纸!")
} }
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOffline { }
err = fmt.Errorf("打印机离线!") tcpClient.s.RUnlock()
} else if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnlineWithoutPaper { }
err = fmt.Errorf("打印机缺纸!") } else {
err = fmt.Errorf("未查询到此printMsg")
}
if err != nil {
printMsg.Status = printMsgErr
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()
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
printMsg.Status = printMsgAlreadySend
dao.UpdateEntity(db, printMsg, "Status", "Comment")
} }
} }
tcpClient.s.RUnlock()
} }
} else { } else {
err = fmt.Errorf("未查询到此printMsg") data, _ := <-printMsgChan
} dataStr := data.(string)
if err != nil { changePrintMsg(dataStr)
printMsg.Status = printMsgErr
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()
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
printMsg.Status = printMsgAlreadySend
dao.UpdateEntity(db, printMsg, "Status", "Comment")
}
}
} }
} }
} }