stc-broadcast.go 22 KB

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