stc-broadcast.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package broadcast
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net"
  7. "pbx-api-gin/internal/app/ami/action"
  8. "pbx-api-gin/internal/app/ami/model"
  9. "pbx-api-gin/internal/app/mysql"
  10. msgdata "pbx-api-gin/internal/app/stc/data"
  11. "pbx-api-gin/pkg/lfshook"
  12. "sync"
  13. "time"
  14. )
  15. var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
  16. func HandleStcCmd(conn net.Conn) {
  17. var buf bytes.Buffer // 用于累积未处理完的数据流
  18. tmp := make([]byte, 1024)
  19. for {
  20. if conn != nil {
  21. n, err := conn.Read(tmp)
  22. if err != nil {
  23. if err != io.EOF {
  24. fmt.Println("Error reading from server:", err)
  25. conn.Close()
  26. }
  27. return
  28. }
  29. // 将新读取的数据追加到缓冲区
  30. buf.Write(tmp[:n])
  31. }
  32. // 尝试从缓冲区中提取完整数据包
  33. for {
  34. packet, err := msgdata.ExtractPacket(&buf)
  35. if err != nil {
  36. break // 没有完整包或出错,等待更多数据
  37. }
  38. // 成功提取一个包,进行处理
  39. go processPacket(packet) // 使用 goroutine 避免阻塞接收
  40. }
  41. }
  42. }
  43. // 处理单个数据包(原 switch 逻辑迁移过来)
  44. func processPacket(packet []byte) {
  45. if len(packet) < 6 {
  46. fmt.Println("Invalid packet length")
  47. return
  48. }
  49. lfshook.NewLogger().Logger.Infof("Get data from STC ===============:%x", packet)
  50. switch packet[5] {
  51. case 0x01: // heartbeat
  52. // handle heartbeat
  53. case 0x02: // STN
  54. StationAnn(packet)
  55. case 0x03: // ACTIVE
  56. Active([1]byte{packet[8]})
  57. case 0x05: // SPC
  58. SpecialAnn(packet)
  59. case 0x06: // EMG
  60. EmgMsg(packet)
  61. case 0x07: // STOP
  62. AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
  63. case 0x08: // DCS
  64. DcsAnn(packet)
  65. case 0x09: // SELF CHECK
  66. SelfCheck(packet)
  67. case 0x0a:
  68. AlarmHandle(packet)
  69. case 0x0b:
  70. AlarmResetAll()
  71. case 0x0c:
  72. RecordStorageConf(packet[8:])
  73. default:
  74. fmt.Printf("Unknown command: %x\n", packet[5])
  75. }
  76. }
  77. /*
  78. func HandleStcCmd(conn net.Conn) {
  79. for {
  80. buffer := make([]byte, 1024)
  81. n, err := conn.Read(buffer)
  82. if err != nil {
  83. fmt.Println("Error reading from server:", err)
  84. return
  85. }
  86. if buffer[5] != 0x01 {
  87. lfshook.NewLogger().Logger.Infof("Get data from STC ===============:%x", buffer[:n])
  88. }
  89. switch buffer[5] {
  90. case 0x01: //heartbeat
  91. case 0x02: //STN
  92. StationAnn(buffer)
  93. case 0x03: //ACTIVE
  94. Active([1]byte{buffer[8]})
  95. case 0x05: //SPC
  96. SpecialAnn(buffer)
  97. case 0x06: //EMG
  98. EmgMsg(buffer)
  99. case 0x07: //STOP
  100. AnnStop([4]byte{buffer[8], buffer[9], buffer[10], buffer[11]})
  101. case 0x08: //DCS
  102. DcsAnn(buffer)
  103. case 0x09: //SELF CHECK
  104. SelfCheck(buffer)
  105. case 0x0a: //
  106. AlarmHandle(buffer)
  107. case 0x0b: //
  108. AlarmResetAll()
  109. case 0x0c: //
  110. RecordStorageConf(buffer[8:])
  111. }
  112. }
  113. }
  114. */
  115. // STN , 自动报站广播
  116. func StationAnn(data []byte) (err error) {
  117. specialVoice := int(data[8])
  118. delay := data[9]
  119. cycleCount := data[10]
  120. datalen := int(data[11])
  121. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  122. //update special voice
  123. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{Special: specialVoice, PaType: "STN"})
  124. if er != nil {
  125. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  126. return er
  127. }
  128. action.PlaybackPacu(filename, int(cycleCount), int(delay), "STN")
  129. return nil
  130. }
  131. // 激活信号
  132. func Active(data [1]byte) {
  133. Num := int(data[0])
  134. switch Num { // 设置全局的激活信号,并通过协议(待定)通知终端注册到对应的激活主机上
  135. case 0:
  136. case 1:
  137. case 8:
  138. case 9:
  139. }
  140. }
  141. // SPC ,特殊服务消息广播
  142. func SpecialAnn(data []byte) {
  143. delay := data[8]
  144. cycleCount := data[9]
  145. datalen := int(data[10])
  146. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-3)
  147. //update pa type
  148. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{PaType: "SPC"})
  149. if er != nil {
  150. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  151. }
  152. if int(cycleCount) == 255 {
  153. action.PlaybackPacu(filename, 9999999, int(delay), "SPC")
  154. } else {
  155. action.PlaybackPacu(filename, int(cycleCount), int(delay), "SPC")
  156. }
  157. }
  158. // EMG ,紧急服务消息广播
  159. func EmgMsg(data []byte) {
  160. delay := data[8]
  161. cycleCount := data[9]
  162. datalen := int(data[10])
  163. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-3)
  164. //update pa type
  165. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{PaType: "EMG"})
  166. if er != nil {
  167. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  168. }
  169. if int(cycleCount) == 255 {
  170. action.PlaybackPacu(filename, 9999999, int(delay), "EMG")
  171. } else {
  172. action.PlaybackPacu(filename, int(cycleCount), int(delay), "EMG")
  173. }
  174. }
  175. // 停止指定类型广播
  176. func AnnStop(data [4]byte) {
  177. switch data[0] {
  178. case 0x03:
  179. case 0x04:
  180. case 0x07:
  181. case 0x08:
  182. case 0x09:
  183. }
  184. for _, ext := range Pacus {
  185. action.Hangup(ext)
  186. }
  187. //update pa type
  188. _, er := mysql.DBOrmInstance.Cols("patype").In("exten", Pacus).Update(&model.Extension{PaType: ""})
  189. if er != nil {
  190. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  191. }
  192. }
  193. // DCS 语音
  194. func DcsAnn(data []byte) {
  195. delay := data[8]
  196. cycleCount := data[9]
  197. datalen := int(data[10])
  198. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-3)
  199. //update pa type
  200. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{PaType: "DCS"})
  201. if er != nil {
  202. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  203. }
  204. if int(cycleCount) == 255 {
  205. action.PlaybackPacu(filename, 9999999, int(delay), "DCS")
  206. } else {
  207. action.PlaybackPacu(filename, int(cycleCount), int(delay), "DCS")
  208. }
  209. }
  210. // 自检广播
  211. func SelfCheck(data []byte) {
  212. check := data[8]
  213. delay := data[9]
  214. cycleCount := data[10]
  215. datalen := int(data[11])
  216. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  217. //update pa type
  218. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{PaType: "CHK"})
  219. if er != nil {
  220. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  221. }
  222. switch check {
  223. case 0x01:
  224. action.PlaybackPacu(filename, int(cycleCount), int(delay), "CHK")
  225. case 0x02:
  226. //asterisk.Hangup(Exten)
  227. }
  228. }
  229. // 全局变量:记录正在抑制的 exten
  230. var (
  231. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  232. suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  233. )
  234. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  235. func suppressKey(exten string, handler byte) string {
  236. return fmt.Sprintf("%s_h%x", exten, handler)
  237. }
  238. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  239. func AlarmHandle(data []byte) {
  240. handler := data[8]
  241. //extlen := data[9]
  242. carr := data[12]
  243. pos := data[13]
  244. exten := fmt.Sprintf("24%c%c", carr, pos)
  245. lfshook.NewLogger().Logger.Infof("================ICP Answer PAD================:%s ", exten)
  246. key := suppressKey(exten, handler)
  247. // 只对 handler == 0x01 做 2 秒去重
  248. if handler == 0x01 {
  249. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  250. lfshook.NewLogger().Logger.Infof("Suppressed duplicate ICP Alarm (handler=0x01) for exten: %s within 2 seconds", exten)
  251. return // 已存在,说明在2秒窗口期内,直接丢弃
  252. }
  253. // 设置2秒后删除该 key,允许下次通过
  254. time.AfterFunc(4*time.Second, func() {
  255. suppressedExts.Delete(key)
  256. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  257. })
  258. }
  259. // 设置2秒后删除该 key,允许下次通过
  260. time.AfterFunc(1*time.Second, func() {
  261. suppressedExts.Delete(key)
  262. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  263. })
  264. switch handler {
  265. case 0x01: //answer(ICP+Alarm+PACU)
  266. err := action.RedirectInQueue(exten, "0402", "ani-rule", "1") // 1车ICP接听
  267. if err != nil {
  268. lfshook.NewLogger().Logger.Infof("================ICP Answer PAD====ERR============ : %+v", err.Error())
  269. }
  270. //invite PACU join in
  271. //action.Hangup("PACU")
  272. //action.ChanSpy("PACU", exten, false, true)
  273. case 0x04: //answer(ICP+Alarm+PACU)
  274. err := action.RedirectInQueue(exten, "0401", "ano-rule", "1") // 1车OCC接听
  275. if err != nil {
  276. //lfshook.NewLogger().Info(err)
  277. lfshook.NewLogger().Logger.Infof("================ICP Answer PAD====ERR============ : %+v", err.Error())
  278. }
  279. //invite PACU join in
  280. //action.Hangup("PACU")
  281. //action.ChanSpy("PACU", exten, false, true)
  282. lfshook.NewLogger().Logger.Infof("================ICP Answer PAD================:%s ", exten)
  283. case 0x02: //hold 重新放回队列里面
  284. err := action.RedirectInQueue(exten, "0300", "default", "1")
  285. if err != nil {
  286. lfshook.NewLogger().Info(err)
  287. }
  288. case 0x03: //hangup
  289. action.Hangup(exten)
  290. }
  291. }
  292. // 挂断所有报警器
  293. func AlarmResetAll() {
  294. var AlarmExts []model.Extension
  295. er := mysql.DBOrmInstance.Where("exttype = ?", "PAIU").Find(&AlarmExts)
  296. if er != nil {
  297. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  298. }
  299. for _, ext := range AlarmExts {
  300. action.Hangup(ext.Extension)
  301. }
  302. }
  303. func RecordStorageConf(data []byte) {
  304. /*padRcd := data[0]
  305. padRcdStorage := data[1]
  306. paRcdStorage := data[2]
  307. cpaRcdStorage := data[3]
  308. padRcdDel := data[4]
  309. PaRcdDel := data[5]
  310. cpaRcdDel := data[6]
  311. //update pa type
  312. _, er := mysql.DBOrmInstance.In("exten", Pacus).Update(&model.Extension{PaType: "CHK"})
  313. if er != nil {
  314. lfshook.NewLogger().Logger.Infof("update special voice to exten err : %+v", er.Error())
  315. }
  316. */
  317. }