Browse Source

优化代码3.0

chengqian 4 months ago
parent
commit
bd8436a635

+ 2 - 2
server/api/v1/devices/dev_screens.go

@@ -51,7 +51,7 @@ func (s *ScreensApi) ScreensList(c *gin.Context) {
 }
 
 func (s *ScreensApi) DelScreens(c *gin.Context) {
-	var reqId request.GetById
+	var reqId request.GetBySn
 	err := c.ShouldBindJSON(&reqId)
 	if err != nil {
 		response.FailWithMessage(err.Error(), c)
@@ -63,7 +63,7 @@ func (s *ScreensApi) DelScreens(c *gin.Context) {
 		return
 	}
 
-	err = ScreensService.DelScreens(reqId.ID)
+	err = ScreensService.DelScreens(reqId.Sn)
 	if err != nil {
 		global.GVA_LOG.Error("删除失败!", zap.Error(err))
 		response.FailWithMessage("删除失败", c)

+ 35 - 43
server/dao/devices/dev_screens.go

@@ -3,7 +3,6 @@ package devices
 import (
 	"gorm.io/gorm"
 	"server/global"
-	"server/model/common/request"
 	"time"
 )
 
@@ -24,9 +23,6 @@ type Screens struct {
 	ScreensSizeId   int            `gorm:"type:int" json:"screensSizeId"`                     //信息屏尺寸
 	IsDeleted       gorm.DeletedAt `gorm:"default:null" json:"isDeleted"`                     //是否删除
 	Status          int            `gorm:"type:int;default:0" json:"status"`                  //在线状态 0=离线,1=在线
-	PlayStatus      int            `gorm:"type:int;default:0" json:"playStatus"`              //播放状态
-	DisplayStatus   int            `gorm:"type:int;default:0" json:"displayStatus"`           //显示模板状态
-	SourceStatus    int            `gorm:"type:int;default:0" json:"sourceStatus"`            //素材状态
 	DayBright       int            `gorm:"type:int;default:100" json:"dayBright"`             //白天亮度
 	NightBright     int            `gorm:"type:int;default:10" json:"nightBright"`            //晚上亮度
 	LastOnlineTime  time.Time      `gorm:"type:datetime;default:NULL" json:"lastOnlineTime"`  //最近在线时间
@@ -47,8 +43,8 @@ func (Screens) TableName() string {
 	return "dev_screens"
 }
 
-func (s Screens) DelScreens(id int) error {
-	err := global.GVA_DB.Model(&s).Where("id = ?", id).Delete(&s).Error
+func (s Screens) DelScreens(sn string) error {
+	err := global.GVA_DB.Model(&s).Where("sn = ?", sn).Delete(&s).Error
 	return err
 }
 func (s Screens) IsExistedByCode() error {
@@ -105,56 +101,47 @@ func QueryScreensToDetail(id int) (screensList []Screens, err error) {
 	return
 }
 
-func UpdateScreensStatusByGatewayId(id, status int) error {
-	var screens Screens
-	err := global.GVA_DB.Model(&screens).
-		Select("status", "play_status", "display_status", "source_status").
-		Where("gateway_id = ?", id).
-		Updates(map[string]interface{}{
-			"status":         status,
-			"play_status":    status,
-			"display_status": status,
-			"source_status":  status,
-		}).Error
-	return err
-}
+//func UpdateScreensStatusByGatewayId(id, status int) error {
+//	var screens Screens
+//	err := global.GVA_DB.Model(&screens).
+//		Select("status").
+//		Where("gateway_id = ?", id).
+//		Updates(map[string]interface{}{
+//			"status":         status,
+//			"play_status":    status,
+//			"display_status": status,
+//			"source_status":  status,
+//		}).Error
+//	return err
+//}
 
-func UpdateScreensStatusAndOnline(code string, sta request.DeviceStatus) error {
+func UpdateScreensStatusAndOnline(code string, sta int) error {
 	var screens Screens
-	return global.GVA_DB.Model(&screens).Select("status", "play_status", "display_status", "source_status", "last_online_time", "is_login").
+	return global.GVA_DB.Model(&screens).Select("status", "last_online_time", "is_login").
 		Where("screens_code = ?", code).Updates(map[string]interface{}{
-		"status":           sta.Status,
-		"play_status":      sta.PlayStatus,
-		"display_status":   sta.DisplayStatus,
-		"source_status":    sta.SourceStatus,
+		"status":           sta,
 		"last_online_time": time.Now(),
-		"is_login":         1,
+		"is_login":         999,
 	}).Error
 }
 
-func UpdateScreensStatusAndOffline(code string, sta request.DeviceStatus) error {
+func UpdateScreensStatusAndOffline(code string, sta int) error {
 	var screens Screens
-	return global.GVA_DB.Model(&screens).Select("status", "play_status", "display_status", "source_status", "last_offline_time", "is_login").
+	return global.GVA_DB.Model(&screens).Select("status", "last_offline_time", "is_login").
 		Where("screens_code = ?", code).Updates(map[string]interface{}{
-		"status":            sta.Status,
-		"play_status":       sta.PlayStatus,
-		"display_status":    sta.DisplayStatus,
-		"source_status":     sta.SourceStatus,
+		"status":            sta,
 		"last_offline_time": time.Now(),
-		"is_login":          0,
+		"is_login":          999,
 	}).Error
 }
 
-func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
-	var screens Screens
-	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,
-		"display_status": sta.DisplayStatus,
-		"source_status":  sta.SourceStatus,
-	}).Error
-}
+//func UpdateScreensStatus(code string, sta int) error {
+//	var screens Screens
+//	return global.GVA_DB.Model(&screens).Select("status").
+//		Where("screens_code = ?", code).Updates(map[string]interface{}{
+//		"status": sta,
+//	}).Error
+//}
 
 // 根据sn查询设备是否存在
 func QueryScreenBySn(sn string) (screens Screens, err error) {
@@ -186,3 +173,8 @@ func (s Screens) QueryOnlineScreens() (screensList []Screens, err error) {
 	err = global.GVA_DB.Model(&Screens{}).Where("status =?", 1).Find(&screensList).Error
 	return
 }
+
+func QueryAllScreens() (screensList []Screens, err error) {
+	err = global.GVA_DB.Model(&Screens{}).Debug().Find(&screensList).Error
+	return
+}

+ 3 - 1
server/main.go

@@ -43,6 +43,8 @@ func main() {
 	logger.InitLog()      //日志
 	mqtt.InitMqtt()       //mqtt来消息时执行
 	utils.ScheduledTask() //定时任务
-	go tcp.ListenTcp()    //监听tcp连接
+	tcp.InitDevices()
+	go tcp.ListenTcp() //监听tcp连接
+	go tcp.IsOnline()  //检测是否在线
 	core.RunWindowsServer()
 }

+ 4 - 0
server/model/common/request/common.go

@@ -13,6 +13,10 @@ type GetById struct {
 	ID int `json:"id" form:"id"` // 主键ID
 }
 
+type GetBySn struct {
+	Sn string `json:"sn" form:"sn"` // sn
+}
+
 func (r *GetById) Uint() uint {
 	return uint(r.ID)
 }

+ 14 - 4
server/service/devices/dev_screens.go

@@ -23,9 +23,15 @@ func (screensService *ScreensService) GetScreensInfoList(info request.PageInfo)
 	keyword := info.Keyword
 	shunt := info.Shunt
 
-	gatewayList, total, err := devices.QueryScreensInfoList(limit, offset, keyword, shunt)
+	screensList, total, err := devices.QueryScreensInfoList(limit, offset, keyword, shunt)
 
-	return gatewayList, total, err
+	for i, screens := range screensList {
+		screensList[i].Status = tcp.Devices[screens.Sn].Info.Status
+		screensList[i].LastOfflineTime = tcp.Devices[screens.Sn].Info.LastOfflineTime
+		screensList[i].LastOnlineTime = tcp.Devices[screens.Sn].Info.LastOnlineTime
+	}
+
+	return screensList, total, err
 }
 
 func (screensService *ScreensService) QueryScreensParms() (list interface{}, err error) {
@@ -43,9 +49,10 @@ func (screensService *ScreensService) QueryScreensParms() (list interface{}, err
 	return params, err
 }
 
-func (screensService *ScreensService) DelScreens(id int) error {
+func (screensService *ScreensService) DelScreens(sn string) error {
 	s := devices.Screens{}
-	err := s.DelScreens(id)
+	err := s.DelScreens(sn)
+	delete(tcp.Devices, sn)
 	return err
 }
 
@@ -56,6 +63,9 @@ func (screensService *ScreensService) AddScreens(screens devices.Screens) error
 		return errors.New("设备编码已存在!")
 	}
 	err = screens.AddScreens()
+
+	//将该屏信息添加到内存中
+	tcp.Devices[screens.Sn] = &tcp.Device{Info: screens}
 	return err
 }
 

+ 67 - 117
server/service/tcp/deviceMgr.go

@@ -10,7 +10,6 @@ import (
 	Dev "server/dao/devices"
 	deviceDao "server/dao/devices"
 	promodel "server/model/common/devices"
-	"server/model/common/request"
 	"server/protocol"
 	"server/utils/logger"
 	"strings"
@@ -18,30 +17,40 @@ import (
 )
 
 var (
-	devices = make(map[string]Device) // 存储连接的全局 map
+	Devices = make(map[string]*Device) // 存储连接的全局 map
 )
 
 type Device struct {
-	info     deviceDao.Screens
-	conn     net.Conn
-	lastTime time.Time
-	stopChan chan struct{} // 添加停止通道
+	Info     deviceDao.Screens
+	Conn     net.Conn
+	LastTime time.Time
+}
+
+// 实例所有设备
+func InitDevices() {
+	// 将数据库中的设备全部离线
+	devs, _ := deviceDao.QueryAllScreens()
+	for _, screen := range devs {
+		if screen.Status != 0 {
+			screen.Status = 0
+			screen.LastOfflineTime = time.Now()
+		}
+		Devices[screen.Sn] = &Device{Info: screen}
+	}
 }
 
 func (s *Device) Start(conn net.Conn) {
-	s.conn = conn
-	s.stopChan = make(chan struct{})
+	s.Conn = conn
 	go s.Process()
-	go s.Handle()
 }
 
 func (s *Device) Process() {
 	// 函数执行完之后关闭连接
-	defer s.conn.Close()
+	defer s.Conn.Close()
 	for {
 		buf := make([]byte, 256)
 		// 将tcp连接读取到的数据读取到byte数组中, 返回读取到的byte的数目
-		n, err := s.conn.Read(buf)
+		n, err := s.Conn.Read(buf)
 		if err != nil {
 			// 从客户端读取数据的过程中发生错误 这里如果没读到可以视为设备离线了
 			logger.Logger.Errorf("read from client failed [离线了], err:%v", err)
@@ -61,119 +70,60 @@ func (s *Device) Process() {
 				logger.Logger.Errorf("Process[case '61'] SN not found \n")
 				break
 			}
-
-			err = Dev.UpdateScreensStatusAndOnline(screens.ScreensCode, request.DeviceStatus{
-				Status:        1,
-				PlayStatus:    1,
-				DisplayStatus: 1,
-				SourceStatus:  1,
-			})
-			if err != nil {
-				logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
-				continue
-			}
-
-			s.info = screens
-			devices[s.info.Sn] = *s
-			s.conn.Write(buffer.Bytes())
-			logger.Logger.Debugf("设备 [%v] 登录", s.info.ScreensName)
+			s.Info = screens
+			Devices[s.Info.Sn] = s
+			s.Conn.Write(buffer.Bytes())
+			logger.Logger.Debugf("设备 [%v] 登录", s.Info.ScreensName)
 		case "91":
-			//判断内存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 screens.IsLogin == 0 {
-					_ = Dev.UpdateScreensStatusAndOnline(screens.ScreensCode, request.DeviceStatus{
-						Status:        s.info.Status,
-						PlayStatus:    s.info.PlayStatus,
-						DisplayStatus: s.info.DisplayStatus,
-						SourceStatus:  s.info.SourceStatus,
-					})
+			dev := Devices[data[34:50]]
+			if dev.Info.Status == 0 {
+				state := 1
+				//上线 则修改数据库上线时间
+				err := Dev.UpdateScreensStatusAndOnline(dev.Info.ScreensCode, state)
+				if err != nil {
+					logger.Logger.Error("Process[case '91'] UpdateScreensStatusAndOnline err", zap.Error(err))
+					continue
 				}
+				dev.Info.Status = 1
+				dev.Info.LastOnlineTime = time.Now()
+				logger.Logger.Debugf("设备 [%v] 上线", dev.Info.ScreensName)
 			}
-			logger.Logger.Debugf("设备 [%v] 心跳", s.info.ScreensName)
-			if data[34:50] != s.info.Sn {
-				continue
-			}
-			s.UpdateInfo(data[82:84], data[86:88], data[90:92])
+			dev.Conn = s.Conn
+			dev.LastTime = time.Now()
+			logger.Logger.Debugf("设备 [%v] 心跳", dev.Info.ScreensName)
 		default:
 			fmt.Println("读取:", data)
 		}
 	}
 }
 
-func (s *Device) Handle() {
-	defer s.conn.Close()
-	t2 := time.NewTicker(6 * time.Minute) //在线监测
-	defer t2.Stop()
-
+func IsOnline() {
+	t := time.NewTicker(1 * time.Minute) //每分钟
 	for {
 		select {
-		case <-t2.C:
-			state := request.DeviceStatus{
-				Status:        1,
-				PlayStatus:    s.info.PlayStatus,
-				DisplayStatus: s.info.DisplayStatus,
-				SourceStatus:  s.info.SourceStatus,
-			}
-			if time.Now().Add(-5*time.Minute).After(s.lastTime) || s.lastTime.IsZero() {
-				//离线
-				state.Status = 0
-				err := Dev.UpdateScreensStatusAndOffline(s.info.ScreensCode, state)
-				if err != nil {
-					logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
-					continue
+		case <-t.C:
+			for _, device := range Devices {
+				//符合条件
+				if (time.Now().Add(-5*time.Minute).After(device.LastTime) || device.LastTime.IsZero()) && device.Info.Status != 0 {
+					//修改数据库和内存
+					//离线
+					state := 0
+					err := Dev.UpdateScreensStatusAndOffline(device.Info.ScreensCode, state)
+					if err != nil {
+						logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
+						continue
+					}
+					device.Info.Status = 0
+					device.Info.LastOfflineTime = time.Now()
+					logger.Logger.Debugf("设备 [%v] 离线了...", device.Info.ScreensName)
 				}
-
-				s.conn.Close()
-				close(s.stopChan)          // 通知协程停止
-				delete(devices, s.info.Sn) //从内存中移除连接
-				logger.Logger.Debugf("设备 [%v] 离线了...", s.info.ScreensName)
-				return // 结束 Handle 协程
-			} else {
-				logger.Logger.Debugf("设备 [%s] 在线", s.info.ScreensCode)
 			}
-			//修改数据库状态
-			err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
-			if err != nil {
-				logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
-				continue
-			}
-			break
-		default:
-			continue
 		}
 	}
 }
 
-func (s *Device) UpdateInfo(playStatus, displayStatus, sourceStatus string) {
-	play, display, source := 0, 0, 0
-	if playStatus == "31" {
-		play = 1
-	}
-	if displayStatus == "30" {
-		display = 1
-	}
-	if sourceStatus == "30" {
-		source = 1
-	}
-	s.info.Status = 1
-	s.info.PlayStatus = play
-	s.info.DisplayStatus = display
-	s.info.SourceStatus = source
-	s.lastTime = time.Now()
-}
-
 func (s *Device) SwitchScreen(onOff int) error {
-	if s.conn == nil {
+	if s.Conn == nil {
 		return errors.New("connection is nil")
 	}
 	pack := protocol.SwitchDataPack{Type: 0x52} //熄屏
@@ -181,7 +131,7 @@ func (s *Device) SwitchScreen(onOff int) error {
 		pack = protocol.SwitchDataPack{Type: 0x51} //亮屏
 	}
 	buf := pack.SwitchScreens() //获取开关屏指令
-	_, err := s.conn.Write(buf.Bytes())
+	_, err := s.Conn.Write(buf.Bytes())
 	if err != nil {
 		logger.Logger.Errorf("SwitchScreen write failed, err:%v", err)
 		return err
@@ -190,13 +140,13 @@ func (s *Device) SwitchScreen(onOff int) error {
 }
 
 func (s *Device) SendInternalCode(content []promodel.InternalCodeContent) error {
-	if s.conn == nil {
+	if s.Conn == nil {
 		return errors.New("connection is nil")
 	}
 	pack := protocol.InternalCodeDataPack{}
 	//获取要写入连接的字节数组
 	buf := pack.SendInternalCode(content)
-	_, err := s.conn.Write(buf.Bytes())
+	_, err := s.Conn.Write(buf.Bytes())
 	if err != nil {
 		logger.Logger.Errorf("SendInternalCode write failed, err:%v", err)
 		return err
@@ -206,12 +156,12 @@ func (s *Device) SendInternalCode(content []promodel.InternalCodeContent) error
 }
 
 func (s *Device) VoiceBroad(broad string) error {
-	if s.conn == nil {
+	if s.Conn == nil {
 		return errors.New("connection is nil")
 	}
 	pack := protocol.VoiceBroadDataPack{}
 	buf := pack.VoiceBroad(broad)
-	_, err := s.conn.Write(buf.Bytes())
+	_, err := s.Conn.Write(buf.Bytes())
 	if err != nil {
 		logger.Logger.Errorf("VoiceBroad write failed, err:%v", err)
 		return err
@@ -220,12 +170,12 @@ func (s *Device) VoiceBroad(broad string) error {
 }
 
 func (s *Device) SetBrightness(bright byte) error {
-	if s.conn == nil {
+	if s.Conn == nil {
 		return errors.New("connection is nil")
 	}
 	pack := protocol.SetBrightnessDataPack{}
 	buf := pack.SetBrightness(bright)
-	_, err := s.conn.Write(buf.Bytes())
+	_, err := s.Conn.Write(buf.Bytes())
 	if err != nil {
 		logger.Logger.Errorf("SetBrightness write failed, err:%v", err)
 		return err
@@ -234,7 +184,7 @@ func (s *Device) SetBrightness(bright byte) error {
 }
 
 func SwitchScreen(sn string, onOff int) error {
-	device := devices[sn]
+	device := Devices[sn]
 	err := device.SwitchScreen(onOff)
 	if err != nil {
 		return err
@@ -243,7 +193,7 @@ func SwitchScreen(sn string, onOff int) error {
 }
 
 func SendInternalCode(sn string, content []promodel.InternalCodeContent) error {
-	device := devices[sn]
+	device := Devices[sn]
 	err := device.SendInternalCode(content)
 	if err != nil {
 		return err
@@ -252,7 +202,7 @@ func SendInternalCode(sn string, content []promodel.InternalCodeContent) error {
 }
 
 func VoiceBroad(sn string, broad string) error {
-	device := devices[sn]
+	device := Devices[sn]
 	err := device.VoiceBroad(broad)
 	if err != nil {
 		return err
@@ -261,7 +211,7 @@ func VoiceBroad(sn string, broad string) error {
 }
 
 func SetBrightness(sn string, bright int) error {
-	device := devices[sn]
+	device := Devices[sn]
 	err := device.SetBrightness(brightnessMap[bright])
 	if err != nil {
 		return err

+ 7 - 8
server/utils/mqtt/mqtt_handle.go

@@ -8,7 +8,6 @@ import (
 	"runtime"
 	"runtime/debug"
 	Dev "server/dao/devices"
-	"server/model/common/request"
 	"server/utils/logger"
 	"strconv"
 	"strings"
@@ -96,13 +95,13 @@ func (o *MqttHandler) Handler() interface{} {
 			if err != nil {
 				logger.Logger.Error("UpdateCameraStatus err", zap.Error(err))
 			}
-		case TopicDeviceScreens:
-			var stat request.DeviceStatus
-			jsoni.Unmarshal([]byte(m.PayloadString()), &stat)
-			err = Dev.UpdateScreensStatus(deviceCode, stat)
-			if err != nil {
-				logger.Logger.Error("UpdateScreensStatus err", zap.Error(err))
-			}
+		//case TopicDeviceScreens:
+		//var stat request.DeviceStatus
+		//jsoni.Unmarshal([]byte(m.PayloadString()), &stat)
+		//err = Dev.UpdateScreensStatus(deviceCode, stat)
+		//if err != nil {
+		//	logger.Logger.Error("UpdateScreensStatus err", zap.Error(err))
+		//}
 		default:
 			fmt.Println("我是主题:::", topic)
 		}

+ 1 - 1
web/src/view/devicesAdmin/screens/screens.vue

@@ -788,7 +788,7 @@ const deleteScreens = (obj) => {
     type: 'warning',
   })
       .then(async() => {
-        const res = await deleteBaseScreens({ ID:obj.ID })
+        const res = await deleteBaseScreens({ Sn:obj.sn })
         if (res.code === 0) {
           ElMessage({
             type: 'success',