Browse Source

添加在线、离线时间段记录

chengqian 4 months ago
parent
commit
6eac40a75b

BIN
server/build/smart_intersection


+ 47 - 23
server/dao/devices/dev_screens.go

@@ -8,28 +8,29 @@ import (
 )
 
 type Screens struct {
-	ID             int            `gorm:"primarykey" json:"ID"`                    //主键ID
-	ScreensName    string         `gorm:"type:varchar(64)" json:"screensName"`     //设备名称
-	ScreensCode    string         `gorm:"type:varchar(64)" json:"screensCode"`     //设备编码
-	Sn             string         `gorm:"type:varchar(64)" json:"sn"`              //设备sn
-	Network        int            `gorm:"type:int" json:"network"`                 //组网方式 0代表网关 1代表4G模块
-	GatewayId      int            `gorm:"type:int" json:"gatewayId"`               //所属网关
-	ScreensBrand   string         `gorm:"type:varchar(60)" json:"screensBrand"`    //屏幕品牌
-	ScreensModel   string         `gorm:"type:varchar(60)" json:"screensModel"`    //屏幕型号
-	IPAddress      string         `gorm:"type:varchar(40)" json:"ipAddress"`       //IP地址
-	InstallTime    time.Time      `gorm:"type:date" json:"installTime"`            //安装时间
-	Shunt          int            `gorm:"type:int" json:"shunt"`                   //所属分路 1主路,2支路
-	ShuntDescribe  string         `gorm:"type:varchar(60)" json:"shuntDescribe"`   //分路描述
-	ResolutionId   int            `gorm:"type:int" json:"resolutionId"`            //分辨率
-	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:30" json:"nightBright"`  //晚上亮度
-	LastOnlineTime time.Time      `gorm:"type:datetime;" json:"lastOnlineTime"`    //最近在线时间
+	ID              int            `gorm:"primarykey" json:"ID"`                              //主键ID
+	ScreensName     string         `gorm:"type:varchar(64)" json:"screensName"`               //设备名称
+	ScreensCode     string         `gorm:"type:varchar(64)" json:"screensCode"`               //设备编码
+	Sn              string         `gorm:"type:varchar(64)" json:"sn"`                        //设备sn
+	Network         int            `gorm:"type:int" json:"network"`                           //组网方式 0代表网关 1代表4G模块
+	GatewayId       int            `gorm:"type:int" json:"gatewayId"`                         //所属网关
+	ScreensBrand    string         `gorm:"type:varchar(60)" json:"screensBrand"`              //屏幕品牌
+	ScreensModel    string         `gorm:"type:varchar(60)" json:"screensModel"`              //屏幕型号
+	IPAddress       string         `gorm:"type:varchar(40)" json:"ipAddress"`                 //IP地址
+	InstallTime     time.Time      `gorm:"type:date" json:"installTime"`                      //安装时间
+	Shunt           int            `gorm:"type:int" json:"shunt"`                             //所属分路 1主路,2支路
+	ShuntDescribe   string         `gorm:"type:varchar(60)" json:"shuntDescribe"`             //分路描述
+	ResolutionId    int            `gorm:"type:int" json:"resolutionId"`                      //分辨率
+	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:30" json:"nightBright"`            //晚上亮度
+	LastOnlineTime  time.Time      `gorm:"type:datetime;default:NULL" json:"lastOnlineTime"`  //最近在线时间
+	LastOfflineTime time.Time      `gorm:"type:datetime;default:NULL" json:"lastOfflineTime"` //最近离线时间
 
 	GateWay     GateWay     `gorm:"foreignkey:GatewayId"`
 	Resolution  Resolution  `gorm:"foreignkey:ResolutionId"`
@@ -111,7 +112,7 @@ func UpdateScreensStatusByGatewayId(id, status int) error {
 	return err
 }
 
-func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
+func UpdateScreensStatusAndOnline(code string, sta request.DeviceStatus) error {
 	var screens Screens
 	return global.GVA_DB.Model(&screens).Select("status", "play_status", "display_status", "source_status", "last_online_time").
 		Where("screens_code = ?", code).Updates(map[string]interface{}{
@@ -123,6 +124,29 @@ func UpdateScreensStatus(code string, sta request.DeviceStatus) error {
 	}).Error
 }
 
+func UpdateScreensStatusAndOffline(code string, sta request.DeviceStatus) error {
+	var screens Screens
+	return global.GVA_DB.Model(&screens).Select("status", "play_status", "display_status", "source_status", "last_offline_time").
+		Where("screens_code = ?", code).Updates(map[string]interface{}{
+		"status":            sta.Status,
+		"play_status":       sta.PlayStatus,
+		"display_status":    sta.DisplayStatus,
+		"source_status":     sta.SourceStatus,
+		"last_offline_time": time.Now(),
+	}).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
+}
+
 // 根据sn查询设备是否存在
 func QueryScreenBySn(sn string) (screens Screens, err error) {
 	err = global.GVA_DB.Model(&screens).Where("sn = ?", sn).First(&screens).Error

+ 2 - 2
server/service/tcp/deviceMgr.go

@@ -63,7 +63,7 @@ func (s *Device) Process() {
 				break
 			}
 
-			err = Dev.UpdateScreensStatus(screens.ScreensCode, request.DeviceStatus{
+			err = Dev.UpdateScreensStatusAndOnline(screens.ScreensCode, request.DeviceStatus{
 				Status:        1,
 				PlayStatus:    1,
 				DisplayStatus: 1,
@@ -117,7 +117,7 @@ func (s *Device) Handle() {
 			if time.Now().Add(-2*time.Minute).After(s.lastTime) || s.lastTime.IsZero() {
 				//离线
 				state.Status = 0
-				err := Dev.UpdateScreensStatus(s.info.ScreensCode, state)
+				err := Dev.UpdateScreensStatusAndOffline(s.info.ScreensCode, state)
 				if err != nil {
 					logger.Logger.Error("[Handle] UpdateScreensStatus err", zap.Error(err))
 					continue

+ 55 - 14
web/src/view/devicesAdmin/screens/screens.vue

@@ -336,35 +336,34 @@
               </el-form-item>
             </el-col>
             <el-col :span="12">
-              <el-form-item label="素材状态:" :inline="false">
-                <div class="onlinebox" :style="{'background': screensInfo.sourceStatus===0 ? 'red':'green' }"></div>
-                <span :style="{ color: screensInfo.sourceStatus === 0 ? 'red' : 'green' }">{{ screensInfo.sourceStatus === 0 ? '异常' : '正常' }}</span>
+              <el-form-item label="安装时间:" :inline="false">
+                {{screensInfo.installTime }}
               </el-form-item>
             </el-col>
           </el-row>
+          <el-row>
+          </el-row>
           <el-row>
             <el-col :span="12">
-              <el-form-item label="播放状态:" :inline="false">
-                <div class="onlinebox" :style="{'background': screensInfo.playStatus===0 ? 'red':'green' }"></div>
-                <span :style="{ color: screensInfo.playStatus === 0 ? 'red' : 'green' }">{{ screensInfo.playStatus === 0 ? '异常' : '正常' }}</span>
+              <el-form-item label="最近上线时间:" :inline="false">
+                {{ screensInfo.lastOnlineTime}}
               </el-form-item>
             </el-col>
             <el-col :span="12">
-              <el-form-item label="模板状态:" :inline="false">
-                <div class="onlinebox" :style="{'background': screensInfo.displayStatus===0 ? 'red':'green' }"></div>
-                <span :style="{ color: screensInfo.displayStatus === 0 ? 'red' : 'green' }">{{ screensInfo.displayStatus === 0 ? '异常' : '正常' }}</span>
+              <el-form-item label="累计在线时长:" :inline="false">
+                <el-tag type="success">{{ onlineSum  }}</el-tag>
               </el-form-item>
             </el-col>
           </el-row>
           <el-row>
             <el-col :span="12">
-              <el-form-item label="安装时间:" :inline="false">
-                {{screensInfo.installTime }}
+              <el-form-item label="上次离线时间:" :inline="false">
+                {{ screensInfo.lastOfflineTime }}
               </el-form-item>
             </el-col>
             <el-col :span="12">
-              <el-form-item label="最近在线时间:" :inline="false">
-                {{ dayjs(screensInfo.lastOnlineTime).format('YYYY-MM-DD HH:mm:ss') }}
+              <el-form-item label="累计离线时长:" :inline="false">
+                <el-tag type="warning">{{ offlineSum  }}</el-tag>
               </el-form-item>
             </el-col>
           </el-row>
@@ -521,6 +520,8 @@ const viewScreensDialog = ref(false)
 const inCodeDialog = ref(false)
 const voiceBroadDialog = ref(false)
 const setBrightDialog = ref(false)
+const onlineSum = ref('0')
+const offlineSum = ref('0')
 
 const rules = ref({
   screensName: [
@@ -547,7 +548,26 @@ const rules = ref({
   ]
 })
 
-
+const TimeConversion = (t) => {
+  const timestampInSec = Math.floor(t / 1000);  // 转换为秒
+  // 计算总天数
+  const totalDays = Math.floor(timestampInSec / (24 * 60 * 60));  // 每天 86400 秒
+  // 计算年数
+  const years = Math.floor(totalDays / 365);
+  // 计算剩余的天数
+  const remainingDays = totalDays % 365;
+
+  // 计算剩余的小时数
+  const hours = Math.floor((timestampInSec % (24 * 60 * 60)) / 3600);  // 每小时 3600 秒
+  // 根据年数、天数和小时数返回相应的格式
+  if (years > 0) {
+    return `${years}年${remainingDays}天${hours}小时`;
+  } else if (remainingDays > 0) {
+    return `${remainingDays}天${hours}小时`;
+  } else {
+    return `${hours}小时`;
+  }
+}
 const getNowDay = ()=>{
   let timestamp = Date.now(); // 获取当前时间戳,单位是毫秒
   screensInfo.value.installTime = new Date(timestamp)
@@ -594,6 +614,27 @@ const view = (row)=>{
   viewScreensDialog.value = true
   screensInfo.value = JSON.parse(JSON.stringify(row))
   screensInfo.value.installTime = new Date(screensInfo.value.installTime).toLocaleDateString()
+
+  if (screensInfo.value.status ===1 ){
+    onlineSum.value = TimeConversion(new Date()-new Date(screensInfo.value.lastOnlineTime))
+    offlineSum.value = TimeConversion(new Date(screensInfo.value.lastOnlineTime)-new Date(screensInfo.value.lastOfflineTime))
+  }else {
+    onlineSum.value = TimeConversion(new Date(screensInfo.value.lastOfflineTime)-new Date(screensInfo.value.lastOnlineTime))
+    offlineSum.value = TimeConversion(new Date()-new Date(screensInfo.value.lastOfflineTime))
+  }
+
+  if (screensInfo.value.lastOnlineTime === '0001-01-01T00:00:00Z' ){
+    screensInfo.value.lastOnlineTime = '——'
+    onlineSum.value = "无"
+  }else {
+    screensInfo.value.lastOnlineTime = dayjs(screensInfo.value.lastOnlineTime).format('YYYY-MM-DD HH:mm:ss')
+  }
+  if (screensInfo.value.lastOfflineTime === '0001-01-01T00:00:00Z' ){
+    screensInfo.value.lastOfflineTime = '——'
+    offlineSum.value = "无"
+  }else {
+    screensInfo.value.lastOfflineTime = dayjs(screensInfo.value.lastOfflineTime).format('YYYY-MM-DD HH:mm:ss')
+  }
 }