123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package service
- import (
- "fmt"
- "github.com/goccy/go-json"
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/device/edge_service"
- "iot_manager_service/app/device/model"
- "iot_manager_service/app/system/service"
- "iot_manager_service/util/cache"
- "iot_manager_service/util/common"
- "iot_manager_service/util/logger"
- "strconv"
- "strings"
- "time"
- )
- var InfoBoardService = new(infoBoardService)
- type infoBoardService struct{}
- func (s *infoBoardService) Get(id int) (model.InfoBoardDetail, *common.Errors) {
- // 创建查询实例
- device := &dao.InfoBoard{
- ID: id,
- }
- getDevice, err := device.GetDevice()
- if err != nil {
- return model.InfoBoardDetail{}, common.FailResponse(err.Error(), nil)
- }
- detail := s.rewriteBoardDetail(getDevice)
- return detail, nil
- }
- func (s *infoBoardService) CreateOrUpdate(userId int, tenantId string, req dao.InfoBoard) *common.Errors {
- // 创建查询实例
- device := req
- device.TenantId = tenantId
- device.UpdateUser = userId
- device.UpdateTime = time.Now()
- if req.ID == 0 {
- device.CreateTime = time.Now()
- device.CreateUser = userId
- if device.IsExistedBySN() {
- logger.Logger.Errorf("Create GetDeviceID err \n")
- return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
- }
- if err := device.Create(); err != nil {
- logger.Logger.Errorf("Create err = %s \n", err.Error())
- return common.FailResponse(err.Error(), nil)
- }
- service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
- common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
- return common.SuccessResponse(common.Succeeded, nil)
- }
- if device.IsExistedByNameAndCode() {
- return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
- }
- if err := device.Update(); err != nil {
- logger.Logger.Errorf("Update err = %s \n", err.Error())
- return common.FailResponse(err.Error(), nil)
- }
- service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
- common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
- return common.SuccessResponse(common.Succeeded, nil)
- }
- func (s *infoBoardService) List(searchValue string, current, size int) ([]model.InfoBoardDetail, int64, *common.Errors) {
- device := dao.InfoBoard{}
- if searchValue != "" {
- device.Sn = searchValue
- device.InfoName = searchValue
- }
- offset := (current - 1) * size
- limit := size
- devices, total, err := device.GetDevices(offset, limit)
- if err != nil {
- return nil, 0, common.FailResponse(err.Error(), nil)
- }
- var details []model.InfoBoardDetail
- for _, d := range devices {
- detail := s.rewriteBoardDetail(d)
- details = append(details, detail)
- }
- 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
- detail.RealStartAndEndTime = fmt.Sprintf("%v-%v", cltData.WakeupTime, cltData.SleepTime)
- }
- }
- }
- return detail
- }
- func (s *infoBoardService) Remove(userId int, tenantId string, id int) *common.Errors {
- // 创建查询实例
- device := &dao.InfoBoard{
- ID: id,
- IsDeleted: 1,
- UpdateUser: userId,
- UpdateTime: time.Now(),
- }
- err := device.Delete()
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
- common.DeviceTypeInfoBoard, common.GetDeviceObject(device.ID, device.InfoName), common.OperationSuccess)
- return nil
- }
- func (s *infoBoardService) GetByGateway(gatewayId int) []dao.InfoBoard {
- device := &dao.InfoBoard{
- GatewayId: gatewayId,
- }
- return device.GetDevicesByGateway()
- }
- func (s *infoBoardService) GetByLampPole(lampPoleId int) []dao.InfoBoard {
- device := &dao.InfoBoard{
- LampPoleId: lampPoleId,
- }
- return device.GetDevicesByLampPole()
- }
- func (s *infoBoardService) GetByResolution(tenantId string, resolution int) []dao.InfoBoard {
- device := &dao.InfoBoard{
- Resolution: resolution,
- TenantId: tenantId,
- }
- return device.GetDevicesByResolution()
- }
- func (s *infoBoardService) GetByIds(tenantId string, ids string) []dao.InfoBoard {
- device := &dao.InfoBoard{
- TenantId: tenantId,
- }
- 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"
- if dev.NeverCloseDown == 1 {
- param["sleep"] = ""
- param["wakeup"] = ""
- } else {
- sleep, wakeup := ledTimeFormat(dev.BootEndTime, dev.BootStartTime)
- param["sleep"] = sleep
- param["wakeup"] = wakeup
- }
- param["reboot"] = "12:30:59" //每日重启时间
- 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 isNight() {
- //晚上不同亮度和音量
- 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)
- }
- // 发布led信息屏节目
- func (s *infoBoardService) EdgePushLedProgram(tenant, sn, playJson string) {
- var edgePost edge_service.CltLedControlReqPost
- edgePost.Codes = []string{sn}
- param := make(map[string]interface{})
- json.Unmarshal([]byte(playJson), ¶m)
- s.reqEdge(tenant, edgePost, param, "spgms")
- }
- // 判断是否晚上
- func isNight() bool {
- now := time.Now()
- hour := now.Hour()
- if hour >= 18 || hour < 6 {
- return true
- } else {
- return false
- }
- }
- // ledTimeFormat led开始 结束时间格式化
- func ledTimeFormat(stime string, etime string) (string, string) {
- startTime := fmt.Sprintf("%v %v:59", time.Now().Format("2006-01-02"), stime)
- endTime := fmt.Sprintf("%v %v:59", time.Now().Format("2006-01-02"), etime)
- ktime, _ := time.Parse("2006-01-02 15:04:05", startTime)
- jtime, _ := time.Parse("2006-01-02 15:04:05", endTime)
- atoi1, _ := strconv.Atoi(ktime.Format("15"))
- atoi2, _ := strconv.Atoi(jtime.Format("15"))
- return fmt.Sprintf("%v:%v:59", atoi1, ktime.Format("4")), fmt.Sprintf("%v:%v:59", atoi2, jtime.Format("4"))
- }
- func (s *infoBoardService) DeviceCount() dao.DeviceStatus {
- var rsp dao.DeviceStatus
- d := dao.InfoBoard{}
- rsp.Set(d.DeviceCount1())
- return rsp
- }
- func (s *infoBoardService) ListDevices() []dao.Device {
- d := dao.InfoBoard{}
- return d.ListDevices1()
- }
|