stc-broadcast.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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("=====active======cab=%s Master=%d=====data:%x======", active.CabNum, master, data)
  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. } else if master == 1 && active.CabNum == "8" {
  327. active.Master = false
  328. }
  329. //Set train info
  330. if TrainInfo != 0 {
  331. //Get train devide info
  332. DevideInfo := TrainInfo & 0x20
  333. if DevideInfo == 0x20 {
  334. active.TrainDevide = 1
  335. active.Master = true //列车断开,设置两边都Master
  336. if !utils.CheckAsterisk() {
  337. utils.ExecCmd("/usr/sbin/asterisk")
  338. }
  339. } else {
  340. active.TrainDevide = 0
  341. }
  342. //Radio fault
  343. RadioFault1 := TrainInfo & 0x03
  344. RadioFault8 := TrainInfo & 0x0c
  345. if RadioFault1 == 0 && RadioFault8 == 1 {
  346. active.RadioFault = 1
  347. } else {
  348. active.RadioFault = 0
  349. }
  350. lfshook.NewLogger().Logger.Infof("==RadioFault1:%x==RadioFault1:%x=DevideInfo:%x", RadioFault1, RadioFault8, DevideInfo)
  351. }
  352. switch Signal {
  353. case 0:
  354. if active.ActivedCab != "" {
  355. active.ActivedCab = ""
  356. action.InActiveHangup()
  357. }
  358. case 1:
  359. //active signal from 8 to 1
  360. if active.ActivedCab == "8" || active.ActivedCab == "" {
  361. active.ActivedCab = "1"
  362. action.InActiveHangup()
  363. }
  364. case 8:
  365. //active signal from 1 to 8
  366. if active.ActivedCab == "1" || active.ActivedCab == "" {
  367. active.ActivedCab = "8"
  368. action.InActiveHangup()
  369. }
  370. }
  371. }
  372. // SPC ,特殊服务消息广播
  373. func SpecialAnn(data []byte) {
  374. delay := data[8]
  375. cycleCount := data[9]
  376. datalen := int(data[10])
  377. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  378. if int(cycleCount) == 255 {
  379. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
  380. } else {
  381. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
  382. }
  383. }
  384. // EMG ,紧急服务消息广播
  385. func EmgMsg(data []byte) {
  386. delay := data[8]
  387. cycleCount := data[9]
  388. datalen := int(data[10])
  389. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  390. if int(cycleCount) == 255 {
  391. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
  392. } else {
  393. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
  394. }
  395. }
  396. // 停止指定类型广播
  397. func AnnStop(data [4]byte) {
  398. //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
  399. switch data[0] {
  400. case 0x03:
  401. action.HangupTask("DCS") //STOP DCS
  402. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  403. case 0x04:
  404. action.HangupTask("EMG") //STOP EMG
  405. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  406. case 0x07:
  407. action.HangupTask("SPC") //STOP SPC
  408. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  409. case 0x08:
  410. action.HangupTask("STN") //STOP STN
  411. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  412. case 0x09:
  413. action.HangupTask("CHK") //STOP CHK
  414. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  415. case 0x0a:
  416. action.HangupTask("VOL") //STOP VOL
  417. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  418. default:
  419. action.InterruptRunningTask("")
  420. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  421. }
  422. }
  423. // DCS 语音
  424. func DcsAnn(data []byte) {
  425. delay := data[8]
  426. cycleCount := data[9]
  427. datalen := int(data[10])
  428. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  429. if int(cycleCount) == 255 {
  430. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
  431. } else {
  432. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
  433. }
  434. }
  435. // tone-test广播
  436. func ToneTest(data []byte) {
  437. check := data[8]
  438. delay := data[9]
  439. cycleCount := data[10]
  440. datalen := int(data[11])
  441. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  442. switch check {
  443. case 0x01: //start
  444. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
  445. case 0x02: //stop
  446. action.HangupAllExcept("")
  447. }
  448. }
  449. // 自检广播
  450. func SelfCheck(data []byte) {
  451. check := data[8]
  452. delay := data[9]
  453. cycleCount := data[10]
  454. //cycleCount := 0x32
  455. datalen := int(data[11])
  456. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  457. switch check {
  458. case 0x01: //start
  459. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
  460. case 0x02: //stop
  461. action.HangupAllExcept("")
  462. }
  463. }
  464. // 全局变量:记录正在抑制的 exten
  465. var (
  466. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  467. //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  468. )
  469. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  470. func suppressKey(exten string, handler byte) string {
  471. return fmt.Sprintf("%s_h%x", exten, handler)
  472. }
  473. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  474. func AlarmHandleICP(data []byte) {
  475. handler := data[8]
  476. carr := data[12]
  477. pos := data[13]
  478. exten := fmt.Sprintf("24%c%c", carr, pos)
  479. key := suppressKey(exten, handler)
  480. //Drop other handler in 2 sec
  481. //PACUs---call---->ICP1
  482. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  483. if handler == 0x01 {
  484. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  485. lfshook.NewLogger().Logger.Infof("Suppressed duplicate ICP Alarm (handler=0x01) for exten: %s within 4 seconds", exten)
  486. return
  487. }
  488. time.AfterFunc(4*time.Second, func() {
  489. suppressedExts.Delete(key)
  490. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  491. })
  492. }
  493. switch handler {
  494. case 0x01: //answer(ICP+Alarm+PACU)
  495. //NotifyPaiu(exten, "answer")
  496. priority.ICPAnswer = 1
  497. if priority.PADStart == 0 {
  498. alstatus.PaStatus("", "PAD", "start")
  499. priority.PADStart = 1
  500. }
  501. lfshook.NewLogger().Logger.Infof("================ ICP Answer PAD ================:%s ", exten)
  502. if active.ActivedCab == "1" {
  503. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  504. //goto ami event ConfbridgeJoin, ICP answer PAD
  505. } else if active.ActivedCab == "8" {
  506. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  507. //goto ami event ConfbridgeJoin, ICP answer PAD
  508. } else if active.ActivedCab == "" { // No cab occupied
  509. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  510. }
  511. case 0x02: //hold 重新放回队列里面
  512. active.NotifyPaiu(exten, "hold")
  513. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  514. if err != nil {
  515. lfshook.NewLogger().Info(err)
  516. }
  517. action.InterruptRunningTask("")
  518. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  519. //action.HangupICP()
  520. case 0x03: //hangup
  521. //NotifyPaiu(exten, "hangup")
  522. lfshook.NewLogger().Logger.Infof("============ Hangup PAD-ICP =============== ")
  523. action.Hangup(exten) //Pad
  524. //action.HangupICP()
  525. action.HangupTask("PAD-ICP")
  526. }
  527. }
  528. // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
  529. func AlarmHandleTMS(data []byte) {
  530. handler := data[8]
  531. //extlen := data[9]
  532. carr := data[12]
  533. pos := data[13]
  534. exten := fmt.Sprintf("24%c%c", carr, pos)
  535. PacuNum := fmt.Sprintf("21%c1", carr)
  536. key := suppressKey(exten, handler)
  537. //Drop other handler in 2 sec
  538. // 只对 handler == 0x01 做 2 秒去重
  539. if handler == 0x01 {
  540. if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
  541. lfshook.NewLogger().Logger.Infof("Suppressed duplicate ICP Alarm (handler=0x01) for exten: %s within 4 seconds", exten)
  542. return // 已存在,说明在2秒窗口期内,直接丢弃
  543. }
  544. // 设置4秒后删除该 key,允许下次通过
  545. time.AfterFunc(4*time.Second, func() {
  546. suppressedExts.Delete(key)
  547. lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
  548. })
  549. }
  550. switch handler {
  551. case 0x01: //answer(ICP+Alarm+PACU)
  552. //PACU---call---->ICP1
  553. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  554. if priority.PADStart == 0 {
  555. alstatus.PaStatus("", "PAD", "start")
  556. priority.PADStart = 1
  557. }
  558. priority.ICPAnswer = 1
  559. lfshook.NewLogger().Logger.Infof("==============TMS Answer PAD Exten:%s PACU:%s==========", exten, PacuNum)
  560. if action.ExtenStatus(PacuNum) == "Idle" {
  561. if active.ActivedCab == "1" {
  562. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  563. //goto ami event BridgeEnter, ICP8 whisper ICP1
  564. } else if active.ActivedCab == "8" {
  565. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
  566. //goto ami event BridgeEnter, ICP1 whisper ICP8
  567. } else if active.ActivedCab == "" { // No cab occupied
  568. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  569. }
  570. } else {
  571. action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
  572. }
  573. case 0x02: //hold 重新放回队列里面
  574. active.NotifyPaiu(exten, "hold")
  575. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  576. if err != nil {
  577. lfshook.NewLogger().Info(err)
  578. }
  579. action.HangupAllLocalChan()
  580. case 0x03: //hangup
  581. //NotifyPaiu(exten, "hangup")
  582. action.Hangup(exten) //Pad
  583. action.HangupTask("PAD-TMS")
  584. //action.HangupTask("PAD-ICP")
  585. }
  586. }
  587. // 挂断所有报警器
  588. func AlarmHoldResetAll(handler byte) {
  589. //hangup all actived PAD
  590. action.HangupAllPAD()
  591. //hangup running task
  592. action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
  593. time.Sleep(time.Millisecond * 100) //wait endpoimt release
  594. }