package main import ( "fmt" "github.com/kardianos/service" "github.com/kptyun/KPTCOMM/comm" "github.com/kptyun/KPTCOMM/mdns" "github.com/kptyun/KPTCOMM/models" "github.com/kptyun/KPTCOMM/pkg/setting" "os" "strings" "time" //"unsafe" ) // @title 科湃腾TMRWatchComm // @version 2.0.0 // @description kptyun_service // @contact.name 朱红宾 // @contact.url https://www.dairyinfo.com.cn // @contact.email kptzhu@163.com type program struct{} func (p *program) Start(s service.Service) error { go p.run() return nil } func (p *program) run() { // 代码写在这儿 comm.StopCharP = make(chan int, 1) comm.StopCharMqtt = make(chan int, 1) if setting.CommSetting.PortName != "" { go comm.ProtectRun() } if setting.MqttSetting.Enable { go comm.Mq_Consumer() } if strings.Trim(setting.ServerSetting.Mdns_servicename, " ") != "" { go mdns.Mdns(strings.Trim(setting.ServerSetting.Mdns_servicename, " "), setting.ServerSetting.Mdns_serviceport) } //routersInit := routers.InitRouter() //readTimeout := setting.ServerSetting.ReadTimeout //writeTimeout := setting.ServerSetting.WriteTimeout //endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort) //maxHeaderBytes := 1 << 20 // //server := &http.Server{ // Addr: endPoint, // Handler: routersInit, // ReadTimeout: readTimeout, // WriteTimeout: writeTimeout, // MaxHeaderBytes: maxHeaderBytes, //} // //_ = server.ListenAndServe() } func (p *program) Stop(s service.Service) error { comm.StopCharP <- 1 comm.StopCharMqtt <- 1 <-time.After(time.Second * 1) return nil } /** * MAIN函数,程序入口 */ func main() { svcConfig := &service.Config{ Name: "TMRWatchCOM", //服务显示名称 DisplayName: "TMRWatchComm", //服务名称 Description: "TMRWatch后台通讯服务", //服务描述 } prg := &program{} s, _ := service.New(prg, svcConfig) if len(os.Args) > 1 { if os.Args[1] == "install" || os.Args[1] == "-install" || os.Args[1] == "-i" || os.Args[1] == "/install" { err := s.Install() if err != nil { fmt.Println(err) } return } if os.Args[1] == "remove" || os.Args[1] == "-uninstall" || os.Args[1] == "-remove" || os.Args[1] == "/uninstall" || os.Args[1] == "-u" { err := s.Uninstall() if err != nil { fmt.Println(err) } return } } //lock() setting.Setup("") //logging.Setup() models.Setup() _ = s.Run() }