index.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package app
  2. import (
  3. "os/exec"
  4. "pbx-api-gin/internal/app/ami/action"
  5. "pbx-api-gin/internal/app/stc"
  6. "pbx-api-gin/internal/app/stc/active"
  7. "pbx-api-gin/internal/app/stc/priority"
  8. "pbx-api-gin/internal/app/stc/socket"
  9. "pbx-api-gin/pkg/lfshook"
  10. "pbx-api-gin/pkg/utils"
  11. "time"
  12. )
  13. func StartApp() {
  14. //init mysql
  15. //mysql.CreateDBInstance()
  16. //Get cab number acording to IP
  17. _, err := SetMasterCabNum()
  18. if err != nil {
  19. lfshook.NewLogger().Infof("Set Role and Cab Num err :%+v", err)
  20. }
  21. lfshook.NewLogger().Infof("=================cab number:%s========Master:%+v===", active.CabNum, active.Master)
  22. //init the active status
  23. active.ActivedCab = ""
  24. //get priority
  25. priority.GetPriority()
  26. // 启动带有重连机制的连接管理协程MC1
  27. go stc.StartStcConnection(socket.Conn, "1")
  28. // 启动带有重连机制的连接管理协程MC8
  29. go stc.StartStcConnection(socket.Conn8, "8")
  30. //启动连接到Master服务器,检查Master是否在线
  31. if active.CabNum == "8" {
  32. socket.ConnectedMaster = false
  33. go stc.StartConnectionToSipServer(socket.ConnToMaster)
  34. }
  35. // 启动其他服务...
  36. // 启动 AMI
  37. go func() {
  38. action.StartAMI(func() {
  39. lfshook.NewLogger().Info("ami callback")
  40. }, []func(event map[string]string){})
  41. }()
  42. //refresh extension status
  43. time.Sleep(3 * time.Second)
  44. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "reload")
  45. }
  46. // Get eth0 IP
  47. func SetMasterCabNum() (string, error) {
  48. cmd := "ip a |grep 10.0.11.11" //check Master IP
  49. out, _ := exec.Command("bash", "-c", cmd).CombinedOutput()
  50. // Init cab number and master role
  51. if len(out) == 0 {
  52. active.Master = false
  53. active.CabNum = "8"
  54. } else {
  55. active.Master = true
  56. active.CabNum = "1"
  57. }
  58. return string(out), nil
  59. }