结构调整

This commit is contained in:
shikong 2025-01-25 16:31:08 +08:00
parent 529e9fc375
commit 59fb38eff8
Signed by: Shikong
GPG Key ID: BD85FF18B373C341
7 changed files with 124 additions and 25 deletions

View File

@ -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
```

View File

@ -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)

View File

@ -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
}

View File

@ -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"
)

1
pkg/manscdp/keepalive.go Normal file
View File

@ -0,0 +1 @@
package manscdp

View File

@ -0,0 +1,10 @@
package charset
type CharSet = string
const (
GB2312 = "GB2312"
GBK = "GBK"
UTF8 = "UTF-8"
GB18030 = "GB18030"
)

View File

@ -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("<?xml version=\"1.0\" encoding=\"%s\" ?>\r\n%s", cs, xmlStr)
csStr := strings.ToUpper(charset)
xmlStr = fmt.Sprintf("<?xml version=\"1.0\" encoding=\"%s\" ?>\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)