Sfoglia il codice sorgente

[优化] 连接优化、远程喊话完善

chengqian 5 mesi fa
parent
commit
89c7d7d45e

+ 1 - 1
server/dao/devices/dev_screens.go

@@ -110,7 +110,7 @@ func UpdateScreensStatusByGatewayId(id, status int) error {
 
 func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
 	var screens Screens
-	return global.GVA_DB.Model(&screens).Debug().Select("status", "play_status", "display_status", "source_status").
+	return global.GVA_DB.Model(&screens).Select("status", "play_status", "display_status", "source_status").
 		Where("screens_code = ?", code).Updates(map[string]interface{}{
 		"status":         sta.Status,
 		"play_status":    sta.PlayStatus,

+ 5 - 5
server/protocol/screens_service.go

@@ -127,7 +127,7 @@ func (x VoiceBroadDataPack) VoiceBroad(broad string) *bytebufferpool.ByteBuffer
 
 	//将语音内容字符串转字节数组,并获取其长度供给内容长度使用
 	gbk, contentLen, _ := Utf8ToGbk([]byte(broad))
-	contentLenBytes := GetBytesArrNo(contentLen+16, 2)
+	contentLenBytes := GetBytesArrNo(contentLen+2, 2)
 
 	data := VoiceBroadData{
 		BroadContentLen: contentLenBytes,
@@ -156,10 +156,10 @@ func (x VoiceBroadData) AppendVoiceBroadContent() *bytebufferpool.ByteBuffer {
 	buf := bytebufferpool.Get()
 	x.FrameHeader = 0xFD
 	x.BroadWord = 0x01
-	x.BroadEncodeFormat = []byte{0x00, //编码格式
-		0x5B, 0x6D, 0x35, 0x32, 0x5D, //男2声
-		0x5B, 0x73, 0x35, 0x5D, //中语速
-		0x5B, 0x76, 0x31, 0x30, 0x5D} //高音量
+	x.BroadEncodeFormat = []byte{0x00}//编码格式
+	/*0x5B, 0x6D, 0x35, 0x32, 0x5D, //男2声
+	0x5B, 0x73, 0x35, 0x5D, //中语速
+	0x5B, 0x76, 0x31, 0x30, 0x5D*/ //高音量
 	x.ReservedWord = []byte{0x00, 0x00}
 
 	buf.WriteByte(x.FrameHeader)

+ 1 - 1
server/radar_test.go

@@ -44,7 +44,7 @@ func Test_verify(t *testing.T) {
 	no := protocol.GetBytesArrNo(30, 2)
 	fmt.Println("no", no)
 	pack := protocol.VoiceBroadDataPack{}
-	broad := pack.VoiceBroad("语音测试abc123")
+	broad := pack.VoiceBroad("欢迎光临")
 	fmt.Println("======", hex.EncodeToString(broad.Bytes()))
 
 }

+ 20 - 11
server/service/tcp/deviceMgr.go

@@ -43,8 +43,8 @@ func (s *Device) Process() {
 		// 将tcp连接读取到的数据读取到byte数组中, 返回读取到的byte的数目
 		n, err := s.conn.Read(buf)
 		if err != nil {
-			// 从客户端读取数据的过程中发生错误
-			logger.Logger.Errorf("read from client failed, err:%v", err)
+			// 从客户端读取数据的过程中发生错误 这里如果没读到可以视为设备离线了
+			logger.Logger.Errorf("read from client failed [离线了], err:%v", err)
 			break
 		}
 		data := hex.EncodeToString(buf[:n])
@@ -59,7 +59,7 @@ func (s *Device) Process() {
 			// 通过sn 查设备是否存在 不存在return 存在就保存con 和 info
 			screens, err := deviceDao.QueryScreenBySn(data[34:50])
 			if errors.Is(err, gorm.ErrRecordNotFound) {
-				logger.Logger.Errorf("Process SN not found \n")
+				logger.Logger.Errorf("Process[case '61'] SN not found \n")
 				break
 			}
 			s.info = screens
@@ -67,6 +67,17 @@ func (s *Device) Process() {
 			s.conn.Write(buffer.Bytes())
 		case "91":
 			logger.Logger.Debug("心跳")
+			//判断内存devices中是否存储了设备(连接)信息
+			if _, exists := devices[data[34:50]]; !exists {
+				//不存在
+				screens, err := deviceDao.QueryScreenBySn(data[34:50])
+				if errors.Is(err, gorm.ErrRecordNotFound) {
+					logger.Logger.Errorf("Process[case '91'] SN not found \n")
+					break
+				}
+				s.info = screens
+				devices[s.info.Sn] = *s
+			}
 			if data[34:50] != s.info.Sn {
 				continue
 			}
@@ -91,7 +102,7 @@ func (s *Device) Handle() {
 				DisplayStatus: 1,
 				SourceStatus:  1,
 			}
-			if time.Now().Add(-2*time.Minute).After(s.lastTime) && !s.lastTime.IsZero() {
+			if time.Now().Add(-2*time.Minute).After(s.lastTime) || s.lastTime.IsZero() {
 				//离线
 				state.Status = 0
 				state.PlayStatus = 0
@@ -100,20 +111,21 @@ func (s *Device) Handle() {
 
 				err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
 				if err != nil {
-					logger.Logger.Error("UpdateScreensStatus err", zap.Error(err))
+					logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
 					continue
 				}
 
 				s.conn.Close()
-				close(s.stopChan) // 通知协程停止
-				return            // 结束 Handle 协程
+				close(s.stopChan)          // 通知协程停止
+				delete(devices, s.info.Sn) //从内存中移除连接
+				return                     // 结束 Handle 协程
 			} else {
 				logger.Logger.Debugf("%s在线", s.info.ScreensCode)
 			}
 			//修改数据库状态
 			err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
 			if err != nil {
-				logger.Logger.Error("UpdateScreensStatus err", zap.Error(err))
+				logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
 				continue
 			}
 			break
@@ -179,7 +191,6 @@ func (s *Device) VoiceBroad(broad string) error {
 	return nil
 }
 
-// 向外暴露的开关操作
 func SwitchScreen(sn string, onOff int) error {
 	device := devices[sn]
 	err := device.SwitchScreen(onOff)
@@ -189,7 +200,6 @@ func SwitchScreen(sn string, onOff int) error {
 	return nil
 }
 
-// 向外暴露的发送内码文字操作
 func SendInternalCode(sn string, content []promodel.InternalCodeContent) error {
 	device := devices[sn]
 	err := device.SendInternalCode(content)
@@ -199,7 +209,6 @@ func SendInternalCode(sn string, content []promodel.InternalCodeContent) error {
 	return nil
 }
 
-// 向外暴露的远程喊话操作
 func VoiceBroad(sn string, broad string) error {
 	device := devices[sn]
 	err := device.VoiceBroad(broad)

+ 2 - 3
web/src/view/devicesAdmin/screens/screens.vue

@@ -422,7 +422,7 @@
             <el-row>
               <el-col :span="24">
                 <el-form-item label="播报内容:" :inline="false" prop="broadContent">
-                  <el-input v-model="voiceBroadForm.broadContent" type="textarea" :maxlength="4" show-word-limit/>
+                  <el-input v-model="voiceBroadForm.broadContent" type="textarea"/>
                 </el-form-item>
               </el-col>
             </el-row>
@@ -741,8 +741,7 @@ const closeVoiceBroadDialog = ()=>{
 }
 
 const enterVoiceBroadDialog = async () => {
-  console.log("----",{sn: nowSn.value, broadContent: voiceBroadForm.value.broadContent})
-  var res = await voiceBroad({sn: nowSn.value, broadContent: voiceBroadForm.brodContent});
+  var res = await voiceBroad({sn: nowSn.value, broadContent: voiceBroadForm.value.broadContent});
   if (res.code === 0) {
     ElMessage({
       type: 'success',