device_log_channel.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package cabinet_pkg
  2. import (
  3. "dy-admin/internal/pcmserver/bus/model"
  4. "dy-admin/internal/pcmserver/global"
  5. "go.uber.org/zap"
  6. "dy-admin/pkg/log"
  7. "encoding/hex"
  8. )
  9. type MsgLog struct {
  10. CabinetID int
  11. MsgType uint8
  12. MsgName string
  13. Message []byte
  14. }
  15. var deviceLogChan chan *MsgLog
  16. func RunDeviceLogGoroutine() {
  17. deviceLogChan = make(chan *MsgLog, 2048)
  18. go run()
  19. }
  20. func run() {
  21. for {
  22. select {
  23. case msgLog := <-deviceLogChan:
  24. if msgLog == nil {
  25. log.Warnf("msgLog is nil")
  26. continue
  27. }
  28. createDeviceLog(msgLog.CabinetID, msgLog.MsgType, msgLog.MsgName, msgLog.Message)
  29. }
  30. }
  31. }
  32. func createDeviceLog(cabinetId int, msgType uint8, msgName string, message []byte) {
  33. err := global.DB.Create(&model.DeviceLog{
  34. CabinetID: cabinetId,
  35. MsgType: msgType,
  36. MsgName: msgName,
  37. Message: hex.EncodeToString(message),
  38. }).Error
  39. if err != nil {
  40. log.Warn("create device log err", zap.Error(err))
  41. }
  42. return
  43. }
  44. func createConnectLog(ip string, msg string) {
  45. err := global.DB.Create(&model.ConnectLog{
  46. IP: ip,
  47. Msg: msg,
  48. }).Error
  49. if err != nil {
  50. log.Warn("create connect log err", zap.Error(err))
  51. }
  52. return
  53. }