From 59fb38eff8092aa5cb93791aaed08a595f396724 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sat, 25 Jan 2025 16:31:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 10 +++++ pkg/handler/message/catalog.go | 28 +++++--------- pkg/manscdp/catalog.go | 71 +++++++++++++++++++++++++++++++++- pkg/manscdp/cmdtype/cmdtype.go | 18 +++++++++ pkg/manscdp/keepalive.go | 1 + pkg/utils/charset/charset.go | 10 +++++ pkg/utils/xml.go | 11 +++--- 7 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 pkg/manscdp/cmdtype/cmdtype.go create mode 100644 pkg/manscdp/keepalive.go create mode 100644 pkg/utils/charset/charset.go diff --git a/README.MD b/README.MD index 6e73afd..da8503f 100644 --- a/README.MD +++ b/README.MD @@ -2,3 +2,13 @@ ```shell go generate -x ``` + +### 运行项目 +```shell +go run cmd/client/main.go +``` + +### 编译项目 +```shell +go build -o bin/client cmd/client/main.go +``` diff --git a/pkg/handler/message/catalog.go b/pkg/handler/message/catalog.go index 562194a..ee7738f 100644 --- a/pkg/handler/message/catalog.go +++ b/pkg/handler/message/catalog.go @@ -1,7 +1,6 @@ package message import ( - "encoding/xml" "fmt" "git.skcks.cn/Shikong/go-gb28181/pkg/config" "git.skcks.cn/Shikong/go-gb28181/pkg/log" @@ -21,26 +20,17 @@ func CatalogHandler(client *sipgo.Client, clientConfig *config.ClientConfig, req log.Log().Info().Msgf("收到查询指令: %s\n%+v\n", query.CmdType, query) tx.Done() - resp := new(manscdp.CatalogResp) - resp.XMLName = xml.Name{Local: "Response"} - resp.DeviceID = clientConfig.DeviceId - resp.CmdType = "Catalog" - resp.SumNum = "1" - resp.DeviceList = new(manscdp.CateLogDeviceList) - resp.DeviceList.XMLName = xml.Name{Local: "DeviceList"} - resp.DeviceList.Num = "1" - resp.DeviceList.Item = make([]manscdp.CateLogDevice, 0) + device := manscdp.NewCateLogDevice(func(device *manscdp.CateLogDevice) { + device.DeviceID = clientConfig.DeviceId + device.Name = "设备名称" + device.Manufacturer = "设备厂商" + device.ErrCode = "0" + device.Port = fmt.Sprintf("%d", clientConfig.ListenPort) + }) - device := manscdp.CateLogDevice{} - device.DeviceID = clientConfig.DeviceId - device.Name = "设备名称" - device.Manufacturer = "设备厂商" - device.ErrCode = "0" - device.Port = fmt.Sprintf("%d", clientConfig.ListenPort) + list := manscdp.NewCateLogDeviceList([]manscdp.CateLogDevice{*device}) - resp.DeviceList.Item = append(resp.DeviceList.Item, device) - - resp.SN = query.SN + resp := manscdp.NewCatalogResp(1, list, query.SN, clientConfig.DeviceId) marshal, _ := utils.XMLMarshal(resp, "gbk") log.Log().Info().Msgf("回复查询指令: %s\n%+v\n", query.CmdType, resp) diff --git a/pkg/manscdp/catalog.go b/pkg/manscdp/catalog.go index 7acb7b0..2e8eab6 100644 --- a/pkg/manscdp/catalog.go +++ b/pkg/manscdp/catalog.go @@ -1,6 +1,11 @@ package manscdp -import "encoding/xml" +import ( + "encoding/xml" + "git.skcks.cn/Shikong/go-gb28181/pkg/manscdp/cmdtype" + "github.com/duke-git/lancet/v2/datastructure/optional" + "strconv" +) // CatalogReq 定义了查询目录的请求结构 type CatalogReq struct { @@ -10,6 +15,15 @@ type CatalogReq struct { DeviceID string `xml:"DeviceID"` } +func NewCatalogReq(cmdType, sn, deviceID string) *CatalogReq { + return &CatalogReq{ + XMLName: xml.Name{Local: "Query"}, + CmdType: cmdtype.Catalog, + SN: sn, + DeviceID: deviceID, + } +} + // CatalogResp 定义了查询目录的响应结构 type CatalogResp struct { XMLName xml.Name `xml:"Response"` @@ -20,6 +34,18 @@ type CatalogResp struct { DeviceID string `xml:"DeviceID"` } +// NewCatalogResp 创建一个新的 CatalogResp 实例 +func NewCatalogResp(sumNum int, deviceList *CateLogDeviceList, sn, deviceID string) *CatalogResp { + return &CatalogResp{ + XMLName: xml.Name{Local: "Response"}, + CmdType: cmdtype.Catalog, + SumNum: strconv.Itoa(sumNum), + DeviceList: deviceList, + SN: sn, + DeviceID: deviceID, + } +} + // CateLogDeviceList 定义了设备列表的结构 type CateLogDeviceList struct { XMLName xml.Name `xml:"DeviceList"` @@ -27,6 +53,15 @@ type CateLogDeviceList struct { Item []CateLogDevice `xml:"Item"` } +// NewCateLogDeviceList 创建一个新的 CateLogDeviceList 实例 +func NewCateLogDeviceList(items []CateLogDevice) *CateLogDeviceList { + return &CateLogDeviceList{ + XMLName: xml.Name{Local: "DeviceList"}, + Num: strconv.Itoa(len(optional.Of(items).OrElse([]CateLogDevice{}))), + Item: items, + } +} + // CateLogDevice 定义了单个设备的详细信息结构 type CateLogDevice struct { XMLName xml.Name `xml:"Item"` @@ -54,3 +89,37 @@ type CateLogDevice struct { ParentID string `xml:"ParentID"` IPAddress string `xml:"IPAddress"` } + +// NewCateLogDevice 创建一个新的 CateLogDevice 实例,并允许通过传入的函数修改初始值 +func NewCateLogDevice(modifier func(*CateLogDevice)) *CateLogDevice { + device := &CateLogDevice{ + XMLName: xml.Name{Local: "Item"}, + Name: "", + Manufacturer: "", + Model: "", + Owner: "", + Block: "", + Address: "", + Parental: "", + SafetyWay: "", + RegisterWay: "", + CertNum: "", + Certifiable: "", + ErrCode: "", + EndTime: "", + Secrecy: "", + Port: "", + Password: "", + Status: "", + Longitude: "", + Latitude: "", + DeviceID: "", + CivilCode: "", + ParentID: "", + IPAddress: "", + } + if modifier != nil { + modifier(device) + } + return device +} diff --git a/pkg/manscdp/cmdtype/cmdtype.go b/pkg/manscdp/cmdtype/cmdtype.go new file mode 100644 index 0000000..fca99c1 --- /dev/null +++ b/pkg/manscdp/cmdtype/cmdtype.go @@ -0,0 +1,18 @@ +package cmdtype + +type CmdType = string + +const ( + Keepalive CmdType = "Keepalive" + DeviceConfig CmdType = "DeviceConfig" + DeviceControl CmdType = "DeviceControl" + DeviceStatus CmdType = "DeviceStatus" + Catalog CmdType = "Catalog" + Alarm CmdType = "Alarm" + MobilePosition CmdType = "MobilePosition" + Broadcast CmdType = "Broadcast" + RecordInfo CmdType = "RecordInfo" + MediaStatus CmdType = "MediaStatus" + ConfigDownload CmdType = "ConfigDownload" + PresetQuery CmdType = "PresetQuery" +) diff --git a/pkg/manscdp/keepalive.go b/pkg/manscdp/keepalive.go new file mode 100644 index 0000000..f2f6601 --- /dev/null +++ b/pkg/manscdp/keepalive.go @@ -0,0 +1 @@ +package manscdp diff --git a/pkg/utils/charset/charset.go b/pkg/utils/charset/charset.go new file mode 100644 index 0000000..9a2405c --- /dev/null +++ b/pkg/utils/charset/charset.go @@ -0,0 +1,10 @@ +package charset + +type CharSet = string + +const ( + GB2312 = "GB2312" + GBK = "GBK" + UTF8 = "UTF-8" + GB18030 = "GB18030" +) diff --git a/pkg/utils/xml.go b/pkg/utils/xml.go index 1887d43..ac64ee3 100644 --- a/pkg/utils/xml.go +++ b/pkg/utils/xml.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/xml" "fmt" + cs "git.skcks.cn/Shikong/go-gb28181/pkg/utils/charset" "github.com/axgle/mahonia" "golang.org/x/net/html/charset" "golang.org/x/text/encoding/simplifiedchinese" @@ -36,8 +37,8 @@ func XMLMarshal(obj interface{}, charset string) ([]byte, error) { } xmlStr := marshal.String() - cs := strings.ToUpper(charset) - xmlStr = fmt.Sprintf("\r\n%s", cs, xmlStr) + csStr := strings.ToUpper(charset) + xmlStr = fmt.Sprintf("\r\n%s", csStr, xmlStr) xmlBytes := &bytes.Buffer{} err = func() (err error) { @@ -45,11 +46,11 @@ func XMLMarshal(obj interface{}, charset string) ([]byte, error) { if err := recover(); err != nil { var t transform.Transformer switch strings.ToUpper(charset) { - case "GBK": + case cs.GBK: t = simplifiedchinese.GBK.NewEncoder() - case "GB2312": + case cs.GB2312: t = simplifiedchinese.HZGB2312.NewEncoder() - case "GB18030": + case cs.GB18030: t = simplifiedchinese.GB18030.NewEncoder() default: err = fmt.Errorf("unsupported charset: %s", charset)