stc-broadcast.go 19 KB

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