index.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package app
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "pbx-api-gin/internal/app/ami/action"
  6. "pbx-api-gin/internal/app/stc"
  7. "pbx-api-gin/internal/app/stc/active"
  8. "pbx-api-gin/internal/app/stc/priority"
  9. "pbx-api-gin/internal/app/stc/socket"
  10. "pbx-api-gin/pkg/lfshook"
  11. "pbx-api-gin/pkg/utils"
  12. "strings"
  13. "time"
  14. )
  15. func StartApp() {
  16. //init mysql
  17. //mysql.CreateDBInstance()
  18. //Get cab number acording to IP
  19. IP, err := getIPByCommand()
  20. if err != nil {
  21. lfshook.NewLogger().Infof("Get IP err :%+v", err)
  22. }
  23. if IP[len(IP)-2:] == "81" {
  24. active.CabNum = "8"
  25. } else {
  26. active.CabNum = "1"
  27. }
  28. lfshook.NewLogger().Infof("=================cab number:%s===========", active.CabNum)
  29. //init the active status
  30. active.Actived = true
  31. //get priority
  32. priority.GetPriority()
  33. // 启动带有重连机制的连接管理协程MC1
  34. go stc.StartStcConnection(socket.Conn, "1")
  35. // 启动带有重连机制的连接管理协程MC8
  36. go stc.StartStcConnection(socket.Conn8, "8")
  37. // 启动其他服务...
  38. // 启动 AMI
  39. go func() {
  40. action.StartAMI(func() {
  41. lfshook.NewLogger().Info("ami callback")
  42. }, []func(event map[string]string){})
  43. }()
  44. //refresh extension status
  45. time.Sleep(3 * time.Second)
  46. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "reload")
  47. }
  48. // Get eth0 IP
  49. func getIPByCommand() (string, error) {
  50. cmd := "ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2"
  51. out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
  52. if err != nil {
  53. return "", err
  54. }
  55. ip := strings.TrimSpace(string(out))
  56. if ip == "" {
  57. return "", fmt.Errorf("no IP address found")
  58. }
  59. return ip, nil
  60. }