This commit is contained in:
邹宗楠
2023-06-01 15:54:49 +08:00
parent 2be3fb329e
commit f27a3ee5e2
2 changed files with 14 additions and 11 deletions

View File

@@ -121,17 +121,17 @@ func handleConn(c net.Conn, t *TcpClient) error {
//5、修改数据库中打印机状态没在连接池中说明是重新连接的
//6、监听心跳时间超过1分多钟就clear掉
if t.getClients(printNo) == nil {
t.addConn(c, printNo, status)
t.buildAllMap(printNo)
addConn(c, t, printNo, status)
buildAllMap(t, printNo)
//t.TimeoutMap[printNo] <- true
t.HandleTcpMessages(printNo)
t.doPrint(printNo)
HandleTcpMessages(t, printNo)
doPrint(t, printNo)
if status == printerStatusOnline {
//t.printFail()
}
changePrinterStatus(printNo, status)
// todo 暂时关闭心跳检测
//t.HandleCheckTcpHeart(printNo)
HandleCheckTcpHeart(t, printNo)
// todo 证明打印机已经被激活,将激活打印机存入数据库,保证用户不能无限制绑定打印机
if err := dao.NotExistsCreate(printNo); err != nil {
globals.SugarLogger.Debugf("监听打印机心跳,不存在则创建 :[%v],printNo[%s]", err, printNo)
@@ -201,7 +201,7 @@ func (t *TcpClient) changePrintMsg(data string, orderNo int64, printNo string) (
return err
}
func (t *TcpClient) HandleTcpMessages(printNo string) {
func HandleTcpMessages(t *TcpClient, printNo string) {
var (
db = dao.GetDB()
offset, pageSize = 0, 10
@@ -267,7 +267,7 @@ func (t *TcpClient) readTimeoutMap(key string) bool {
return t.TimeoutMap[key]
}
func (t *TcpClient) doPrint(key string) (err error) {
func doPrint(t *TcpClient, key string) (err error) {
var (
db = dao.GetDB()
)
@@ -323,6 +323,7 @@ func (t *TcpClient) doPrint(key string) (err error) {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
//close(t.TimeoutMap[key])
delete(t.TimeoutMap, key)
t = nil
} else {
//等待回调
dataStr := <-t.CallBackMap[key]
@@ -477,16 +478,17 @@ func (t *TcpClient) doPrint(key string) (err error) {
}
//检测心跳
func (t *TcpClient) HandleCheckTcpHeart(key string) {
func HandleCheckTcpHeart(t *TcpClient, key string) {
go func() {
for {
if t.TimeoutMap[key] == true {
statusTime := t.getPrintStatusTime(key)
if !utils.IsTimeZero(statusTime) {
//1分钟内没心跳判断打印机掉线了
if time.Now().Sub(statusTime) > time.Minute+time.Second*10 {
if time.Now().Sub(statusTime) > time.Second*70 {
globals.SugarLogger.Debugf("超过一分十秒没有心跳的打印机[%s]", key)
changePrinterStatus(key, printerStatusOffline)
t = nil
delete(t.TimeoutMap, key)
}
}
@@ -495,6 +497,7 @@ func (t *TcpClient) HandleCheckTcpHeart(key string) {
close(t.MsgMap[key])
close(t.CallBackMap[key])
t.delConn(key)
t = nil
return
}
}

View File

@@ -151,7 +151,7 @@ func (t *TcpClient) delConn(key string) {
}
//添加到连接池中
func (t *TcpClient) addConn(c net.Conn, key string, status int) {
func addConn(c net.Conn, t *TcpClient, key string, status int) {
t.Lock()
defer t.Unlock()
t.Clients[key] = &PrintInfo{
@@ -190,7 +190,7 @@ func (t *TcpClient) getTimeOut(key string) bool {
return t.TimeoutMap[key]
}
func (t *TcpClient) buildAllMap(key string) {
func buildAllMap(t *TcpClient, key string) {
t.Lock()
defer t.Unlock()
t.MsgMap[key] = make(chan *model.PrintMsg, 1024)