index.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. //init the active status
  30. active.Actived = true
  31. // 启动带有重连机制的连接管理协程MC1
  32. go stc.StartStcConnection(socket.Conn, "1")
  33. // 启动带有重连机制的连接管理协程MC8
  34. go stc.StartStcConnection(socket.Conn, "8")
  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 getIPByCommand() (string, error) {
  48. cmd := "ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2"
  49. out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
  50. if err != nil {
  51. return "", err
  52. }
  53. ip := strings.TrimSpace(string(out))
  54. if ip == "" {
  55. return "", fmt.Errorf("no IP address found")
  56. }
  57. return ip, nil
  58. }