index.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package app
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "pbx-api-gin/internal/app/ami/action"
  6. "pbx-api-gin/internal/app/mysql"
  7. "pbx-api-gin/internal/app/stc"
  8. "pbx-api-gin/internal/app/stc/active"
  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. // 启动带有重连机制的连接管理协程MC1
  30. go stc.StartStcConnection(socket.Conn, "1")
  31. // 启动带有重连机制的连接管理协程MC8
  32. go stc.StartStcConnection(socket.Conn, "8")
  33. // 启动其他服务...
  34. // 启动 AMI
  35. go func() {
  36. action.StartAMI(func() {
  37. lfshook.NewLogger().Info("ami callback")
  38. }, []func(event map[string]string){})
  39. }()
  40. //refresh extension status
  41. time.Sleep(3 * time.Second)
  42. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "reload")
  43. }
  44. // Get eth0 IP
  45. func getIPByCommand() (string, error) {
  46. cmd := "ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2"
  47. out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
  48. if err != nil {
  49. return "", err
  50. }
  51. ip := strings.TrimSpace(string(out))
  52. if ip == "" {
  53. return "", fmt.Errorf("no IP address found")
  54. }
  55. return ip, nil
  56. }