12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package server
- import (
- "context"
- "dlt645-server/protocol"
- "fmt"
- "github.com/gogf/gf/net/gtcp"
- "github.com/gogf/gf/os/glog"
- gatewayV2 "sparrow-sdk/v2"
- "sync"
- )
- type Server struct {
- closeChan chan struct{}
- server *gtcp.Server
- ctx context.Context
- addr string
- port int
- grMu sync.Mutex
- grWG sync.WaitGroup
- message protocol.Message
- gateWay *gatewayV2.Gateway
- packetCtx *protocol.PacketContext
- data *protocol.Data
- }
- func NewServer(ctx context.Context, addr string, port int, gw *gatewayV2.Gateway, packetCtx *protocol.PacketContext, data *protocol.Data) *Server {
- return &Server{
- closeChan: make(chan struct{}),
- ctx: ctx,
- addr: addr,
- port: port,
- gateWay: gw,
- packetCtx: packetCtx,
- data: data,
- }
- }
- func (s *Server) Start() error {
- glog.Printf("服务端启动[%s:%d]", s.addr, s.port)
- server := gtcp.NewServer(fmt.Sprintf("%s:%d", s.addr, s.port), s.onClientConnect)
- s.server = server
- return s.server.Run()
- }
- func (s *Server) Stop() {
- s.server.Close()
- }
- func (s *Server) onClientConnect(conn *gtcp.Conn) {
- glog.Debugf("新的设备接入:%s", conn.RemoteAddr())
- c := NewClient(s, conn)
- c.closeHandler = func(id string, c *Client) {
- glog.Debugf("客户端断开:%s", id)
- }
- _ = c.SendGetAddress(s.packetCtx, s.data)
- go c.SendLoop(s.packetCtx, s.data)
- go c.GetActivePower(s.packetCtx)
- go c.GetVBlock(s.packetCtx)
- go c.GetIBlock(s.packetCtx)
- }
- func (s *Server) ReportStatus(subId string, data interface{}) error {
- return s.gateWay.ReportStatus(subId, "status", data)
- }
|