dujunchen 2 tygodni temu
rodzic
commit
c526469f71

+ 32 - 25
internal/app/ami/action/index.go

@@ -2,8 +2,6 @@ package action
 
 import (
 	"net"
-	"pbx-api-gin/internal/app/ami/model"
-	"pbx-api-gin/internal/app/mysql"
 	alstatus "pbx-api-gin/internal/app/stc/sendstatus"
 	"pbx-api-gin/internal/pkg/configs"
 	"pbx-api-gin/pkg/lfshook"
@@ -28,59 +26,68 @@ const pacu8 = "2181"
 //type Operation func(src, dst string, whisper, bargein bool)
 
 func HandleAMI(event map[string]string, conn net.Conn) {
+
 	switch event["Event"] {
 	case "DialBegin":
-
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
 		if utils.IsPAIU(event["CallerIDNum"]) && utils.IsICP(event["DestCallerIDNum"]) {
 
 			alstatus.AlarmStatus(event["CallerIDNum"], "dial", conn) // Alarm dial ICP start
 		}
+	case "Newchannel":
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
+		if utils.IsPAIU(event["CallerIDNum"]) && event["Exten"] == "0300" { // Alarm dial queue start
 
+			alstatus.AlarmStatus(event["CallerIDNum"], "dial", conn)
+		}
 	case "DialEnd":
-
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
 		if utils.IsPAIU(event["CallerIDNum"]) {
 			alstatus.AlarmStatus(event["CallerIDNum"], "Idle", conn) // Alarm dial end
 		}
 	case "QueueCallerJoin":
-
-		if utils.IsPAIU(event["CallerIDNum"]) {
-			alstatus.AlarmStatus(event["CallerIDNum"], "queue", conn) // Alarm join the queue
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
+		if utils.IsPAIU(event["CallerIDNum"]) { // Alarm join the queue
+			alstatus.AlarmStatus(event["CallerIDNum"], "queue", conn)
 
 			switch string(event["CallerIDNum"][2]) { // connect the pacu to pad acording to the pad number
 			case "1":
-				ChanSpy(pacu1, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu1, false, true) //connect pacu to pad
 			case "2":
-				ChanSpy(pacu2, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu2, false, true) //connect pacu to pad
 			case "3":
-				ChanSpy(pacu3, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu3, false, true) //connect pacu to pad
 			case "4":
-				ChanSpy(pacu4, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu4, false, true) //connect pacu to pad
 			case "5":
-				ChanSpy(pacu5, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu5, false, true) //connect pacu to pad
 			case "6":
-				ChanSpy(pacu6, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu6, false, true) //connect pacu to pad
 			case "7":
-				ChanSpy(pacu7, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu7, false, true) //connect pacu to pad
 			case "8":
-				ChanSpy(pacu8, event["CallerIDNum"], false, true) //connect pacu to pad
+				ChanSpy(event["CallerIDNum"], pacu8, false, true) //connect pacu to pad
 			}
 		}
 
 	case "ExtensionStatus":
-
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
 		if event["StatusText"] == "Idle" || event["StatusText"] == "Unavailable" {
-			status := &model.Extension{
-				Extension: event["Exten"],
-				Status:    event["StatusText"],
+			//status := &model.Extension{
+			//	Extension: event["Exten"],
+			//	Status:    event["StatusText"],
+			//}
+			//_, err := mysql.DBOrmInstance.Where("exten = ?", status.Extension).Cols("status").Update(status)
+			//if err != nil {
+			//	lfshook.NewLogger().Infof("update extension status err : %+v", err.Error())
+			//}
+
+			if len(event["Exten"]) > 3 {
+				alstatus.AlarmStatus(event["Exten"], event["StatusText"], conn) // Alarm idle + unavailable
 			}
-			_, err := mysql.DBOrmInstance.Where("exten = ?", status.Extension).Cols("status").Update(status)
-			if err != nil {
-				lfshook.NewLogger().Infof("update extension status err : %+v", err.Error())
-			}
-			alstatus.AlarmStatus(event["Exten"], event["StatusText"], conn) // Alarm idle + unavailable
 		}
 	case "BridgeEnter":
-
+		lfshook.NewLogger().Infof("=========%s", event["Event"])
 		if utils.IsPAIU(event["CallerIDNum"]) {
 			alstatus.AlarmStatus(event["CallerIDNum"], "connect", conn) // Alarm connected
 		}

+ 4 - 1
internal/app/index.go

@@ -1,6 +1,7 @@
 package app
 
 import (
+	"net"
 	"pbx-api-gin/internal/app/ami/action"
 	"pbx-api-gin/internal/app/mysql"
 	"pbx-api-gin/internal/app/stc"
@@ -8,10 +9,12 @@ import (
 	"pbx-api-gin/pkg/lfshook"
 )
 
+var conn net.Conn
+
 func StartApp() {
 	mysql.CreateDBInstance()
 
-	conn := stc.CreateConnection("192.168.17.14", 6090)
+	conn = stc.CreateConnection("172.16.0.11", 10100)
 
 	go action.StartAMI(func() {
 		lfshook.NewLogger().Info("ami callback")

+ 2 - 9
internal/app/mysql/index.go

@@ -1,22 +1,14 @@
 package mysql
 
 import (
-	"fmt"
-	"os"
-	"pbx-api-gin/internal/pkg/configs"
-	"pbx-api-gin/pkg/lfshook"
-	"syscall"
-
 	_ "github.com/go-sql-driver/mysql"
-	"github.com/sirupsen/logrus"
 	"xorm.io/xorm"
-	"xorm.io/xorm/log"
 )
 
 var DBOrmInstance *xorm.Engine
 
 func CreateDBInstance() {
-	var err error
+	/*var err error
 	// DBOrmInstance, err = xorm.NewEngine("sqlite3", "playcall.db")
 	url := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8",
 		configs.ConfigGlobal.MysqlDBUser,
@@ -49,4 +41,5 @@ func CreateDBInstance() {
 		}
 		DBOrmInstance.SetLogger(log.NewSimpleLogger(info))
 	}
+	*/
 }

+ 1 - 3
internal/app/stc/broadcast/stc-broadcast.go

@@ -12,8 +12,6 @@ import (
 
 var Pacus = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
 
-//var AlarmExtens = []string{"2111", "2121", "2131", "2141", "2151", "2161", "2171", "2181"}
-
 func HandleStcCmd(conn net.Conn) {
 
 	for {
@@ -23,7 +21,7 @@ func HandleStcCmd(conn net.Conn) {
 			fmt.Println("Error reading from server:", err)
 			return
 		}
-		fmt.Println("Received from server:", string(buffer[:n]))
+		lfshook.NewLogger().Logger.Infof("Get data from STC :%+v", buffer[:n])
 
 		switch buffer[5] {
 		case 0x01: //heartbeat

+ 3 - 2
internal/app/stc/index.go

@@ -30,7 +30,7 @@ func CreateConnection(ServerAddr string, Port int) net.Conn {
 	// connect server
 	conn, err := net.DialTCP("tcp", &net.TCPAddr{
 		IP:   net.ParseIP("0.0.0.0"),
-		Port: 0,
+		Port: 10201,
 	}, &net.TCPAddr{
 		IP:   net.ParseIP(ServerAddr),
 		Port: Port,
@@ -49,7 +49,8 @@ func CreateConnection(ServerAddr string, Port int) net.Conn {
 
 func Sendheartbeat(conn net.Conn) {
 	var count uint8
-	//init heartbeat data
+	//init heartbeat data  ============================
+	//return
 	protocol := msgdata.NewProtocol()
 	protocol.SourceID = 0x02
 	protocol.DestinationID = 0x01

+ 8 - 14
internal/app/stc/sendstatus/status.go

@@ -3,10 +3,9 @@ package alstatus
 import (
 	"fmt"
 	"net"
-	"pbx-api-gin/api/model"
-	"pbx-api-gin/internal/app/mysql"
 	msgdata "pbx-api-gin/internal/app/stc/data"
 	"pbx-api-gin/pkg/lfshook"
+	"pbx-api-gin/pkg/utils"
 )
 
 func SendToStc(conn net.Conn, data []byte) {
@@ -21,22 +20,17 @@ func SendToStc(conn net.Conn, data []byte) {
 func AlarmStatus(exten string, status string, conn net.Conn) {
 
 	//check exten if it is a alarm exten
-	var data model.Extension
-	_, er := mysql.DBOrmInstance.Where("exttype = ? and exten = ?", "PAIU", exten).Get(&data)
-	if er != nil {
-		lfshook.NewLogger().Logger.Infof("get alarm exten err : %+v", er.Error())
-	}
-
-	if len(data.Extension) == 0 {
+	if !utils.IsPAIU(exten) { // if not alarm device , return
 		return
 	}
 
 	protocol := msgdata.NewProtocol()
 	protocol.MessageID = 0x26
 	protocol.DataLength = 0x04
+	protocol.Data = make([]byte, 4)
 
-	protocol.Data[0] = byte(int(exten[3] - 0)) //车厢号
-	protocol.Data[1] = byte(int(exten[2] - 0)) //位置号
+	protocol.Data[0] = exten[2] - '0' //车厢号
+	protocol.Data[1] = exten[3] - '0' //位置号
 
 	//报警器工作状态
 	switch status {
@@ -46,8 +40,8 @@ func AlarmStatus(exten string, status string, conn net.Conn) {
 	case "idle", "Idle": //idle
 		protocol.Data[2] = 0x01
 
-	//case "dial": //dial
-	//	protocol.Data[2] = 0x02
+	case "dial": //dial
+		protocol.Data[2] = 0x02
 
 	case "queue": //PAD alarm
 		protocol.Data[2] = 0x03
@@ -61,7 +55,7 @@ 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)
 }
 

+ 12 - 4
pkg/utils/utils.go

@@ -110,20 +110,28 @@ func IsChannel(input string) bool {
 
 // 判断是否是广播分机
 func IsPAIU(input string) bool {
-
-	return input[:2] == "24"
+	if len(input) == 4 {
+		return input[:2] == "24"
+	}
+	return false
 }
 
 // 判断是否是广播分机
 func IsPACU(input string) bool {
 
-	return input[:2] == "21"
+	if len(input) == 4 {
+		return input[:2] == "21"
+	}
+	return false
 }
 
 // 判断是否是司机控制盒
 func IsICP(input string) bool {
 
-	return input[:2] == "23"
+	if len(input) == 4 {
+		return input[:2] == "23"
+	}
+	return false
 }
 
 // 判断文件是否存在