From 6b84c1658e6918f9c5e6a8413a22cf813ae2b3fa Mon Sep 17 00:00:00 2001 From: suyl <770236076@qq.com> Date: Thu, 22 Jul 2021 10:57:54 +0800 Subject: [PATCH] aa --- business/jxstore/event/event_tcp.go | 104 ++++++++++++---------------- 1 file changed, 46 insertions(+), 58 deletions(-) diff --git a/business/jxstore/event/event_tcp.go b/business/jxstore/event/event_tcp.go index c7bcced37..93c00cc8c 100644 --- a/business/jxstore/event/event_tcp.go +++ b/business/jxstore/event/event_tcp.go @@ -111,8 +111,9 @@ var ( regexpQrr = regexp.MustCompile(byteSignQrRight + "(.*?)" + byteSignQrRightE) regexpSound = regexp.MustCompile(byteSignSound + "(.*?)" + byteSignSoundE) - printMsgChan = make(chan interface{}) - printMsgChanFail = make(chan *model.PrintMsg) + printMsgChan = make(chan *model.PrintMsg) + printMsgCallbackChan = make(chan string) + printMsgChanFail = make(chan *model.PrintMsg) ) type PrintInfo struct { @@ -227,7 +228,7 @@ func handleConn(c net.Conn) { } } else if strings.Contains(data, printText) { globals.SugarLogger.Debugf("handleConn print callback: %v", data) - printMsgChan <- data + printMsgCallbackChan <- 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) for _, printMsg := range prints { printMsgChan <- printMsg - printMsg.Status = printMsgAlreadyLoad - dao.UpdateEntity(db, printMsg, "Status") - flag: - for { - obj, _ := <-printMsgChan - if _, ok := obj.(*model.PrintMsg); ok { - continue - } else { - if data, ok2 := obj.(string); ok2 { - changePrintMsg(data) - break flag - } - } + select { + case data, _ := <-printMsgCallbackChan: + changePrintMsg(data) } } } } -func doPrint2(printMsgChan chan interface{}) (err error) { +func doPrint2(printMsgChan chan *model.PrintMsg) (err error) { var ( db = dao.GetDB() ) for { select { - case obj, _ := <-printMsgChan: - printMsg, ok := obj.(*model.PrintMsg) - if ok { - var ( - data []byte - c net.Conn - ) - if printMsg != nil { - 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("打印机缺纸!") + case printMsg, _ := <-printMsgChan: + var ( + data []byte + c net.Conn + ) + if printMsg != nil { + 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 { - err = fmt.Errorf("未查询到此printMsg") + tcpClient.s.RUnlock() } - if err != nil { - printMsg.Status = printMsgErr - printMsg.Comment = err.Error() - dao.UpdateEntity(db, printMsg, "Status", "Comment") - delete(tcpClient.Clients, printMsg.PrintNo) - if c != nil { + } 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 { - 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") - } + } else { + globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data)) + printMsg.Status = printMsgAlreadySend + dao.UpdateEntity(db, printMsg, "Status", "Comment") } } }