Ver Fonte

update acvtive

dujunchen há 1 semana atrás
pai
commit
0fcda02dea

+ 8 - 0
internal/app/ami/action/index.go

@@ -3,6 +3,7 @@ package action
 import (
 	"pbx-api-gin/internal/app/ami/model"
 	"pbx-api-gin/internal/app/mysql"
+	"pbx-api-gin/internal/app/stc/active"
 	alstatus "pbx-api-gin/internal/app/stc/sendstatus"
 	"pbx-api-gin/internal/app/stc/socket"
 	"pbx-api-gin/internal/pkg/configs"
@@ -29,7 +30,14 @@ const pacu8 = "2181"
 
 func HandleAMI(event map[string]string) {
 
+	//return if not actived
+	if !active.Actived {
+		return
+	}
+
+	// MC1 conn or MC8 conn
 	conn := socket.Conn
+
 	if conn == nil {
 		lfshook.NewLogger().Infof("===HandleAMI===conn==nil=")
 		return

+ 2 - 0
internal/app/index.go

@@ -30,6 +30,7 @@ func StartApp() {
 
 	// 启动带有重连机制的连接管理协程
 	go stc.StartStcConnection(socket.Conn)
+
 	// 启动其他服务...
 	// 启动 AMI
 	go func() {
@@ -39,6 +40,7 @@ func StartApp() {
 	}()
 }
 
+// Get eth0 IP
 func getIPByCommand() (string, error) {
 	cmd := "ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2"
 	out, err := exec.Command("bash", "-c", cmd).CombinedOutput()

+ 3 - 0
internal/app/stc/active/index.go

@@ -2,3 +2,6 @@ package active
 
 var ActiveCab string
 var CabNum string
+var Actived bool
+
+var ActivedMC int

+ 50 - 35
internal/app/stc/broadcast/stc-broadcast.go

@@ -41,7 +41,6 @@ func HandleStcCmd(ctx context.Context, conn net.Conn) {
 					}
 					return
 				}
-
 				// 将新读取的数据追加到缓冲区
 				buf.Write(tmp[:n])
 			}
@@ -64,34 +63,37 @@ func processPacket(packet []byte) {
 		fmt.Println("Invalid packet length")
 		return
 	}
-
 	lfshook.NewLogger().Logger.Infof("Get data from STC ===============:%x", packet)
+	//check if the cmd type is avtive
+	if packet[5] == 0x03 { // ACTIVE
+		Active([1]byte{packet[7]})
+	}
 
-	switch packet[5] {
-	case 0x01: // heartbeat
-		// handle heartbeat
-	case 0x02: // STN
-		StationAnn(packet)
-	case 0x03: // ACTIVE
-		Active([1]byte{packet[8]})
-	case 0x05: // SPC
-		SpecialAnn(packet)
-	case 0x06: // EMG
-		EmgMsg(packet)
-	case 0x07: // STOP
-		AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
-	case 0x08: // DCS
-		DcsAnn(packet)
-	case 0x09: // SELF CHECK
-		SelfCheck(packet)
-	case 0x0a:
-		AlarmHandle(packet)
-	case 0x0b:
-		AlarmResetAll()
-	case 0x0c:
-		RecordStorageConf(packet[8:])
-	default:
-		fmt.Printf("Unknown command: %x\n", packet[5])
+	if active.Actived {
+		switch packet[5] {
+		case 0x01: // heartbeat
+			// handle heartbeat
+		case 0x02: // STN
+			StationAnn(packet)
+		case 0x05: // SPC
+			SpecialAnn(packet)
+		case 0x06: // EMG
+			EmgMsg(packet)
+		case 0x07: // STOP
+			AnnStop([4]byte{packet[8], packet[9], packet[10], packet[11]})
+		case 0x08: // DCS
+			DcsAnn(packet)
+		case 0x09: // SELF CHECK
+			SelfCheck(packet)
+		case 0x0a:
+			AlarmHandle(packet)
+		case 0x0b:
+			AlarmResetAll()
+		case 0x0c:
+			RecordStorageConf(packet[8:])
+		default:
+			fmt.Printf("Unknown command: %x\n", packet[5])
+		}
 	}
 }
 
@@ -126,6 +128,7 @@ func Active(data [1]byte) {
 	var info model.Sysinfo
 
 	Num := int(data[0])
+	lfshook.NewLogger().Logger.Infof("Active data : %x", Num)
 
 	switch Num { // 设置全局的激活信号,并通过协议(待定)通知终端注册到对应的激活主机上
 	case 0:
@@ -133,7 +136,7 @@ func Active(data [1]byte) {
 		info.Name = "cab_active"
 		info.Value = "0"
 
-		_, er := mysql.DBOrmInstance.Update(&info)
+		_, er := mysql.DBOrmInstance.Where("name = ?", "cab_active").Update(&info)
 		if er != nil {
 			lfshook.NewLogger().Logger.Infof("update sysinfo err : %+v", er.Error())
 			return
@@ -142,11 +145,13 @@ func Active(data [1]byte) {
 		if active.CabNum == "1" {
 			info.Name = "cab_active"
 			info.Value = "1"
+			active.Actived = true
 		} else {
 			info.Name = "cab_active"
 			info.Value = "0"
+			active.Actived = false
 		}
-		_, er := mysql.DBOrmInstance.Update(&info)
+		_, er := mysql.DBOrmInstance.Where("name = ?", "cab_active").Update(&info)
 		if er != nil {
 			lfshook.NewLogger().Logger.Infof("update sysinfo err : %+v", er.Error())
 			return
@@ -156,12 +161,14 @@ func Active(data [1]byte) {
 		if active.CabNum == "8" {
 			info.Name = "cab_active"
 			info.Value = "1"
+			active.Actived = true
 		} else {
 			info.Name = "cab_active"
 			info.Value = "0"
+			active.Actived = false
 		}
 
-		_, er := mysql.DBOrmInstance.Update(&info)
+		_, er := mysql.DBOrmInstance.Where("name = ?", "cab_active").Update(&info)
 		if er != nil {
 			lfshook.NewLogger().Logger.Infof("update sysinfo err : %+v", er.Error())
 			return
@@ -283,7 +290,7 @@ func SelfCheck(data []byte) {
 // 全局变量:记录正在抑制的 exten
 var (
 	suppressedExts = sync.Map{} // map[string]struct{},值存在即表示被抑制
-	suppressionMu  sync.Mutex   // 保护初始化和清理操作(可选)
+	//suppressionMu  sync.Mutex   // 保护初始化和清理操作(可选)
 )
 
 // suppressKey 生成用于抑制的 key(可以根据需求扩展)
@@ -302,6 +309,7 @@ func AlarmHandle(data []byte) {
 	lfshook.NewLogger().Logger.Infof("================ICP Answer PAD================:%s ", exten)
 	key := suppressKey(exten, handler)
 
+	//Drop other handler in 2 sec
 	// 只对 handler == 0x01 做 2 秒去重
 	if handler == 0x01 {
 		if _, loaded := suppressedExts.LoadOrStore(key, struct{}{}); loaded {
@@ -310,7 +318,7 @@ func AlarmHandle(data []byte) {
 		}
 
 		// 设置2秒后删除该 key,允许下次通过
-		time.AfterFunc(4*time.Second, func() {
+		time.AfterFunc(2*time.Second, func() {
 			suppressedExts.Delete(key)
 			lfshook.NewLogger().Logger.Debugf("Suppression released for key: %s", key)
 		})
@@ -325,9 +333,16 @@ func AlarmHandle(data []byte) {
 	switch handler {
 	case 0x01: //answer(ICP+Alarm+PACU)
 		//NotifyPaiu(exten, "answer")
-		err := action.RedirectInQueue(exten, "0402", "ani-rule", "1") // 1车ICP接听PAIU
-		if err != nil {
-			lfshook.NewLogger().Logger.Infof("================ICP Answer PAD====ERR============ : %+v", err.Error())
+		if active.CabNum == "1" {
+			err := action.RedirectInQueue(exten, "0402", "ani-rule", "1") // 1车ICP接听PAIU
+			if err != nil {
+				lfshook.NewLogger().Logger.Infof("=========Cab1=======ICP Answer PAD====ERR============ : %+v", err.Error())
+			}
+		} else {
+			err := action.RedirectInQueue(exten, "0402", "ani-rule", "8") // 8车ICP接听PAIU
+			if err != nil {
+				lfshook.NewLogger().Logger.Infof("=========Cab8=======ICP Answer PAD====ERR============ : %+v", err.Error())
+			}
 		}
 		//invite PACU join in
 		//action.Hangup("PACU")

+ 12 - 20
internal/app/stc/index.go

@@ -14,18 +14,10 @@ import (
 	"time"
 )
 
-const RemotePort = 10100
-const LocalPort = 10201
-
-//const RemoteAddr = "192.168.17.14"
-
-const RemoteAddr = "10.0.11.11"
-
 func StartStcConnection(conn net.Conn) {
-	//var conn net.Conn
-	//var wg sync.WaitGroup
+
 	var connMux sync.Mutex // 保护 conn 的读写
-	//var err error
+
 	for {
 		// 尝试建立连接
 		conn1, err := CreateConnection()
@@ -33,6 +25,7 @@ func StartStcConnection(conn net.Conn) {
 			time.Sleep(2 * time.Second)
 			continue
 		}
+
 		connMux.Lock()
 		oldConn := conn
 		socket.Conn = conn1
@@ -46,22 +39,21 @@ func StartStcConnection(conn net.Conn) {
 
 		// 使用 context 控制所有协程的生命周期
 		ctx, cancel := context.WithCancel(context.Background())
-		lfshook.NewLogger().Logger.Infof("Connect success :%s:%d", RemoteAddr, RemotePort)
-		// 启动消息处理和心跳协程
-		// 启动消息处理
+
+		// 启动消息处理MC1
 		go func() {
 			defer func() {
 				cancel() // 一旦任一协程退出,取消所有
 			}()
-			broadcast.HandleStcCmd(ctx, conn1) // 改造 HandleStcCmd 接收 ctx
+			broadcast.HandleStcCmd(ctx, socket.Conn) // 改造 HandleStcCmd 接收 ctx
 		}()
 
-		// 启动心跳
+		// 启动心跳MC1
 		go func() {
 			defer func() {
 				cancel()
 			}()
-			Sendheartbeat(ctx, conn1) // 改造 Sendheartbeat 接收 ctx
+			Sendheartbeat(ctx, socket.Conn) // 改造 Sendheartbeat 接收 ctx
 		}()
 
 		// 等待连接断开(监听连接状态)
@@ -78,20 +70,20 @@ func StartStcConnection(conn net.Conn) {
 
 // 返回错误而不是终止程序
 func CreateConnection() (net.Conn, error) {
-	lfshook.NewLogger().Logger.Infof("========Connect server  IP:%s  :Port:%d", RemoteAddr, RemotePort)
+	lfshook.NewLogger().Logger.Infof("========Connect server  IP:%s  :Port:%d", socket.RemoteAddr, socket.RemotePort)
 	// 创建 Dialer
 	dialer := &net.Dialer{
-		LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: LocalPort}, // 固定本地端口
+		LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort}, // 固定本地端口
 		Control:   controlTCPConn,
 	}
 
-	DialAddr := fmt.Sprintf("%s:%d", RemoteAddr, RemotePort)
+	DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr, socket.RemotePort)
 	conn, err := dialer.Dial("tcp", DialAddr)
 	if err != nil {
 		lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
 		return nil, err
 	}
-
+	lfshook.NewLogger().Logger.Infof("Connect success :%s:%d", socket.RemoteAddr, socket.RemotePort)
 	return conn, nil
 }
 

+ 6 - 2
internal/app/stc/sendstatus/status.go

@@ -3,6 +3,7 @@ package alstatus
 import (
 	"fmt"
 	"net"
+	"pbx-api-gin/internal/app/stc/active"
 	msgdata "pbx-api-gin/internal/app/stc/data"
 	"pbx-api-gin/pkg/lfshook"
 	"pbx-api-gin/pkg/utils"
@@ -57,8 +58,11 @@ func AlarmStatus(exten string, status string, conn net.Conn) {
 		fmt.Println("Encode error:", errEn)
 		return
 	}
-	lfshook.NewLogger().Logger.Infof("===AlarmStatus=ext:%s===carr:%x==========pos:%x=========status:%x", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
-	SendToStc(conn, encoded)
+	//check if actived
+	if active.Actived {
+		lfshook.NewLogger().Logger.Infof("===AlarmStatus=ext:%s===carr:%x==========pos:%x=========status:%x", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
+		SendToStc(conn, encoded)
+	}
 }
 
 // report broadcast status to STC

+ 9 - 0
internal/app/stc/socket/index.go

@@ -3,3 +3,12 @@ package socket
 import "net"
 
 var Conn net.Conn
+var Conn8 net.Conn
+
+const RemotePort = 10100
+const LocalPort = 10201
+
+//const RemoteAddr = "192.168.17.14"
+
+const RemoteAddr = "10.0.11.11"
+const RemoteAddr8 = "10.0.11.81"