This commit is contained in:
suyl
2021-07-21 18:27:21 +08:00
parent 6d6fc074ad
commit 61a271b297

View File

@@ -142,6 +142,8 @@ func ListenTcp() {
globals.SugarLogger.Debugf("begin listenTcp port 8000......") globals.SugarLogger.Debugf("begin listenTcp port 8000......")
go HandleTcpMessages() go HandleTcpMessages()
go HandleCheckTcpHeart() go HandleCheckTcpHeart()
go doPrint(printMsgChanFail)
go doPrint2(printMsgChan)
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
@@ -272,32 +274,34 @@ func doPrint(printMsgChanFail chan *model.PrintMsg) (err error) {
db = dao.GetDB() db = dao.GetDB()
) )
for { for {
printMsg := <-printMsgChanFail select {
var ( case printMsg, _ := <-printMsgChanFail:
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)
}
} }
} }
} tcpClient.s.RUnlock()
tcpClient.s.RUnlock() if c != nil {
if c != nil { if _, err = c.Write(data); err != nil {
if _, err = c.Write(data); err != nil { globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err) delete(tcpClient.Clients, printMsg.PrintNo)
delete(tcpClient.Clients, printMsg.PrintNo) c.Close()
c.Close() } else {
} else { globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data)) printMsg.Status = printMsgAlreadySend
printMsg.Status = printMsgAlreadySend dao.UpdateEntity(db, printMsg, "Status", "Comment")
dao.UpdateEntity(db, printMsg, "Status", "Comment") }
} }
} }
} }
@@ -359,7 +363,6 @@ func HandleTcpMessages() {
) )
for { for {
//一直读? //一直读?
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 {
printMsgChan <- printMsg printMsgChan <- printMsg
@@ -372,49 +375,51 @@ func doPrint2(printMsgChan chan *model.PrintMsg) (err error) {
db = dao.GetDB() db = dao.GetDB()
) )
for { for {
printMsg := <-printMsgChan select {
var ( case printMsg, _ := <-printMsgChan:
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()
} }
tcpClient.s.RUnlock() } else {
err = fmt.Errorf("未查询到此printMsg")
} }
} else { if err != nil {
err = fmt.Errorf("未查询到此printMsg") printMsg.Status = printMsgErr
} printMsg.Comment = err.Error()
if err != nil { dao.UpdateEntity(db, printMsg, "Status", "Comment")
printMsg.Status = printMsgErr delete(tcpClient.Clients, printMsg.PrintNo)
printMsg.Comment = err.Error() if c != nil {
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 { }
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data)) } else {
printMsg.Status = printMsgAlreadySend if c != nil {
dao.UpdateEntity(db, printMsg, "Status", "Comment") 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")
}
} }
} }
} }