stc-broadcast.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package broadcast
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "pbx-api-gin/internal/app/ami/action"
  7. "pbx-api-gin/internal/app/ami/model"
  8. "pbx-api-gin/internal/app/mysql"
  9. "pbx-api-gin/pkg/lfshook"
  10. )
  11. func HandleStcCmd(conn net.Conn) {
  12. for {
  13. buffer := make([]byte, 1024)
  14. n, err := conn.Read(buffer)
  15. if err != nil {
  16. fmt.Println("Error reading from server:", err)
  17. return
  18. }
  19. fmt.Println("Received from server:", string(buffer[:n]))
  20. switch buffer[5] {
  21. case 0x01: //heartbeat
  22. case 0x02: //STN
  23. StationAnn([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  24. case 0x03: //ACTIVE
  25. Active([1]byte{buffer[8]})
  26. case 0x05: //SPC
  27. SpecialAnn([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  28. case 0x06: //EMG
  29. EmgMsg([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  30. case 0x07: //STOP
  31. AnnStop([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  32. case 0x08: //DCS
  33. DcsAnn([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  34. case 0x09: //SELF CHECK
  35. SelfCheck([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  36. case 0x0a: //
  37. AlarmHandle([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  38. case 0x0b: //
  39. AlarmResetAll([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  40. case 0x0c: //
  41. RecordStorageConf([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  42. }
  43. }
  44. }
  45. /*
  46. func SendToStc(conn net.Conn, buff []byte) {
  47. _, err := conn.Write(buff)
  48. if err != nil {
  49. fmt.Println("send msg err:", err)
  50. }
  51. }
  52. */
  53. func StationAnn(data [4]byte) (err error) {
  54. specialVoice := int(data[0])
  55. Exten := "2411"
  56. delay := data[1]
  57. cycleCount := data[2]
  58. timeLen := data[3]
  59. filename := "welcome"
  60. //update special voice
  61. _, er := mysql.DBOrmInstance.Where("exten = ?", Exten).Update(&model.Extension{Special: specialVoice})
  62. if er != nil {
  63. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  64. return er
  65. }
  66. action.Playback(filename, int(cycleCount), Exten, int(timeLen), int(delay))
  67. return nil
  68. }
  69. func Active(data [1]byte) {
  70. Num := int(data[0])
  71. switch Num { // 设置全局的激活信号,并通过协议(待定)通知终端注册到对应的激活主机上
  72. case 0:
  73. case 1:
  74. case 8:
  75. case 9:
  76. }
  77. }
  78. func SpecialAnn(data [4]byte) {
  79. //specialVoice := int(data[0])
  80. Exten := "2411"
  81. delay := data[0]
  82. cycleCount := data[1]
  83. timeLen := data[2]
  84. filename := "welcome"
  85. if int(cycleCount) == 255 {
  86. action.Playback(filename, 9999999, Exten, int(timeLen), int(delay))
  87. } else {
  88. action.Playback(filename, int(cycleCount), Exten, int(timeLen), int(delay))
  89. }
  90. }
  91. func EmgMsg(data [4]byte) {
  92. //specialVoice := int(data[0])
  93. Exten := "2411"
  94. delay := data[0]
  95. cycleCount := data[1]
  96. timeLen := data[2]
  97. filename := "welcome"
  98. if int(cycleCount) == 255 {
  99. action.Playback(filename, 9999999, Exten, int(timeLen), int(delay))
  100. } else {
  101. action.Playback(filename, int(cycleCount), Exten, int(timeLen), int(delay))
  102. }
  103. }
  104. func AnnStop(data [4]byte) {
  105. if data[0] == 0x03 {
  106. //asterisk.Hangup("2412")
  107. } else if data[0] == 0x04 {
  108. //asterisk.Hangup("2412")
  109. } else if data[0] == 0x07 {
  110. //asterisk.Hangup("2412")
  111. } else if data[0] == 0x08 {
  112. //asterisk.Hangup("2412")
  113. } else if data[0] == 0x09 {
  114. //asterisk.Hangup("2412")
  115. }
  116. }
  117. func DcsAnn(data [4]byte) {
  118. Exten := "2411"
  119. delay := data[0]
  120. cycleCount := data[1]
  121. timeLen := data[2]
  122. filename := "welcome"
  123. if cycleCount == 255 {
  124. action.Playback(filename, 9999999, Exten, int(timeLen), int(delay))
  125. } else {
  126. action.Playback(filename, int(cycleCount), Exten, int(timeLen), int(delay))
  127. }
  128. }
  129. func SelfCheck(data [4]byte) {
  130. check := data[0]
  131. delay := data[1]
  132. cycleCount := data[2]
  133. timeLen := data[3]
  134. filename := "welcome"
  135. Exten := ""
  136. if check == 0x01 {
  137. action.Playback(filename, int(cycleCount), Exten, int(timeLen), int(delay))
  138. } else if check == 0x02 {
  139. //asterisk.Hangup(Exten)
  140. }
  141. }
  142. func AlarmHandle(data [4]byte) {
  143. handler := data[0]
  144. //len := data[1]
  145. //src := data[2]
  146. switch handler {
  147. case 0x01: //answer
  148. case 0x02: //hold
  149. case 0x03: //hangup
  150. }
  151. }
  152. func AlarmResetAll(data [4]byte) {
  153. if bytes.Equal(data[:], make([]byte, len(data))) {
  154. //reset all the alarm
  155. }
  156. }
  157. func RecordStorageConf(data [4]byte) {
  158. }