stc-broadcast.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. utils.LoggerDebug.Printf("STN : Clean priority.TaskCreating = '' !")
  160. priority.TaskCreating = ""
  161. }
  162. alstatus.PaStatus("", "STN", "refuse")
  163. }
  164. }
  165. time.Sleep(1 * time.Second)
  166. if priority.TaskCreating == "STN" {
  167. utils.LoggerDebug.Printf("STN : Clean priority.TaskCreating = '' !")
  168. priority.TaskCreating = ""
  169. }
  170. case 0x05: // SPC
  171. if active.ActivedCab != "" {
  172. //检查是否有任务正在创建
  173. action.WaitTaskCreate("SPC")
  174. if priority.CheckPriority("SPC") {
  175. runningTaskName := action.InterruptRunningTask("SPC") //SPC interrupt other
  176. if runningTaskName != "" {
  177. time.Sleep(time.Millisecond * 100) //wait endpoint release
  178. }
  179. SpecialAnn(packet)
  180. } else {
  181. if priority.TaskCreating == "SPC" {
  182. utils.LoggerDebug.Printf("SPC : Clean priority.TaskCreating = '' !")
  183. priority.TaskCreating = ""
  184. }
  185. alstatus.PaStatus("", "SPC", "refuse")
  186. }
  187. }
  188. time.Sleep(1 * time.Second)
  189. if priority.TaskCreating == "SPC" {
  190. utils.LoggerDebug.Printf("SPC : Clean priority.TaskCreating = '' !")
  191. priority.TaskCreating = ""
  192. }
  193. case 0x06: // EMG
  194. if active.ActivedCab != "" {
  195. //检查是否有任务正在创建
  196. action.WaitTaskCreate("EMG")
  197. if priority.CheckPriority("EMG") {
  198. runningTaskName := action.InterruptRunningTask("EMG") //EMG interrupt other
  199. if runningTaskName != "" {
  200. time.Sleep(time.Millisecond * 100) //wait endpoint release
  201. }
  202. EmgMsg(packet)
  203. } else {
  204. if priority.TaskCreating == "EMG" {
  205. utils.LoggerDebug.Printf("EMG : Clean priority.TaskCreating = '' !")
  206. priority.TaskCreating = ""
  207. }
  208. alstatus.PaStatus("", "EMG", "refuse")
  209. }
  210. }
  211. time.Sleep(1 * time.Second)
  212. if priority.TaskCreating == "EMG" {
  213. utils.LoggerDebug.Printf("EMG : Clean priority.TaskCreating = '' !")
  214. priority.TaskCreating = ""
  215. }
  216. case 0x07: // STOP
  217. AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
  218. case 0x08: // DCS
  219. if active.ActivedCab != "" {
  220. //检查是否有任务正在创建
  221. action.WaitTaskCreate("DCS")
  222. if priority.CheckPriority("DCS") {
  223. runningTaskName := action.InterruptRunningTask("DCS") //DCS interrupt other
  224. if runningTaskName != "" {
  225. time.Sleep(time.Millisecond * 100) //wait endpoint release
  226. }
  227. DcsAnn(packet)
  228. } else {
  229. if priority.TaskCreating == "DCS" {
  230. utils.LoggerDebug.Printf("DCS : Clean priority.TaskCreating = '' !")
  231. priority.TaskCreating = ""
  232. }
  233. alstatus.PaStatus("", "DCS", "refuse")
  234. }
  235. time.Sleep(1 * time.Second)
  236. if priority.TaskCreating == "DCS" {
  237. utils.LoggerDebug.Printf("DCS : Clean priority.TaskCreating = '' !")
  238. priority.TaskCreating = ""
  239. }
  240. }
  241. case 0x09: // SELF CHECK
  242. if active.ActivedCab != "" {
  243. //检查是否有任务正在创建
  244. action.WaitTaskCreate("CHK")
  245. if priority.CheckPriority("CHK") {
  246. runningTaskName := action.InterruptRunningTask("CHK") //CHK interrupt other
  247. if runningTaskName != "" {
  248. time.Sleep(time.Millisecond * 100) //wait endpoint release
  249. }
  250. SelfCheck(packet)
  251. } else {
  252. if priority.TaskCreating == "CHK" {
  253. utils.LoggerDebug.Printf("CHK : Clean priority.TaskCreating = '' !")
  254. priority.TaskCreating = ""
  255. }
  256. alstatus.PaStatus("", "CHK", "refuse")
  257. }
  258. }
  259. time.Sleep(1 * time.Second)
  260. if priority.TaskCreating == "CHK" {
  261. utils.LoggerDebug.Printf("CHK : Clean priority.TaskCreating = '' !")
  262. priority.TaskCreating = ""
  263. }
  264. case 0x0a: // Tone-test
  265. if active.ActivedCab != "" {
  266. //检查是否有任务正在创建
  267. action.WaitTaskCreate("VOL")
  268. if priority.CheckPriority("VOL") {
  269. runningTaskName := action.InterruptRunningTask("VOL") //VOL interrupt other
  270. if runningTaskName != "" {
  271. time.Sleep(time.Millisecond * 100) //wait endpoint release
  272. }
  273. ToneTest(packet)
  274. } else {
  275. if priority.TaskCreating == "VOL" {
  276. utils.LoggerDebug.Printf("VOL : Clean priority.TaskCreating = '' !")
  277. priority.TaskCreating = ""
  278. }
  279. alstatus.PaStatus("", "VOL", "refuse")
  280. }
  281. }
  282. time.Sleep(1 * time.Second)
  283. if priority.TaskCreating == "VOL" {
  284. utils.LoggerDebug.Printf("VOL : Clean priority.TaskCreating = '' !")
  285. priority.TaskCreating = ""
  286. }
  287. case 0x0e: //TMS answer PAD
  288. handler := packet[8]
  289. //Drop other handler in 2 sec
  290. //PACUs---call---->ICP1
  291. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  292. if handler == 0x01 { //answer PAD
  293. //检查是否有任务正在创建
  294. action.WaitTaskCreate("PAD-TMS")
  295. //if packet[8] == 0x01 { //answer PAD
  296. if priority.CheckPriority("PAD-TMS") {
  297. //Before Answer PAD
  298. //if packet[8] == 0x01 {
  299. runningTaskName := action.InterruptRunningTask("PAD-TMS") //PAD-TMS interrupt other
  300. if runningTaskName != "" {
  301. time.Sleep(time.Millisecond * 100) //wait endpoint release
  302. }
  303. //}
  304. if active.QueueTimer != nil {
  305. if active.QueueTimer.Stop() {
  306. utils.LoggerDebug.Printf("Stop PAD timer true !")
  307. } else {
  308. utils.LoggerDebug.Printf("Stop PAD timer false !")
  309. }
  310. }
  311. AlarmHandleTMS(packet)
  312. } else {
  313. if priority.TaskCreating == "PAD-TMS" {
  314. utils.LoggerDebug.Printf("PAD-TMS : Clean priority.TaskCreating = '' !")
  315. priority.TaskCreating = ""
  316. }
  317. alstatus.PaStatus("", "PAD-TMS", "refuse")
  318. }
  319. } else { //hangup + hold
  320. AlarmHandleTMS(packet)
  321. }
  322. time.Sleep(1 * time.Second)
  323. if priority.TaskCreating == "PAD-TMS" {
  324. utils.LoggerDebug.Printf("PAD-TMS : Clean priority.TaskCreating = '' !")
  325. priority.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 PADTimeout 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. //Drop other handler in 2 sec
  336. //PACUs---call---->ICP1
  337. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  338. if handler == 0x01 {
  339. //检查是否有任务正在创建
  340. action.WaitTaskCreate("PAD-ICP")
  341. //if packet[8] == 0x01 { //answer PAD
  342. if priority.CheckPriority("PAD-ICP") {
  343. //Before Answer PAD
  344. //if packet[8] == 0x01 {
  345. runningTaskName := action.InterruptRunningTask("PAD-ICP") //PAD-ICP interrupt other
  346. if runningTaskName != "" {
  347. time.Sleep(time.Millisecond * 100) //wait endpoint release
  348. }
  349. //}
  350. if active.QueueTimer != nil {
  351. utils.LoggerDebug.Printf("PAD Timeout timer != nil !")
  352. if active.QueueTimer.Stop() {
  353. utils.LoggerDebug.Printf("Stop PAD timer true !")
  354. } else {
  355. utils.LoggerDebug.Printf("Stop PAD timer false !")
  356. }
  357. }
  358. AlarmHandleICP(packet)
  359. } else {
  360. if priority.TaskCreating == "PAD-ICP" {
  361. utils.LoggerDebug.Printf("PAD-ICP : Clean priority.TaskCreating = '' !")
  362. priority.TaskCreating = ""
  363. }
  364. alstatus.PaStatus("", "PAD-ICP", "refuse")
  365. }
  366. } else { //hangup + hold
  367. AlarmHandleICP(packet)
  368. }
  369. time.Sleep(1 * time.Second)
  370. if priority.TaskCreating == "PAD-ICP" {
  371. utils.LoggerDebug.Printf("PAD-ICP : Clean priority.TaskCreating = '' !")
  372. priority.TaskCreating = ""
  373. }
  374. //case 0xf1: //Set remote master
  375. //default:
  376. //fmt.Printf("Unknown command: %x\n", packet[5])
  377. }
  378. }
  379. func PadTimeOutSetting(data []byte) {
  380. Seconds := data[0]
  381. if Seconds != 0 {
  382. active.PADTimeout = int(Seconds)
  383. //lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", active.PADTimeout)
  384. }
  385. }
  386. // STN , 自动报站广播
  387. func StationAnn(data []byte) (err error) {
  388. specialVoice := int(data[8])
  389. delay := data[9]
  390. cycleCount := data[10]
  391. datalen := int(data[11])
  392. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  393. //set spc voice tag
  394. priority.SpecialVoice = specialVoice
  395. utils.LoggerDebug.Printf("Type:STN FileName:%x Count:%x SpecialVoice:%+v Interval:%+v", filename, cycleCount, specialVoice, delay)
  396. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "STN")
  397. return nil
  398. }
  399. // 激活信号
  400. func Active(data [3]byte) {
  401. //var info model.Sysinfo
  402. Signal := int(data[0])
  403. master := int(data[1])
  404. TrainInfo := int(data[2])
  405. lfshook.NewLogger().Logger.Infof("=====active:%x======cab=%s Master=%d=====data:%x======ActivedCabDelay:%x", Signal, active.CabNum, master, data, active.ActivedCabDelay)
  406. //Set Master
  407. if master == 1 && active.CabNum == "1" {
  408. active.Master = true
  409. if !utils.CheckAsterisk() {
  410. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  411. cmd := exec.Command("service", "asterisk", "start")
  412. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  413. err := cmd.Run()
  414. if err != nil {
  415. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  416. return
  417. }
  418. }
  419. } else if master == 8 && active.CabNum == "8" {
  420. active.Master = true
  421. if !utils.CheckAsterisk() {
  422. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  423. cmd := exec.Command("service", "asterisk", "start")
  424. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  425. err := cmd.Run()
  426. if err != nil {
  427. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  428. return
  429. }
  430. }
  431. } else if master == 8 && active.CabNum == "1" {
  432. if utils.CheckAsterisk() {
  433. lfshook.NewLogger().Infof("Check asterisk , if running slave, run cmd service asterisk stop !")
  434. cmd := exec.Command("service", "asterisk", "stop")
  435. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  436. err := cmd.Run()
  437. if err != nil {
  438. lfshook.NewLogger().Infof("Failed to stop asterisk: %v", err)
  439. return
  440. }
  441. }
  442. } else if master == 1 && active.CabNum == "8" {
  443. active.Master = false
  444. }
  445. //Set train info
  446. if TrainInfo != 0 {
  447. //Get train devide info
  448. DevideInfo := TrainInfo & 0x20
  449. if DevideInfo == 0x20 {
  450. active.TrainDevide = 1
  451. active.Master = true //列车断开,设置两边都Master
  452. if !utils.CheckAsterisk() {
  453. lfshook.NewLogger().Infof("Check asterisk , if not running , run cmd service asterisk start !")
  454. cmd := exec.Command("service", "asterisk", "start")
  455. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  456. err := cmd.Run()
  457. if err != nil {
  458. lfshook.NewLogger().Infof("Failed to start asterisk: %v", err)
  459. return
  460. }
  461. }
  462. } else {
  463. active.TrainDevide = 0
  464. }
  465. //Radio fault
  466. RadioFault1 := TrainInfo & 0x03
  467. RadioFault8 := TrainInfo & 0x0c
  468. if RadioFault1 == 0 && RadioFault8 == 1 {
  469. active.RadioFault = 1
  470. } else {
  471. active.RadioFault = 0
  472. }
  473. utils.LoggerDebug.Printf("RadioFault1:%x RadioFault1:%x DevideInfo:%x", RadioFault1, RadioFault8, DevideInfo)
  474. }
  475. switch Signal {
  476. case 0:
  477. if active.ActivedCab != "" {
  478. active.ActivedCab = ""
  479. action.InActiveHangup()
  480. }
  481. case 1:
  482. //active signal from 8 to 1
  483. if active.ActivedCab == "8" || active.ActivedCab == "" {
  484. active.ActivedCab = "1"
  485. active.ActivedCabDelay = "1"
  486. action.InActiveHangup()
  487. }
  488. case 8:
  489. //active signal from 1 to 8
  490. if active.ActivedCab == "1" || active.ActivedCab == "" {
  491. active.ActivedCab = "8"
  492. active.ActivedCabDelay = "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. utils.LoggerDebug.Printf("Type:SPC FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
  504. if int(cycleCount) == 255 {
  505. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "SPC")
  506. } else {
  507. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "SPC")
  508. }
  509. }
  510. // EMG ,紧急服务消息广播
  511. func EmgMsg(data []byte) {
  512. delay := data[8]
  513. cycleCount := data[9]
  514. datalen := int(data[10])
  515. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  516. utils.LoggerDebug.Printf("Type:EMG FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
  517. if int(cycleCount) == 255 {
  518. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "EMG")
  519. } else {
  520. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "EMG")
  521. }
  522. }
  523. // 停止指定类型广播
  524. func AnnStop(data [4]byte) {
  525. //lfshook.NewLogger().Logger.Infof("=========AnnStop Type %x", data[0])
  526. utils.LoggerDebug.Printf("Stop PA Type:%x (DCS=3,EMG=4,SPC=7,STN=8,SelfCheck=9,ToneTest=10)", data[0])
  527. switch data[0] {
  528. case 0x03:
  529. action.HangupTask("DCS") //STOP DCS
  530. time.Sleep(time.Millisecond * 100) //wait endpoint release
  531. case 0x04:
  532. action.HangupTask("EMG") //STOP EMG
  533. time.Sleep(time.Millisecond * 100) //wait endpoint release
  534. case 0x07:
  535. action.HangupTask("SPC") //STOP SPC
  536. time.Sleep(time.Millisecond * 100) //wait endpoint release
  537. case 0x08:
  538. action.HangupTask("STN") //STOP STN
  539. time.Sleep(time.Millisecond * 100) //wait endpoint release
  540. case 0x09:
  541. action.HangupTask("CHK") //STOP CHK
  542. time.Sleep(time.Millisecond * 100) //wait endpoint release
  543. case 0x0a:
  544. action.HangupTask("VOL") //STOP VOL
  545. time.Sleep(time.Millisecond * 100) //wait endpoint release
  546. default:
  547. action.InterruptRunningTask("")
  548. time.Sleep(time.Millisecond * 100) //wait endpoint release
  549. }
  550. }
  551. // DCS 语音
  552. func DcsAnn(data []byte) {
  553. delay := data[8]
  554. cycleCount := data[9]
  555. datalen := int(data[10])
  556. filename := msgdata.SubstrByRune(string(data[11:]), 0, datalen-4)
  557. utils.LoggerDebug.Printf("Type:DCS FileName:%x Count:%x Interval:%+v", filename, cycleCount, delay)
  558. if int(cycleCount) == 255 {
  559. action.PlaybackPacu(strconv.Quote(filename), 9999999, int(delay), "DCS")
  560. } else {
  561. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "DCS")
  562. }
  563. }
  564. // tone-test广播
  565. func ToneTest(data []byte) {
  566. check := data[8]
  567. delay := data[9]
  568. cycleCount := data[10]
  569. datalen := int(data[11])
  570. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  571. utils.LoggerDebug.Printf("Type:ToneTest FileName:%x Count:%x Interval:%+v Action:%x (0x01=start/0x02=stop)", filename, cycleCount, delay, check)
  572. switch check {
  573. case 0x01: //start
  574. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "VOL")
  575. case 0x02: //stop
  576. action.HangupAllExcept("")
  577. }
  578. }
  579. // 自检广播
  580. func SelfCheck(data []byte) {
  581. check := data[8]
  582. delay := data[9]
  583. cycleCount := data[10]
  584. //cycleCount := 0x32
  585. datalen := int(data[11])
  586. filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
  587. utils.LoggerDebug.Printf("Type:SelfCehck FileName:%x Count:%x Interval:%+v Action:%x (0x01=start/0x02=stop)", filename, cycleCount, delay, check)
  588. switch check {
  589. case 0x01: //start
  590. action.PlaybackPacu(strconv.Quote(filename), int(cycleCount), int(delay), "CHK")
  591. case 0x02: //stop
  592. action.HangupAllExcept("")
  593. }
  594. }
  595. // 全局变量:记录正在抑制的 exten
  596. var (
  597. suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
  598. //suppressionMu sync.Mutex // 保护初始化和清理操作(可选)
  599. )
  600. // suppressKey 生成用于抑制的 key(可以根据需求扩展)
  601. func suppressKey(exten string, handler byte) string {
  602. return fmt.Sprintf("%s_h%x", exten, handler)
  603. }
  604. // ICP操作乘客报警(根据激活信息判断转到1车还是8车================)
  605. func AlarmHandleICP(data []byte) {
  606. handler := data[8]
  607. carr := data[12]
  608. pos := data[13]
  609. exten := fmt.Sprintf("24%c%c", carr, pos)
  610. utils.LoggerDebug.Printf("ICP Handle PAD:%s Action:%x (answer=1,hold=2,hangup=3)", exten, handler)
  611. switch handler {
  612. case 0x01: //answer(ICP+Alarm+PACU)
  613. //NotifyPaiu(exten, "answer")
  614. priority.ICPAnswer = 1
  615. //if priority.PADStart == 0 {
  616. alstatus.PaStatus("", "PAD", "start")
  617. priority.InterruptedPad = ""
  618. // priority.PADStart = 1
  619. //}
  620. utils.LoggerDebug.Printf("ICP Answer PAD:%s .", exten)
  621. if active.ActivedCab == "1" {
  622. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  623. //goto ami event ConfbridgeJoin, ICP answer PAD
  624. } else if active.ActivedCab == "8" {
  625. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  626. //goto ami event ConfbridgeJoin, ICP answer PAD
  627. } else if active.ActivedCab == "" { // No cab occupied
  628. if active.ActivedCabDelay == "1" {
  629. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  630. } else if active.ActivedCabDelay == "8" {
  631. action.Dial("0402", "0511", "pad-rule-pacus", "ani8", exten, "8") // PACUs dial ICP8
  632. } else {
  633. action.Dial("0402", "0511", "pad-rule-pacus", "ani1", exten, "1") // PACUs dial ICP1
  634. }
  635. }
  636. case 0x02: //hold 重新放回队列里面
  637. utils.LoggerDebug.Printf("ICP Hold PAD-ICP PAD:%s !", exten)
  638. active.NotifyPaiu(exten, "hold")
  639. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  640. if err != nil {
  641. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  642. }
  643. action.InterruptRunningTask("")
  644. time.Sleep(time.Millisecond * 100) //wait endpoint release
  645. //action.HangupICP()
  646. case 0x03: //hangup
  647. //NotifyPaiu(exten, "hangup")
  648. utils.LoggerDebug.Printf("STC Hangup PAD-ICP !")
  649. action.Hangup(exten) //Pad
  650. //action.HangupICP()
  651. action.HangupTask("PAD-ICP")
  652. }
  653. }
  654. // TMS操作乘客报警(根据激活信息判断转到1车还是8车================)
  655. func AlarmHandleTMS(data []byte) {
  656. handler := data[8]
  657. //extlen := data[9]
  658. carr := data[12]
  659. pos := data[13]
  660. exten := fmt.Sprintf("24%c%c", carr, pos)
  661. PacuNum := fmt.Sprintf("21%c1", carr)
  662. utils.LoggerDebug.Printf("TMS Handle PAD:%s PACU:%s Action:%x (answer=1,hold=2,hangup=3)", exten, PacuNum, handler)
  663. switch handler {
  664. case 0x01: //answer(ICP+Alarm+PACU)
  665. //PACU---call---->ICP1
  666. //PAD---->Chanspy(WEq)-->ICP1;PAD--->Call---->ICP2
  667. //if priority.PADStart == 0 {
  668. alstatus.PaStatus("", "PAD", "start")
  669. priority.InterruptedPad = ""
  670. //priority.PADStart = 1
  671. //}
  672. priority.ICPAnswer = 1
  673. utils.LoggerDebug.Printf("TMS Answer PAD:%s PACU:%s", exten, PacuNum)
  674. if action.ExtenStatus(PacuNum) == "Idle" {
  675. if active.ActivedCab == "1" {
  676. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  677. //goto ami event BridgeEnter, ICP8 whisper ICP1
  678. } else if active.ActivedCab == "8" {
  679. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
  680. //goto ami event BridgeEnter, ICP1 whisper ICP8
  681. } else if active.ActivedCab == "" { // No cab occupied
  682. if active.ActivedCabDelay == "1" {
  683. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  684. } else if active.ActivedCabDelay == "8" {
  685. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "8") // PACU dial ICP8
  686. } else {
  687. action.Dial("0403", PacuNum, "pad-tms-dial-pacu", PacuNum, exten, "1") // PACU dial ICP1
  688. }
  689. }
  690. } else {
  691. action.RedirectInQueue(exten, "0405", "default", exten) // PAD dial ICPs
  692. }
  693. case 0x02: //hold 重新放回队列里面
  694. utils.LoggerDebug.Printf("ICP Hold PAD-TMS PAD:%s !", exten)
  695. active.NotifyPaiu(exten, "hold")
  696. err := action.RedirectInQueue(exten, "0300", "queues-icp-redirect", "1")
  697. if err != nil {
  698. utils.LoggerDebug.Printf("RedirectInQueue err:%+v", err)
  699. }
  700. action.HangupAllLocalChan()
  701. case 0x03: //hangup
  702. //NotifyPaiu(exten, "hangup")
  703. utils.LoggerDebug.Printf("STC Hangup PAD-TMS !")
  704. action.Hangup(exten) //Pad
  705. action.HangupTask("PAD-TMS")
  706. //action.HangupTask("PAD-ICP")
  707. }
  708. }
  709. // 挂断所有报警器
  710. func AlarmHoldResetAll(handler byte) {
  711. utils.LoggerDebug.Printf("Alarm Hold/Reset All !")
  712. //hangup all actived PAD
  713. action.HangupAllPAD()
  714. //hangup running task
  715. action.InterruptRunningTask("AlarmHoldResetAll") //Reset PAD ALL
  716. time.Sleep(time.Millisecond * 100) //wait endpoint release
  717. }