index.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package priority
  2. import (
  3. "os"
  4. "pbx-api-gin/pkg/lfshook"
  5. "strconv"
  6. "github.com/sirupsen/logrus"
  7. "gopkg.in/ini.v1"
  8. )
  9. var ICPAnswer = 0
  10. var OCCAnswer = 0
  11. var RunningTypePriority = 0
  12. var RunningType = ""
  13. var RunningPATaskChan = ""
  14. var TmpRunningTypePriority = 0
  15. var TmpRunningType = ""
  16. var SpecialVoice = 0
  17. var PADStart = 0
  18. var PADOccStart = 0
  19. var PAStart = 0
  20. var InterruptedPad = ""
  21. var filePath = "/etc/asterisk/priority.conf"
  22. type BroadcastResumeParas struct {
  23. FileName string `json:"filename"`
  24. Count int `json:"count"`
  25. Delay int `json:"delay"`
  26. BroadcastType string `json:"broadcastType"`
  27. }
  28. type PriorityAll struct {
  29. ManuPa string `json:"manualPa"`
  30. CabCab string `json:"cabCab"`
  31. PADICP string `json:"padIcp"`
  32. PADTMS string `json:"padTms"`
  33. PADOCC string `json:"padOcc"`
  34. CPA string `json:"cpa"`
  35. EMG string `json:"emg"`
  36. SPC string `json:"spc"`
  37. DCS string `json:"dcs"`
  38. STN string `json:"stn"`
  39. CHK string `json:"chk"`
  40. VOL string `json:"vol"`
  41. }
  42. var Priority PriorityAll
  43. func GetPriority() {
  44. _, err := os.Stat(filePath)
  45. if err != nil {
  46. logrus.Error(err)
  47. return
  48. }
  49. iniFile, err := ini.Load(filePath)
  50. if err != nil {
  51. logrus.Error(err)
  52. return
  53. }
  54. Priority.CHK = iniFile.Section("general").Key("CHK").Value()
  55. Priority.STN = iniFile.Section("general").Key("STN").Value()
  56. Priority.DCS = iniFile.Section("general").Key("DCS").Value()
  57. Priority.SPC = iniFile.Section("general").Key("SPC").Value()
  58. Priority.EMG = iniFile.Section("general").Key("EMG").Value()
  59. Priority.CPA = iniFile.Section("general").Key("CPA").Value()
  60. Priority.PADOCC = iniFile.Section("general").Key("PAD-OCC").Value()
  61. Priority.PADTMS = iniFile.Section("general").Key("PAD-TMS").Value()
  62. Priority.PADICP = iniFile.Section("general").Key("PAD-ICP").Value()
  63. Priority.CabCab = iniFile.Section("general").Key("CabCab").Value()
  64. Priority.ManuPa = iniFile.Section("general").Key("ManuPa").Value()
  65. Priority.VOL = iniFile.Section("general").Key("VOL").Value()
  66. }
  67. func GetPriorityByKey(key string) int {
  68. ret := ""
  69. switch key {
  70. case "VOL":
  71. ret = Priority.VOL
  72. case "CHK":
  73. ret = Priority.CHK
  74. case "STN":
  75. ret = Priority.STN
  76. case "DCS":
  77. ret = Priority.DCS
  78. case "SPC":
  79. ret = Priority.SPC
  80. case "EMG":
  81. ret = Priority.EMG
  82. case "CPA":
  83. ret = Priority.CPA
  84. case "PAD-OCC":
  85. ret = Priority.PADOCC
  86. case "PAD-TMS":
  87. ret = Priority.PADTMS
  88. case "PAD-ICP":
  89. ret = Priority.PADICP
  90. case "CabCab":
  91. ret = Priority.CabCab
  92. case "ManuPa":
  93. ret = Priority.ManuPa
  94. default:
  95. ret = ""
  96. }
  97. retPriority, err := strconv.Atoi(ret)
  98. if err != nil {
  99. return 0
  100. }
  101. return retPriority
  102. }
  103. // check priority , if the running priority is lowwer than the to run priority
  104. func CheckPriority(runType string) bool {
  105. //return true
  106. lfshook.NewLogger().Logger.Infof("=========Check Pri runType:%s====RunningTypePriority:%d====SpecialVoice:%d========", runType, RunningTypePriority, SpecialVoice)
  107. //Check special voice can not interrupt
  108. if SpecialVoice == 1 {
  109. return false
  110. }
  111. //No any runing task now
  112. if RunningTypePriority == 0 {
  113. return true
  114. }
  115. //Get the to run priority number in the config file
  116. ret := GetPriorityByKey(runType)
  117. //special same priority number task return true
  118. if ret == RunningTypePriority {
  119. switch runType {
  120. case "PAD-ICP":
  121. return true
  122. case "PAD-TMS":
  123. return true
  124. /* case "VOL":
  125. return true
  126. case "CHK":
  127. return true*/
  128. }
  129. }
  130. //if the running task priority is lowwer
  131. if ret < RunningTypePriority && ret != 0 {
  132. return true
  133. } else {
  134. //C2C run together with other task except ..
  135. if RunningType == "C2C" {
  136. if runType != "PA" && runType != "PAD-ICP" && runType != "PAD-TMS" && runType != "C2C" {
  137. return true
  138. }
  139. } else if runType == "C2C" {
  140. if RunningType != "PA" && RunningType != "PAD-ICP" && RunningType != "PAD-TMS" && RunningType != "C2C" {
  141. return true
  142. }
  143. }
  144. }
  145. return false
  146. }
  147. // Clean all priority tag
  148. func CleanPriorityTag() {
  149. lfshook.NewLogger().Logger.Infof("=========CleanPriorityTag===============")
  150. ICPAnswer = 0
  151. OCCAnswer = 0
  152. RunningTypePriority = 0
  153. RunningType = ""
  154. SpecialVoice = 0
  155. RunningPATaskChan = ""
  156. }