From 2919bea2ffc28003aaefdf9619dc39cda11c1fd1 Mon Sep 17 00:00:00 2001
From: qixiaoning <jony.kee@outlook.com>
Date: 星期二, 26 八月 2025 08:47:32 +0800
Subject: [PATCH] 推送相关功能调整,udp开启关闭增加mqtt发送消息,设置推送字段名称修改

---
 push-service/mqtt/config.go           |   62 ++++++++++++++++++++
 push-service/controllers/eventPush.go |   23 +++++++
 push-service/main.go                  |   43 ++++++++-----
 push-service/controllers/pushSet.go   |   24 ++++----
 4 files changed, 123 insertions(+), 29 deletions(-)

diff --git a/push-service/controllers/eventPush.go b/push-service/controllers/eventPush.go
index 048a46d..95853ac 100644
--- a/push-service/controllers/eventPush.go
+++ b/push-service/controllers/eventPush.go
@@ -1,6 +1,9 @@
 package controllers
 
 import (
+	"encoding/json"
+	"vamicro/push-service/models"
+	"vamicro/push-service/mqtt"
 	"vamicro/push-service/service"
 	"vamicro/push-service/vo"
 
@@ -127,6 +130,26 @@
 	}
 	sv := service.NewEventPushService(h.Bk)
 	if sv.ChangeStatus(statusBody.Id, statusBody.Enable) {
+		//娑堟伅鎻愰啋
+		var epE models.EventPush
+		epE.GetById(statusBody.Id)
+
+		//udp鎺ㄩ��
+		if epE.PushType == 1 {
+			msg := map[string]string{
+				"type":   "change", // 娉ㄦ剰閫楀彿
+				"status": "",       // 鏈�鍚庝竴涓厓绱犱篃闇�瑕侀�楀彿锛圙o 璇硶瑕佹眰锛�
+				"id":     statusBody.Id,
+			}
+			if statusBody.Enable {
+				msg["status"] = "start"
+			} else {
+				msg["status"] = "stop"
+			}
+			jsonStr, _ := json.Marshal(msg)
+			go mqtt.Client.Publish(mqtt.MQTT_CONFIG_TOPIC, jsonStr)
+		}
+
 		return &bhomeclient.Reply{Success: true, Msg: "淇敼鎴愬姛"}
 	} else {
 		return &bhomeclient.Reply{Success: false, Msg: "淇敼澶辫触"}
diff --git a/push-service/controllers/pushSet.go b/push-service/controllers/pushSet.go
index b2aa7ab..a8f967c 100644
--- a/push-service/controllers/pushSet.go
+++ b/push-service/controllers/pushSet.go
@@ -32,9 +32,9 @@
 		Alias:   "camInfo",
 		Checked: true,
 		Children: []vo.PushSetMenu{
-			{Checked: true, Id: "cameraId", Name: "鎽勫儚鏈篒D", Alias: "cameraId"},
-			{Checked: true, Id: "cameraName", Name: "鎽勫儚鏈哄悕绉�", Alias: "cameraName"},
-			{Checked: true, Id: "cameraAddr", Name: "鎽勫儚鏈哄湴鍧�", Alias: "cameraAddr"},
+			{Checked: true, Id: "camera_id", Name: "鎽勫儚鏈篒D", Alias: "camera_id"},
+			{Checked: true, Id: "camera_name", Name: "鎽勫儚鏈哄悕绉�", Alias: "camera_name"},
+			{Checked: true, Id: "camera_addr", Name: "鎽勫儚鏈哄湴鍧�", Alias: "camera_addr"},
 		},
 	}
 	// sceneInfoSet := vo.PushSetMenu{
@@ -57,12 +57,12 @@
 		Alias:   "aiTasks",
 		Checked: true,
 		Children: []vo.PushSetMenu{
-			{Checked: true, Id: "taskId", Name: "浠诲姟id", Alias: "taskId"},
-			{Checked: true, Id: "taskName", Name: "浠诲姟鍚嶇О", Alias: "taskName"},
-			{Checked: true, Id: "aiTasks.#.eventLevel", Name: "浜嬩欢绛夌骇", Alias: "eventLevel"},
-			{Checked: true, Id: "aiTasks.#.workingTime", Name: "鏃堕棿娈�", Alias: "workingTime"},
-			{Checked: true, Id: "aiTasks.#.rules", Name: "浠诲姟鎻忚堪", Alias: "rules"},
-			{Checked: true, Id: "aiTasks.#.taskDescription", Name: "澶囨敞", Alias: "taskDescription"},
+			{Checked: true, Id: "task_id", Name: "浠诲姟id", Alias: "task_id"},
+			{Checked: true, Id: "task_name", Name: "浠诲姟鍚嶇О", Alias: "task_name"},
+			{Checked: true, Id: "event_level", Name: "浜嬩欢绛夌骇", Alias: "event_level"},
+			{Checked: true, Id: "working_time", Name: "鏃堕棿娈�", Alias: "working_time"},
+			{Checked: true, Id: "rules", Name: "浠诲姟鎻忚堪", Alias: "rules"},
+			{Checked: true, Id: "task_description", Name: "澶囨敞", Alias: "task_description"},
 		},
 	}
 	serverSet := vo.PushSetMenu{
@@ -71,9 +71,9 @@
 		Alias:   "serverInfo",
 		Checked: true,
 		Children: []vo.PushSetMenu{
-			{Checked: true, Id: "analyServerId", Name: "璁惧ID", Alias: "analyServerId"},
-			{Checked: true, Id: "analyServerIp", Name: "璁惧IP", Alias: "analyServerIp"},
-			{Checked: true, Id: "analyServerName", Name: "璁惧鍚嶇О", Alias: "analyServerName"},
+			{Checked: true, Id: "analy_server_id", Name: "璁惧ID", Alias: "analy_server_id"},
+			{Checked: true, Id: "analy_server_ip", Name: "璁惧IP", Alias: "analy_server_ip"},
+			{Checked: true, Id: "analy_server_name", Name: "璁惧鍚嶇О", Alias: "analy_server_name"},
 		},
 	}
 	dataSet := vo.PushSetMenu{
diff --git a/push-service/main.go b/push-service/main.go
index e714d87..dd633ca 100644
--- a/push-service/main.go
+++ b/push-service/main.go
@@ -1,10 +1,6 @@
 package main
 
 import (
-	"basic.com/valib/bhomeclient.git"
-	"basic.com/valib/bhomedbapi.git"
-	"basic.com/valib/logger.git"
-	"basic.com/valib/version.git"
 	"context"
 	"flag"
 	"os"
@@ -13,15 +9,22 @@
 	"vamicro/config"
 	"vamicro/push-service/controllers"
 	"vamicro/push-service/models"
+	"vamicro/push-service/mqtt"
 	"vamicro/push-service/service"
+
+	"basic.com/valib/bhomeclient.git"
+	"basic.com/valib/bhomedbapi.git"
+	"basic.com/valib/logger.git"
+	vaversion "basic.com/valib/version.git"
+	"github.com/go-basic/uuid"
 )
 
 var (
 	procName = service.ProcName
-	proc = &bhomeclient.ProcInfo{
+	proc     = &bhomeclient.ProcInfo{
 		Name: procName, //杩涚▼鍚嶇О
-		ID: procName, //杩涚▼id
-		Info: "", //杩涚▼鐨勬弿杩颁俊鎭紝鐢ㄤ簬鍖哄垎鍚屼竴杩涚▼鍚嶇О涓嬪涓繘绋�
+		ID:   procName, //杩涚▼id
+		Info: "",       //杩涚▼鐨勬弿杩颁俊鎭紝鐢ㄤ簬鍖哄垎鍚屼竴杩涚▼鍚嶇О涓嬪涓繘绋�
 	}
 	env = flag.String("e", "pro", "")
 )
@@ -32,20 +35,24 @@
 
 	config.Init(*env)
 	// 鏃ュ織鍒濆鍖�
-	var logFile = config.LogConf.Path + "vamicro-"+procName+".log"
+	var logFile = config.LogConf.Path + "vamicro-" + procName + ".log"
 	logger.InitLogger(logFile, config.LogConf.Level, config.LogConf.MaxSize, config.LogConf.MaxBackups, config.LogConf.MaxAge)
 	logger.Info("log init success !")
 }
 
-
-func main(){
+func main() {
 	models.Init()
 	defer models.CloseDB()
+
+	//鍒濆鍖杕qtt
+	mqtt.LoadConfig()
+	mqtt.Client.Init(mqtt.Options.Broker, "smart-ai-helmet-manager"+uuid.New(), mqtt.Options.Username, mqtt.Options.Password)
+
 	ctx, cancel := context.WithCancel(context.Background())
 	fm, pubTopics := initFuncMap()
-	var reg = &bhomeclient.RegisterInfo {
-		Proc: *proc,
-		Channel: nil,
+	var reg = &bhomeclient.RegisterInfo{
+		Proc:     *proc,
+		Channel:  nil,
 		PubTopic: pubTopics,
 		SubTopic: []string{},
 	}
@@ -54,7 +61,7 @@
 	signal.Notify(q, os.Interrupt, os.Kill, syscall.SIGTERM)
 
 	ms, err := bhomeclient.NewMicroNode(ctx, q, config.Server.AnalyServerId, reg, logger.Debug)
-	if err !=nil {
+	if err != nil {
 		return
 	}
 
@@ -68,10 +75,12 @@
 	ms.DeRegister()
 	cancel()
 	ms.Free()
+
 }
 
 const urlPrefix = "/data/api-v"
-func initFuncMap() (map[string]bhomeclient.MicroFunc,[]string) {
+
+func initFuncMap() (map[string]bhomeclient.MicroFunc, []string) {
 	funcMap := make(map[string]bhomeclient.MicroFunc)
 	ec := new(controllers.EventPushController)
 	psc := new(controllers.PushSetController)
@@ -85,8 +94,8 @@
 
 	funcMap[urlPrefix+"/eventPush/getPushSet"] = psc.GetPushSet
 	var pubTopics []string
-	for key,_ := range funcMap {
+	for key, _ := range funcMap {
 		pubTopics = append(pubTopics, key)
 	}
 	return funcMap, pubTopics
-}
\ No newline at end of file
+}
diff --git a/push-service/mqtt/config.go b/push-service/mqtt/config.go
new file mode 100644
index 0000000..ad749ea
--- /dev/null
+++ b/push-service/mqtt/config.go
@@ -0,0 +1,62 @@
+package mqtt
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"os"
+
+	"github.com/go-basic/uuid"
+)
+
+type Config struct {
+	Broker   string `json:"broker"`
+	ClientId string `json:"client_id"`
+	Username string `json:"username"`
+	Password string `json:"password"`
+}
+
+const configPath = "/opt/vasystem/config/mqtt.conf"
+
+const MQTT_CONFIG_TOPIC = "smartai/config"
+
+var Options Config
+
+func LoadConfig() {
+	if !pathExists(configPath) {
+		SaveConfig()
+	} else {
+		file, _ := os.Open(configPath)
+		defer file.Close()
+
+		fd := json.NewDecoder(file)
+
+		fd.Decode(&Options)
+
+		fmt.Printf("%v\n", Options)
+	}
+}
+
+func SaveConfig() {
+	Options = Config{
+		"192.168.1.235:1883",
+		"smart-ai-helmet-manager" + uuid.New(),
+		"admin",
+		"admin",
+	}
+
+	bytes, err := json.Marshal(Options)
+	if err == nil {
+		ioutil.WriteFile(configPath, bytes, 0744)
+	}
+}
+
+func pathExists(path string) bool {
+	_, err := os.Stat(path)
+	if err == nil {
+		return true
+	}
+
+	fmt.Printf("%v", err.Error())
+	return false
+}

--
Gitblit v1.8.0