stc-broadcast.go 22 KB

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