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" "os/signal" "syscall" "time" "vamicro/config" "vamicro/devicemanage-service/controllers" "vamicro/devicemanage-service/models" "vamicro/devicemanage-service/service" ) var ( procName = "devicemanage-service" proc = &bhomeclient.ProcInfo{ Name: procName, //进程名称 ID: procName, //进程id Info: "", //进程的描述信息,用于区分同一进程名称下多个进程 } env = flag.String("e", "pro", "") ) func init() { flag.Parse() vaversion.Usage() config.Init(*env) // 日志初始化 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(){ flag.Parse() models.Init() defer models.CloseDB() ctx, cancel := context.WithCancel(context.Background()) fm,pubTopics := initFuncMap() var reg = &bhomeclient.RegisterInfo { Proc: *proc, Channel: nil, PubTopic: pubTopics, SubTopic: []string{ service.CollectDeviceTopic }, } q := make(chan os.Signal, 1) signal.Notify(q, os.Interrupt, os.Kill, syscall.SIGTERM) ms, err := bhomeclient.NewMicroNode(ctx, q, config.Server.AnalyServerId, reg, logger.Debug) if err !=nil { return } bhomedbapi.InitLog(logger.Debug) bhomedbapi.InitGetNetNode(ms.GetLocalNetNodeByTopic) bhomedbapi.InitDoReq(ms.RequestOnly) go dealSubMsg(ctx, ms) go testPubNetMsg(ms) go ms.StartServer(fm) service.DealApply() go controllers.RecvApprove(ctx) <-q ms.DeRegister() cancel() ms.Free() } //测试发布全网消息 func testPubNetMsg(ms *bhomeclient.MicroNode) { netTopic := "globalPublishTopic" tk := time.NewTicker(3 * time.Second) for { select { case <-tk.C: pmsg := []byte("hello world"+time.Now().Format("2006-01-02 15:04:05")) n := ms.PublishNetTimeout(nil, netTopic, pmsg, 1000) logger.Debug("PublishNetTimeOut n:", n) default: time.Sleep(1* time.Second) } } } //处理订阅消息 func dealSubMsg(ctx context.Context, ms *bhomeclient.MicroNode) { for { select { case <-ctx.Done(): return case msg := <-ms.SubCh: logger.Debug("recv sub msg topic:", string(msg.Topic), " data:", string(msg.Data)) if string(msg.Topic) == service.CollectDeviceTopic { service.CollectManageDeviceInfo(msg.Data) } } } } const urlPrefix= "/data/api-v" func initFuncMap() (map[string]bhomeclient.MicroFunc,[]string) { m := make(map[string]bhomeclient.MicroFunc) dc := new(controllers.DeviceController) m[urlPrefix+"/device/search"] = dc.Search m[urlPrefix+"/device/addApply"] = dc.AddApply m[urlPrefix+"/device/applyApprove"] = dc.ApplyApprove m[urlPrefix+"/device/types"] = dc.Types m[urlPrefix+"/device/pageByType"] = dc.PageByType m[urlPrefix+"/device/detail"] = dc.Detail m[urlPrefix+"/device/remoteCreateCluster"] = dc.RemoteCreateCluster m[urlPrefix+"/device/remoteSearchCluster"] = dc.RemoteSearchCluster m[urlPrefix+"/device/remoteJoinCluster"] = dc.RemoteJoinCluster m[urlPrefix+"/device/remoteReboot"] = dc.RemoteReboot m[urlPrefix+"/device/remoteRemove"] = dc.RemoteRemove m[urlPrefix+"/device/remoteUninstall"] = dc.RemoteUninstall m[urlPrefix+"/device/remoteUpgrade"] = dc.RemoteUpgrade m[urlPrefix+"/device/remoteSysUpdate"] = dc.RemoteSysUpdate var pubTopics []string for key,_ := range m { pubTopics = append(pubTopics, key) } return m, pubTopics }