dujunchen 3 недель назад
Родитель
Сommit
0314518248
4 измененных файлов с 101 добавлено и 9 удалено
  1. 6 3
      internal/app/ami/action/call.go
  2. 36 6
      internal/app/ami/action/index.go
  3. 3 0
      internal/app/index.go
  4. 56 0
      pkg/utils/file.go

+ 6 - 3
internal/app/ami/action/call.go

@@ -26,6 +26,8 @@ func InActiveHangup() {
 			HangupRunningTask("InActiveHangup")
 		case "VOL":
 			HangupRunningTask("InActiveHangup")
+		case "PAD-OCC":
+			HangupRunningTask("InActiveHangup")
 		}
 	}
 }
@@ -139,10 +141,8 @@ func HangupRunningTask(toRunTask string) {
 				break
 
 			} else {
-				//1. Hangup connected PAD
-				Hangup(priority.RunningPATaskChan)
 
-				//2. Redirect all the other pads in 0301 to 0300
+				//1. Redirect all the other pads in 0301 to 0300
 				resCaller, err := QueueStatus("0301", "") // check OCC queue, get entries
 				if err != nil {
 					lfshook.NewLogger().Infof("===QueueStatus==%+v", err)
@@ -152,6 +152,9 @@ func HangupRunningTask(toRunTask string) {
 					time.Sleep(time.Microsecond * 200)
 				}
 
+				//2. Hangup connected PAD
+				Hangup(priority.RunningPATaskChan)
+
 				//3. Hangup OI & ICP
 				HangupIO()
 				HangupAllLocalChan()

+ 36 - 6
internal/app/ami/action/index.go

@@ -1,6 +1,8 @@
 package action
 
 import (
+	"fmt"
+	"os"
 	"pbx-api-gin/internal/app/stc/active"
 	"pbx-api-gin/internal/app/stc/priority"
 	alstatus "pbx-api-gin/internal/app/stc/sendstatus"
@@ -54,20 +56,48 @@ func HandleAMI(event map[string]string) {
 	case "UserEvent": // RCD filename; PA;CPA; CabCab
 		lfshook.NewLogger().Infof("========event:%s   File:%s", event["Event"], event["FILENAME"])
 
-		if event["UserEvent"] == "SetRecordFile" { //get  record file name
+		if event["UserEvent"] == "SetRecordFile" { //Get  record file name
 			//延时一下1秒;
-			time.Sleep(time.Second)
-			//检测录音文件是否存在;
+			//time.Sleep(time.Second)
+
+			//检测录音文件是否存在;最多检测5次,每次间隔1秒
+			var fileExists bool
+			for i := 0; i < 5; i++ {
+				if _, err := os.Stat(event["FILENAME"]); err == nil {
+					fileExists = true
+					lfshook.NewLogger().Infof("File found: %s", event["FILENAME"])
+					break
+				} else if os.IsNotExist(err) {
+					lfshook.NewLogger().Infof("File not found (attempt %d): %s", i+1, event["FILENAME"])
+				} else {
+					lfshook.NewLogger().Errorf("Error checking file: %v", err)
+				}
+				time.Sleep(time.Second) // 等待1秒
+			}
+
+			if !fileExists {
+				lfshook.NewLogger().Errorf("File not found after 5 attempts: %s", event["FILENAME"])
+				// 可选:发送错误通知或跳过后续处理
+				break
+			}
 
 			//检测录音文件是否超过3min;
+			duration, err := utils.GetDuration(event["FILENAME"])
+			if err != nil {
+				lfshook.NewLogger().Errorf("Get duration err: %+v", err)
+			}
 
 			//如果超过则切割
+			if duration > 180 {
 
-			//执行加密操作
+			}
 
-			//切割之后推送生成的文件名;
+			//执行加密操作
+			DstFile := fmt.Sprintf("%s.bin", event["FILENAME"])
+			utils.AudioFileEncode(DstFile, event["FILENAME"])
 
-			alstatus.SendRecordFile(event["FILENAME"], event["RecordType"])
+			//切割&加密之后推送生成的文件名;
+			alstatus.SendRecordFile(DstFile, event["RecordType"])
 
 		} else if event["UserEvent"] == "CallType" && (event["Type"] == "PA" || event["Type"] == "CPA") { //PA start; check manual PA priority
 			//PA & CPA interrupt others

+ 3 - 0
internal/app/index.go

@@ -65,4 +65,7 @@ func SetMasterCabNum() {
 		active.CabNum = "1"
 		active.Master = true
 	}
+
+	active.CabNum = "1"
+	active.Master = true
 }

+ 56 - 0
pkg/utils/file.go

@@ -1,7 +1,12 @@
 package utils
 
 import (
+	"encoding/base64"
+	"io"
 	"os"
+	"os/exec"
+	"strconv"
+	"strings"
 
 	"github.com/sirupsen/logrus"
 	"gopkg.in/ini.v1"
@@ -40,3 +45,54 @@ func GetLocationName() string {
 	logrus.Infof("use location %s", timeZoneCache)
 	return timeZoneCache
 }
+
+func GetDuration(filePath string) (int, error) {
+	cmd := exec.Command("soxi", "-D", filePath)
+	output, err := cmd.Output()
+	if err != nil {
+		return 0, err
+	}
+
+	durationStr := strings.TrimSpace(string(output))
+	duration, err := strconv.ParseFloat(durationStr, 64)
+	if err != nil {
+		return 0, err
+	}
+	return int(duration), nil
+}
+
+func AudioFileEncode(dstFile, srcFile string) error {
+	data := "sripis123"
+	encoded := base64.StdEncoding.EncodeToString([]byte(data))
+
+	// Step 2: 创建一个新文件,写入 Base64 编码结果
+	outputFile, err := os.Create(dstFile)
+	if err != nil {
+		return err
+	}
+	defer outputFile.Close()
+
+	_, err = outputFile.WriteString(encoded)
+	if err != nil {
+		return err
+	}
+
+	// Step 3: 打开 audio.wav 文件
+	audioFile, err := os.Open(srcFile)
+	if err != nil {
+		return err
+	}
+	defer audioFile.Close()
+
+	// Step 4: 将 audio.wav 的内容追加到 output.bin 文件末尾
+	_, err = audioFile.Seek(0, io.SeekStart) // 确保从文件开头读取
+	if err != nil {
+		return err
+	}
+
+	_, err = io.Copy(outputFile, audioFile)
+	if err != nil {
+		return err
+	}
+	return err
+}