package service
|
|
import (
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/bhomedbapi.git"
|
"encoding/json"
|
"github.com/satori/go.uuid"
|
"strings"
|
"vamicro/config"
|
"vamicro/stack-service/models"
|
"vamicro/stack-service/vo"
|
)
|
|
type FileStackService struct {
|
Bk bhomeclient.Broker
|
}
|
|
func (sv FileStackService) FindAll() []models.FileStack {
|
var model models.FileStack
|
list, _ := model.FindAll()
|
if list == nil {
|
list = []models.FileStack{}
|
}
|
return list
|
}
|
|
func (sv FileStackService) FindAllByPage(fType int, fileName string, page int, size int) (result vo.PageResult) {
|
var crApi bhomedbapi.CameraRuleApi
|
filterCamIds := ""
|
if fType == 3 { //未配规则
|
flag, allRules := crApi.FindAll()
|
if flag && len(allRules) > 0 {
|
for _,c := range allRules {
|
if c.CameraInfo != nil && c.CameraInfo.Id != "" && len(c.Rules) > 0 {
|
filterCamIds +="'"+c.CameraInfo.Id+"',"
|
}
|
}
|
}
|
if filterCamIds != "" {
|
filterCamIds = strings.TrimRight(filterCamIds, ",")
|
}
|
}
|
|
var model models.FileStack
|
result.Total = model.Total(fileName, fType, &filterCamIds)
|
|
|
if result.Total > 0 {
|
arr, _ := model.FindAllByPage(fType, fileName, page, size, &filterCamIds)
|
var pageList = make([]vo.FileStackWithWH, 0)
|
if arr != nil {
|
var resolutions []vo.Resolution
|
if config.Server.Resolutions != nil && len(config.Server.Resolutions) > 0 {
|
for _, rwh := range config.Server.Resolutions {
|
resolutions = append(resolutions, vo.Resolution{
|
Width: rwh.Width,
|
Height: rwh.Height,
|
})
|
}
|
} else {
|
dRe0 := vo.Resolution{
|
Width: 0,
|
Height: 0,
|
}
|
dRe1 := vo.Resolution{
|
Width: 1080,
|
Height: 720,
|
}
|
dRe2 := vo.Resolution{
|
Width: 1920,
|
Height: 1080,
|
}
|
dRe3 := vo.Resolution{
|
Width: 2688,
|
Height: 1520,
|
}
|
resolutions = []vo.Resolution{ dRe0, dRe1, dRe2, dRe3}
|
}
|
|
for _,sck := range arr {
|
if !crApi.ExistRunningTask(sck.Id) {
|
sck.Status = bhomeclient.Stack_Status_NoRule
|
}
|
v := vo.FileStackWithWH{}
|
v.FileStack = sck
|
v.Resolutions = resolutions
|
pageList = append(pageList, v)
|
}
|
}
|
result.DataList = pageList
|
} else {
|
result.DataList = []vo.FileStackWithWH{}
|
}
|
return result
|
}
|
|
func (sv FileStackService) Save(fsE *models.FileStack) bool {
|
if fsE.Id == "" {
|
fsE.Id = bhomeclient.Id_Stack_Pre + uuid.NewV4().String()
|
if fsE.Add() {
|
dbMsg := protomsg.DbChangeMessage{
|
Id: fsE.Id,
|
Table: protomsg.TableChanged_T_FileStack,
|
Action: protomsg.DbAction_Insert,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
} else {
|
if fsE.Update() {
|
dbMsg := protomsg.DbChangeMessage{
|
Id: fsE.Id,
|
Table: protomsg.TableChanged_T_FileStack,
|
Action: protomsg.DbAction_Update,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
}
|
return false
|
}
|
|
func (sv FileStackService) UpdateStatus(ids []string, status int) bool {
|
var sck models.FileStack
|
ret := false
|
if status == bhomeclient.Stack_Status_Doing {
|
ret = sck.UpdateStatus(ids, status)
|
} else if status == bhomeclient.Stack_Status_Done {
|
for _,sckId := range ids {
|
if sck.HavingWorkFiles(sckId) {
|
sck.UpdateStatus([]string{ sckId }, bhomeclient.Stack_Status_Doing)
|
} else {
|
sck.UpdateStatus([]string{ sckId }, bhomeclient.Stack_Status_Done)
|
}
|
}
|
ret = true
|
}
|
if ret {
|
dbMsg := protomsg.DbChangeMessage{
|
Id: strings.Join(ids, ","),
|
Table: protomsg.TableChanged_T_FileStack,
|
Action: protomsg.DbAction_Update,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
}
|
return ret
|
}
|
|
func (sv FileStackService) UpdateEnable(id string, enable bool) bool {
|
var fs models.FileStack
|
if fs.UpdateEnable(id, enable) {
|
dbMsg := protomsg.DbChangeMessage{
|
Table: protomsg.TableChanged_T_FileStack,
|
Action: protomsg.DbAction_Update,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
return false
|
}
|
|
func (sv FileStackService) UpdateVideoEnable(enable bool) bool {
|
var fset models.FileAnalysisSetting
|
if fset.UpdateVideoEnable(enable) {
|
dbMsg := protomsg.DbChangeMessage{
|
Table: protomsg.TableChanged_T_FileSetting,
|
Action: protomsg.DbAction_Update,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
return false
|
}
|
|
func (sv FileStackService) DeleteById(id string) bool {
|
var fs models.FileStack
|
if fs.DeleteById(id) {
|
dbMsg := protomsg.DbChangeMessage{
|
Table: protomsg.TableChanged_T_FileStack,
|
Action: protomsg.DbAction_Delete,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
return false
|
}
|
|
func (sv FileStackService) UpdateChannelCount(count int) bool {
|
var fas models.FileAnalysisSetting
|
if fas.UpdateChannelCount(count) {
|
dbMsg := protomsg.DbChangeMessage{
|
Table: protomsg.TableChanged_T_FileSetting,
|
Action: protomsg.DbAction_Update,
|
}
|
pb,_ := json.Marshal(dbMsg)
|
sv.Bk.Publish(ProcName, pb)
|
return true
|
}
|
return false
|
}
|