246 lines
7.2 KiB
Go
246 lines
7.2 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type ConfigProfileEditor struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
BusinessName string `json:"business_name"`
|
|
Description string `json:"description"`
|
|
DeviceCode string `json:"device_code"`
|
|
SiteName string `json:"site_name"`
|
|
Queue ConfigProfileQueueEditor `json:"queue"`
|
|
Instances []ConfigProfileInstanceEditor `json:"instances"`
|
|
Raw map[string]any `json:"raw"`
|
|
}
|
|
|
|
type ConfigProfileQueueEditor struct {
|
|
Size string `json:"size"`
|
|
Strategy string `json:"strategy"`
|
|
}
|
|
|
|
type ConfigProfileInstanceEditor struct {
|
|
Name string `json:"name"`
|
|
Template string `json:"template"`
|
|
DisplayName string `json:"display_name"`
|
|
RTSPURL string `json:"rtsp_url"`
|
|
PublishHLSPath string `json:"publish_hls_path"`
|
|
PublishRTSPPort string `json:"publish_rtsp_port"`
|
|
PublishRTSPPath string `json:"publish_rtsp_path"`
|
|
ChannelNo string `json:"channel_no"`
|
|
AdvancedParams map[string]any `json:"advanced_params"`
|
|
Delete bool `json:"delete"`
|
|
}
|
|
|
|
func (s *ConfigPreviewService) GetProfileEditor(name string) (*ConfigProfileEditor, error) {
|
|
raw, path, err := s.readAssetJSON("profiles", name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
queueMap, _ := raw["queue"].(map[string]any)
|
|
instancesRaw, _ := raw["instances"].([]any)
|
|
instances := make([]ConfigProfileInstanceEditor, 0, len(instancesRaw))
|
|
deviceCode := ""
|
|
siteName := ""
|
|
for _, item := range instancesRaw {
|
|
instanceMap, _ := item.(map[string]any)
|
|
paramsMap, _ := instanceMap["params"].(map[string]any)
|
|
if deviceCode == "" {
|
|
deviceCode = stringValue(paramsMap["device_code"])
|
|
}
|
|
if siteName == "" {
|
|
siteName = stringValue(paramsMap["site_name"])
|
|
}
|
|
advanced := cloneMap(paramsMap)
|
|
for _, key := range []string{
|
|
"display_name",
|
|
"device_code",
|
|
"site_name",
|
|
"rtsp_url",
|
|
"publish_hls_path",
|
|
"publish_rtsp_port",
|
|
"publish_rtsp_path",
|
|
"channel_no",
|
|
} {
|
|
delete(advanced, key)
|
|
}
|
|
if len(advanced) == 0 {
|
|
advanced = nil
|
|
}
|
|
instances = append(instances, ConfigProfileInstanceEditor{
|
|
Name: stringValue(instanceMap["name"]),
|
|
Template: stringValue(instanceMap["template"]),
|
|
DisplayName: stringValue(paramsMap["display_name"]),
|
|
RTSPURL: stringValue(paramsMap["rtsp_url"]),
|
|
PublishHLSPath: stringValue(paramsMap["publish_hls_path"]),
|
|
PublishRTSPPort: valueString(paramsMap["publish_rtsp_port"]),
|
|
PublishRTSPPath: stringValue(paramsMap["publish_rtsp_path"]),
|
|
ChannelNo: stringValue(paramsMap["channel_no"]),
|
|
AdvancedParams: advanced,
|
|
})
|
|
}
|
|
return &ConfigProfileEditor{
|
|
Name: firstString(raw["name"], name),
|
|
Path: path,
|
|
BusinessName: stringValue(raw["business_name"]),
|
|
Description: stringValue(raw["description"]),
|
|
DeviceCode: deviceCode,
|
|
SiteName: siteName,
|
|
Queue: ConfigProfileQueueEditor{
|
|
Size: valueString(queueMap["size"]),
|
|
Strategy: stringValue(queueMap["strategy"]),
|
|
},
|
|
Instances: instances,
|
|
Raw: raw,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ConfigPreviewService) BuildProfileDocument(editor ConfigProfileEditor) (map[string]any, error) {
|
|
name := strings.TrimSpace(editor.Name)
|
|
if name == "" {
|
|
return nil, fmt.Errorf("profile name is required")
|
|
}
|
|
if err := validateConfigName(name); err != nil {
|
|
return nil, fmt.Errorf("invalid profile name: %w", err)
|
|
}
|
|
|
|
if len(editor.Instances) == 0 {
|
|
return nil, fmt.Errorf("at least one instance is required")
|
|
}
|
|
seen := map[string]struct{}{}
|
|
instances := make([]map[string]any, 0, len(editor.Instances))
|
|
for _, inst := range editor.Instances {
|
|
if inst.Delete {
|
|
continue
|
|
}
|
|
instanceName := strings.TrimSpace(inst.Name)
|
|
if instanceName == "" {
|
|
return nil, fmt.Errorf("instance name is required")
|
|
}
|
|
if err := validateConfigName(instanceName); err != nil {
|
|
return nil, fmt.Errorf("invalid instance name %q: %w", instanceName, err)
|
|
}
|
|
if _, ok := seen[instanceName]; ok {
|
|
return nil, fmt.Errorf("duplicate instance name: %s", instanceName)
|
|
}
|
|
seen[instanceName] = struct{}{}
|
|
|
|
rtspURL := strings.TrimSpace(inst.RTSPURL)
|
|
if rtspURL == "" {
|
|
return nil, fmt.Errorf("rtsp url is required for %s", instanceName)
|
|
}
|
|
|
|
params := map[string]any{}
|
|
setString(params, "display_name", inst.DisplayName)
|
|
setString(params, "device_code", editor.DeviceCode)
|
|
setString(params, "site_name", editor.SiteName)
|
|
setString(params, "rtsp_url", rtspURL)
|
|
setString(params, "publish_hls_path", inst.PublishHLSPath)
|
|
setString(params, "publish_rtsp_path", inst.PublishRTSPPath)
|
|
setString(params, "channel_no", inst.ChannelNo)
|
|
|
|
if port := strings.TrimSpace(inst.PublishRTSPPort); port != "" {
|
|
value, err := strconv.Atoi(port)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("publish rtsp port must be a number for %s", instanceName)
|
|
}
|
|
params["publish_rtsp_port"] = value
|
|
}
|
|
|
|
for key, value := range cloneMap(inst.AdvancedParams) {
|
|
params[key] = value
|
|
}
|
|
|
|
instance := map[string]any{
|
|
"name": instanceName,
|
|
"params": params,
|
|
}
|
|
setString(instance, "template", inst.Template)
|
|
instances = append(instances, instance)
|
|
}
|
|
if len(instances) == 0 {
|
|
return nil, fmt.Errorf("at least one active instance is required")
|
|
}
|
|
|
|
doc := map[string]any{
|
|
"name": name,
|
|
"instances": instances,
|
|
}
|
|
setString(doc, "business_name", editor.BusinessName)
|
|
setString(doc, "description", editor.Description)
|
|
|
|
queue := map[string]any{}
|
|
if size := strings.TrimSpace(editor.Queue.Size); size != "" {
|
|
value, err := strconv.Atoi(size)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("queue size must be a number")
|
|
}
|
|
queue["size"] = value
|
|
}
|
|
setString(queue, "strategy", editor.Queue.Strategy)
|
|
if len(queue) > 0 {
|
|
doc["queue"] = queue
|
|
}
|
|
|
|
return doc, nil
|
|
}
|
|
|
|
func (s *ConfigPreviewService) SaveProfileEditor(editor ConfigProfileEditor) error {
|
|
doc, err := s.BuildProfileDocument(editor)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, err := marshalConfigJSON(doc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if s != nil && s.assets != nil {
|
|
return s.assets.SaveProfile(
|
|
strings.TrimSpace(editor.Name),
|
|
firstProfileTemplate(editor.Instances),
|
|
strings.TrimSpace(editor.BusinessName),
|
|
strings.TrimSpace(editor.Description),
|
|
string(body),
|
|
)
|
|
}
|
|
root := s.mediaRepoRoot()
|
|
if root == "" {
|
|
return fmt.Errorf("media repo path is not configured")
|
|
}
|
|
path := filepath.Join(root, "configs", "profiles", strings.TrimSpace(editor.Name)+".json")
|
|
return os.WriteFile(path, body, 0o644)
|
|
}
|
|
|
|
func setString(m map[string]any, key string, value string) {
|
|
if strings.TrimSpace(value) != "" {
|
|
m[key] = strings.TrimSpace(value)
|
|
}
|
|
}
|
|
|
|
func marshalConfigJSON(doc map[string]any) ([]byte, error) {
|
|
body, err := json.MarshalIndent(doc, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append(body, '\n'), nil
|
|
}
|
|
|
|
func firstProfileTemplate(instances []ConfigProfileInstanceEditor) string {
|
|
for _, inst := range instances {
|
|
if inst.Delete {
|
|
continue
|
|
}
|
|
if v := strings.TrimSpace(inst.Template); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|