Files
elevator-server/main.go
2025-01-02 09:32:39 +08:00

123 lines
3.5 KiB
Go

package main
import (
"DT/core/gorm"
"DT/core/logger"
"DT/core/viper"
"DT/global"
"DT/httpserver"
"DT/tcpserver"
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func init() {
viper.InitViper()
logger.InitLogger()
gorm.InitGorm()
gorm.AutoMigrate(global.Db)
}
func main() {
handler := &tcpserver.TCPHandler{}
tcpHost := global.AppConf.Service.Tcp.Host
tcpPort := global.AppConf.Service.Tcp.Port
tcpServer := tcpserver.NewServer(tcpHost+":"+tcpPort, handler.HandleClient)
handler.Server = tcpServer
httpHost := global.AppConf.Service.Http.Host
httpPort := global.AppConf.Service.Http.Port
httpServer := httpserver.NewServer(httpHost+":"+httpPort, tcpServer)
handler.Hub = httpServer.GetHub()
go func() {
if err := tcpServer.Start(); err != nil {
fmt.Printf("TCP server error: %v\n", err)
}
}()
go func() {
fmt.Printf("Listening and serving HTTP on %s:%s\r\n", httpHost, httpPort)
if err := httpServer.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Printf("HTTP server error: %v\n", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutting down servers...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpServer.Stop(ctx); err != nil {
fmt.Printf("Error stopping HTTP server: %v\n", err)
}
tcpServer.Stop()
fmt.Println("All servers stopped")
}
// 客户端发送 {"Type":"reg","Imei":"8610123456","Pwd":"123456","Ver":"v1.0.0"}
// 服务器回复 {"Type":"reg","Time":1734948442}
// 心跳
// 客户端{"Type":"ping"}
// 服务端{"Type":"pong"}
// 服务端发 {\"Type\":\"ota\",\"Ip\":\"http://8.141.8.18/V1002.bin\"}
// 客户端回复 {"Type":"ota","State":"1"}
// 开始实时上传数据
// 服务端发 {\"Type\":\"start\"}
// 客户端回复 {"Type":"start","Data":"{\"Type\":\"ota\",\"Ip\":\"192.168.31.1:80\",\"File\":\"/xxx/1.bin\"}"}
// 停止实时上传数据
// 服务端发 {\"Type\":\"stop\"}
// 客户端回复 {"Type":"stop","State":"1"}
// 修改IP端口
// 服务端发 {\"Type\":\"server\",\"Ip\":\"1222\",\"Port\":\"1222\"}
// 客户端回复 {"Type":"server","State":"1"}
// 设置楼层信息
// 服务器端发 {"Type":"SetConfig","Conf":{"zong":"10","ceng":["-2","-1","1","2","3","4","5","6","7","8"],"height":"4"}}
// 客户端回复 {"Type":"SetConfig","State":"1"}
// 获取楼层信息
// 服务器端发 {"Type":"GetConfig"}
// 客户端回复 {"Type":"GetConfig","Conf":{"zong":"10","ceng":["-2","-1","1","2","3","4","5","6","7","8"],"height":"4"}}
// 设置第一段语音
// 服务器端发 {"Type":"mp3a","jg":"10","MpData":"内容" }
// 客户端回复 {"Type":"mp3a","State":"1"}
// 设置第二段语音
// 服务器端发 {"Type":"mp3b","jg":"10","MpData":"内容"}
// 客户端回复 {"Type":"mp3b","State":"1"}
// 设置第三段语音
// 服务器端发 {"Type":"mp3c","jg":"10","MpData":"内容"}
// 客户端回复 {"Type":"mp3c","State":"1"}
// 服务端设置语音配置
// 服务器端发 {"Type":"SetVoiceConf","VoiceConf":{"fyr":"0","yl":"8","ys":"8","yd":"8","isPlay":"0"}}
// 客户端回复 {"Type":"SetVoiceConf","State":"1"}
// 服务端获取客户端语音配置
// 服务器端发 {"Type":"GetVoiceConf"}
// 客户端回复 {"Type":"GetVoiceConf","VoiceConf":{"fyr":"0","yl":"8","ys":"8","yd":"8","isPlay":"0"}}
//播放
//{\n\t"Type":"ping"}
//{\"Type\":\"txt\",\"content\":\"c4e3bac3a3accac0bde7a3a1\","jg":"21""} 间隔秒