channel.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package action
  2. import (
  3. "crm-api/internal/app/ami"
  4. "crm-api/pkg/lfshook"
  5. "crm-api/pkg/utils"
  6. "errors"
  7. "strconv"
  8. "strings"
  9. )
  10. type CoreShowChannelResVO struct {
  11. CallerIDName string `json:"callerIDName"`
  12. CallerIDNum string `json:"callerIDNumber"`
  13. Channel string `json:"channel"`
  14. ConnectedLineName string `json:"connectedLineName"`
  15. ConnectedLineNum string `json:"connectedLineNumber"`
  16. Duration string `json:"duration"`
  17. DurationSecond int `json:"durationSecond"`
  18. }
  19. // CoreShowChannels 获取通话通道
  20. func CoreShowChannels() (result []CoreShowChannelResVO, err error) {
  21. // 通过 src 查询对应通道
  22. _, events, err := ami.AminInstance.Send(map[string]string{
  23. "Action": "CoreShowChannels",
  24. })
  25. if err != nil {
  26. lfshook.NewLogger().Errorf("core show channels error %+v", err)
  27. return nil, err
  28. }
  29. lfshook.NewLogger().Tracef("events %+v", events)
  30. result = make([]CoreShowChannelResVO, 0)
  31. for _, event := range events {
  32. if event.Data["Event"] == "CoreShowChannel" {
  33. channel := CoreShowChannelResVO{
  34. CallerIDName: event.Data["CallerIDName"],
  35. CallerIDNum: event.Data["CallerIDNum"],
  36. ConnectedLineName: event.Data["ConnectedLineName"],
  37. ConnectedLineNum: event.Data["ConnectedLineNum"],
  38. Channel: event.Data["Channel"],
  39. Duration: event.Data["Duration"],
  40. DurationSecond: utils.TimeStringToSecond(event.Data["Duration"]),
  41. }
  42. result = append(result, channel)
  43. }
  44. }
  45. lfshook.NewLogger().Tracef("channels %+v", result)
  46. return result, nil
  47. }
  48. type CoreShowCallsResVO struct {
  49. Active int `json:"active"`
  50. Processed int `json:"processed"`
  51. }
  52. // CoreShowCalls 获取当前通话数量
  53. // Response: Success
  54. // Message: Command output follows
  55. // Output: 66 active calls
  56. // Output: 6090 calls processed
  57. func CoreShowCalls() (result CoreShowCallsResVO, err error) {
  58. result = CoreShowCallsResVO{}
  59. data, _, err := ami.AminInstance.Send(map[string]string{
  60. "Action": "Command",
  61. "Command": "core show calls",
  62. })
  63. if err != nil {
  64. lfshook.NewLogger().Errorf("core show calls error %+v", err)
  65. return result, err
  66. }
  67. // lfshook.NewLogger().Infof("data %+v", data)
  68. lines := strings.Split(data["Output"], "\n")
  69. for index, line := range lines {
  70. data := strings.Fields(line)
  71. if len(data) == 0 {
  72. continue
  73. }
  74. switch index {
  75. case 0:
  76. //:0 active calls
  77. result.Active, _ = strconv.Atoi(data[0])
  78. case 1:
  79. //:1 call processed
  80. result.Processed, _ = strconv.Atoi(data[0])
  81. }
  82. }
  83. return
  84. }
  85. // GetChannelByExten 通过 exten 查询对应通道
  86. func GetChannelByExten(exten string) (channel string, err error) {
  87. lfshook.NewLogger().Infof("GetChannelByExten %s", exten)
  88. _, events, err := ami.AminInstance.Send(map[string]string{
  89. "Action": "CoreShowChannels",
  90. })
  91. if err != nil {
  92. lfshook.NewLogger().Errorf("core show channels error %+v", err)
  93. return "", err
  94. }
  95. for _, event := range events {
  96. lfshook.NewLogger().Infof("CoreShowChannels event Data %+v", event.Data)
  97. if event.Data["Event"] == "CoreShowChannel" && event.Data["ConnectedLineNum"] == exten && event.Data["CallerIDNum"] != exten {
  98. channel = event.Data["Channel"]
  99. lfshook.NewLogger().Infof("GetChannelByExten get channel %s", channel)
  100. break
  101. }
  102. }
  103. if channel == "" {
  104. lfshook.NewLogger().Errorf("not found channel %s", exten)
  105. return "", errors.New("not found channel")
  106. }
  107. return channel, nil
  108. }