index.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 SpecialVoice = 0
  14. var RunningPATaskChan = ""
  15. type PriorityAll struct {
  16. ManuPa string `json:"manualPa"`
  17. CabCab string `json:"cabCab"`
  18. PADICP string `json:"padIcp"`
  19. PADTMS string `json:"padTms"`
  20. PADOCC string `json:"padOcc"`
  21. CPA string `json:"cpa"`
  22. EMG string `json:"emg"`
  23. SPC string `json:"spc"`
  24. DCS string `json:"dcs"`
  25. STN string `json:"stn"`
  26. CHK string `json:"chk"`
  27. VOL string `json:"vol"`
  28. }
  29. var Priority PriorityAll
  30. func GetPriority() {
  31. filePath := "/etc/asterisk/priority.conf"
  32. _, err := os.Stat(filePath)
  33. if err != nil {
  34. logrus.Error(err)
  35. return
  36. }
  37. iniFile, err := ini.Load(filePath)
  38. if err != nil {
  39. logrus.Error(err)
  40. return
  41. }
  42. Priority.CHK = iniFile.Section("general").Key("CHK").Value()
  43. Priority.STN = iniFile.Section("general").Key("STN").Value()
  44. Priority.DCS = iniFile.Section("general").Key("DCS").Value()
  45. Priority.SPC = iniFile.Section("general").Key("SPC").Value()
  46. Priority.EMG = iniFile.Section("general").Key("EMG").Value()
  47. Priority.CPA = iniFile.Section("general").Key("CPA").Value()
  48. Priority.PADOCC = iniFile.Section("general").Key("PAD-OCC").Value()
  49. Priority.PADTMS = iniFile.Section("general").Key("PAD-TMS").Value()
  50. Priority.PADICP = iniFile.Section("general").Key("PAD-ICP").Value()
  51. Priority.CabCab = iniFile.Section("general").Key("CabCab").Value()
  52. Priority.ManuPa = iniFile.Section("general").Key("ManuPa").Value()
  53. Priority.VOL = iniFile.Section("general").Key("VOL").Value()
  54. }
  55. func GetPriorityByKey(key string) string {
  56. filePath := "/etc/asterisk/priority.conf"
  57. _, err := os.Stat(filePath)
  58. if err != nil {
  59. logrus.Error(err)
  60. return ""
  61. }
  62. iniFile, err := ini.Load(filePath)
  63. if err != nil {
  64. logrus.Error(err)
  65. return ""
  66. }
  67. return iniFile.Section("general").Key(key).Value()
  68. }
  69. // check priority , if the running priority is lowwer than the to run priority
  70. func CheckPriority(runType string) bool {
  71. lfshook.NewLogger().Logger.Infof("=========Check Pri runType:%s====RunningTypePriority:%d============", runType, RunningTypePriority)
  72. //check special voice
  73. if SpecialVoice == 1 {
  74. return false
  75. }
  76. //no any runing task
  77. if RunningTypePriority == 0 {
  78. return true
  79. }
  80. //get the priority to run in the config file
  81. ret, err := strconv.Atoi(GetPriorityByKey(runType))
  82. if err != nil {
  83. logrus.Error(err)
  84. return false
  85. }
  86. lfshook.NewLogger().Logger.Infof("=========Check Pri GetPriorityByKey:%d============", ret)
  87. //if the running task priority is lowwer
  88. if ret < RunningTypePriority && ret != 0 {
  89. return true
  90. }
  91. return false
  92. }
  93. // Clean all priority tag
  94. func CleanPriorityTag() {
  95. ICPAnswer = 0
  96. OCCAnswer = 0
  97. RunningTypePriority = 0
  98. RunningType = ""
  99. SpecialVoice = 0
  100. RunningPATaskChan = ""
  101. }