stc-broadcast.go 23 KB

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