This commit is contained in:
suyl
2021-08-03 14:51:27 +08:00
parent 7a6f3b20d7
commit aeee9731cd

View File

@@ -199,56 +199,54 @@ func (t *TcpClient) doPrint(key string) (err error) {
globals.SugarLogger.Debugf("doPrint timeout") globals.SugarLogger.Debugf("doPrint timeout")
return return
default: default:
printMsg, ok := <-t.MsgMap[key] select {
if !ok { case printMsg, ok := <-t.MsgMap[key]:
return if !ok{
} return
var (
data []byte
c net.Conn
)
if printMsg != nil {
if err = checkPrintMsg(db, printMsg); err == nil {
status := t.getPrintStatus(printMsg.PrintNo)
switch status {
//只有在线才打印内容
case printerStatusOnline:
if c = t.getPrintConn(printMsg.PrintNo); c != nil {
data, err = buildMsg(printMsg)
}
case printerStatusOffline:
err = fmt.Errorf("打印机离线!")
case printerStatusOnlineWithoutPaper:
err = fmt.Errorf("打印机缺纸!")
default:
err = fmt.Errorf("打印机状态未知!")
}
} }
if err != nil { var (
globals.SugarLogger.Debugf("doPrint2 err printNo:%s, msgID:%s, err: %s", printMsg.PrintNo, printMsg.MsgID, err.Error()) data []byte
//t.delConn(printMsg.PrintNo) c net.Conn
printMsg.Status = printMsgErr )
printMsg.Comment = err.Error() if printMsg != nil {
dao.UpdateEntity(db, printMsg, "Status", "Comment") if err = checkPrintMsg(db, printMsg); err == nil {
if t.isExist(key) { status := t.getPrintStatus(printMsg.PrintNo)
t.clear(key) switch status {
globals.SugarLogger.Debugf("handleTcpMessages clear1...") //只有在线才打印内容
} case printerStatusOnline:
} else { if c = t.getPrintConn(printMsg.PrintNo); c != nil {
if c != nil { data, err = buildMsg(printMsg)
if _, err = c.Write(data); err != nil { }
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err) case printerStatusOffline:
//t.delConn(printMsg.PrintNo) err = fmt.Errorf("打印机离线!")
if t.isExist(key) { case printerStatusOnlineWithoutPaper:
t.clear(key) err = fmt.Errorf("打印机缺纸!")
globals.SugarLogger.Debugf("handleTcpMessages clear2...") default:
err = fmt.Errorf("打印机状态未知!")
}
}
if err != nil {
globals.SugarLogger.Debugf("doPrint2 err printNo:%s, msgID:%s, err: %s", printMsg.PrintNo, printMsg.MsgID, err.Error())
//t.delConn(printMsg.PrintNo)
printMsg.Status = printMsgErr
printMsg.Comment = err.Error()
dao.UpdateEntity(db, printMsg, "Status", "Comment")
close(t.TimeoutMap[key])
globals.SugarLogger.Debugf("handleTcpMessages clear1...")
} else {
if c != nil {
if _, err = c.Write(data); err != nil {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
//t.delConn(printMsg.PrintNo)
close(t.TimeoutMap[key])
globals.SugarLogger.Debugf("handleTcpMessages clear2...")
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
//等待回调
dataStr := <-t.CallBackMap[printMsg.PrintNo]
a, b := getCallbackMsgInfo(dataStr)
t.changePrintMsg(dataStr, a, b)
} }
} else {
globals.SugarLogger.Debugf("handleTcpMessages success, data: %v", hex.EncodeToString(data))
//等待回调
dataStr := <-t.CallBackMap[printMsg.PrintNo]
a, b := getCallbackMsgInfo(dataStr)
t.changePrintMsg(dataStr, a, b)
} }
} }
} }
@@ -256,6 +254,8 @@ func (t *TcpClient) doPrint(key string) (err error) {
} }
} }
}() }()
close(t.MsgMap[key])
close(t.CallBackMap[key])
return err return err
} }
@@ -273,11 +273,9 @@ func (t *TcpClient) HandleCheckTcpHeart(key string) {
//2分钟内没心跳判断打印机掉线了 //2分钟内没心跳判断打印机掉线了
if time.Now().Sub(t.getPrintStatusTime(key)) > time.Minute+time.Second*3 { if time.Now().Sub(t.getPrintStatusTime(key)) > time.Minute+time.Second*3 {
changePrinterStatus(key, printerStatusOffline) changePrinterStatus(key, printerStatusOffline)
if t.isExist(key) { globals.SugarLogger.Debugf("HandleCheckTcpHeart clear...")
globals.SugarLogger.Debugf("HandleCheckTcpHeart clear...") //t.clear(key)
//t.clear(key) close(t.TimeoutMap[key])
close(t.TimeoutMap[key])
}
} }
} }
} }
@@ -299,4 +297,5 @@ func (t *TcpClient) HandleCheckTcpHeart(key string) {
//} //}
} }
}() }()
t.Clients[key].C.Close()
} }