stc-broadcast.go 20 KB

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