123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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
- }
- func NewServer(ctx context.Context, addr string, port int, gw *gatewayV2.Gateway) *Server {
- return &Server{
- closeChan: make(chan struct{}),
- ctx: ctx,
- addr: addr,
- port: port,
- gateWay: gw,
- }
- }
- 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)
- }
- ctx := new(protocol.PacketContext)
- data := new(protocol.Data)
- go c.ReadLoop(ctx, data)
- addressChan := make(chan struct{}, 1)
- avChan := make(chan struct{}, 1)
- aiChan := make(chan struct{}, 1)
- bvChan := make(chan struct{}, 1)
- biChan := make(chan struct{}, 1)
- cvChan := make(chan struct{}, 1)
- ciChan := make(chan struct{}, 1)
- powerChan := make(chan struct{}, 1)
- addressChan <- struct{}{}
- go c.SendGetAddress(ctx, addressChan, avChan)
- go c.GetActivePower(ctx, powerChan)
- go c.GetAV(ctx, avChan, aiChan)
- go c.GetAI(ctx, aiChan, bvChan)
- go c.GetBV(ctx, bvChan, biChan)
- go c.GetBI(ctx, biChan, cvChan)
- go c.GetCV(ctx, cvChan, ciChan)
- go c.GetCI(ctx, ciChan, powerChan)
- }
- func (s *Server) ReportStatus(subId string, data interface{}) error {
- return s.gateWay.ReportStatus(subId, "status", data)
- }
|