qixiaoning
6 天以前 2919bea2ffc28003aaefdf9619dc39cda11c1fd1
推送相关功能调整,udp开启关闭增加mqtt发送消息,设置推送字段名称修改
1个文件已添加
3个文件已修改
152 ■■■■ 已修改文件
push-service/controllers/eventPush.go 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
push-service/controllers/pushSet.go 24 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
push-service/main.go 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
push-service/mqtt/config.go 62 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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": "",       // 最后一个元素也需要逗号(Go 语法要求)
                "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: "修改失败"}
push-service/controllers/pushSet.go
@@ -32,9 +32,9 @@
        Alias:   "camInfo",
        Checked: true,
        Children: []vo.PushSetMenu{
            {Checked: true, Id: "cameraId", Name: "摄像机ID", Alias: "cameraId"},
            {Checked: true, Id: "cameraName", Name: "摄像机名称", Alias: "cameraName"},
            {Checked: true, Id: "cameraAddr", Name: "摄像机地址", Alias: "cameraAddr"},
            {Checked: true, Id: "camera_id", Name: "摄像机ID", 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{
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()
    //初始化mqtt
    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
}
}
push-service/mqtt/config.go
New file
@@ -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
}