chore: upload current project state
This commit is contained in:
+6
-130
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -49,9 +47,7 @@ func (c *WSClient) IsConnected() bool {
|
||||
return c.connected
|
||||
}
|
||||
|
||||
const (
|
||||
serviceConfigFile = "/usr/local/etc/service.conf"
|
||||
)
|
||||
|
||||
|
||||
// DeviceData struct
|
||||
type DeviceData struct {
|
||||
@@ -74,6 +70,11 @@ type DeviceData struct {
|
||||
AlarmPauseUntil int64 `json:"-"` // Pause alarm untilw
|
||||
HostUUID string `json:"host_uuid"`
|
||||
Address string `json:"address"`
|
||||
// New fields for new configuration format
|
||||
FireLeaveDetectArea []Area `json:"fire_leave_detect_area,omitempty"`
|
||||
FireCheckDetectArea []Area `json:"fire_check_detect_area,omitempty"`
|
||||
FireLeaveParam FireLeaveParam `json:"fire_leave_param,omitempty"`
|
||||
FireCheckParam FireCheckParam `json:"fire_check_param,omitempty"`
|
||||
}
|
||||
|
||||
type InferenceParams struct {
|
||||
@@ -85,16 +86,6 @@ type InferenceParams struct {
|
||||
Port int `json:"port"` // HTTP server port
|
||||
}
|
||||
|
||||
type Area struct {
|
||||
ID int `json:"id"`
|
||||
Pts []Point `json:"pts"`
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
// WSMessage struct
|
||||
type WSMessage struct {
|
||||
ID int64 `json:"id,omitempty"`
|
||||
@@ -584,126 +575,11 @@ func (d *DeviceData) ToInferenceParams(port int) InferenceParams {
|
||||
}
|
||||
}
|
||||
|
||||
// SaveServiceConfig saves multiple device configurations to service config file
|
||||
func SaveServiceConfig(deviceDataList []DeviceData) error {
|
||||
configData, err := json.MarshalIndent(deviceDataList, "", " ")
|
||||
if err != nil {
|
||||
logger.Logger.Printf("Failed to serialize service configuration: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(serviceConfigFile, configData, 0644); err != nil {
|
||||
logger.Logger.Printf("Failed to write service config file: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Logger.Printf("Service configuration saved to: %s, %d devices", serviceConfigFile, len(deviceDataList))
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveSingleServiceConfig saves single device configuration (compatible with existing calls)
|
||||
func SaveSingleServiceConfig(deviceData DeviceData) error {
|
||||
// First read existing configuration
|
||||
existingConfigs, err := LoadServiceConfig()
|
||||
if err != nil {
|
||||
// If reading fails, create new configuration list
|
||||
existingConfigs = []DeviceData{deviceData}
|
||||
} else {
|
||||
// Find if configuration with same DeviceUUID already exists
|
||||
found := false
|
||||
for i, existing := range existingConfigs {
|
||||
if existing.DeviceUUID == deviceData.DeviceUUID {
|
||||
existingConfigs[i] = deviceData
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// If not exists, add to list
|
||||
if !found {
|
||||
existingConfigs = append(existingConfigs, deviceData)
|
||||
}
|
||||
}
|
||||
|
||||
return SaveServiceConfig(existingConfigs)
|
||||
}
|
||||
|
||||
// LoadServiceConfig loads multiple device data from service config file
|
||||
func LoadServiceConfig() ([]DeviceData, error) {
|
||||
if _, err := os.Stat(serviceConfigFile); os.IsNotExist(err) {
|
||||
logger.Logger.Printf("Service config file does not exist: %s", serviceConfigFile)
|
||||
return nil, fmt.Errorf("service config file does not exist")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(serviceConfigFile)
|
||||
if err != nil {
|
||||
logger.Logger.Printf("Failed to read service config file: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if file content is empty
|
||||
if len(data) == 0 {
|
||||
logger.Logger.Printf("Service config file is empty")
|
||||
return nil, fmt.Errorf("service config file is empty")
|
||||
}
|
||||
|
||||
// Parse as DeviceData array
|
||||
var deviceDataList []DeviceData
|
||||
if err := json.Unmarshal(data, &deviceDataList); err != nil {
|
||||
logger.Logger.Printf("Failed to parse service config file: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Filter out configurations with empty DeviceUUID
|
||||
var validDeviceDataList []DeviceData
|
||||
for _, deviceData := range deviceDataList {
|
||||
if deviceData.DeviceUUID != "" {
|
||||
validDeviceDataList = append(validDeviceDataList, deviceData)
|
||||
}
|
||||
}
|
||||
const FixedUploadURL = "http://192.168.80.168/api/v1/upload/video"
|
||||
for i := range validDeviceDataList {
|
||||
if validDeviceDataList[i].UploadURL == "" {
|
||||
validDeviceDataList[i].UploadURL = FixedUploadURL
|
||||
logger.Logger.Printf("Auto-completed UploadURL: %s (DeviceUUID=%s)",
|
||||
FixedUploadURL, validDeviceDataList[i].DeviceUUID)
|
||||
}
|
||||
}
|
||||
if len(validDeviceDataList) == 0 {
|
||||
logger.Logger.Printf("No valid device configurations found in service config file")
|
||||
return nil, fmt.Errorf("no valid device configurations")
|
||||
}
|
||||
|
||||
logger.Logger.Printf("Loaded %d device configurations from service config file", len(validDeviceDataList))
|
||||
return validDeviceDataList, nil
|
||||
|
||||
}
|
||||
|
||||
// ProcessDetectAreaForInference processes detection area data, converts to format required by inference program
|
||||
func ProcessDetectAreaForInference(areas []Area) string {
|
||||
if len(areas) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Only take the first detection area
|
||||
area := areas[0]
|
||||
var points []string
|
||||
|
||||
for _, point := range area.Pts {
|
||||
// Directly round, remove decimals
|
||||
x := int(point.X)
|
||||
y := int(point.Y)
|
||||
points = append(points, fmt.Sprintf("%d,%d", x, y))
|
||||
}
|
||||
|
||||
return strings.Join(points, ",")
|
||||
}
|
||||
|
||||
func ProcessDeviceDataForInference(data DeviceData) DeviceData {
|
||||
// Create a copy, only process fields that need conversion
|
||||
processed := data
|
||||
processed.DetectAreaStr = ProcessDetectAreaForInference(data.DetectArea)
|
||||
return processed
|
||||
}
|
||||
|
||||
func (c *WSClient) SendAsync(message interface{}) {
|
||||
go func() {
|
||||
|
||||
Reference in New Issue
Block a user