stc-broadcast.go 9.9 KB

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