stc-broadcast.go 24 KB

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