拆分不同类型到不同的文件

This commit is contained in:
钱纯净
2024-10-28 22:59:29 +08:00
parent 6ec87de116
commit 9eac8cbc1a
8 changed files with 196 additions and 201 deletions

BIN
docs/plan20241028-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

View File

@@ -12,10 +12,10 @@ import (
const (
// Prefix 固定帧头
Prefix = 0x0c
Prefix = 0x0C
// PrefixResp 响应帧头
PrefixResp = 0xb1cb7400
PrefixResp = 0xB1CB7400
)
type Message interface {

52
protocol/model_connect.go Normal file
View File

@@ -0,0 +1,52 @@
package protocol
import (
"errors"
)
var (
MConnect = connect{}
MHeart = heart{}
MStockCount = stockCount{}
MStockQuote = stockQuote{}
MStockList = stockList{}
MStockMinute = stockMinute{}
MStockMinuteTrade = stockMinuteTrade{}
)
type ConnectResp struct {
Info string
}
type connect struct{}
func (connect) Frame() *Frame {
return &Frame{
Control: Control01,
Type: TypeConnect,
Data: []byte{0x01},
}
}
func (connect) Decode(bs []byte) (*ConnectResp, error) {
if len(bs) < 68 {
return nil, errors.New("数据长度不足")
}
//前68字节暂时还不知道是什么
return &ConnectResp{Info: string(UTF8ToGBK(bs[68:]))}, nil
}
/*
*/
type heart struct{}
func (this *heart) Frame() *Frame {
return &Frame{
Control: Control01,
Type: TypeHeart,
}
}

View File

@@ -0,0 +1,25 @@
package protocol
import "errors"
type StockCountResp struct {
Count uint16
}
type stockCount struct{}
// Frame 0c0200000001080008004e04000075c73301
func (this *stockCount) Frame(exchange Exchange) *Frame {
return &Frame{
Control: Control01,
Type: TypeStockCount,
Data: []byte{exchange.Uint8(), 0x0, 0x75, 0xc7, 0x33, 0x01}, //后面的4字节不知道啥意思
}
}
func (this *stockCount) Decode(bs []byte) (*StockCountResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
return &StockCountResp{Count: Uint16(bs)}, nil
}

View File

@@ -0,0 +1,62 @@
package protocol
import (
"errors"
"fmt"
"github.com/injoyai/conv"
)
type StockListResp struct {
Count uint16
List []*Stock
}
type Stock struct {
Name string //股票名称
Code string //股票代码
VolUnit uint16 //未知
DecimalPoint int8 //未知
PreClose float64 //未知
}
func (this *Stock) String() string {
return fmt.Sprintf("%s(%s)", this.Code, this.Name)
}
type stockList struct{}
func (stockList) Frame(exchange Exchange, starts ...uint16) *Frame {
start := conv.DefaultUint16(0, starts...)
return &Frame{
Control: Control01,
Type: TypeStockList,
Data: []byte{exchange.Uint8(), 0x0, uint8(start), uint8(start >> 8)},
}
}
func (stockList) Decode(bs []byte) (*StockListResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
resp := &StockListResp{
Count: Uint16(bs[:2]),
}
bs = bs[2:]
for i := uint16(0); i < resp.Count; i++ {
sec := &Stock{
Code: string(bs[:6]),
VolUnit: Uint16(bs[6:8]),
Name: string(UTF8ToGBK(bs[8:16])),
DecimalPoint: int8(bs[20]),
PreClose: getVolume(Uint32(bs[21:25])),
}
bs = bs[29:]
resp.List = append(resp.List, sec)
}
return resp, nil
}

View File

@@ -0,0 +1,55 @@
package protocol
import (
"errors"
)
type StockMinuteResp struct {
Count uint16
List []PriceLevel
}
type stockMinute struct{}
func (this *stockMinute) Frame(exchange Exchange, code string) (*Frame, error) {
if len(code) != 6 {
return nil, errors.New("股票代码长度错误")
}
codeBs := []byte(code)
codeBs = append(codeBs, 0x0, 0x0, 0x0, 0x0)
return &Frame{
Control: Control01,
Type: TypeStockMinute,
Data: append([]byte{exchange.Uint8(), 0x0}, codeBs...),
}, nil
}
func (this *stockMinute) Decode(bs []byte) (*StockMinuteResp, error) {
if len(bs) < 6 {
return nil, errors.New("数据长度不足")
}
resp := &StockMinuteResp{
Count: Uint16(bs[:2]),
}
//2-6字节是啥?
bs = bs[6:]
price := Price(0)
for i := uint16(0); i < resp.Count; i++ {
bs, price = GetPrice(bs)
var unknown Price
bs, unknown = GetPrice(bs) //这个是什么
_ = unknown
//logs.Debug(price, unknown)
var number int
bs, number = CutInt(bs)
resp.List = append(resp.List, PriceLevel{
Price: price,
Number: number,
})
}
return resp, nil
}

View File

@@ -3,153 +3,9 @@ package protocol
import (
"errors"
"fmt"
"github.com/injoyai/conv"
"github.com/injoyai/logs"
"strings"
)
var (
MConnect = connect{}
MHeart = heart{}
MStockCount = stockCount{}
MStockQuote = stockQuote{}
MStockList = stockList{}
MStockMinute = stockMinute{}
MStockMinuteTrade = stockMinuteTrade{}
)
type ConnectResp struct {
Info string
}
type connect struct{}
func (connect) Frame() *Frame {
return &Frame{
Control: Control01,
Type: TypeConnect,
Data: []byte{0x01},
}
}
func (connect) Decode(bs []byte) (*ConnectResp, error) {
if len(bs) < 68 {
return nil, errors.New("数据长度不足")
}
//前68字节暂时还不知道是什么
return &ConnectResp{Info: string(UTF8ToGBK(bs[68:]))}, nil
}
/*
*/
type heart struct{}
func (this *heart) Frame() *Frame {
return &Frame{
Control: Control01,
Type: TypeHeart,
}
}
/*
*/
type StockCountResp struct {
Count uint16
}
type stockCount struct{}
// Frame 0c0200000001080008004e04000075c73301
func (this *stockCount) Frame(exchange Exchange) *Frame {
return &Frame{
Control: Control01,
Type: TypeStockCount,
Data: []byte{exchange.Uint8(), 0x0, 0x75, 0xc7, 0x33, 0x01}, //后面的4字节不知道啥意思
}
}
func (this *stockCount) Decode(bs []byte) (*StockCountResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
return &StockCountResp{Count: Uint16(bs)}, nil
}
/*
*/
type StockListResp struct {
Count uint16
List []*Stock
}
type Stock struct {
Name string //股票名称
Code string //股票代码
VolUnit uint16 //未知
DecimalPoint int8 //未知
PreClose float64 //未知
}
func (this *Stock) String() string {
return fmt.Sprintf("%s(%s)", this.Code, this.Name)
}
type stockList struct{}
func (stockList) Frame(exchange Exchange, starts ...uint16) *Frame {
start := conv.DefaultUint16(0, starts...)
return &Frame{
Control: Control01,
Type: TypeStockList,
Data: []byte{exchange.Uint8(), 0x0, uint8(start), uint8(start >> 8)},
}
}
func (stockList) Decode(bs []byte) (*StockListResp, error) {
if len(bs) < 2 {
return nil, errors.New("数据长度不足")
}
resp := &StockListResp{
Count: Uint16(bs[:2]),
}
bs = bs[2:]
for i := uint16(0); i < resp.Count; i++ {
sec := &Stock{
Code: string(bs[:6]),
VolUnit: Uint16(bs[6:8]),
Name: string(UTF8ToGBK(bs[8:16])),
DecimalPoint: int8(bs[20]),
PreClose: getVolume(Uint32(bs[21:25])),
}
bs = bs[29:]
resp.List = append(resp.List, sec)
}
return resp, nil
}
/*
*/
type StockQuotesResp []*StockQuote
func (this StockQuotesResp) String() string {
@@ -313,58 +169,3 @@ func (this stockQuote) Decode(bs []byte) StockQuotesResp {
return resp
}
/*
*/
type StockMinuteResp struct {
Count uint16
List []PriceLevel
}
type stockMinute struct{}
func (this *stockMinute) Frame(exchange Exchange, code string) (*Frame, error) {
if len(code) != 6 {
return nil, errors.New("股票代码长度错误")
}
codeBs := []byte(code)
codeBs = append(codeBs, 0x0, 0x0, 0x0, 0x0)
return &Frame{
Control: Control01,
Type: TypeStockMinute,
Data: append([]byte{exchange.Uint8(), 0x0}, codeBs...),
}, nil
}
func (this *stockMinute) Decode(bs []byte) (*StockMinuteResp, error) {
if len(bs) < 6 {
return nil, errors.New("数据长度不足")
}
resp := &StockMinuteResp{
Count: Uint16(bs[:2]),
}
//2-6字节是啥?
bs = bs[6:]
price := Price(0)
for i := uint16(0); i < resp.Count; i++ {
bs, price = GetPrice(bs)
var what Price
bs, what = GetPrice(bs) //这个是什么
logs.Debug(price, what)
var number int
bs, number = CutInt(bs)
resp.List = append(resp.List, PriceLevel{
Price: price,
Number: number,
})
}
return resp, nil
}