/
multiSendPropertyLogic.go
114 lines (108 loc) · 3.27 KB
/
multiSendPropertyLogic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package interact
import (
"context"
"github.com/i-Things/things/shared/errors"
"github.com/i-Things/things/shared/utils"
"github.com/i-Things/things/src/disvr/pb/di"
"github.com/i-Things/things/src/dmsvr/pb/dm"
"golang.org/x/sync/errgroup"
"sync"
"github.com/i-Things/things/src/apisvr/internal/svc"
"github.com/i-Things/things/src/apisvr/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MultiSendPropertyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
retMsg []*types.DeviceInteractMultiSendPropertyMsg
err error
mutex sync.Mutex
}
func NewMultiSendPropertyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MultiSendPropertyLogic {
return &MultiSendPropertyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MultiSendPropertyLogic) MultiSendProperty(req *types.DeviceInteractMultiSendPropertyReq) (resp *types.DeviceInteractMultiSendPropertyResp, err error) {
if req.ProductID != "" && len(req.DeviceNames) != 0 {
err := l.SendProperty(req.ProductID, req.DeviceNames, req.Data, req.ShadowControl)
return &types.DeviceInteractMultiSendPropertyResp{List: l.retMsg}, err
}
if req.GroupID != 0 || req.AreaID != 0 {
var ds []*dm.DeviceInfo
if req.GroupID != 0 {
dgRet, err := l.svcCtx.DeviceG.GroupDeviceIndex(l.ctx, &dm.GroupDeviceIndexReq{
GroupID: req.GroupID,
})
if err != nil {
return nil, err
}
ds = dgRet.List
}
if req.AreaID != 0 {
//企业版功能
return nil, errors.Company
}
var devices = map[string][]string{} //key 是产品id value是设备名列表
for _, v := range ds {
if p := devices[v.ProductID]; p != nil {
devices[v.ProductID] = append(p, v.DeviceName)
continue
}
devices[v.ProductID] = []string{v.DeviceName}
}
for p, d := range devices {
var eg errgroup.Group
productID := p
deviceNames := d
eg.Go(func() error {
err := l.SendProperty(productID, deviceNames, req.Data, req.ShadowControl)
if err != nil {
return err
}
return nil
})
err := eg.Wait()
if err != nil {
return nil, err
}
}
return &types.DeviceInteractMultiSendPropertyResp{List: l.retMsg}, nil
}
return nil, errors.Parameter.AddMsg("产品id设备名或分组id或区域id必须填一个")
}
func (l *MultiSendPropertyLogic) SendProperty(productID string, deviceNames []string, data string, shadowControl int64) error {
list := make([]*types.DeviceInteractMultiSendPropertyMsg, 0)
dmReq := &di.MultiSendPropertyReq{
ProductID: productID,
DeviceNames: deviceNames,
Data: data,
ShadowControl: shadowControl,
}
dmResp, err := l.svcCtx.DeviceInteract.MultiSendProperty(l.ctx, dmReq)
if err != nil {
er := errors.Fmt(err)
l.Errorf("%s.rpc.MultiSendProperty productID=%v deviceNames=%v data=%v err=%+v", utils.FuncName(), productID, deviceNames, data, er)
return er
}
if len(dmResp.List) > 0 {
for _, v := range dmResp.List {
list = append(list, &types.DeviceInteractMultiSendPropertyMsg{
ProductID: productID,
DeviceName: v.DeviceName,
Code: v.Code,
Status: v.Status,
ClientToken: v.ClientToken,
SysMsg: v.SysMsg,
SysCode: v.SysCode,
})
}
}
l.mutex.Lock()
defer l.mutex.Unlock()
l.retMsg = append(l.retMsg, list...)
return nil
}