Skip to content

Commit

Permalink
chg demo
Browse files Browse the repository at this point in the history
  • Loading branch information
MacBookAirM2 committed Mar 13, 2023
1 parent 662c2b4 commit 4ac10e8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 121 deletions.
11 changes: 6 additions & 5 deletions examples/zinx_decoder/bili/router/bili0x10router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
Expand All @@ -18,8 +19,8 @@ func (this *Data0x10Router) Handle(request ziface.IRequest) {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case BiliData:
_data := _response.(BiliData)
case decode.HtlvCrcData:
_data := _response.(decode.HtlvCrcData)
//zlog.Ins().DebugF("Data0x10Router %v \n", _data)
buffer := pack10(_data)
request.GetConnection().Send(buffer)
Expand All @@ -29,13 +30,13 @@ func (this *Data0x10Router) Handle(request ziface.IRequest) {

// 头码 功能码 数据长度 Body CRC
// A2 10 0E 0102030405060708091011121314 050B
func pack10(_data BiliData) []byte {
func pack10(_data decode.HtlvCrcData) []byte {
buffer := bytes.NewBuffer([]byte{})
buffer.WriteByte(0xA1)
buffer.WriteByte(_data.funcode)
buffer.WriteByte(_data.Funcode)
buffer.WriteByte(0x1E)
//3~9:唯一设备码 将IMEI码转换为16进制
buffer.Write(_data.body[:7])
buffer.Write(_data.Body[:7])
//10~14:园区代码 后台根据幼儿园生成的唯一代码
buffer.Write([]byte{10, 11, 12, 13, 14})
//15~18:时间戳 实际当前北京时间的时间戳,转换为16进制
Expand Down
11 changes: 6 additions & 5 deletions examples/zinx_decoder/bili/router/bili0x13router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/znet"
)
Expand All @@ -17,8 +18,8 @@ func (this *Data0x13Router) Handle(request ziface.IRequest) {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case BiliData:
_data := _response.(BiliData)
case decode.HtlvCrcData:
_data := _response.(decode.HtlvCrcData)
fmt.Println("Data0x13Router", _data)
buffer := pack13(_data)
request.GetConnection().Send(buffer)
Expand All @@ -28,13 +29,13 @@ func (this *Data0x13Router) Handle(request ziface.IRequest) {

// 头码 功能码 数据长度 Body CRC
// A2 10 0E 0102030405060708091011121314 050B
func pack13(_data BiliData) []byte {
func pack13(_data decode.HtlvCrcData) []byte {
buffer := bytes.NewBuffer([]byte{})
buffer.WriteByte(0xA1)
buffer.WriteByte(_data.funcode)
buffer.WriteByte(_data.Funcode)
buffer.WriteByte(0x0E)
//3~9:3~6:用户卡号 用户IC卡卡号
buffer.Write(_data.body[:4])
buffer.Write(_data.Body[:4])
//7:卡状态: 0x00-未绑定(如服务器未查询到该IC卡时)
//0x01-已绑定
//0x02-解除绑定(如服务器查询到该IC卡解除绑定时下发)
Expand Down
11 changes: 6 additions & 5 deletions examples/zinx_decoder/bili/router/bili0x14router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/znet"
)
Expand All @@ -17,8 +18,8 @@ func (this *Data0x14Router) Handle(request ziface.IRequest) {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case BiliData:
_data := _response.(BiliData)
case decode.HtlvCrcData:
_data := _response.(decode.HtlvCrcData)
fmt.Println("Data0x14Router", _data)
buffer := pack14(_data)
request.GetConnection().Send(buffer)
Expand All @@ -28,9 +29,9 @@ func (this *Data0x14Router) Handle(request ziface.IRequest) {

// 头码 功能码 数据长度 Body CRC
// A2 10 0E 0102030405060708091011121314 050B
func pack14(_data BiliData) []byte {
_data.data[0] = 0xA1
buffer := bytes.NewBuffer(_data.data[:len(_data.data)-2])
func pack14(_data decode.HtlvCrcData) []byte {
_data.Data[0] = 0xA1
buffer := bytes.NewBuffer(_data.Data[:len(_data.Data)-2])
crc := utils.GetCrC(buffer.Bytes())
buffer.Write(crc)
return buffer.Bytes()
Expand Down
11 changes: 6 additions & 5 deletions examples/zinx_decoder/bili/router/bili0x15router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/znet"
)
Expand All @@ -17,8 +18,8 @@ func (this *Data0x15Router) Handle(request ziface.IRequest) {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case BiliData:
_data := _response.(BiliData)
case decode.HtlvCrcData:
_data := _response.(decode.HtlvCrcData)
fmt.Println("Data0x15Router", _data)
buffer := pack15(_data)
request.GetConnection().Send(buffer)
Expand All @@ -28,13 +29,13 @@ func (this *Data0x15Router) Handle(request ziface.IRequest) {

// 头码 功能码 数据长度 Body CRC
// A2 10 0E 0102030405060708091011121314 050B
func pack15(_data BiliData) []byte {
func pack15(_data decode.HtlvCrcData) []byte {
buffer := bytes.NewBuffer([]byte{})
buffer.WriteByte(0xA1)
buffer.WriteByte(_data.funcode)
buffer.WriteByte(_data.Funcode)
buffer.WriteByte(0x26)
//3~9:设备代码 将IMEI码转换为16进制
buffer.Write(_data.body[:7])
buffer.Write(_data.Body[:7])
//10:机型代码 A8(即热式幼儿园机)
buffer.WriteByte(0xA8)
//11:主机状态1 Bit0:0-待机中,1-运行中
Expand Down
11 changes: 6 additions & 5 deletions examples/zinx_decoder/bili/router/bili0x16router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/znet"
)
Expand All @@ -17,8 +18,8 @@ func (this *Data0x16Router) Handle(request ziface.IRequest) {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case BiliData:
_data := _response.(BiliData)
case decode.HtlvCrcData:
_data := _response.(decode.HtlvCrcData)
fmt.Println("Data0x16Router", _data)
buffer := pack16(_data)
request.GetConnection().Send(buffer)
Expand All @@ -28,9 +29,9 @@ func (this *Data0x16Router) Handle(request ziface.IRequest) {

// 头码 功能码 数据长度 Body CRC
// A2 10 0E 0102030405060708091011121314 050B
func pack16(_data BiliData) []byte {
_data.data[0] = 0xA1
buffer := bytes.NewBuffer(_data.data[:len(_data.data)-2])
func pack16(_data decode.HtlvCrcData) []byte {
_data.Data[0] = 0xA1
buffer := bytes.NewBuffer(_data.Data[:len(_data.Data)-2])
crc := utils.GetCrC(buffer.Bytes())
buffer.Write(crc)
return buffer.Bytes()
Expand Down
83 changes: 0 additions & 83 deletions examples/zinx_decoder/bili/router/biliinterceptor.go

This file was deleted.

35 changes: 22 additions & 13 deletions examples/zinx_decoder/decode/htlvcrcdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
package decode

import (
"encoding/hex"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/ziface"
)

const HEADER_SIZE = 5

type HtlvCrcData struct {
head byte //头码
funcode byte //功能码
length byte //数据长度
data []byte //数据内容
crc []byte //CRC校验
Data []byte //数据内容
Head byte //头码
Funcode byte //功能码
Length byte //数据长度
Body []byte //数据内容
Crc []byte //CRC校验
}

type HtlvCrcDecoder struct {
Expand All @@ -49,16 +52,22 @@ func (this *HtlvCrcDecoder) Intercept(chain ziface.Chain) ziface.Response {
data := iMessage.GetData()
fmt.Println("1htlvData", data)
datasize := len(data)
htlvData := HtlvCrcData{}
htlvData := HtlvCrcData{
Data: data,
}
if datasize >= HEADER_SIZE {
htlvData.head = data[0]
htlvData.funcode = data[1]
htlvData.length = data[2]
htlvData.data = data[3 : 3+htlvData.length]
htlvData.crc = data[htlvData.length+3 : datasize]
iMessage.SetMsgID(uint32(htlvData.funcode)) //funcode作为msgID
htlvData.Head = data[0]
htlvData.Funcode = data[1]
htlvData.Length = data[2]
htlvData.Body = data[3 : datasize-2]
htlvData.Crc = data[datasize-2 : datasize]
if !utils.CheckCRC(data[:datasize-2], htlvData.Crc) {
fmt.Println("crc校验失败", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc))
return nil
}
iMessage.SetMsgID(uint32(htlvData.Funcode))
iRequest.SetResponse(htlvData)
fmt.Println("2htlvData", htlvData)
//zlog.Ins().DebugF("2htlvData %s \n", hex.EncodeToString(htlvData.data))
}
}
}
Expand Down

0 comments on commit 4ac10e8

Please sign in to comment.