stc-broadcast.go 22 KB

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