test hear

This commit is contained in:
suyl
2021-07-14 14:46:35 +08:00
parent 3b0af01f75
commit 9c9b450dc7

View File

@@ -111,8 +111,9 @@ var (
)
type PrintInfo struct {
C net.Conn
Status int // 2 //在线缺纸 1 //在线 -1 //离线
C net.Conn
Status int // 2 //在线缺纸 1 //在线 -1 //离线
StatusTime time.Time
}
//连接的客户端,吧每个客户端都放进来
@@ -135,6 +136,7 @@ func ListenTcp() {
}
globals.SugarLogger.Debugf("begin listenTcp port 8000......")
go HandleTcpMessages()
go HandleCheckTcpHeart()
for {
c, err := l.Accept()
if err != nil {
@@ -188,10 +190,12 @@ func handleConn(c net.Conn) {
printNoData, _ := hex.DecodeString(data[len(heartText) : len(data)-8])
printNo = string(printNoData)
globals.SugarLogger.Debugf("handleConn printno :[%v]", printNo)
tcpClient.s.Lock()
if tcpClient.Clients[printNo] == nil {
printInfo := &PrintInfo{
C: c,
Status: printStatus2JxStatus(data[len(data)-8 : len(data)-6]),
C: c,
Status: printStatus2JxStatus(data[len(data)-8 : len(data)-6]),
StatusTime: time.Now(),
}
tcpClient.Clients[printNo] = printInfo
printFail()
@@ -203,8 +207,10 @@ func handleConn(c net.Conn) {
if tcpClient.Clients[printNo].Status != status {
tcpClient.Clients[printNo].Status = status
}
tcpClient.Clients[printNo].StatusTime = time.Now()
}
}
tcpClient.s.Unlock()
} else if strings.Contains(data, printText) {
globals.SugarLogger.Debugf("handleConn print: %v", data)
changePrintMsg(data)
@@ -225,12 +231,14 @@ func printFail() (err error) {
)
if printMsg != nil {
if err = checkPrintMsg(printMsg); err == nil {
tcpClient.s.RLock()
if tcpClient.Clients[printMsg.PrintNo] != nil {
if tcpClient.Clients[printMsg.PrintNo].C != nil {
c = tcpClient.Clients[printMsg.PrintNo].C
data, err = buildMsg(printMsg)
}
}
tcpClient.s.RUnlock()
if c != nil {
if _, err = c.Write(data); err != nil {
globals.SugarLogger.Debugf("handleTcpMessages err [%v]", err)
@@ -312,12 +320,14 @@ func HandleTcpMessages() {
)
if printMsg != nil {
if err = checkPrintMsg(printMsg); err == nil {
tcpClient.s.RLock()
if tcpClient.Clients[printMsg.PrintNo] != nil {
if tcpClient.Clients[printMsg.PrintNo].C != nil {
c = tcpClient.Clients[printMsg.PrintNo].C
data, err = buildMsg(printMsg)
}
}
tcpClient.s.RUnlock()
//else {
// changePrinterStatus(printMsg.PrintNo, printerStatusOffline)
//}
@@ -350,23 +360,16 @@ func HandleTcpMessages() {
}
}
func changePrinterStatus(printNo string, status int) (err error) {
var (
printer = &model.Printer{}
db = dao.GetDB()
)
sql := `
SELECT * FROM printer WHERE print_no = ?
`
sqlParams := []interface{}{printNo}
if err = dao.GetRow(db, printer, sql, sqlParams); err == nil {
if printer.ID != 0 {
if printer.Status != status {
dao.UpdateEntity(db, printer, "Status")
func HandleCheckTcpHeart() {
for {
tcpClient.s.Lock()
for _, v := range tcpClient.Clients {
if time.Now().Sub(v.StatusTime) > time.Minute+time.Second {
v.Status = printerStatusOffline
}
}
tcpClient.s.Unlock()
}
return err
}
func buildMsg(printMsg *model.PrintMsg) (data []byte, err error) {