This commit is contained in:
suyl
2021-07-22 10:57:54 +08:00
parent c7bc1abe3c
commit 6b84c1658e

View File

@@ -111,8 +111,9 @@ var (
regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE) regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE)
regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE) regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE)
printMsgChan = make(chan interface{}) printMsgChan = make(chan *model.PrintMsg)
printMsgChanFail = make(chan *model.PrintMsg) printMsgCallbackChan = make(chan string)
printMsgChanFail = make(chan *model.PrintMsg)
) )
type PrintInfo struct { type PrintInfo struct {
@@ -227,7 +228,7 @@ 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)
printMsgChan <- data printMsgCallbackChan <- data
//changePrintMsg(data) //changePrintMsg(data)
} }
} }
@@ -370,76 +371,63 @@ func HandleTcpMessages() {
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 {
printMsgChan <- printMsg printMsgChan <- printMsg
printMsg.Status = printMsgAlreadyLoad select {
dao.UpdateEntity(db, printMsg, "Status") case data, _ := <-printMsgCallbackChan:
flag: changePrintMsg(data)
for {
obj, _ := <-printMsgChan
if _, ok := obj.(*model.PrintMsg); ok {
continue
} else {
if data, ok2 := obj.(string); ok2 {
changePrintMsg(data)
break flag
}
}
} }
} }
} }
} }
func doPrint2(printMsgChan chan interface{}) (err error) { func doPrint2(printMsgChan chan *model.PrintMsg) (err error) {
var ( var (
db = dao.GetDB() db = dao.GetDB()
) )
for { for {
select { select {
case obj, _ := <-printMsgChan: case printMsg, _ := <-printMsgChan:
printMsg, ok := obj.(*model.PrintMsg) var (
if ok { data []byte
var ( c net.Conn
data []byte )
c net.Conn if printMsg != nil {
) if err = checkPrintMsg(db, printMsg); err == nil {
if printMsg != nil { tcpClient.s.RLock()
if err = checkPrintMsg(db, printMsg); err == nil { if tcpClient.Clients[printMsg.PrintNo] != nil {
tcpClient.s.RLock() if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline {
if tcpClient.Clients[printMsg.PrintNo] != nil { if tcpClient.Clients[printMsg.PrintNo].C != nil {
if tcpClient.Clients[printMsg.PrintNo].Status == printerStatusOnline { c = tcpClient.Clients[printMsg.PrintNo].C
if tcpClient.Clients[printMsg.PrintNo].C != nil { data, err = buildMsg(printMsg)
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("打印机离线!")
} 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")
}
} }
} }
} }