go808

package module
v0.0.0-...-36f3bb7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2022 License: MIT Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

部标 JT/T808(2013) 协议服务器

go808 是使用 Golang 语言开发的部标 JT/T808(2013) 协议服务器。此项目的目的是为了减少重复工作,加快开发速度。开发者无需关心 JT/T808 协议封包解包,自动进行字节流协议与结构体互转,只需专注具体业务的逻辑处理。

特点

易于扩展,开发者可自行添加自定义协议。

使用简单,几行代码即可启用一个支持 JT/T808 协议的服务器。

安装

$ go get github.com/vladpi/go808

示例

1. 上报位置
package main

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"github.com/vladpi/go808"
	"github.com/vladpi/go808/protocol"
	"github.com/vladpi/go808/protocol/extra"
)

// 处理上报位置
func handleReportLocation(session *go808.Session, message *protocol.Message) {
	// 打印消息
	entity := message.Body.(*protocol.T808_0x0200)
	fields := log.Fields{
		"IccID": message.Header.IccID,
		"警告":    fmt.Sprintf("0x%x", entity.Alarm),
		"状态":    fmt.Sprintf("0x%x", entity.Status),
		"纬度":    entity.Lat,
		"经度":    entity.Lng,
		"海拔":    entity.Altitude,
		"速度":    entity.Speed,
		"方向":    entity.Direction,
		"时间":    entity.Time,
	}

	for _, ext := range entity.Extras {
		switch ext.ID() {
		case extra.Extra_0x01{}.ID():
			fields["行驶里程"] = ext.(*extra.Extra_0x01).Value()
		case extra.Extra_0x02{}.ID():
			fields["剩余油量"] = ext.(*extra.Extra_0x02).Value()
		}
	}
	log.WithFields(fields).Info("上报终端位置信息")

	// 回复平台应答
	session.Reply(message, protocol.T808_0x8100_ResultSuccess)
}

func main() {
	server, _ := go808.NewServer(go808.Options{
		Keepalive:       60,
	})
	server.AddHandler(protocol.MsgT808_0x0200, handleReportLocation)
	server.Run("tcp", 8808)
}
2. 下发->应答回调
package main

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"github.com/vladpi/go808"
	"github.com/vladpi/go808/protocol"
)

// 处理终端鉴权
func handleAuthentication(session *go808.Session, message *protocol.Message) {
	// 回复平台应答
	session.Reply(message, protocol.T808_0x8100_ResultSuccess)

	// 查询终端参数
	session.Request(new(protocol.T808_0x8104), func(answer *protocol.Message) {
		response := answer.Body.(*protocol.T808_0x0104)
		for _, param := range response.Params {
			fmt.Println("参数ID", param.ID())
		}
	})
}

func main() {
	server, _ := go808.NewServer(go808.Options{
		Keepalive:       60,
	})
	server.AddHandler(protocol.MsgT808_0x0200, handleAuthentication)
	server.Run("tcp", 8808)
}
3. 分包合并
package main

import (
	log "github.com/sirupsen/logrus"
	"github.com/vladpi/go808"
	"github.com/vladpi/go808/protocol"
)

// 处理上传媒体
func handleUploadMediaPacket(session *go808.Session, message *protocol.Message) {
	entity := message.Body.(*protocol.T808_0x0801)

	// 读取完整数据包
	fullPacket := make([]byte, 1024*1024)
	entity.Packet.Read(fullPacket[:])

	session.Send(&protocol.T808_0x8800{
		MediaID: entity.MediaID,
	})
}

func main() {
	server, _ := go808.NewServer(go808.Options{
		Keepalive:       60,
		AutoMergePacket: true, // 自动合并分包
	})
	server.AddHandler(protocol.MsgT808_0x0801, handleUploadMediaPacket)
	server.Run("tcp", 8808)
}
4. 自定义协议

TODO...

开发进度

消息ID 消息名称 是否完成
0x0001 终端通用应答 √
0x8001 平台通用应答 √
0x0002 终端心跳 √
0x8003 补传分包请求 √
0x0100 终端注册 √
0x8100 终端注册应答 √
0x0003 终端注销 √
0x0102 终端鉴权 √
0x8103 设置终端参数 √
0x8104 查询终端参数 √
0x0104 查询终端参数应答 √
0x8105 终端控制 √
0x8106 查询指定终端参数 √
0x8107 查询终端属性 √
0x0107 查询终端属性应答 √
0x8108 下发终端升级包 √
0x0108 终端升级结果通知 √
0x0200 位置信息汇报 √
0x8201 位置信息查询 √
0x0201 位置信息查询应答 √
0x8202 临时位置跟踪控制 √
0x8203 人工确认报警消息 √
0x8300 文本信息下发 √
0x8301 事件设置 √
0x0301 事件报告 √
0x8302 提问下发 √
0x0302 提问应答 √
0x8303 信息点播菜单设置 √
0x0303 信息点播/取消 √
0x8304 信息服务 √
0x8400 电话回拨 √
0x8401 设置电话本 √
0x8500 车辆控制 √
0x0500 车辆控制应答 √
0x8600 设置圆形区域 √
0x8601 删除圆形区域 √
0x8602 设置矩形区域 √
0x8603 删除矩形区域 √
0x8604 设置多边形区域 √
0x8605 删除多边形区域 √
0x8606 设置路线 √
0x8607 删除路线 √
0x8700 行驶记录仪数据采集命令 √
0x0700 行驶记录仪数据上传 √
0x8701 行驶记录仪参数下传命令 √
0x0701 电子运单上报 √
0x0702 驾驶员身份信息采集上报 √
0x8702 上报驾驶员身份信息请求 √
0x0704 定位数据批量上传 √
0x0705 CAN 总线数据上传 √
0x0800 多媒体事件信息上传 √
0x0801 多媒体数据上传 √
0x8800 多媒体数据上传应答 √
0x8801 摄像头立即拍摄命令 √
0x0805 摄像头立即拍摄命令应答 √
0x8802 存储多媒体数据检索 √
0x0802 存储多媒体数据检索应答 √
0x8803 存储多媒体数据上传 √
0x8804 录音开始命令 √
0x8805 单条存储多媒体数据检索上传命令 √
0x8900 数据下行透传 √
0x0900 数据上行透传 √
0x0901 数据压缩上报 √
0x8A00 平台 RSA 公钥 √
0x0A00 终端 RSA 公钥 √

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogLevel

func SetLogLevel(level Level) error

设置日志级别

Types

type CountdownTimer

type CountdownTimer struct {
	// contains filtered or unexported fields
}

倒计时器

func NewCountdownTimer

func NewCountdownTimer(seconds int64, callback func(string)) *CountdownTimer

创建倒计时器

func (*CountdownTimer) Remove

func (timer *CountdownTimer) Remove(key string)

删除计时

func (*CountdownTimer) SetExpiration

func (timer *CountdownTimer) SetExpiration(seconds int64)

设置过期时间

func (*CountdownTimer) Update

func (timer *CountdownTimer) Update(key string)

更新时间

type Level

type Level string

日志等级

var (
	PanicLevel Level = "panic"
	FatalLevel Level = "fatal"
	ErrorLevel Level = "error"
	WarnLevel  Level = "warn"
	InfoLevel  Level = "info"
	DebugLevel Level = "debug"
	TraceLevel Level = "trace"
)

type MessageHandler

type MessageHandler func(*Session, *protocol.Message)

type MultipartFile

type MultipartFile struct {
	IccID uint64
	MsgID protocol.MsgID
	Tag   uint32
	Sum   uint16
}

多分包文件

func (MultipartFile) IsFull

func (m MultipartFile) IsFull() bool

是否完整

func (MultipartFile) Merge

func (m MultipartFile) Merge() (io.ReadCloser, error)

合并文件

func (MultipartFile) Write

func (m MultipartFile) Write(seq uint16, data []byte) error

写入分包文件

type Options

type Options struct {
	Keepalive       int64
	AutoMergePacket bool
	CloseHandler    func(*Session)
	PrivateKey      *rsa.PrivateKey
}

服务器选项

type Protocol

type Protocol struct {
	// contains filtered or unexported fields
}

func (Protocol) NewCodec

func (p Protocol) NewCodec(rw io.ReadWriter) (link.Codec, error)

创建编解码器

type ProtocolCodec

type ProtocolCodec struct {
	// contains filtered or unexported fields
}

编解码器

func (*ProtocolCodec) Close

func (codec *ProtocolCodec) Close() error

关闭读写

func (*ProtocolCodec) GetPublicKey

func (codec *ProtocolCodec) GetPublicKey() *rsa.PublicKey

获取RSA公钥

func (*ProtocolCodec) Receive

func (codec *ProtocolCodec) Receive() (interface{}, error)

接收消息

func (*ProtocolCodec) Send

func (codec *ProtocolCodec) Send(msg interface{}) error

发送消息

func (*ProtocolCodec) SetPublicKey

func (codec *ProtocolCodec) SetPublicKey(publicKey *rsa.PublicKey)

设置RSA公钥

type Server

type Server struct {
	// contains filtered or unexported fields
}

协议服务器

func NewServer

func NewServer(options Options) (*Server, error)

创建服务

func (*Server) AddHandler

func (server *Server) AddHandler(msgID protocol.MsgID, handler MessageHandler)

添加消息处理

func (*Server) Broadcast

func (server *Server) Broadcast(entity protocol.Entity) int

广播消息

func (*Server) GetPrivateKey

func (server *Server) GetPrivateKey() *rsa.PrivateKey

获取平台私钥

func (*Server) GetSession

func (server *Server) GetSession(id uint64) (*Session, bool)

获取Session

func (*Server) Run

func (server *Server) Run(network string, port int) error

运行服务

func (*Server) Stop

func (server *Server) Stop()

停止服务

type Session

type Session struct {
	UserData interface{}
	// contains filtered or unexported fields
}

终端会话

func (*Session) Close

func (session *Session) Close() error

关闭连接

func (*Session) GetPublicKey

func (session *Session) GetPublicKey() *rsa.PublicKey

获取RSA公钥

func (*Session) GetServer

func (session *Session) GetServer() *Server

获取服务实例

func (*Session) ID

func (session *Session) ID() uint64

获取ID

func (*Session) Reply

func (session *Session) Reply(msg *protocol.Message, result protocol.Result) (uint16, error)

回复消息

func (*Session) Request

func (session *Session) Request(entity protocol.Entity, cb func(answer *protocol.Message)) (uint16, error)

发起请求

func (*Session) Send

func (session *Session) Send(entity protocol.Entity) (uint16, error)

发送消息

func (*Session) SetPublicKey

func (session *Session) SetPublicKey(publicKey *rsa.PublicKey)

设置RSA公钥

Directories

Path Synopsis
example
client command
server command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址