stc-broadcast.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. //lfshook.NewLogger().Logger.Infof("===Get Heartbeat===packet:%x===========", packet)
  156. //dataLen := binary.BigEndian.Uint16(packet[6:8])
  157. //if dataLen == 10 {
  158. // processHeartbeat(packet[8:])
  159. //} else {
  160. //lfshook.NewLogger().Logger.Infof("=========Heartbeat data err !====len < 10=========")
  161. //}
  162. case 0x02: // STN
  163. if active.ActivedCab != "" {
  164. if priority.CheckPriority("STN") {
  165. action.InterruptRunningTask("STN") //STN interrupt other
  166. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  167. StationAnn(packet)
  168. } else {
  169. alstatus.PaStatus("", "STN", "refuse")
  170. }
  171. }
  172. case 0x05: // SPC
  173. if active.ActivedCab != "" {
  174. if priority.CheckPriority("SPC") {
  175. action.InterruptRunningTask("SPC") //SPC interrupt other
  176. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  177. SpecialAnn(packet)
  178. } else {
  179. alstatus.PaStatus("", "SPC", "refuse")
  180. }
  181. }
  182. case 0x06: // EMG
  183. if active.ActivedCab != "" {
  184. if priority.CheckPriority("EMG") {
  185. action.InterruptRunningTask("EMG") //EMG interrupt other
  186. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  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. action.InterruptRunningTask("DCS") //DCS interrupt other
  198. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  199. DcsAnn(packet)
  200. } else {
  201. alstatus.PaStatus("", "DCS", "refuse")
  202. }
  203. }
  204. case 0x09: // SELF CHECK
  205. if active.ActivedCab != "" {
  206. if priority.CheckPriority("CHK") {
  207. action.InterruptRunningTask("CHK") //CHK interrupt other
  208. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  209. SelfCheck(packet)
  210. } else {
  211. alstatus.PaStatus("", "CHK", "refuse")
  212. }
  213. }
  214. case 0x0a: // Tone-test
  215. if active.ActivedCab != "" {
  216. if priority.CheckPriority("VOL") {
  217. action.InterruptRunningTask("VOL") //VOL interrupt other
  218. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  219. ToneTest(packet)
  220. } else {
  221. alstatus.PaStatus("", "VOL", "refuse")
  222. }
  223. }
  224. case 0x0e: //TMS answer PAD
  225. if priority.CheckPriority("PAD-TMS") {
  226. //Before Answer PAD
  227. if packet[8] == 0x01 {
  228. action.InterruptRunningTask("PAD-TMS") //PAD-ICP interrupt other
  229. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  230. }
  231. AlarmHandleTMS(packet)
  232. if active.QueueTimer != nil {
  233. if active.QueueTimer.Stop() {
  234. utils.LoggerDebug.Printf("Stop PAD timer true !")
  235. } else {
  236. utils.LoggerDebug.Printf("Stop PAD timer false !")
  237. }
  238. }
  239. } else {
  240. alstatus.PaStatus("", "PAD-TMS", "refuse")
  241. }
  242. case 0x0b: // reset all PAD
  243. AlarmHoldResetAll(packet[8]) // reset all pad
  244. case 0x0c: // Set PAD timeout
  245. //lfshook.NewLogger().Logger.Infof("==type 0x0c===Get data from STC ====%x", packet)
  246. PadTimeOutSetting(packet[8:]) // timeout setting
  247. getInfofromSTC(packet[8:])
  248. case 0x0d: // ICP answer PAD
  249. if priority.CheckPriority("PAD-ICP") {
  250. //Before Answer PAD
  251. if packet[8] == 0x01 {
  252. action.InterruptRunningTask("PAD-ICP") //PAD-ICP interrupt other
  253. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  254. }
  255. if active.QueueTimer != nil {
  256. if active.QueueTimer.Stop() {
  257. utils.LoggerDebug.Printf("Stop PAD timer true !")
  258. } else {
  259. utils.LoggerDebug.Printf("Stop PAD timer false !")
  260. }
  261. }
  262. AlarmHandleICP(packet)
  263. } else {
  264. alstatus.PaStatus("", "PAD-ICP", "refuse")
  265. }
  266. case 0xf1: //Set remote master
  267. //default:
  268. //fmt.Printf("Unknown command: %x\n", packet[5])
  269. }
  270. }
  271. func PadTimeOutSetting(data []byte) {
  272. Seconds := data[7]
  273. if Seconds != 0 {
  274. active.PADTimeout = int(Seconds)
  275. //lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", active.PADTimeout)
  276. }
  277. }
  278. // STN , 自动报站广播
  279. func StationAnn(data []byte) (err error) {
  280. specialVoice := int(data[8])
  281. delay := data[9]
  282. cycleCount := data[10]
  283. datalen := int(data[11])
  284. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  285. //set spc voice tag
  286. priority.SpecialVoice = specialVoice
  287. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "STN")
  288. return nil
  289. }
  290. // 激活信号
  291. func Active(data [3]byte) {
  292. //var info model.Sysinfo
  293. Signal := int(data[0])
  294. master := int(data[1])
  295. TrainInfo := int(data[2])
  296. lfshook.NewLogger().Logger.Infof("=====active:%x======cab=%s Master=%d=====data:%x======", Signal, active.CabNum, master, data)
  297. //Set Master
  298. if master == 1 && active.CabNum == "1" {
  299. active.Master = true
  300. if !utils.CheckAsterisk() {
  301. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  302. cmd := exec.Command("service", "asterisk", "start")
  303. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  304. err := cmd.Run()
  305. if err != nil {
  306. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  307. return
  308. }
  309. }
  310. } else if master == 8 && active.CabNum == "8" {
  311. active.Master = true
  312. if !utils.CheckAsterisk() {
  313. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  314. cmd := exec.Command("service", "asterisk", "start")
  315. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  316. err := cmd.Run()
  317. if err != nil {
  318. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  319. return
  320. }
  321. }
  322. } else if master == 8 && active.CabNum == "1" {
  323. if utils.CheckAsterisk() {
  324. lfshook.NewLogger().Infof("Check asterisk , if running slave, run cmd service asterisk stop !")
  325. cmd := exec.Command("service", "asterisk", "stop")
  326. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  327. err := cmd.Run()
  328. if err != nil {
  329. lfshook.NewLogger().Infof("Failed to stop asterisk: %v", err)
  330. return
  331. }
  332. }
  333. } else if master == 1 && active.CabNum == "8" {
  334. active.Master = false
  335. }
  336. //Set train info
  337. if TrainInfo != 0 {
  338. //Get train devide info
  339. DevideInfo := TrainInfo & 0x20
  340. if DevideInfo == 0x20 {
  341. active.TrainDevide = 1
  342. active.Master = true //列车断开,设置两边都Master
  343. if !utils.CheckAsterisk() {
  344. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  345. cmd := exec.Command("service", "asterisk", "start")
  346. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  347. err := cmd.Run()
  348. if err != nil {
  349. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  350. return
  351. }
  352. }
  353. } else {
  354. active.TrainDevide = 0
  355. }
  356. //Radio fault
  357. RadioFault1 := TrainInfo & 0x03
  358. RadioFault8 := TrainInfo & 0x0c
  359. if RadioFault1 == 0 && RadioFault8 == 1 {
  360. active.RadioFault = 1
  361. } else {
  362. active.RadioFault = 0
  363. }
  364. utils.LoggerDebug.Printf("RadioFault1:%x RadioFault1:%x DevideInfo:%x", RadioFault1, RadioFault8, DevideInfo)
  365. }
  366. switch Signal {
  367. case 0:
  368. if active.ActivedCab != "" {
  369. active.ActivedCab = ""
  370. action.InActiveHangup()
  371. }
  372. case 1:
  373. //active signal from 8 to 1
  374. if active.ActivedCab == "8" || active.ActivedCab == "" {
  375. active.ActivedCab = "1"
  376. action.InActiveHangup()
  377. }
  378. case 8:
  379. //active signal from 1 to 8
  380. if active.ActivedCab == "1" || active.ActivedCab == "" {
  381. active.ActivedCab = "8"
  382. action.InActiveHangup()
  383. }
  384. }
  385. }
  386. // SPC ,特殊服务消息广播
  387. func SpecialAnn(data []byte) {
  388. delay := data[8]
  389. cycleCount := data[9]
  390. datalen := int(data[10])
  391. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  392. if int(cycleCount) == 255 {
  393. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
  394. } else {
  395. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
  396. }
  397. }
  398. // EMG ,紧急服务消息广播
  399. func EmgMsg(data []byte) {
  400. delay := data[8]
  401. cycleCount := data[9]
  402. datalen := int(data[10])
  403. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  404. if int(cycleCount) == 255 {
  405. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
  406. } else {
  407. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
  408. }
  409. }
  410. // 停止指定类型广播
  411. func AnnStop(data [4]byte) {
  412. //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
  413. switch data[0] {
  414. case 0x03:
  415. action.HangupTask("DCS") //STOP DCS
  416. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  417. case 0x04:
  418. action.HangupTask("EMG") //STOP EMG
  419. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  420. case 0x07:
  421. action.HangupTask("SPC") //STOP SPC
  422. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  423. case 0x08:
  424. action.HangupTask("STN") //STOP STN
  425. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  426. case 0x09:
  427. action.HangupTask("CHK") //STOP CHK
  428. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  429. case 0x0a:
  430. action.HangupTask("VOL") //STOP VOL
  431. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  432. default:
  433. action.InterruptRunningTask("")
  434. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  435. }
  436. }
  437. // DCS 语音
  438. func DcsAnn(data []byte) {
  439. delay := data[8]
  440. cycleCount := data[9]
  441. datalen := int(data[10])
  442. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  443. if int(cycleCount) == 255 {
  444. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
  445. } else {
  446. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
  447. }
  448. }
  449. // tone-test广播
  450. func ToneTest(data []byte) {
  451. check := data[8]
  452. delay := data[9]
  453. cycleCount := data[10]
  454. datalen := int(data[11])
  455. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  456. switch check {
  457. case 0x01: //start
  458. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
  459. case 0x02: //stop
  460. action.HangupAllExcept("")
  461. }
  462. }
  463. // 自检广播
  464. func SelfCheck(data []byte) {
  465. check := data[8]
  466. delay := data[9]
  467. cycleCount := data[10]
  468. //cycleCount := 0x32
  469. datalen := int(data[11])
  470. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  471. switch check {
  472. case 0x01: //start
  473. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
  474. case 0x02: //stop
  475. action.HangupAllExcept("")
  476. }
  477. }
  478. // 全局变量:记录正在抑制的 exten
  479. var (
  480. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  481. //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  482. )
  483. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  484. func suppressKey(exten string, handler byte) string {
  485. return fmt.Sprintf("%s_h%x", exten, handler)
  486. }
  487. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  488. func AlarmHandleICP(data []byte) {
  489. handler := data[8]
  490. carr := data[12]
  491. pos := data[13]
  492. exten := fmt.Sprintf("24%c%c", carr, pos)
  493. key := suppressKey(exten, handler)
  494. //Drop other handler in 2 sec
  495. //PACUs---call---->ICP1
  496. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  497. if handler == 0x01 {
  498. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  499. utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD:%s within 4 seconds", exten)
  500. return
  501. }
  502. time.AfterFunc(4*time.Second, func() {
  503. suppressedExts.Delete(key)
  504. utils.LoggerDebug.Printf("Suppression released for key: %s", key)
  505. })
  506. }
  507. switch handler {
  508. case 0x01: //answer(ICP+Alarm+PACU)
  509. //NotifyPaiu(exten, "answer")
  510. priority.ICPAnswer = 1
  511. if priority.PADStart == 0 {
  512. alstatus.PaStatus("", "PAD", "start")
  513. priority.PADStart = 1
  514. }
  515. utils.LoggerDebug.Printf("ICP Answer PAD:%s .", exten)
  516. if active.ActivedCab == "1" {
  517. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  518. //goto ami event ConfbridgeJoin, ICP answer PAD
  519. } else if active.ActivedCab == "8" {
  520. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  521. //goto ami event ConfbridgeJoin, ICP answer PAD
  522. } else if active.ActivedCab == "" { // No cab occupied
  523. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  524. }
  525. case 0x02: //hold 重新放回队列里面
  526. utils.LoggerDebug.Printf("ICP Hold PAD-ICP PAD:%s !", exten)
  527. active.NotifyPaiu(exten, "hold")
  528. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  529. if err != nil {
  530. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  531. }
  532. action.InterruptRunningTask("")
  533. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  534. //action.HangupICP()
  535. case 0x03: //hangup
  536. //NotifyPaiu(exten, "hangup")
  537. utils.LoggerDebug.Printf("STC Hangup PAD-ICP !")
  538. action.Hangup(exten) //Pad
  539. //action.HangupICP()
  540. action.HangupTask("PAD-ICP")
  541. }
  542. }
  543. // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
  544. func AlarmHandleTMS(data []byte) {
  545. handler := data[8]
  546. //extlen := data[9]
  547. carr := data[12]
  548. pos := data[13]
  549. exten := fmt.Sprintf("24%c%c", carr, pos)
  550. PacuNum := fmt.Sprintf("21%c1", carr)
  551. key := suppressKey(exten, handler)
  552. //Drop other handler in 2 sec
  553. // 只对 handler == 0x01 做 2 秒去重
  554. if handler == 0x01 {
  555. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  556. utils.LoggerDebug.Printf("Suppressed duplicate ICP Alarm (handler=0x01) for PAD:%s within 4 seconds", exten)
  557. return // 已存在,说明在2秒窗口期内,直接丢弃
  558. }
  559. // 设置4秒后删除该 key,允许下次通过
  560. time.AfterFunc(4*time.Second, func() {
  561. suppressedExts.Delete(key)
  562. utils.LoggerDebug.Printf("Suppression released for key: %s", key)
  563. })
  564. }
  565. switch handler {
  566. case 0x01: //answer(ICP+Alarm+PACU)
  567. //PACU---call---->ICP1
  568. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  569. if priority.PADStart == 0 {
  570. alstatus.PaStatus("", "PAD", "start")
  571. priority.PADStart = 1
  572. }
  573. priority.ICPAnswer = 1
  574. utils.LoggerDebug.Printf("TMS Answer PAD:%s PACU:%s", exten, PacuNum)
  575. if action.ExtenStatus(PacuNum) == "Idle" {
  576. if active.ActivedCab == "1" {
  577. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  578. //goto ami event BridgeEnter, ICP8 whisper ICP1
  579. } else if active.ActivedCab == "8" {
  580. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
  581. //goto ami event BridgeEnter, ICP1 whisper ICP8
  582. } else if active.ActivedCab == "" { // No cab occupied
  583. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  584. }
  585. } else {
  586. action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
  587. }
  588. case 0x02: //hold 重新放回队列里面
  589. utils.LoggerDebug.Printf("ICP Hold PAD-TMS PAD:%s !", exten)
  590. active.NotifyPaiu(exten, "hold")
  591. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  592. if err != nil {
  593. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  594. }
  595. action.HangupAllLocalChan()
  596. case 0x03: //hangup
  597. //NotifyPaiu(exten, "hangup")
  598. utils.LoggerDebug.Printf("STC Hangup PAD-TMS !")
  599. action.Hangup(exten) //Pad
  600. action.HangupTask("PAD-TMS")
  601. //action.HangupTask("PAD-ICP")
  602. }
  603. }
  604. // 挂断所有报警器
  605. func AlarmHoldResetAll(handler byte) {
  606. //hangup all actived PAD
  607. action.HangupAllPAD()
  608. //hangup running task
  609. action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
  610. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  611. }