terminal_manager.go
5.1 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package service
import (
"container/list"
"github.com/gookit/event"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"sync"
)
var (
DownEntityEvent = "down_entity"
)
type TerminalManager struct {
TerminalDeviceList []*TerminalDevice
TerminalDevices sync.Map
}
func NewTerminalManager() *TerminalManager {
return &TerminalManager{
TerminalDeviceList: make([]*TerminalDevice, 0),
TerminalDevices: sync.Map{},
}
}
func (term *TerminalManager) GetDevice(terminalId string) (*TerminalDevice, bool) {
device, ok := term.TerminalDevices.Load(terminalId)
if !ok {
device := NewTerminalDevice(terminalId)
term.TerminalDevices.Store(terminalId, device)
log.Logger.Debug("【TerminalManager】 终端上线 add new device:"+terminalId, map[string]interface{}{"device": device})
term.TerminalDeviceList = append(term.TerminalDeviceList, device)
return nil, false
}
if v, ok := device.(*TerminalDevice); ok {
return v, ok
}
return nil, false
}
func (term *TerminalManager) PopDownEntityByDevice(sn string) (DownEntity, bool) {
device, ok := term.GetDevice(sn)
if !ok {
return nil, false
}
return device.PopDownEntity()
}
func (term *TerminalManager) AddDownEntityByDevice(sn string, entity interface{}) bool {
device, ok := term.GetDevice(sn)
if !ok {
return false
}
device.AddDownEntity(entity)
return true
}
func (term *TerminalManager) BroadcastDownEntity(downEntity interface{}) {
if len(term.TerminalDeviceList) == 0 {
log.Logger.Debug("【TerminalManager】 当前在线终端:0 广播命令退出")
return
}
for i := range term.TerminalDeviceList {
term.TerminalDeviceList[i].AddDownEntity(downEntity)
}
}
// Listen Event
func (term *TerminalManager) DownEntityEvent(e event.Event) error {
entity := e.Get("entity")
if entity != nil {
term.BroadcastDownEntity(entity)
}
return nil
}
func (term *TerminalManager) SyncUser(e event.Event) error {
user := e.Get("user")
userBase := e.Get("userBase")
//if user != nil && userBase == nil {
// assertUser := user.(*domain.User)
// if assertUser.Ext.DepName != "制造中心" {
// log.Logger.Debug("【TerminalManager】 当前用户部门不是 [制造中心] 不进行同步", map[string]interface{}{"user": user})
// return nil
// }
// term.BroadcastDownEntity(NewUpdateUserDownEntity(generateSn(), assertUser.Ext.IcCardNumber, assertUser.Ext.UserName))
// return nil
//}
if user != nil && userBase != nil {
assertUser := user.(*domain.User)
assertUserBase := userBase.(*domain.UserBase)
//if assertUser.Ext.DepName != "制造中心" {
// log.Logger.Debug("【TerminalManager】 当前用户部门不是 [制造中心] 不进行同步", map[string]interface{}{"user": user})
// return nil
//}
if len(assertUser.Ext.IcCardNumber) == 0 {
log.Logger.Debug("【TerminalManager】 当前用户ICCard未设置 不进行同步", map[string]interface{}{"user": user})
return nil
}
term.BroadcastDownEntity(NewUpdateUserDownEntity(generateSn(), assertUser.Ext.IcCardNumber, assertUserBase.UserInfo.UserName))
if len(assertUserBase.UserInfo.FacePortrait) > 0 {
term.BroadcastDownEntity(NewUpdateUserFacePortraitDownEntity(generateSn(), assertUser.Ext.IcCardNumber, assertUserBase.UserInfo.FacePortrait))
}
if len(assertUserBase.UserInfo.FingerprintPortrait) > 0 {
term.BroadcastDownEntity(NewUpdateUserFingerprintPortraitDownEntity(generateSn(), assertUser.Ext.IcCardNumber, assertUserBase.UserInfo.FingerprintPortrait))
}
}
return nil
}
func (term *TerminalManager) EnableUser(e event.Event) error {
user := e.Get("user")
if user != nil {
assertUser := user.(*domain.User)
if assertUser.EnableStatus == int(domain.UserStatusEnable) {
term.SyncUser(e)
} else if assertUser.EnableStatus == int(domain.UserStatusDisable) {
term.BroadcastDownEntity(NewDeleteDownEntity(generateSn(), assertUser.Ext.IcCardNumber, string(UserInfo)))
}
}
return nil
}
func (term *TerminalManager) CreateUser(e event.Event) error {
return term.SyncUser(e)
}
func (term *TerminalManager) UpdateUser(e event.Event) error {
return term.EnableUser(e)
}
type TerminalDevice struct {
Id string
DownEntityList *list.List
}
func NewTerminalDevice(terminalId string) *TerminalDevice {
return &TerminalDevice{
Id: terminalId,
DownEntityList: list.New(),
}
}
func (device *TerminalDevice) PopDownEntity() (DownEntity, bool) {
element := device.DownEntityList.Front()
if element == nil {
return nil, false
}
device.DownEntityList.Remove(element)
if v, ok := element.Value.(DownEntity); ok {
log.Logger.Debug("【TerminalManager】 发送命令 pop down entity to Sender cmd:"+v.DownCommand(), map[string]interface{}{"entity": v})
return v, ok
}
return nil, false
}
func (device *TerminalDevice) AddDownEntity(downEntity interface{}) {
v, ok := downEntity.(DownEntity)
var cmd string
if ok {
cmd = v.DownCommand()
}
log.Logger.Debug("【TerminalManager】 添加命令 add down entity to Profile cmd:"+cmd, map[string]interface{}{"entity": downEntity})
device.DownEntityList.PushBack(downEntity)
}