terminal_manager.go 5.1 KB
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)
}