| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package action
- import (
- "errors"
- "fmt"
- "pbx-api-gin/internal/app/stc/priority"
- "pbx-api-gin/pkg/lfshook"
- "pbx-api-gin/pkg/utils"
- "time"
- )
- // Hangup 挂断指定分机或通道
- func Hangup(channel string) {
- lfshook.NewLogger().Infof("Hangup extensions/channel :%s", channel)
- if !utils.IsChannel(channel) {
- channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
- }
- action := map[string]string{
- "Action": "hangup",
- "Channel": channel,
- }
- //lfshook.NewLogger().Infof("hangup action :%+v", action)
- if _, _, err := AminInstance.Send(action); err != nil {
- lfshook.NewLogger().Errorf("Hangup %+v", err)
- }
- }
- // Hangup task
- func HangupTask() {
- _, taskInfo, ok := priority.RegistryTask.HighestPriorityRunningTask()
- if ok {
- lfshook.NewLogger().Infof("HangupTask Check TaskName:%s ", taskInfo.RunType)
- ConfbridgeKick(taskInfo.ConfbridgeID, "all")
- Hangup(taskInfo.RunChannel)
- time.Sleep(100 * time.Millisecond)
- }
- }
- // Hangup all calls
- func HangupAll() {
- utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
- }
- // 获取分机状态
- func ExtenStatus(exten string) (Status string) {
- action := map[string]string{
- "Action": "ExtensionState",
- "Exten": exten,
- "Context": "default",
- }
- res, _, err := AminInstance.Send(action)
- if err != nil {
- lfshook.NewLogger().Errorf("%+v", err)
- return ""
- }
- //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
- return res["StatusText"]
- }
- func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
- action := map[string]string{
- "Action": "ConfbridgeKick",
- "Conference": confnum,
- "Channel": channel,
- }
- res, _, err = AminInstance.Send(action)
- if err != nil {
- return nil, err
- }
- lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
- if res["Response"] != "Success" {
- return nil, errors.New(res["Message"])
- }
- return res, nil
- }
- func ConfbridgeList(confnum string) (chans []string, err error) {
- action := map[string]string{
- "Action": "ConfbridgeList",
- "Conference": confnum,
- }
- res, events, err := AminInstance.Send(action)
- if err != nil {
- return nil, err
- }
- lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
- if res["Response"] == "Success" {
- for _, event := range events {
- if event.Data["Event"] == "ConfbridgeList" {
- chans = append(chans, event.Data["Channel"])
- }
- }
- return chans, nil
- } else {
- return nil, errors.New(res["Message"])
- }
- }
- func ConfbridgeReinvite(src, context, confID string) {
- if ExtenStatus(src) != "Idle" {
- lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
- return
- }
- chanel := fmt.Sprintf("Local/%s@%s", src, context)
- action := map[string]string{
- "Action": "Originate",
- "Channel": chanel,
- "Exten": "000",
- "Context": "confbridge-join",
- "CallerID": fmt.Sprintf("%s<%s>", "", ""),
- "Priority": "1",
- "Variable": fmt.Sprintf("CBID=%s", confID),
- "async": "true",
- }
- lfshook.NewLogger().Infof("dial action %+v", action)
- res, _, err := AminInstance.Send(action)
- if err != nil {
- lfshook.NewLogger().Errorf("%+v", err)
- }
- lfshook.NewLogger().Info(res)
- }
- // Dial 拨打号码
- func Dial(src, dst, dialrule, callerID, callerName string) {
- chanel := fmt.Sprintf("%s/%s@default", "Local", src)
- action := map[string]string{
- "Action": "Originate",
- "Channel": chanel,
- "Exten": dst,
- "Context": dialrule,
- "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
- "Priority": "1",
- "async": "true",
- }
- lfshook.NewLogger().Infof("dial action %+v", action)
- res, _, err := AminInstance.Send(action)
- if err != nil {
- lfshook.NewLogger().Errorf("%+v", err)
- }
- lfshook.NewLogger().Info(res)
- }
|