初步完成接收发卡报文
This commit is contained in:
@@ -3,7 +3,7 @@ package common
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/eclipse/paho.golang/autopaho"
|
||||
"game-driver/pkg/utils"
|
||||
"github.com/eclipse/paho.golang/paho"
|
||||
"go.uber.org/zap"
|
||||
"sync"
|
||||
@@ -19,7 +19,6 @@ type DeviceMan interface {
|
||||
type Device struct {
|
||||
mu sync.Mutex
|
||||
C context.Context
|
||||
cm *autopaho.ConnectionManager
|
||||
|
||||
topic string
|
||||
status atomic.Int32
|
||||
@@ -46,29 +45,27 @@ func (d *Device) Status() int {
|
||||
|
||||
// PublishStatus 推送设备状态
|
||||
func (d *Device) PublishStatus() {
|
||||
err := d.cm.AwaitConnection(d.C)
|
||||
err := utils.GlobalMqttClient.AwaitConnection(d.C)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = d.cm.Publish(d.C, &paho.Publish{
|
||||
_, _ = utils.GlobalMqttClient.Publish(d.C, &paho.Publish{
|
||||
Topic: d.topic,
|
||||
Payload: []byte(fmt.Sprint(d.Status())),
|
||||
QoS: 1,
|
||||
})
|
||||
}
|
||||
|
||||
func DefaultDevice(ctx context.Context, cm *autopaho.ConnectionManager, topic string) *Device {
|
||||
func DefaultDevice(ctx context.Context, topic string) *Device {
|
||||
return &Device{
|
||||
C: ctx,
|
||||
cm: cm,
|
||||
topic: topic,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDevice(ctx context.Context, cm *autopaho.ConnectionManager, topic string, onChange func()) *Device {
|
||||
func NewDevice(ctx context.Context, topic string, onChange func()) *Device {
|
||||
return &Device{
|
||||
C: ctx,
|
||||
cm: cm,
|
||||
topic: topic,
|
||||
OnChange: onChange,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user