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