Browse Source

新增定时开关屏幕功能

chengqian 4 months ago
parent
commit
dfa024881a

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

@@ -208,14 +208,14 @@ func (s *ScreensApi) VoiceBroad(c *gin.Context) {
 }
 
 // 调节亮度
-func (s *ScreensApi) SetBrightAudio(c *gin.Context) {
+func (s *ScreensApi) SetBrightAudioLed(c *gin.Context) {
 	var sb model.ReqBrightness
 	err := c.ShouldBindJSON(&sb)
 	if err != nil {
 		response.FailWithMessage(err.Error(), c)
 		return
 	}
-	err = ScreensService.SetBrightAudio(sb)
+	err = ScreensService.SetBrightAudioLed(sb)
 	if err != nil {
 		global.GVA_LOG.Error("操作失败!", zap.Error(err))
 		response.FailWithMessage("操作失败", c)

+ 9 - 5
server/dao/devices/dev_screens.go

@@ -33,8 +33,10 @@ type Screens struct {
 	LastOfflineTime time.Time      `gorm:"type:datetime;default:NULL" json:"lastOfflineTime"` //最近离线时间
 	IsLogin         int            `gorm:"type:int;default:0" json:"isLogin"`                 //是否登录 0=未登录,1=登录
 	SwitchSn        string         `gorm:"type:varchar(25)" json:"switchSn"`                  //时控开关sn
-	AudioOn         string         `gorm:"type:varchar(10);default:'00:00'" json:"audioOn"`   //开时间
-	AudioOff        string         `gorm:"type:varchar(10);default:'00:00'" json:"audioOff"`  //关时间
+	AudioOn         string         `gorm:"type:varchar(10);default:'00:00'" json:"audioOn"`   //声音开时间
+	AudioOff        string         `gorm:"type:varchar(10);default:'00:00'" json:"audioOff"`  //声音关时间
+	LedOn           string         `gorm:"type:varchar(10);default:'00:00'" json:"ledOn"`     //屏幕亮屏时间
+	LedOff          string         `gorm:"type:varchar(10);default:'00:00'" json:"ledOff"`    //屏幕熄屏时间
 
 	GateWay     GateWay     `gorm:"foreignkey:GatewayId"`
 	Resolution  Resolution  `gorm:"foreignkey:ResolutionId"`
@@ -163,16 +165,18 @@ func QueryScreenBySn(sn string) (screens Screens, err error) {
 	return screens, nil
 }
 
-// 根据sn更新白天晚上亮度
-func (s Screens) UpdateBrightAudioBySn(sn string, dayBright, nightBright int, audioOn, audioOff string) error {
+// 根据sn更新白天晚上亮度、声音、屏幕开关时间
+func (s Screens) UpdateBrightAudioLedBySn(sn string, dayBright, nightBright int, audioOn, audioOff, ledOn, ledOff string) error {
 	err := global.GVA_DB.Model(&s).
-		Select("day_bright", "night_bright", "audio_on", "audio_off").
+		Select("day_bright", "night_bright", "audio_on", "audio_off", "led_on", "led_off").
 		Where("sn = ?", sn).
 		Updates(map[string]interface{}{
 			"day_bright":   dayBright,
 			"night_bright": nightBright,
 			"audio_on":     audioOn,
 			"audio_off":    audioOff,
+			"led_on":       ledOn,
+			"led_off":      ledOff,
 		}).Error
 	return err
 }

+ 2 - 0
server/model/devices/dev_screens.go

@@ -47,6 +47,8 @@ type ReqBrightness struct {
 	AudioOn     string `json:"audioOn"`
 	AudioOff    string `json:"audioOff"`
 	SwitchSn    string `json:"switchSn"`
+	LedOn       string `json:"ledOn"`
+	LedOff      string `json:"ledOff"`
 }
 
 type BatchUpdateDeviceTime struct {

+ 4 - 4
server/router/devices/dev_screens.go

@@ -19,10 +19,10 @@ func (l *ScreensRouter) InitScreensRouter(Router *gin.RouterGroup, RouterPub *gi
 		screensRouter.POST("addScreens", baseApi.AddScreens)      //添加显示屏
 		screensRouter.DELETE("delScreens", baseApi.DelScreens)    // 删除显示屏
 
-		screensRouter.POST("switchScreens", baseApi.SwitchScreens)       // 熄屏-亮屏
-		screensRouter.POST("sendInternalCode", baseApi.SendInternalCode) // 发送内码文字
-		screensRouter.POST("voiceBroad", baseApi.VoiceBroad)             // 远程喊话
-		screensRouter.POST("setBrightAudio", baseApi.SetBrightAudio)     // 调节亮度和声音开关
+		screensRouter.POST("switchScreens", baseApi.SwitchScreens)         // 熄屏-亮屏
+		screensRouter.POST("sendInternalCode", baseApi.SendInternalCode)   // 发送内码文字
+		screensRouter.POST("voiceBroad", baseApi.VoiceBroad)               // 远程喊话
+		screensRouter.POST("setBrightAudioLed", baseApi.SetBrightAudioLed) // 调节亮度、声音开关时间、屏幕开关
 	}
 	{
 		screensRouterWithoutRecord.POST("getScreensList", baseApi.ScreensList) // 分页获取网关列表

+ 2 - 2
server/service/devices/dev_screens.go

@@ -100,10 +100,10 @@ func (screensService *ScreensService) VoiceBroad(sn string, content string) erro
 }
 
 // 设置屏幕亮度和声音开关时间
-func (screensService *ScreensService) SetBrightAudio(sb model.ReqBrightness) error {
+func (screensService *ScreensService) SetBrightAudioLed(sb model.ReqBrightness) error {
 	//先将白天、晚上亮度 声音开关时间 更新到数据库
 	screens := devices.Screens{}
-	err := screens.UpdateBrightAudioBySn(sb.Sn, sb.DayBright, sb.NightBright, sb.AudioOn, sb.AudioOff)
+	err := screens.UpdateBrightAudioLedBySn(sb.Sn, sb.DayBright, sb.NightBright, sb.AudioOn, sb.AudioOff, sb.LedOn, sb.LedOff)
 	if err != nil {
 		logger.Logger.Errorf("SetBright faild,error: %v \n", err)
 		return errors.New("亮度更新数据库失败!")

+ 12 - 0
server/service/tcp/deviceMgr.go

@@ -173,6 +173,9 @@ func (s *Device) UpdateInfo(playStatus, displayStatus, sourceStatus string) {
 }
 
 func (s *Device) SwitchScreen(onOff int) error {
+	if s.conn == nil {
+		return errors.New("connection is nil")
+	}
 	pack := protocol.SwitchDataPack{Type: 0x52} //熄屏
 	if onOff == 1 {
 		pack = protocol.SwitchDataPack{Type: 0x51} //亮屏
@@ -187,6 +190,9 @@ func (s *Device) SwitchScreen(onOff int) error {
 }
 
 func (s *Device) SendInternalCode(content []promodel.InternalCodeContent) error {
+	if s.conn == nil {
+		return errors.New("connection is nil")
+	}
 	pack := protocol.InternalCodeDataPack{}
 	//获取要写入连接的字节数组
 	buf := pack.SendInternalCode(content)
@@ -200,6 +206,9 @@ func (s *Device) SendInternalCode(content []promodel.InternalCodeContent) error
 }
 
 func (s *Device) VoiceBroad(broad string) error {
+	if s.conn == nil {
+		return errors.New("connection is nil")
+	}
 	pack := protocol.VoiceBroadDataPack{}
 	buf := pack.VoiceBroad(broad)
 	_, err := s.conn.Write(buf.Bytes())
@@ -211,6 +220,9 @@ func (s *Device) VoiceBroad(broad string) error {
 }
 
 func (s *Device) SetBrightness(bright byte) error {
+	if s.conn == nil {
+		return errors.New("connection is nil")
+	}
 	pack := protocol.SetBrightnessDataPack{}
 	buf := pack.SetBrightness(bright)
 	_, err := s.conn.Write(buf.Bytes())

+ 30 - 0
server/task/devices.go

@@ -41,3 +41,33 @@ func SetBrightnessAtNight() {
 		_ = tcp.SetBrightness(s.Sn, s.NightBright)
 	}
 }
+
+// 定时开关屏
+func SetTimeSwitchScreens() {
+	currentTime := time.Now().Format("15:04")
+
+	screens := devices.Screens{}
+	screensList, _ := screens.QueryOnlineScreens()
+
+	for _, s := range screensList {
+		//如果两个时间相等表示常亮
+		if s.LedOn == s.LedOff {
+			continue
+		}
+		if s.LedOff == currentTime {
+			//0代表关屏
+			err := tcp.SwitchScreen(s.Sn, 0)
+			if err != nil {
+				continue
+			}
+		}
+
+		if s.LedOn == currentTime {
+			//1代表开屏
+			err := tcp.SwitchScreen(s.Sn, 1)
+			if err != nil {
+				continue
+			}
+		}
+	}
+}

+ 6 - 0
server/utils/task.go

@@ -21,5 +21,11 @@ func ScheduledTask() {
 	_ = c.AddFunc("0 0 18 * * ?", func() {
 		task.SetBrightnessAtNight()
 	})
+
+	//添加每小时的整十分钟的定时任务 控制设备开关
+	_ = c.AddFunc("0 */10 * * * ?", func() {
+		task.SetTimeSwitchScreens()
+	})
+	
 	c.Start()
 }

+ 2 - 2
web/src/api/screens.js

@@ -63,9 +63,9 @@ export const voiceBroad = (data) => {
     })
 }
 
-export const setBrightAudio = (data) => {
+export const setBrightAudioLed = (data) => {
     return service({
-        url: '/screens/setBrightAudio',
+        url: '/screens/setBrightAudioLed',
         method: 'post',
         data: data
     })

+ 30 - 5
web/src/view/devicesAdmin/screens/screens.vue

@@ -493,12 +493,35 @@
                 </el-form-item>
               </el-col>
             </el-row>
+            <el-divider content-position="left">屏幕<span style="color: red">(默认00:00-00:00表示常亮)</span> </el-divider>
+            <el-row :gutter="25">
+              <el-col :span="11">
+                <el-form-item label="亮:" :inline="false">
+                  <el-select v-model="setBrightForm.ledOn" placeholder="请设置时间">
+                    <template #prefix>
+                      <span style="padding-left: 3px;padding-top: 4px;"><el-icon><Clock /></el-icon></span>
+                    </template>
+                    <el-option v-for=" x in timeArr" :label="x" :value="x" />
+                  </el-select>
+                </el-form-item>
+              </el-col>
+              <el-col :span="11">
+                <el-form-item label="熄:" :inline="false">
+                  <el-select v-model="setBrightForm.ledOff" placeholder="请设置时间">
+                    <template #prefix>
+                      <span style="padding-left: 3px;padding-top: 4px;"><el-icon><Clock /></el-icon></span>
+                    </template>
+                    <el-option v-for=" x in timeArr" :label="x" :value="x" />
+                  </el-select>
+                </el-form-item>
+              </el-col>
+            </el-row>
           </el-form>
         <template #footer>
           <div class="dialog-footer">
             <el-button @click="closeSetBrightDialog">取消</el-button>
             <el-button type="primary" @click="enterSetBrightDialog">
-              发送
+              确定
             </el-button>
           </div>
         </template>
@@ -514,7 +537,7 @@ import {
   setScreensInfo,
   addScreens,
   switchScreens,
-  sendInternalCode, voiceBroad, setBrightAudio
+  sendInternalCode, voiceBroad, setBrightAudioLed
 } from "@/api/screens";
 import { ref,reactive} from 'vue'
 import {ElMessage, ElMessageBox} from "element-plus";
@@ -896,7 +919,7 @@ const openSetBrightDialog = async (obj)=>{
     return
   }
   console.log("=====",obj)
-  setBrightForm.value = {dayBright:obj.dayBright,nightBright:obj.nightBright,audioOn:obj.audioOn,audioOff:obj.audioOff,switchSn:obj.switchSn}
+  setBrightForm.value = {dayBright:obj.dayBright,nightBright:obj.nightBright,audioOn:obj.audioOn,audioOff:obj.audioOff,switchSn:obj.switchSn,ledOn:obj.ledOn,ledOff:obj.ledOff}
   setBrightDialog.value = true
   nowSn.value = obj.sn
 }
@@ -908,13 +931,15 @@ const closeSetBrightDialog = ()=>{
 }
 
 const enterSetBrightDialog = async () => {
-  var res = await setBrightAudio({
+  var res = await setBrightAudioLed({
     sn: nowSn.value,
     dayBright: setBrightForm.value.dayBright,
     nightBright: setBrightForm.value.nightBright,
     audioOn: setBrightForm.value.audioOn,
     audioOff: setBrightForm.value.audioOff,
-    switchSn: setBrightForm.value.switchSn
+    switchSn: setBrightForm.value.switchSn,
+    ledOn:setBrightForm.value.ledOn,
+    ledOff:setBrightForm.value.ledOff
   });
   if (res.code === 0) {
     ElMessage({