index.go 1.5 KB

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