|
@@ -1,12 +1,17 @@
|
|
package service
|
|
package service
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/goccy/go-json"
|
|
"iot_manager_service/app/device/dao"
|
|
"iot_manager_service/app/device/dao"
|
|
|
|
+ "iot_manager_service/app/device/edge_service"
|
|
"iot_manager_service/app/device/model"
|
|
"iot_manager_service/app/device/model"
|
|
"iot_manager_service/app/system/service"
|
|
"iot_manager_service/app/system/service"
|
|
"iot_manager_service/util/cache"
|
|
"iot_manager_service/util/cache"
|
|
"iot_manager_service/util/common"
|
|
"iot_manager_service/util/common"
|
|
"iot_manager_service/util/logger"
|
|
"iot_manager_service/util/logger"
|
|
|
|
+ "strconv"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -19,16 +24,12 @@ func (s *infoBoardService) Get(id int) (*model.InfoBoardDetail, *common.Errors)
|
|
device := &dao.InfoBoard{
|
|
device := &dao.InfoBoard{
|
|
ID: id,
|
|
ID: id,
|
|
}
|
|
}
|
|
- err := device.GetDevice()
|
|
|
|
|
|
+ getDevice, err := device.GetDevice()
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, common.FailResponse(err.Error(), nil)
|
|
return nil, common.FailResponse(err.Error(), nil)
|
|
}
|
|
}
|
|
- endTime, state := cache.GetDeviceState(device.Sn)
|
|
|
|
- return &model.InfoBoardDetail{InfoBoard: *device,
|
|
|
|
- RunState: state,
|
|
|
|
- NetworkState: state,
|
|
|
|
- EndLineTime: endTime,
|
|
|
|
- }, nil
|
|
|
|
|
|
+ detail := s.rewriteBoardDetail(getDevice)
|
|
|
|
+ return &detail, nil
|
|
}
|
|
}
|
|
|
|
|
|
func (s *infoBoardService) CreateOrUpdate(userId int64, tenantId int, req dao.InfoBoard) *common.Errors {
|
|
func (s *infoBoardService) CreateOrUpdate(userId int64, tenantId int, req dao.InfoBoard) *common.Errors {
|
|
@@ -81,17 +82,71 @@ func (s *infoBoardService) List(searchValue string, current, size int) ([]model.
|
|
}
|
|
}
|
|
var details []model.InfoBoardDetail
|
|
var details []model.InfoBoardDetail
|
|
for _, d := range devices {
|
|
for _, d := range devices {
|
|
- endTime, state := cache.GetDeviceState(d.Sn)
|
|
|
|
- details = append(details, model.InfoBoardDetail{
|
|
|
|
- InfoBoard: d,
|
|
|
|
- RunState: state,
|
|
|
|
- NetworkState: state,
|
|
|
|
- EndLineTime: endTime,
|
|
|
|
- })
|
|
|
|
|
|
+ detail := s.rewriteBoardDetail(d)
|
|
|
|
+ details = append(details, detail)
|
|
}
|
|
}
|
|
return details, total, nil
|
|
return details, total, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 从缓存中读取真实状态
|
|
|
|
+func (s *infoBoardService) rewriteBoardDetail(d dao.InfoBoard) model.InfoBoardDetail {
|
|
|
|
+ endTime, state := cache.GetDeviceState(d.Sn)
|
|
|
|
+ detail := model.InfoBoardDetail{
|
|
|
|
+ InfoBoard: d,
|
|
|
|
+ RunState: state,
|
|
|
|
+ NetworkState: state,
|
|
|
|
+ EndLineTime: endTime,
|
|
|
|
+ }
|
|
|
|
+ cacheMap := cache.GetDeviceLedData(d.Sn)
|
|
|
|
+ if cacheMap != nil {
|
|
|
|
+ marshal, err := json.Marshal(cacheMap)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Logger.Errorf("rewriteBoardDetail err1 = %v \n", err)
|
|
|
|
+ return detail
|
|
|
|
+ }
|
|
|
|
+ var cltData model.CltledData
|
|
|
|
+ err = json.Unmarshal(marshal, &cltData)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Logger.Errorf("rewriteBoardDetail err2 = %v \n", err)
|
|
|
|
+ return detail
|
|
|
|
+ }
|
|
|
|
+ detail.Power = "不在线"
|
|
|
|
+ if cltData.RealWidth != "" {
|
|
|
|
+ detail.ResolutionName = fmt.Sprintf("%v * %v", cltData.RealWidth, cltData.RealHeight)
|
|
|
|
+ detail.OnTheAir = cltData.Playing
|
|
|
|
+
|
|
|
|
+ detail.Temperature = cltData.Colortemperature
|
|
|
|
+ f, err := strconv.ParseFloat(cltData.Brightness, 64)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Logger.Errorf("rewriteBoardDetail err3 = %v \n", err)
|
|
|
|
+ return detail
|
|
|
|
+ }
|
|
|
|
+ mapRange := common.MapRange(f, 0, 255, 1, 100)
|
|
|
|
+ detail.Luminance = fmt.Sprintf("%v %%", int(mapRange))
|
|
|
|
+
|
|
|
|
+ f, err = strconv.ParseFloat(cltData.Musicvolume, 64)
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Logger.Errorf("rewriteBoardDetail err3 = %v \n", err)
|
|
|
|
+ return detail
|
|
|
|
+ }
|
|
|
|
+ mapRange = common.MapRange(f, 0, 15, 1, 100)
|
|
|
|
+ detail.Volume = fmt.Sprintf("%v %%", int(mapRange))
|
|
|
|
+
|
|
|
|
+ detail.RebootTime = cltData.RebootTime
|
|
|
|
+ detail.Power = "开机"
|
|
|
|
+ if cltData.Powerstatus != "1" {
|
|
|
|
+ detail.Power = "关机" //真实其实是睡眠状态
|
|
|
|
+ }
|
|
|
|
+ if cltData.WakeupTime != "" {
|
|
|
|
+ detail.NeverCloseDown = 2
|
|
|
|
+ detail.BootStartTime = cltData.WakeupTime
|
|
|
|
+ detail.BootEndTime = cltData.SleepTime
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return detail
|
|
|
|
+}
|
|
|
|
+
|
|
func (s *infoBoardService) Remove(userId int64, tenantId int, id int) *common.Errors {
|
|
func (s *infoBoardService) Remove(userId int64, tenantId int, id int) *common.Errors {
|
|
// 创建查询实例
|
|
// 创建查询实例
|
|
device := &dao.InfoBoard{
|
|
device := &dao.InfoBoard{
|
|
@@ -137,3 +192,94 @@ func (s *infoBoardService) GetByIds(tenantId int, ids string) []dao.InfoBoard {
|
|
}
|
|
}
|
|
return device.GetDevicesByIds(ids)
|
|
return device.GetDevicesByIds(ids)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// EdgeCmd 发送远程 命令 ,如果是 cmdNum=101 or 6 时 ,必需要带id
|
|
|
|
+func (s *infoBoardService) EdgeCmd(tenant, sn string, cmdNum, id int) {
|
|
|
|
+ action := "cmd"
|
|
|
|
+ var edgePost edge_service.CltLedControlReqPost
|
|
|
|
+ edgePost.Codes = []string{sn}
|
|
|
|
+ param := make(map[string]interface{})
|
|
|
|
+ switch cmdNum {
|
|
|
|
+ case 1: //关机
|
|
|
|
+ param["command"] = "sleep"
|
|
|
|
+ s.reqEdge(tenant, edgePost, param, action)
|
|
|
|
+ case 2: //开机
|
|
|
|
+ param["command"] = "wakeup"
|
|
|
|
+ s.reqEdge(tenant, edgePost, param, action)
|
|
|
|
+ case 3: //重启
|
|
|
|
+ param["command"] = "reboot"
|
|
|
|
+ s.reqEdge(tenant, edgePost, param, action)
|
|
|
|
+ case 4: //重新节目
|
|
|
|
+ case 6: //更新排程
|
|
|
|
+ device := &dao.InfoBoard{
|
|
|
|
+ ID: id,
|
|
|
|
+ }
|
|
|
|
+ dev, _ := device.GetDevice()
|
|
|
|
+ action = "ssched"
|
|
|
|
+ param["sleep"] = dev.BootEndTime
|
|
|
|
+ param["wakeup"] = dev.BootStartTime
|
|
|
|
+ if dev.NeverCloseDown == 1 {
|
|
|
|
+ param["sleep"] = ""
|
|
|
|
+ param["wakeup"] = ""
|
|
|
|
+ }
|
|
|
|
+ param["reboot"] = "01:30" //每日重启时间
|
|
|
|
+ s.reqEdge(tenant, edgePost, param, action)
|
|
|
|
+ case 101: //更新亮度 音量, 这里需要做成定时任务去跑
|
|
|
|
+ device := &dao.InfoBoard{
|
|
|
|
+ ID: id,
|
|
|
|
+ }
|
|
|
|
+ dev, err := device.GetDevice()
|
|
|
|
+ if err == nil && dev.Condition != "" {
|
|
|
|
+ split := strings.Split(dev.Condition, ",")
|
|
|
|
+ brightness := split[0]
|
|
|
|
+ musicvolume := split[1]
|
|
|
|
+ if isEight() {
|
|
|
|
+ //晚上不同亮度和音量
|
|
|
|
+ brightness = split[2]
|
|
|
|
+ musicvolume = split[3]
|
|
|
|
+ }
|
|
|
|
+ // 亮度
|
|
|
|
+ f, _ := strconv.ParseFloat(brightness, 64)
|
|
|
|
+ mapRange := common.MapRange(f, 0, 100, 0, 255)
|
|
|
|
+ param1 := make(map[string]interface{})
|
|
|
|
+ param1["brightness"] = int(mapRange)
|
|
|
|
+ action = "sb"
|
|
|
|
+ s.reqEdge(tenant, edgePost, param1, action)
|
|
|
|
+ //音量
|
|
|
|
+ f1, _ := strconv.ParseFloat(musicvolume, 64)
|
|
|
|
+ mapRange2 := common.MapRange(f1, 0, 100, 0, 15)
|
|
|
|
+ param2 := make(map[string]interface{})
|
|
|
|
+ param2["musicvolume"] = int(mapRange2)
|
|
|
|
+ action = "svol"
|
|
|
|
+ s.reqEdge(tenant, edgePost, param2, action)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//公用请求边缘云端
|
|
|
|
+func (s *infoBoardService) reqEdge(tenant string, edgePost edge_service.CltLedControlReqPost, param map[string]interface{}, action string) {
|
|
|
|
+ edgePost.Param = param
|
|
|
|
+ req := edge_service.CltLedControlReq{
|
|
|
|
+ Tenant: tenant,
|
|
|
|
+ Action: action,
|
|
|
|
+ PostData: edgePost,
|
|
|
|
+ }
|
|
|
|
+ edge_service.CltLedControlService{}.RequestApi(req)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 判断是否晚上
|
|
|
|
+func isEight() bool {
|
|
|
|
+ now := time.Now()
|
|
|
|
+ hour := now.Hour()
|
|
|
|
+ if hour >= 18 || hour < 6 {
|
|
|
|
+ //fmt.Println("现在是晚上")
|
|
|
|
+ return true
|
|
|
|
+ } else {
|
|
|
|
+ //fmt.Println("现在不是晚上")
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *infoBoardService) CronSync() {
|
|
|
|
+
|
|
|
|
+}
|