结构调整
This commit is contained in:
parent
529e9fc375
commit
59fb38eff8
10
README.MD
10
README.MD
@ -2,3 +2,13 @@
|
|||||||
```shell
|
```shell
|
||||||
go generate -x
|
go generate -x
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 运行项目
|
||||||
|
```shell
|
||||||
|
go run cmd/client/main.go
|
||||||
|
```
|
||||||
|
|
||||||
|
### 编译项目
|
||||||
|
```shell
|
||||||
|
go build -o bin/client cmd/client/main.go
|
||||||
|
```
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package message
|
package message
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.skcks.cn/Shikong/go-gb28181/pkg/config"
|
"git.skcks.cn/Shikong/go-gb28181/pkg/config"
|
||||||
"git.skcks.cn/Shikong/go-gb28181/pkg/log"
|
"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)
|
log.Log().Info().Msgf("收到查询指令: %s\n%+v\n", query.CmdType, query)
|
||||||
tx.Done()
|
tx.Done()
|
||||||
|
|
||||||
resp := new(manscdp.CatalogResp)
|
device := manscdp.NewCateLogDevice(func(device *manscdp.CateLogDevice) {
|
||||||
resp.XMLName = xml.Name{Local: "Response"}
|
device.DeviceID = clientConfig.DeviceId
|
||||||
resp.DeviceID = clientConfig.DeviceId
|
device.Name = "设备名称"
|
||||||
resp.CmdType = "Catalog"
|
device.Manufacturer = "设备厂商"
|
||||||
resp.SumNum = "1"
|
device.ErrCode = "0"
|
||||||
resp.DeviceList = new(manscdp.CateLogDeviceList)
|
device.Port = fmt.Sprintf("%d", clientConfig.ListenPort)
|
||||||
resp.DeviceList.XMLName = xml.Name{Local: "DeviceList"}
|
})
|
||||||
resp.DeviceList.Num = "1"
|
|
||||||
resp.DeviceList.Item = make([]manscdp.CateLogDevice, 0)
|
|
||||||
|
|
||||||
device := manscdp.CateLogDevice{}
|
list := manscdp.NewCateLogDeviceList([]manscdp.CateLogDevice{*device})
|
||||||
device.DeviceID = clientConfig.DeviceId
|
|
||||||
device.Name = "设备名称"
|
|
||||||
device.Manufacturer = "设备厂商"
|
|
||||||
device.ErrCode = "0"
|
|
||||||
device.Port = fmt.Sprintf("%d", clientConfig.ListenPort)
|
|
||||||
|
|
||||||
resp.DeviceList.Item = append(resp.DeviceList.Item, device)
|
resp := manscdp.NewCatalogResp(1, list, query.SN, clientConfig.DeviceId)
|
||||||
|
|
||||||
resp.SN = query.SN
|
|
||||||
|
|
||||||
marshal, _ := utils.XMLMarshal(resp, "gbk")
|
marshal, _ := utils.XMLMarshal(resp, "gbk")
|
||||||
log.Log().Info().Msgf("回复查询指令: %s\n%+v\n", query.CmdType, resp)
|
log.Log().Info().Msgf("回复查询指令: %s\n%+v\n", query.CmdType, resp)
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package manscdp
|
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 定义了查询目录的请求结构
|
// CatalogReq 定义了查询目录的请求结构
|
||||||
type CatalogReq struct {
|
type CatalogReq struct {
|
||||||
@ -10,6 +15,15 @@ type CatalogReq struct {
|
|||||||
DeviceID string `xml:"DeviceID"`
|
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 定义了查询目录的响应结构
|
// CatalogResp 定义了查询目录的响应结构
|
||||||
type CatalogResp struct {
|
type CatalogResp struct {
|
||||||
XMLName xml.Name `xml:"Response"`
|
XMLName xml.Name `xml:"Response"`
|
||||||
@ -20,6 +34,18 @@ type CatalogResp struct {
|
|||||||
DeviceID string `xml:"DeviceID"`
|
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 定义了设备列表的结构
|
// CateLogDeviceList 定义了设备列表的结构
|
||||||
type CateLogDeviceList struct {
|
type CateLogDeviceList struct {
|
||||||
XMLName xml.Name `xml:"DeviceList"`
|
XMLName xml.Name `xml:"DeviceList"`
|
||||||
@ -27,6 +53,15 @@ type CateLogDeviceList struct {
|
|||||||
Item []CateLogDevice `xml:"Item"`
|
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 定义了单个设备的详细信息结构
|
// CateLogDevice 定义了单个设备的详细信息结构
|
||||||
type CateLogDevice struct {
|
type CateLogDevice struct {
|
||||||
XMLName xml.Name `xml:"Item"`
|
XMLName xml.Name `xml:"Item"`
|
||||||
@ -54,3 +89,37 @@ type CateLogDevice struct {
|
|||||||
ParentID string `xml:"ParentID"`
|
ParentID string `xml:"ParentID"`
|
||||||
IPAddress string `xml:"IPAddress"`
|
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
|
||||||
|
}
|
||||||
|
18
pkg/manscdp/cmdtype/cmdtype.go
Normal file
18
pkg/manscdp/cmdtype/cmdtype.go
Normal 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
1
pkg/manscdp/keepalive.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package manscdp
|
10
pkg/utils/charset/charset.go
Normal file
10
pkg/utils/charset/charset.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package charset
|
||||||
|
|
||||||
|
type CharSet = string
|
||||||
|
|
||||||
|
const (
|
||||||
|
GB2312 = "GB2312"
|
||||||
|
GBK = "GBK"
|
||||||
|
UTF8 = "UTF-8"
|
||||||
|
GB18030 = "GB18030"
|
||||||
|
)
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
cs "git.skcks.cn/Shikong/go-gb28181/pkg/utils/charset"
|
||||||
"github.com/axgle/mahonia"
|
"github.com/axgle/mahonia"
|
||||||
"golang.org/x/net/html/charset"
|
"golang.org/x/net/html/charset"
|
||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
@ -36,8 +37,8 @@ func XMLMarshal(obj interface{}, charset string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
xmlStr := marshal.String()
|
xmlStr := marshal.String()
|
||||||
cs := strings.ToUpper(charset)
|
csStr := strings.ToUpper(charset)
|
||||||
xmlStr = fmt.Sprintf("<?xml version=\"1.0\" encoding=\"%s\" ?>\r\n%s", cs, xmlStr)
|
xmlStr = fmt.Sprintf("<?xml version=\"1.0\" encoding=\"%s\" ?>\r\n%s", csStr, xmlStr)
|
||||||
|
|
||||||
xmlBytes := &bytes.Buffer{}
|
xmlBytes := &bytes.Buffer{}
|
||||||
err = func() (err error) {
|
err = func() (err error) {
|
||||||
@ -45,11 +46,11 @@ func XMLMarshal(obj interface{}, charset string) ([]byte, error) {
|
|||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
var t transform.Transformer
|
var t transform.Transformer
|
||||||
switch strings.ToUpper(charset) {
|
switch strings.ToUpper(charset) {
|
||||||
case "GBK":
|
case cs.GBK:
|
||||||
t = simplifiedchinese.GBK.NewEncoder()
|
t = simplifiedchinese.GBK.NewEncoder()
|
||||||
case "GB2312":
|
case cs.GB2312:
|
||||||
t = simplifiedchinese.HZGB2312.NewEncoder()
|
t = simplifiedchinese.HZGB2312.NewEncoder()
|
||||||
case "GB18030":
|
case cs.GB18030:
|
||||||
t = simplifiedchinese.GB18030.NewEncoder()
|
t = simplifiedchinese.GB18030.NewEncoder()
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unsupported charset: %s", charset)
|
err = fmt.Errorf("unsupported charset: %s", charset)
|
||||||
|
Loading…
Reference in New Issue
Block a user