stc-broadcast.go 19 KB

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