stc-broadcast.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. package broadcast
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "fmt"
  7. "io"
  8. "net"
  9. "os/exec"
  10. "pbx-api-gin/internal/app/ami/action"
  11. "pbx-api-gin/internal/app/stc/active"
  12. msgdata "pbx-api-gin/internal/app/stc/data"
  13. "pbx-api-gin/internal/app/stc/priority"
  14. alstatus "pbx-api-gin/internal/app/stc/sendstatus"
  15. "pbx-api-gin/pkg/lfshook"
  16. "pbx-api-gin/pkg/utils"
  17. "strconv"
  18. "sync"
  19. "syscall"
  20. "time"
  21. )
  22. var tagLog = 0
  23. func HandleStcCmd(ctx context.Context, conn net.Conn) {
  24. for {
  25. select {
  26. case <-ctx.Done():
  27. return
  28. default:
  29. var buf bytes.Buffer
  30. tmp := make([]byte, 1024)
  31. if conn != nil {
  32. n, err := conn.Read(tmp)
  33. if err != nil {
  34. if err != io.EOF {
  35. conn.Close()
  36. }
  37. return
  38. }
  39. buf.Write(tmp[:n])
  40. }
  41. /*for {
  42. packet, err := msgdata.ExtractPacket(&buf)
  43. if err != nil {
  44. lfshook.NewLogger().Logger.Infof("ExtractPacket:%+v", err)
  45. break
  46. }
  47. go processPacket(packet)
  48. }*/
  49. //lfshook.NewLogger().Logger.Infof("buf:%x==============================", buf.Bytes())
  50. for {
  51. packet, err := msgdata.ExtractPacket(&buf)
  52. if err != nil {
  53. utils.LoggerDebug.Printf("Parse error: %v, resetting buffer", err)
  54. buf.Reset() // 解析失败,清空避免污染
  55. break
  56. }
  57. if packet == nil {
  58. break // 当前无完整包,等待下次 ReadFrom
  59. }
  60. //处理 packet...
  61. go processPacket(packet)
  62. }
  63. }
  64. }
  65. }
  66. // get train info and pacu info from heartbeat
  67. func getInfofromSTC(info []byte) {
  68. //dataCount := info[0]
  69. //Get train info
  70. value := binary.BigEndian.Uint16(info[2:4]) //info[2] + info[3] 列车号数据
  71. //value := 3
  72. active.TrainNum = "TS" + strconv.Itoa(int(value))
  73. lfshook.NewLogger().Logger.Infof("TrainNum: %s", active.TrainNum)
  74. //Get ICP volume
  75. icpBit8 := info[9] & 0xF
  76. icpBit1 := (info[9] >> 4) & 0xF
  77. icpVol1 := 0
  78. icpVol8 := 0
  79. if icpBit1 == 0xe {
  80. icpVol1 = 100
  81. } else {
  82. icpVol1 = int(icpBit1) * 7
  83. }
  84. if icpBit8 == 0xe {
  85. icpVol8 = 100
  86. } else {
  87. icpVol8 = int(icpBit8) * 7
  88. }
  89. active.DeviceEndpoint.ICPInfo[0].Volume = fmt.Sprintf("%d", icpVol1)
  90. active.DeviceEndpoint.ICPInfo[0].ID = "1"
  91. active.DeviceEndpoint.ICPInfo[1].Volume = fmt.Sprintf("%d", icpVol8)
  92. active.DeviceEndpoint.ICPInfo[1].ID = "8"
  93. //Get Pacu info
  94. eidsStat := info[1] //PACU mute
  95. pacuStat := info[4] //PACU status
  96. pacuVolume := binary.BigEndian.Uint32(info[5:9]) //info[5]-info[8]
  97. //lfshook.NewLogger().Logger.Infof("=====pacuVolume====info[5:9]=======%x", info[5:9])
  98. //lfshook.NewLogger().Logger.Infof("=====pacuStatus====info[4]=======%x", info[4])
  99. for i := 0; i < 8; i++ {
  100. eidsBit := (eidsStat >> i) & 0x01 // 右移 i 位,再与 1 取最低位
  101. //fmt.Printf("eidsBit bit %d = %d\n", i, eidsBit) // bit 0 是 LSB(最低位,最右)
  102. //get 1 bit every time
  103. pacuStatBit := (pacuStat >> i) & 0x01
  104. //fmt.Printf("pacuStatBit bit %d = %d\n", i, pacuStatBit)
  105. //get 4 bit every time
  106. //var pacuVolBit byte
  107. pacuVolBit := (pacuVolume >> (i * 4)) & 0x0000000F
  108. //fmt.Printf("pacuVolBit bit %d = %x\n", i, pacuVolBit)
  109. active.DeviceEndpoint.PacuInfo[i].ID = strconv.Itoa(i + 1)
  110. if eidsBit == 1 {
  111. active.DeviceEndpoint.PacuInfo[i].Mute = true
  112. } else {
  113. active.DeviceEndpoint.PacuInfo[i].Mute = false
  114. }
  115. if pacuStatBit == 0 {
  116. active.DeviceEndpoint.PacuInfo[i].Status = "normal"
  117. } else {
  118. active.DeviceEndpoint.PacuInfo[i].Status = "abnormal"
  119. }
  120. if int(pacuVolBit) == 14 {
  121. active.DeviceEndpoint.PacuInfo[i].Volume = "100"
  122. } else {
  123. active.DeviceEndpoint.PacuInfo[i].Volume = strconv.Itoa(int(pacuVolBit) * 7)
  124. }
  125. //lfshook.NewLogger().Logger.Infof("PACU INFO===ID:%s===Mute:%+v===Stat:%s===Vol:%s", active.DeviceEndpoint.PacuInfo[i].ID, active.DeviceEndpoint.PacuInfo[i].Mute, active.DeviceEndpoint.PacuInfo[i].Status, active.DeviceEndpoint.PacuInfo[i].Volume)
  126. }
  127. }
  128. // 处理单个数据包(原 switch 逻辑迁移过来)
  129. func processPacket(packet []byte) {
  130. if len(packet) < 6 {
  131. utils.LoggerDebug.Printf("Get data wrong length from STC !")
  132. return
  133. }
  134. //for recv data log debug
  135. if packet[5] != 0x03 && packet[5] != 0x0c && packet[5] != 0x01 {
  136. utils.LoggerDebug.Printf("Get data from STC:%x", packet)
  137. }
  138. //check if the cmd type is avtive
  139. if packet[5] == 0x03 { // ACTIVE
  140. Active([3]byte{packet[8], packet[9], packet[10]})
  141. return
  142. }
  143. //check if Master role
  144. if !active.Master {
  145. if tagLog == 0 {
  146. utils.LoggerDebug.Printf("Not Master Role , Ignore all data from STC !")
  147. tagLog = 1
  148. }
  149. return
  150. }
  151. tagLog = 0
  152. switch packet[5] {
  153. case 0x01: //heartbeat
  154. return
  155. case 0x02: // STN
  156. if active.ActivedCab != "" {
  157. if priority.CheckPriority("STN") {
  158. //检查是否有任务真正创建
  159. runningTaskName := action.InterruptRunningTask("STN") //STN interrupt other
  160. if runningTaskName != "" {
  161. time.Sleep(time.Millisecond * 100) //wait endpoint release
  162. }
  163. StationAnn(packet)
  164. } else {
  165. alstatus.PaStatus("", "STN", "refuse")
  166. }
  167. }
  168. case 0x05: // SPC
  169. if active.ActivedCab != "" {
  170. if priority.CheckPriority("SPC") {
  171. runningTaskName := action.InterruptRunningTask("SPC") //SPC interrupt other
  172. if runningTaskName != "" {
  173. time.Sleep(time.Millisecond * 100) //wait endpoint release
  174. }
  175. SpecialAnn(packet)
  176. } else {
  177. alstatus.PaStatus("", "SPC", "refuse")
  178. }
  179. }
  180. case 0x06: // EMG
  181. if active.ActivedCab != "" {
  182. if priority.CheckPriority("EMG") {
  183. runningTaskName := action.InterruptRunningTask("EMG") //EMG interrupt other
  184. if runningTaskName != "" {
  185. time.Sleep(time.Millisecond * 100) //wait endpoint release
  186. }
  187. EmgMsg(packet)
  188. } else {
  189. alstatus.PaStatus("", "EMG", "refuse")
  190. }
  191. }
  192. case 0x07: // STOP
  193. AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
  194. case 0x08: // DCS
  195. if active.ActivedCab != "" {
  196. if priority.CheckPriority("DCS") {
  197. runningTaskName := action.InterruptRunningTask("DCS") //DCS interrupt other
  198. if runningTaskName != "" {
  199. time.Sleep(time.Millisecond * 100) //wait endpoint release
  200. }
  201. DcsAnn(packet)
  202. } else {
  203. alstatus.PaStatus("", "DCS", "refuse")
  204. }
  205. }
  206. case 0x09: // SELF CHECK
  207. if active.ActivedCab != "" {
  208. if priority.CheckPriority("CHK") {
  209. runningTaskName := action.InterruptRunningTask("CHK") //CHK interrupt other
  210. if runningTaskName != "" {
  211. time.Sleep(time.Millisecond * 100) //wait endpoint release
  212. }
  213. SelfCheck(packet)
  214. } else {
  215. alstatus.PaStatus("", "CHK", "refuse")
  216. }
  217. }
  218. case 0x0a: // Tone-test
  219. if active.ActivedCab != "" {
  220. if priority.CheckPriority("VOL") {
  221. runningTaskName := action.InterruptRunningTask("VOL") //VOL interrupt other
  222. if runningTaskName != "" {
  223. time.Sleep(time.Millisecond * 100) //wait endpoint release
  224. }
  225. ToneTest(packet)
  226. } else {
  227. alstatus.PaStatus("", "VOL", "refuse")
  228. }
  229. }
  230. case 0x0e: //TMS answer PAD
  231. if priority.CheckPriority("PAD-TMS") {
  232. //Before Answer PAD
  233. if packet[8] == 0x01 {
  234. runningTaskName := action.InterruptRunningTask("PAD-TMS") //PAD-TMS interrupt other
  235. if runningTaskName != "" {
  236. time.Sleep(time.Millisecond * 100) //wait endpoint release
  237. }
  238. }
  239. AlarmHandleTMS(packet)
  240. if active.QueueTimer != nil {
  241. if active.QueueTimer.Stop() {
  242. utils.LoggerDebug.Printf("Stop PAD timer true !")
  243. } else {
  244. utils.LoggerDebug.Printf("Stop PAD timer false !")
  245. }
  246. }
  247. } else {
  248. alstatus.PaStatus("", "PAD-TMS", "refuse")
  249. }
  250. case 0x0b: // reset all PAD
  251. AlarmHoldResetAll(packet[8]) // reset all pad
  252. case 0x0c: // Set PAD timeout
  253. //lfshook.NewLogger().Logger.Infof("==type 0x0c===Get data from STC ====%x", packet)
  254. PadTimeOutSetting(packet[8:]) // timeout setting
  255. getInfofromSTC(packet[8:])
  256. case 0x0d: // ICP answer PAD
  257. if priority.CheckPriority("PAD-ICP") {
  258. //Before Answer PAD
  259. if packet[8] == 0x01 {
  260. runningTaskName := action.InterruptRunningTask("PAD-ICP") //PAD-ICP interrupt other
  261. if runningTaskName != "" {
  262. time.Sleep(time.Millisecond * 100) //wait endpoint release
  263. }
  264. }
  265. if active.QueueTimer != nil {
  266. if active.QueueTimer.Stop() {
  267. utils.LoggerDebug.Printf("Stop PAD timer true !")
  268. } else {
  269. utils.LoggerDebug.Printf("Stop PAD timer false !")
  270. }
  271. }
  272. AlarmHandleICP(packet)
  273. } else {
  274. alstatus.PaStatus("", "PAD-ICP", "refuse")
  275. }
  276. //case 0xf1: //Set remote master
  277. //default:
  278. //fmt.Printf("Unknown command: %x\n", packet[5])
  279. }
  280. }
  281. func PadTimeOutSetting(data []byte) {
  282. Seconds := data[7]
  283. if Seconds != 0 {
  284. active.PADTimeout = int(Seconds)
  285. //lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", active.PADTimeout)
  286. }
  287. }
  288. // STN , 自动报站广播
  289. func StationAnn(data []byte) (err error) {
  290. specialVoice := int(data[8])
  291. delay := data[9]
  292. cycleCount := data[10]
  293. datalen := int(data[11])
  294. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  295. //set spc voice tag
  296. priority.SpecialVoice = specialVoice
  297. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "STN")
  298. return nil
  299. }
  300. // 激活信号
  301. func Active(data [3]byte) {
  302. //var info model.Sysinfo
  303. Signal := int(data[0])
  304. master := int(data[1])
  305. TrainInfo := int(data[2])
  306. lfshook.NewLogger().Logger.Infof("=====active:%x======cab=%s Master=%d=====data:%x======", Signal, active.CabNum, master, data)
  307. //Set Master
  308. if master == 1 && active.CabNum == "1" {
  309. active.Master = true
  310. if !utils.CheckAsterisk() {
  311. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  312. cmd := exec.Command("service", "asterisk", "start")
  313. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  314. err := cmd.Run()
  315. if err != nil {
  316. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  317. return
  318. }
  319. }
  320. } else if master == 8 && active.CabNum == "8" {
  321. active.Master = true
  322. if !utils.CheckAsterisk() {
  323. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  324. cmd := exec.Command("service", "asterisk", "start")
  325. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  326. err := cmd.Run()
  327. if err != nil {
  328. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  329. return
  330. }
  331. }
  332. } else if master == 8 && active.CabNum == "1" {
  333. if utils.CheckAsterisk() {
  334. lfshook.NewLogger().Infof("Check asterisk , if running slave, run cmd service asterisk stop !")
  335. cmd := exec.Command("service", "asterisk", "stop")
  336. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  337. err := cmd.Run()
  338. if err != nil {
  339. lfshook.NewLogger().Infof("Failed to stop asterisk: %v", err)
  340. return
  341. }
  342. }
  343. } else if master == 1 && active.CabNum == "8" {
  344. active.Master = false
  345. }
  346. //Set train info
  347. if TrainInfo != 0 {
  348. //Get train devide info
  349. DevideInfo := TrainInfo & 0x20
  350. if DevideInfo == 0x20 {
  351. active.TrainDevide = 1
  352. active.Master = true //列车断开,设置两边都Master
  353. if !utils.CheckAsterisk() {
  354. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  355. cmd := exec.Command("service", "asterisk", "start")
  356. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  357. err := cmd.Run()
  358. if err != nil {
  359. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  360. return
  361. }
  362. }
  363. } else {
  364. active.TrainDevide = 0
  365. }
  366. //Radio fault
  367. RadioFault1 := TrainInfo & 0x03
  368. RadioFault8 := TrainInfo & 0x0c
  369. if RadioFault1 == 0 && RadioFault8 == 1 {
  370. active.RadioFault = 1
  371. } else {
  372. active.RadioFault = 0
  373. }
  374. utils.LoggerDebug.Printf("RadioFault1:%x RadioFault1:%x DevideInfo:%x", RadioFault1, RadioFault8, DevideInfo)
  375. }
  376. switch Signal {
  377. case 0:
  378. if active.ActivedCab != "" {
  379. active.ActivedCab = ""
  380. action.InActiveHangup()
  381. }
  382. case 1:
  383. //active signal from 8 to 1
  384. if active.ActivedCab == "8" || active.ActivedCab == "" {
  385. active.ActivedCab = "1"
  386. action.InActiveHangup()
  387. }
  388. case 8:
  389. //active signal from 1 to 8
  390. if active.ActivedCab == "1" || active.ActivedCab == "" {
  391. active.ActivedCab = "8"
  392. action.InActiveHangup()
  393. }
  394. }
  395. }
  396. // SPC ,特殊服务消息广播
  397. func SpecialAnn(data []byte) {
  398. delay := data[8]
  399. cycleCount := data[9]
  400. datalen := int(data[10])
  401. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  402. if int(cycleCount) == 255 {
  403. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
  404. } else {
  405. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
  406. }
  407. }
  408. // EMG ,紧急服务消息广播
  409. func EmgMsg(data []byte) {
  410. delay := data[8]
  411. cycleCount := data[9]
  412. datalen := int(data[10])
  413. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  414. if int(cycleCount) == 255 {
  415. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
  416. } else {
  417. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
  418. }
  419. }
  420. // 停止指定类型广播
  421. func AnnStop(data [4]byte) {
  422. //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
  423. switch data[0] {
  424. case 0x03:
  425. action.HangupTask("DCS") //STOP DCS
  426. time.Sleep(time.Millisecond * 100) //wait endpoint release
  427. case 0x04:
  428. action.HangupTask("EMG") //STOP EMG
  429. time.Sleep(time.Millisecond * 100) //wait endpoint release
  430. case 0x07:
  431. action.HangupTask("SPC") //STOP SPC
  432. time.Sleep(time.Millisecond * 100) //wait endpoint release
  433. case 0x08:
  434. action.HangupTask("STN") //STOP STN
  435. time.Sleep(time.Millisecond * 100) //wait endpoint release
  436. case 0x09:
  437. action.HangupTask("CHK") //STOP CHK
  438. time.Sleep(time.Millisecond * 100) //wait endpoint release
  439. case 0x0a:
  440. action.HangupTask("VOL") //STOP VOL
  441. time.Sleep(time.Millisecond * 100) //wait endpoint release
  442. default:
  443. action.InterruptRunningTask("")
  444. time.Sleep(time.Millisecond * 100) //wait endpoint release
  445. }
  446. }
  447. // DCS 语音
  448. func DcsAnn(data []byte) {
  449. delay := data[8]
  450. cycleCount := data[9]
  451. datalen := int(data[10])
  452. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  453. if int(cycleCount) == 255 {
  454. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
  455. } else {
  456. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
  457. }
  458. }
  459. // tone-test广播
  460. func ToneTest(data []byte) {
  461. check := data[8]
  462. delay := data[9]
  463. cycleCount := data[10]
  464. datalen := int(data[11])
  465. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  466. switch check {
  467. case 0x01: //start
  468. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
  469. case 0x02: //stop
  470. action.HangupAllExcept("")
  471. }
  472. }
  473. // 自检广播
  474. func SelfCheck(data []byte) {
  475. check := data[8]
  476. delay := data[9]
  477. cycleCount := data[10]
  478. //cycleCount := 0x32
  479. datalen := int(data[11])
  480. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  481. switch check {
  482. case 0x01: //start
  483. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
  484. case 0x02: //stop
  485. action.HangupAllExcept("")
  486. }
  487. }
  488. // 全局变量:记录正在抑制的 exten
  489. var (
  490. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  491. //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  492. )
  493. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  494. func suppressKey(exten string, handler byte) string {
  495. return fmt.Sprintf("%s_h%x", exten, handler)
  496. }
  497. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  498. func AlarmHandleICP(data []byte) {
  499. handler := data[8]
  500. carr := data[12]
  501. pos := data[13]
  502. exten := fmt.Sprintf("24%c%c", carr, pos)
  503. key := suppressKey(exten, handler)
  504. //Drop other handler in 2 sec
  505. //PACUs---call---->ICP1
  506. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  507. if handler == 0x01 {
  508. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  509. utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD:%s within 4 seconds", exten)
  510. return
  511. }
  512. time.AfterFunc(4*time.Second, func() {
  513. suppressedExts.Delete(key)
  514. utils.LoggerDebug.Printf("Suppression released for key: %s", key)
  515. })
  516. }
  517. switch handler {
  518. case 0x01: //answer(ICP+Alarm+PACU)
  519. //NotifyPaiu(exten, "answer")
  520. priority.ICPAnswer = 1
  521. if priority.PADStart == 0 {
  522. alstatus.PaStatus("", "PAD", "start")
  523. priority.PADStart = 1
  524. }
  525. utils.LoggerDebug.Printf("ICP Answer PAD:%s .", exten)
  526. if active.ActivedCab == "1" {
  527. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  528. //goto ami event ConfbridgeJoin, ICP answer PAD
  529. } else if active.ActivedCab == "8" {
  530. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  531. //goto ami event ConfbridgeJoin, ICP answer PAD
  532. } else if active.ActivedCab == "" { // No cab occupied
  533. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  534. }
  535. case 0x02: //hold 重新放回队列里面
  536. utils.LoggerDebug.Printf("ICP Hold PAD-ICP PAD:%s !", exten)
  537. active.NotifyPaiu(exten, "hold")
  538. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  539. if err != nil {
  540. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  541. }
  542. action.InterruptRunningTask("")
  543. time.Sleep(time.Millisecond * 100) //wait endpoint release
  544. //action.HangupICP()
  545. case 0x03: //hangup
  546. //NotifyPaiu(exten, "hangup")
  547. utils.LoggerDebug.Printf("STC Hangup PAD-ICP !")
  548. action.Hangup(exten) //Pad
  549. //action.HangupICP()
  550. action.HangupTask("PAD-ICP")
  551. }
  552. }
  553. // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
  554. func AlarmHandleTMS(data []byte) {
  555. handler := data[8]
  556. //extlen := data[9]
  557. carr := data[12]
  558. pos := data[13]
  559. exten := fmt.Sprintf("24%c%c", carr, pos)
  560. PacuNum := fmt.Sprintf("21%c1", carr)
  561. key := suppressKey(exten, handler)
  562. //Drop other handler in 2 sec
  563. // 只对 handler == 0x01 做 2 秒去重
  564. if handler == 0x01 {
  565. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  566. utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD:%s within 4 seconds", exten)
  567. return // 已存在,说明在2秒窗口期内,直接丢弃
  568. }
  569. // 设置4秒后删除该 key,允许下次通过
  570. time.AfterFunc(4*time.Second, func() {
  571. suppressedExts.Delete(key)
  572. utils.LoggerDebug.Printf("Suppression released for key: %s", key)
  573. })
  574. }
  575. switch handler {
  576. case 0x01: //answer(ICP+Alarm+PACU)
  577. //PACU---call---->ICP1
  578. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  579. if priority.PADStart == 0 {
  580. alstatus.PaStatus("", "PAD", "start")
  581. priority.PADStart = 1
  582. }
  583. priority.ICPAnswer = 1
  584. utils.LoggerDebug.Printf("TMS Answer PAD:%s PACU:%s", exten, PacuNum)
  585. if action.ExtenStatus(PacuNum) == "Idle" {
  586. if active.ActivedCab == "1" {
  587. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  588. //goto ami event BridgeEnter, ICP8 whisper ICP1
  589. } else if active.ActivedCab == "8" {
  590. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
  591. //goto ami event BridgeEnter, ICP1 whisper ICP8
  592. } else if active.ActivedCab == "" { // No cab occupied
  593. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  594. }
  595. } else {
  596. action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
  597. }
  598. case 0x02: //hold 重新放回队列里面
  599. utils.LoggerDebug.Printf("ICP Hold PAD-TMS PAD:%s !", exten)
  600. active.NotifyPaiu(exten, "hold")
  601. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  602. if err != nil {
  603. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  604. }
  605. action.HangupAllLocalChan()
  606. case 0x03: //hangup
  607. //NotifyPaiu(exten, "hangup")
  608. utils.LoggerDebug.Printf("STC Hangup PAD-TMS !")
  609. action.Hangup(exten) //Pad
  610. action.HangupTask("PAD-TMS")
  611. //action.HangupTask("PAD-ICP")
  612. }
  613. }
  614. // 挂断所有报警器
  615. func AlarmHoldResetAll(handler byte) {
  616. //hangup all actived PAD
  617. action.HangupAllPAD()
  618. //hangup running task
  619. action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
  620. time.Sleep(time.Millisecond * 100) //wait endpoint release
  621. }