stc-broadcast.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package broadcast
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net"
  8. "pbx-api-gin/internal/app/ami/action"
  9. "pbx-api-gin/internal/app/stc/active"
  10. msgdata "pbx-api-gin/internal/app/stc/data"
  11. "pbx-api-gin/internal/app/stc/priority"
  12. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  13. "pbx-api-gin/pkg/lfshook"
  14. "pbx-api-gin/pkg/utils"
  15. "strconv"
  16. "sync"
  17. "time"
  18. )
  19. var tagLog = 0
  20. func HandleStcCmd(ctx context.Context, conn net.Conn) {
  21. for {
  22. select {
  23. case <-ctx.Done():
  24. return
  25. default:
  26. var buf bytes.Buffer
  27. tmp := make([]byte, 1024)
  28. if conn != nil {
  29. n, err := conn.Read(tmp)
  30. if err != nil {
  31. if err != io.EOF {
  32. conn.Close()
  33. }
  34. return
  35. }
  36. buf.Write(tmp[:n])
  37. }
  38. for {
  39. packet, err := msgdata.ExtractPacket(&buf)
  40. if err != nil {
  41. break
  42. }
  43. go processPacket(packet)
  44. }
  45. }
  46. }
  47. }
  48. // 处理单个数据包(原 switch 逻辑迁移过来)
  49. func processPacket(packet []byte) {
  50. if len(packet) < 6 {
  51. lfshook.NewLogger().Logger.Infof("Get data wrong length from STC !")
  52. return
  53. }
  54. //for recv data log debug
  55. if packet[5] != 0x03 && packet[5] != 0x0c && packet[5] != 0x01 {
  56. lfshook.NewLogger().Logger.Infof("Get data from STC:%x", packet)
  57. }
  58. //check if the cmd type is avtive
  59. if packet[5] == 0x03 { // ACTIVE
  60. Active([2]byte{packet[8], packet[9]})
  61. return
  62. }
  63. //check if Master role
  64. if !active.Master {
  65. if tagLog == 0 {
  66. lfshook.NewLogger().Logger.Infof("=========Not Master Role Ignore data !=============")
  67. tagLog = 1
  68. }
  69. return
  70. }
  71. tagLog = 0
  72. switch packet[5] {
  73. case 0x01: //heartbeat
  74. break
  75. case 0x02: // STN
  76. if active.ActivedCab != "" {
  77. if priority.CheckPriority("STN") {
  78. action.InterruptRunningTask("STN") //STN interrupt other
  79. StationAnn(packet)
  80. } else {
  81. alstatus.PaStatus("", "STN", "refuse")
  82. }
  83. }
  84. case 0x05: // SPC
  85. if active.ActivedCab != "" {
  86. if priority.CheckPriority("SPC") {
  87. action.InterruptRunningTask("SPC") //SPC interrupt other
  88. SpecialAnn(packet)
  89. } else {
  90. alstatus.PaStatus("", "SPC", "refuse")
  91. }
  92. }
  93. case 0x06: // EMG
  94. if active.ActivedCab != "" {
  95. if priority.CheckPriority("EMG") {
  96. action.InterruptRunningTask("EMG") //EMG interrupt other
  97. EmgMsg(packet)
  98. } else {
  99. alstatus.PaStatus("", "EMG", "refuse")
  100. }
  101. }
  102. case 0x07: // STOP
  103. AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
  104. case 0x08: // DCS
  105. if active.ActivedCab != "" {
  106. if priority.CheckPriority("DCS") {
  107. action.InterruptRunningTask("DCS") //DCS interrupt other
  108. DcsAnn(packet)
  109. } else {
  110. alstatus.PaStatus("", "DCS", "refuse")
  111. }
  112. }
  113. case 0x09: // SELF CHECK
  114. if active.ActivedCab != "" {
  115. if priority.CheckPriority("CHK") {
  116. action.InterruptRunningTask("CHK") //CHK interrupt other
  117. SelfCheck(packet)
  118. } else {
  119. alstatus.PaStatus("", "CHK", "refuse")
  120. }
  121. }
  122. case 0x0a: // Tone-test
  123. if active.ActivedCab != "" {
  124. if priority.CheckPriority("VOL") {
  125. action.InterruptRunningTask("VOL") //VOL interrupt other
  126. ToneTest(packet)
  127. } else {
  128. alstatus.PaStatus("", "VOL", "refuse")
  129. }
  130. }
  131. case 0x0e: //TMS answer PAD
  132. if priority.CheckPriority("PAD-TMS") {
  133. //Before Answer PAD
  134. if packet[8] == 0x01 {
  135. action.InterruptRunningTask("PAD-TMS") //PAD-ICP interrupt other
  136. }
  137. AlarmHandleTMS(packet)
  138. if active.SetTimer {
  139. active.QueueTimer.Stop()
  140. active.SetTimer = false
  141. }
  142. } else {
  143. alstatus.PaStatus("", "PAD-TMS", "refuse")
  144. }
  145. case 0x0b: // reset all PAD
  146. AlarmHoldResetAll(packet[8]) // reset all pad
  147. //case 0x0c: // recored config
  148. // RecordStorageConf(packet[8:]) // RCD setting
  149. case 0x0d: // ICP answer PAD
  150. if priority.CheckPriority("PAD-ICP") {
  151. //Before Answer PAD
  152. if packet[8] == 0x01 {
  153. action.InterruptRunningTask("PAD-ICP") //PAD-ICP interrupt other
  154. }
  155. if active.SetTimer {
  156. active.QueueTimer.Stop()
  157. active.SetTimer = false
  158. }
  159. AlarmHandleICP(packet)
  160. } else {
  161. alstatus.PaStatus("", "PAD-ICP", "refuse")
  162. }
  163. case 0xf1: //Set remote master
  164. //default:
  165. //fmt.Printf("Unknown command: %x\n", packet[5])
  166. }
  167. }
  168. // STN , 自动报站广播
  169. func StationAnn(data []byte) (err error) {
  170. specialVoice := int(data[8])
  171. delay := data[9]
  172. cycleCount := data[10]
  173. datalen := int(data[11])
  174. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  175. //set spc voice tag
  176. priority.SpecialVoice = specialVoice
  177. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "STN")
  178. return nil
  179. }
  180. // 激活信号
  181. func Active(data [2]byte) {
  182. //var info model.Sysinfo
  183. Signal := int(data[0])
  184. //check asterisk available
  185. if active.Master { // master true
  186. if !utils.CheckAsterisk() { //check asterisk not available and set master false
  187. active.Master = false
  188. utils.ExecCmd("/etc/init.d/asterisk", "stop")
  189. }
  190. } else { // master false, check and start asterisk
  191. //Master == false
  192. if !utils.CheckAsterisk() {
  193. if active.CabNum == "8" {
  194. utils.ExecCmd("/etc/init.d/asterisk", "start")
  195. }
  196. }
  197. //Master == false cab == 1
  198. if active.CabNum == "1" {
  199. if utils.CheckAsterisk() {
  200. utils.ExecCmd("/etc/init.d/asterisk", "stop")
  201. }
  202. }
  203. }
  204. switch Signal {
  205. case 0:
  206. if active.ActivedCab != "" {
  207. active.ActivedCab = ""
  208. action.InActiveHangup()
  209. }
  210. case 1:
  211. //active signal from 8 to 1
  212. if active.ActivedCab == "8" || active.ActivedCab == "" {
  213. active.ActivedCab = "1"
  214. action.InActiveHangup()
  215. }
  216. case 8:
  217. //active signal from 1 to 8
  218. if active.ActivedCab == "1" || active.ActivedCab == "" {
  219. active.ActivedCab = "8"
  220. action.InActiveHangup()
  221. }
  222. }
  223. }
  224. // SPC ,特殊服务消息广播
  225. func SpecialAnn(data []byte) {
  226. delay := data[8]
  227. cycleCount := data[9]
  228. datalen := int(data[10])
  229. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  230. if int(cycleCount) == 255 {
  231. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
  232. } else {
  233. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
  234. }
  235. }
  236. // EMG ,紧急服务消息广播
  237. func EmgMsg(data []byte) {
  238. delay := data[8]
  239. cycleCount := data[9]
  240. datalen := int(data[10])
  241. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  242. if int(cycleCount) == 255 {
  243. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
  244. } else {
  245. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
  246. }
  247. }
  248. // 停止指定类型广播
  249. func AnnStop(data [4]byte) {
  250. //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
  251. switch data[0] {
  252. case 0x03:
  253. action.HangupTask("DCS") //STOP DCS
  254. case 0x04:
  255. action.HangupTask("EMG") //STOP EMG
  256. case 0x07:
  257. action.HangupTask("SPC") //STOP SPC
  258. case 0x08:
  259. action.HangupTask("STN") //STOP STN
  260. case 0x09:
  261. action.HangupTask("CHK") //STOP CHK
  262. case 0x0a:
  263. action.HangupTask("VOL") //STOP VOL
  264. default:
  265. action.InterruptRunningTask("")
  266. }
  267. }
  268. // DCS 语音
  269. func DcsAnn(data []byte) {
  270. delay := data[8]
  271. cycleCount := data[9]
  272. datalen := int(data[10])
  273. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  274. if int(cycleCount) == 255 {
  275. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
  276. } else {
  277. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
  278. }
  279. }
  280. // tone-test广播
  281. func ToneTest(data []byte) {
  282. check := data[8]
  283. delay := data[9]
  284. cycleCount := data[10]
  285. datalen := int(data[11])
  286. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  287. switch check {
  288. case 0x01: //start
  289. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
  290. case 0x02: //stop
  291. action.HangupAllExcept("")
  292. }
  293. }
  294. // 自检广播
  295. func SelfCheck(data []byte) {
  296. check := data[8]
  297. delay := data[9]
  298. //cycleCount := data[10]
  299. cycleCount := 0x32
  300. datalen := int(data[11])
  301. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  302. switch check {
  303. case 0x01: //start
  304. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
  305. case 0x02: //stop
  306. action.HangupAllExcept("")
  307. }
  308. }
  309. // 全局变量:记录正在抑制的 exten
  310. var (
  311. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  312. //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  313. )
  314. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  315. func suppressKey(exten string, handler byte) string {
  316. return fmt.Sprintf("%s_h%x", exten, handler)
  317. }
  318. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  319. func AlarmHandleICP(data []byte) {
  320. handler := data[8]
  321. carr := data[12]
  322. pos := data[13]
  323. exten := fmt.Sprintf("24%c%c", carr, pos)
  324. key := suppressKey(exten, handler)
  325. //Drop other handler in 2 sec
  326. //PACUs---call---->ICP1
  327. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  328. if handler == 0x01 {
  329. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  330. lfshook.NewLogger().Logger.Infof("Suppressed duplicate ICP Alarm (handler=0x01) for exten: %s within 4 seconds", exten)
  331. return
  332. }
  333. time.AfterFunc(4*time.Second, func() {
  334. suppressedExts.Delete(key)
  335. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  336. })
  337. }
  338. switch handler {
  339. case 0x01: //answer(ICP+Alarm+PACU)
  340. //NotifyPaiu(exten, "answer")
  341. priority.ICPAnswer = 1
  342. if priority.PADStart == 0 {
  343. alstatus.PaStatus("", "PAD", "start")
  344. priority.PADStart = 1
  345. }
  346. lfshook.NewLogger().Logger.Infof("================ ICP Answer PAD ================:%s ", exten)
  347. if active.ActivedCab == "1" {
  348. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  349. //goto ami event ConfbridgeJoin, ICP answer PAD
  350. } else if active.ActivedCab == "8" {
  351. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  352. //goto ami event ConfbridgeJoin, ICP answer PAD
  353. } else if active.ActivedCab == "" { // No cab occupied
  354. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  355. }
  356. case 0x02: //hold 重新放回队列里面
  357. active.NotifyPaiu(exten, "hold")
  358. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  359. if err != nil {
  360. lfshook.NewLogger().Info(err)
  361. }
  362. action.InterruptRunningTask("")
  363. //action.HangupICP()
  364. case 0x03: //hangup
  365. //NotifyPaiu(exten, "hangup")
  366. lfshook.NewLogger().Logger.Infof("============ Hangup PAD-ICP =============== ")
  367. action.Hangup(exten) //Pad
  368. //action.HangupICP()
  369. action.HangupTask("PAD-ICP")
  370. }
  371. }
  372. // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
  373. func AlarmHandleTMS(data []byte) {
  374. handler := data[8]
  375. //extlen := data[9]
  376. carr := data[12]
  377. pos := data[13]
  378. exten := fmt.Sprintf("24%c%c", carr, pos)
  379. PacuNum := fmt.Sprintf("21%c%c", carr, pos)
  380. key := suppressKey(exten, handler)
  381. //Drop other handler in 2 sec
  382. // 只对 handler == 0x01 做 2 秒去重
  383. if handler == 0x01 {
  384. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  385. lfshook.NewLogger().Logger.Infof("Suppressed duplicate ICP Alarm (handler=0x01) for exten: %s within 4 seconds", exten)
  386. return // 已存在,说明在2秒窗口期内,直接丢弃
  387. }
  388. // 设置4秒后删除该 key,允许下次通过
  389. time.AfterFunc(4*time.Second, func() {
  390. suppressedExts.Delete(key)
  391. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  392. })
  393. }
  394. switch handler {
  395. case 0x01: //answer(ICP+Alarm+PACU)
  396. //PACU---call---->ICP1
  397. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  398. if priority.PADStart == 0 {
  399. alstatus.PaStatus("", "PAD", "start")
  400. priority.PADStart = 1
  401. }
  402. priority.ICPAnswer = 1
  403. lfshook.NewLogger().Logger.Infof("==============TMS Answer PAD Exten:%s PACU:%s==========", exten, PacuNum)
  404. if action.ExtenStatus(PacuNum) == "Idle" {
  405. if active.ActivedCab == "1" {
  406. action.Dial("0403", PacuNum, "default", PacuNum, exten, "1") // PACU dial ICP1
  407. //goto ami event BridgeEnter, ICP8 whisper ICP1
  408. } else if active.ActivedCab == "8" {
  409. action.Dial("0403", PacuNum, "default", PacuNum, exten, "8") // PACU dial ICP8
  410. //goto ami event BridgeEnter, ICP1 whisper ICP8
  411. } else if active.ActivedCab == "" { // No cab occupied
  412. action.Dial("0403", PacuNum, "default", PacuNum, exten, "1") // PACU dial ICP1
  413. }
  414. } else {
  415. action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
  416. }
  417. case 0x02: //hold 重新放回队列里面
  418. active.NotifyPaiu(exten, "hold")
  419. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  420. if err != nil {
  421. lfshook.NewLogger().Info(err)
  422. }
  423. action.HangupAllLocalChan()
  424. case 0x03: //hangup
  425. //NotifyPaiu(exten, "hangup")
  426. action.Hangup(exten) //Pad
  427. action.HangupTask("PAD-ICP")
  428. }
  429. }
  430. // 挂断所有报警器
  431. func AlarmHoldResetAll(handler byte) {
  432. //hangup all actived PAD
  433. action.HangupAllPAD()
  434. //hangup running task
  435. action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
  436. }