dujunchen 1 bulan lalu
induk
melakukan
3ee25ef53f

+ 1 - 1
Makefile

@@ -14,7 +14,7 @@ release-32:
 swagger:
 	swag init -g ../internal/app/http_server/swagger/swagger.go --dir ./api --exclude ./api/admin --output ./web/swagger
 
-release-panel-arm: 
+release-arm: 
 	GOOS=linux GOARCH=arm go build -tags "panel" -ldflags "-X 'main.buildDateTime=$(buildDateTime)' -X 'main.gitCommitCode=$(gitCommitCode)' -X 'main.goVersion=${goVersion}' -s -w" -o ./deployments/pbx-panel-arm ./cmd/main.go
 	
 apidoc:

+ 7 - 2
internal/app/ami/action/call.go

@@ -144,6 +144,7 @@ func InterruptRunningTask(toRunTask string) {
 		}
 	}
 
+	//lfshook.NewLogger().Infof("InterruptRunningTask RunningTask:%+v  ", task)
 	//same type return
 	if toRunTask == taskName {
 		if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" || toRunTask == "PAD-OCC" {
@@ -182,6 +183,10 @@ func InterruptRunningTask(toRunTask string) {
 		}
 
 	case "PAD-ICP", "PAD-TMS": // Interrupt PAD task running,
+		if toRunTask == "PAD-ICP" || toRunTask == "PAD-TMS" {
+			break
+		}
+
 		priority.InterruptedPad = "PAD-ICP"
 
 		lfshook.NewLogger().Infof("InterruptRunningTask interrupt PAD-ICP/PAD-TMS ,Running type :%s !", task.RunType)
@@ -519,8 +524,8 @@ func SetPadTimer() {
 			}
 		}
 		lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
-		//active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
-		active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
+		active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
+			//active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
 			//if both not active , return
 			if active.ActivedCab == "" {
 				return

+ 2 - 2
internal/app/ami/action/channel.go

@@ -18,7 +18,7 @@ func CoreShowChannels() (result []model.CoreShowChannelResVO, err error) {
 		return nil, err
 	}
 
-	lfshook.NewLogger().Tracef("events %+v", events)
+	lfshook.NewLogger().Infof("events %+v", events)
 	result = make([]model.CoreShowChannelResVO, 0)
 	for _, event := range events {
 		if event.Data["Event"] == "CoreShowChannel" {
@@ -35,7 +35,7 @@ func CoreShowChannels() (result []model.CoreShowChannelResVO, err error) {
 			result = append(result, channel)
 		}
 	}
-	lfshook.NewLogger().Tracef("channels %+v", result)
+	lfshook.NewLogger().Infof("channels %+v", result)
 	return result, nil
 }
 

+ 89 - 61
internal/app/ami/action/index.go

@@ -258,22 +258,22 @@ func HandleAMI(event map[string]string) {
 				priority.OCCAnswer = 0
 				priority.PADOccStart = 0
 
-				//clean confbridge
+				/*//clean confbridge
 				taskTmp, _ := priority.RegistryTask.Get("PAD-OCC")
 				ConfbridgeKick(taskTmp.ConfbridgeID, "all")
 
-				//check resume
-				priority.RegistryTask.StopAndUnregister("PAD-OCC")
-				taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
-				if ok {
-					if taskName == "EMG" {
-						EMGConfbridgeReinvite(task.ConfbridgeID)
-					} else if taskName == "CPA" {
-						CPAConfbridgeReinvite(task.ConfbridgeID)
-					}
-				}
-
-				break
+					//check resume
+						priority.RegistryTask.StopAndUnregister("PAD-OCC")
+						taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
+						if ok {
+							if taskName == "EMG" {
+								EMGConfbridgeReinvite(task.ConfbridgeID)
+							} else if taskName == "CPA" {
+								CPAConfbridgeReinvite(task.ConfbridgeID)
+							}
+						}
+				*/
+				//break
 			} else { //OCC queue is not empty
 
 				time.Sleep(time.Millisecond * 100) //wait io idle
@@ -333,57 +333,81 @@ func HandleAMI(event map[string]string) {
 			//lfshook.NewLogger().Infof("===hangup PAD =======================%s", number)
 
 			if utils.IsPAIU(number) {
-				res, _ := QueueStatus("0300", "")  // check ICP queue ,if empty PAD end
-				res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
-				if res == nil || res1 == nil {
-					return
-				}
-				lfshook.NewLogger().Infof("===Hangup  PAD====== ICP Queue calls:%s  OCC Queue calls:%s", res.Calls, res1.Calls)
-				//if res.Calls == "0" && res1.Calls == "0" {
-
-				if priority.PADStart == 1 {
-					//SetPadTimer()
-					alstatus.PaStatus("", "PAD", "end")
-					priority.PADStart = 0
-					//priority.PADTMSStart = 0
-
-					//clean confbridge
-					//PAD-ICP
-					taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
-					if ok {
-						ConfbridgeKick(taskTmp.ConfbridgeID, "all")
-					}
+
+				//clean confbridge & clean task info
+				//PAD-ICP
+				taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
+				if ok {
+					ConfbridgeKick(taskTmp.ConfbridgeID, "all")
 					priority.RegistryTask.StopAndUnregister("PAD-ICP")
+				}
 
-					// PAD-TMS
-					taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
-					if ok {
-						ConfbridgeKick(taskTmp.ConfbridgeID, "all")
-					}
+				// PAD-TMS
+				taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
+				if ok {
+					ConfbridgeKick(taskTmp.ConfbridgeID, "all")
 					priority.RegistryTask.StopAndUnregister("PAD-TMS")
+				}
 
-					//PAD-OCC
-					taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
-					if ok {
-						ConfbridgeKick(taskTmp.ConfbridgeID, "all")
-					}
+				//PAD-OCC
+				taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
+				if ok {
+					ConfbridgeKick(taskTmp.ConfbridgeID, "all")
 					priority.RegistryTask.StopAndUnregister("PAD-OCC")
+				}
 
-					//check resume
-					taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
-					if ok {
-						if taskName == "EMG" {
-							EMGConfbridgeReinvite(task.ConfbridgeID)
-						} else if taskName == "CPA" {
-							CPAConfbridgeReinvite(task.ConfbridgeID)
+				//Check PAD END
+				res, _ := QueueStatus("0300", "")  // check ICP queue ,if empty PAD end
+				res1, _ := QueueStatus("0301", "") // check OCC queue ,if empty PAD end
+				if res == nil || res1 == nil {
+					return
+				}
+				lfshook.NewLogger().Infof("===Hangup  PAD====== ICP Queue calls:%s  OCC Queue calls:%s", res.Calls, res1.Calls)
+				if res.Calls == "0" && res1.Calls == "0" {
+
+					if priority.PADStart == 1 {
+						//SetPadTimer()
+						alstatus.PaStatus("", "PAD", "end")
+						priority.PADStart = 0
+						//priority.PADTMSStart = 0
+						/*
+							//clean confbridge
+							//PAD-ICP
+							taskTmp, ok := priority.RegistryTask.Get("PAD-ICP")
+							if ok {
+								ConfbridgeKick(taskTmp.ConfbridgeID, "all")
+							}
+							priority.RegistryTask.StopAndUnregister("PAD-ICP")
+
+							// PAD-TMS
+							taskTmp, ok = priority.RegistryTask.Get("PAD-TMS")
+							if ok {
+								ConfbridgeKick(taskTmp.ConfbridgeID, "all")
+							}
+							priority.RegistryTask.StopAndUnregister("PAD-TMS")
+
+							//PAD-OCC
+							taskTmp, ok = priority.RegistryTask.Get("PAD-OCC")
+							if ok {
+								ConfbridgeKick(taskTmp.ConfbridgeID, "all")
+							}
+							priority.RegistryTask.StopAndUnregister("PAD-OCC")
+						*/
+						//check resume
+						taskName, task, ok := priority.RegistryTask.HighestPriorityRunningTask1()
+						if ok {
+							if taskName == "EMG" {
+								EMGConfbridgeReinvite(task.ConfbridgeID)
+							} else if taskName == "CPA" {
+								CPAConfbridgeReinvite(task.ConfbridgeID)
+							}
 						}
 					}
-				}
 
-				priority.ICPAnswer = 0
-				priority.OCCAnswer = 0
-				break
-				//}
+					priority.ICPAnswer = 0
+					priority.OCCAnswer = 0
+					break
+				}
 			}
 		}
 
@@ -432,8 +456,8 @@ func HandleAMI(event map[string]string) {
 					}
 				}
 				lfshook.NewLogger().Logger.Infof("=========Start PAD timer !======%d=======", active.PADTimeout)
-				//active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
-				active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
+				active.QueueTimer = time.AfterFunc(time.Duration(active.PADTimeout)*time.Second, func() { // check the PAD 30s timeout
+					//active.QueueTimer = time.AfterFunc(30*time.Second, func() { // check the PAD 30s timeout
 					//if both not active , return
 					if active.ActivedCab == "" {
 						return
@@ -962,12 +986,11 @@ func HandleAMI(event map[string]string) {
 	case "DialEnd":
 		//lfshook.NewLogger().Infof("=========%s", event["Event"])
 		//Cab Cab start
-		if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == " 0400" && event["DialStatus"] == "ANSWER" {
-
+		if utils.IsICP(event["CallerIDNum"]) && event["Exten"] == "0400" && event["DialStatus"] == "ANSWER" {
 			//================================
 			task := priority.TaskInfo{
-				RunChannel:   "",
-				LocalChan:    "",
+				RunChannel:   event["Channel"],
+				LocalChan:    event["Channel"],
 				RunType:      "C2C",
 				Priority:     priority.AllTasks.C2C.Priority,
 				ConfbridgeID: "",
@@ -1037,6 +1060,11 @@ func HandleAMI(event map[string]string) {
 				alstatus.OccPad("start")
 				priority.PADOccStart = 1
 			}
+
+			if priority.PADStart == 0 {
+				alstatus.PaStatus("", "PAD", "start")
+				priority.PADStart = 1
+			}
 		}
 
 		if utils.IsPACU(event["CallerIDNum"]) && utils.IsPAIU(event["CallerIDName"]) { //ICP and PACU connected

+ 11 - 10
internal/app/stc/broadcast/stc-broadcast.go

@@ -52,7 +52,7 @@ func HandleStcCmd(ctx context.Context, conn net.Conn) {
 				}
 				go processPacket(packet)
 			}*/
-			lfshook.NewLogger().Logger.Infof("buf:%x==============================", buf.Bytes())
+			//lfshook.NewLogger().Logger.Infof("buf:%x==============================", buf.Bytes())
 			for {
 				packet, err := msgdata.ExtractPacket(&buf)
 				if err != nil {
@@ -78,7 +78,7 @@ func processHeartbeat(info []byte) {
 	value := binary.BigEndian.Uint16(info[2:4]) //info[2] +  info[3]
 	active.TrainNum = "TS" + strconv.Itoa(int(value))
 
-	lfshook.NewLogger().Logger.Infof("TrainNum: %s", active.TrainNum)
+	//lfshook.NewLogger().Logger.Infof("TrainNum: %s", active.TrainNum)
 
 	//Get ICP volume
 	icpBit8 := info[9] & 0xF
@@ -94,7 +94,7 @@ func processHeartbeat(info []byte) {
 
 	pacuVolume := binary.BigEndian.Uint32(info[5:9]) //info[5]-info[8]
 
-	lfshook.NewLogger().Logger.Infof("=eidsStat:%x=======pacuStat:%x=====", eidsStat, pacuStat)
+	//lfshook.NewLogger().Logger.Infof("=eidsStat:%x=======pacuStat:%x=====", eidsStat, pacuStat)
 
 	for i := 0; i < 8; i++ {
 		eidsBit := (eidsStat >> i) & 0x01 // 右移 i 位,再与 1 取最低位
@@ -121,7 +121,7 @@ func processHeartbeat(info []byte) {
 			active.DeviceEndpoint.PacuInfo[i].Status = "abnormal"
 		}
 		active.DeviceEndpoint.PacuInfo[i].Volume = strconv.Itoa(int(pacuVolBit))
-		lfshook.NewLogger().Logger.Infof("PACU INFO===ID:%s===Mute:%+v===Stat:%s===Vol:%s", active.DeviceEndpoint.PacuInfo[i].ID, active.DeviceEndpoint.PacuInfo[i].Mute, active.DeviceEndpoint.PacuInfo[i].Status, active.DeviceEndpoint.PacuInfo[i].Volume)
+		//lfshook.NewLogger().Logger.Infof("PACU INFO===ID:%s===Mute:%+v===Stat:%s===Vol:%s", active.DeviceEndpoint.PacuInfo[i].ID, active.DeviceEndpoint.PacuInfo[i].Mute, active.DeviceEndpoint.PacuInfo[i].Status, active.DeviceEndpoint.PacuInfo[i].Volume)
 	}
 }
 
@@ -134,7 +134,7 @@ func processPacket(packet []byte) {
 	}
 
 	//for recv data log debug
-	if packet[5] != 0x03 && packet[5] != 0x0c /* && packet[5] != 0x01*/ {
+	if packet[5] != 0x03 && packet[5] != 0x0c && packet[5] != 0x01 {
 		lfshook.NewLogger().Logger.Infof("Get data from STC:%x", packet)
 	}
 
@@ -161,7 +161,7 @@ func processPacket(packet []byte) {
 		if dataLen == 10 {
 			processHeartbeat(packet[8:])
 		} else {
-			lfshook.NewLogger().Logger.Infof("=========Heartbeat data err !====len < 10=========")
+			//lfshook.NewLogger().Logger.Infof("=========Heartbeat data err !====len < 10=========")
 		}
 	case 0x02: // STN
 		if active.ActivedCab != "" {
@@ -289,7 +289,7 @@ func PadTimeOutSetting(data []byte) {
 	Seconds := data[7]
 	if Seconds != 0 {
 		active.PADTimeout = int(Seconds)
-		//lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", int(Seconds))
+		//lfshook.NewLogger().Logger.Infof("=========Set PAD Timeout seconds to %d ! ============", active.PADTimeout)
 	}
 
 }
@@ -472,8 +472,8 @@ func SelfCheck(data []byte) {
 
 	check := data[8]
 	delay := data[9]
-	//cycleCount := data[10]
-	cycleCount := 0x32
+	cycleCount := data[10]
+	//cycleCount := 0x32
 	datalen := int(data[11])
 
 	filename := msgdata.SubstrByRune(string(data[12:]), 0, datalen-4)
@@ -623,7 +623,8 @@ func AlarmHandleTMS(data []byte) {
 	case 0x03: //hangup
 		//NotifyPaiu(exten, "hangup")
 		action.Hangup(exten) //Pad
-		action.HangupTask("PAD-ICP")
+		action.HangupTask("PAD-TMS")
+		//action.HangupTask("PAD-ICP")
 	}
 }
 

+ 1 - 1
internal/app/stc/data/msgdata.go

@@ -197,6 +197,6 @@ func ExtractPacket(buf *bytes.Buffer) ([]byte, error) {
 	// 从 buffer 中移除已处理部分
 	buf.Next(endIdx + 1)
 	//lfshook.NewLogger().Logger.Infof("extracted packet len=%d, start=0x7f8e9d, end=0xFE", packetLen)
-	lfshook.NewLogger().Logger.Infof("=============return============== %x", packet)
+	//lfshook.NewLogger().Logger.Infof("=============return============== %x", packet)
 	return packet, nil
 }

+ 48 - 5
internal/app/stc/index.go

@@ -2,6 +2,7 @@ package stc
 
 import (
 	"context"
+	"errors"
 	"fmt"
 	"io"
 	"net"
@@ -11,6 +12,7 @@ import (
 	"pbx-api-gin/internal/app/stc/socket"
 	"pbx-api-gin/pkg/lfshook"
 	"pbx-api-gin/pkg/utils"
+	"strings"
 	"sync"
 	"syscall"
 
@@ -308,13 +310,23 @@ func RecvFromSipServer() {
 				//安全截取实际读到的字节
 				data := buf[:n]
 
+				if active.Master == true {
+					if _, werr := conn.Write([]byte("1")); werr != nil {
+						lfshook.NewLogger().Logger.Infof("Failed to write 'ok' to client: %+v", werr)
+					}
+				} else {
+					if _, werr := conn.Write([]byte("0")); werr != nil {
+						lfshook.NewLogger().Logger.Infof("Failed to write 'ok' to client: %+v", werr)
+					}
+				}
+
 				//Set master = true
 				if data[8] == 0x01 && data[5] == 0xf1 {
 					active.Master = true
-					return // set to master , stop recv
-				} else if data[8] == 0x00 && data[5] == 0xf1 {
-					active.Master = false
-				}
+					//return // set to master , stop recv
+				} // else if data[8] == 0x00 && data[5] == 0xf1 {
+				//active.Master = false
+				//}
 				//lfshook.NewLogger().Logger.Infof("Client  received %d bytes: hex=%x", n, data)
 			}
 
@@ -333,7 +345,7 @@ func RecvFromSipServer() {
 		}
 		lfshook.NewLogger().Logger.Infof("Connection closed from cab1 set Master = true !")
 		active.Master = true
-		return
+		//return
 	}
 }
 
@@ -365,9 +377,11 @@ func SendToRemoteMaster(ctx context.Context, conn net.Conn) {
 			// 编码并发送数据...
 			encoded, err := protocol.Encode()
 			if err != nil {
+				active.Master = true
 				return
 			}
 
+			conn.SetWriteDeadline(time.Now().Add(1000 * time.Millisecond)) //发送1秒超时
 			if conn != nil {
 				_, err = conn.Write(encoded)
 				if err != nil {
@@ -375,6 +389,35 @@ func SendToRemoteMaster(ctx context.Context, conn net.Conn) {
 					return // 触发重连
 				}
 			}
+
+			conn.SetReadDeadline(time.Now().Add(1000 * time.Millisecond)) //接收1秒超时
+			buf := make([]byte, 64)                                       // 足够接收短响应
+			n, err := conn.Read(buf)
+			if err != nil {
+				if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
+					lfshook.NewLogger().Logger.Infof("Read timeout waiting for remote response — may be OK (no response expected?)")
+				} else if errors.Is(err, io.EOF) || strings.Contains(err.Error(), "broken pipe") {
+					lfshook.NewLogger().Logger.Infof("Remote closed connection during read: %+v", err)
+					conn.Close()
+					return
+				} else {
+					lfshook.NewLogger().Logger.Infof("Read from remote failed unexpectedly: %+v", err)
+				}
+				continue // 不中断循环,继续下一轮
+			}
+
+			if n > 0 {
+				response := strings.TrimSpace(string(buf[:n]))
+				//lfshook.NewLogger().Logger.Infof("Remote responded: [%s]", response)
+				//可选:校验响应,例如
+				if response == "1" {
+					active.Master = false
+
+					if utils.CheckAsterisk() { //check asterisk available then stop asterisk
+						utils.ExecCmd("/etc/init.d/asterisk", "stop", "PBX")
+					}
+				}
+			}
 		}
 	}
 }

+ 12 - 4
internal/app/stc/priority/index.go

@@ -135,10 +135,18 @@ func CheckPriority(runType string) bool {
 				return true
 			case "PAD-TMS":
 				return true
-				/*	case "VOL":
-						return true
-					case "CHK":
-						return true*/
+			case "EMG":
+				return true
+			case "SPC":
+				return true
+			case "DCS":
+				return true
+			case "STN":
+				return true
+			case "CHK":
+				return true
+			case "VOL":
+				return true
 			}
 		}