读source code to learn GOLang
Fold structure, as follows:
/Users/mac/Downloads/Jt808-master/client_sample.go
package main // 定义包名
import (
. "Jt808/common"
"Jt808/service/handler"
"Jt808/tcp"
"fmt"
"github.com/vaughan0/go-ini" // can import url as lib
"io/ioutil"
"jt808"
"log"
"os"
"strconv"
"strings"
)
var GlobalCfg, _ = ini.LoadFile("conf/config.ini") // read config, _ means not deal the result.
func main() {
logger := log.Logger{}
//读配置文件
server, ok := GlobalCfg.Get("COMMON", "server") // <ok : > means if get success result ,if OK = TRue, sever has value ,else NULL.
if !ok {
fmt.Println("server ip/port not set")
return
}
_startImei, ok := GlobalCfg.Get("COMMON", "start_imei")
if !ok {
fmt.Println("start imei not set")
return
}
points, err := initRoute("conf/route.txt")
if err != nil {
fmt.Printf("routes is not ok ")
return
}
simpleTcpClinet := tcp.New(server, 1000)
// 生成一个模拟器
terminal := jt808.TerminalInfo{}
terminal.SetGpsInterval(10).SetHeartBeatInterval(180).SetImei(_startImei)
err = simpleTcpClinet.HandleService(handler.HandleJT808Msg).Set("device", terminal).Set("routes", &points).Use(&terminal).SetTimeout(1000).Dial()
if err != nil {
logger.Println(err)
return
}
simpleTcpClinet.Run()
}
func initRoute(filePath string) ([]Point, error) { //define func funname(parameters) (return <can be a tuple,or parameters>)
bytes, err := readAll(filePath)
if err != nil {
return nil, err
}
route := string(bytes) //将读入的字节转换成字符串
points := strings.Split(route, ";") //将每组字符串以;标记分开
pos := make([]Point, len(points), len(points)) //定义一个pos切片
for index, po := range points {
values := strings.Split(po, ",") //将每组数据 用,号分开之后存入values,
//fmt.Println(len(values), index)
if len(values) != 5 { //判断每组数据的个数,小于5个说明数据不完整,string类型
fmt.Println("unknown format")
continue
}
pos[index].Lon, _ = strconv.ParseFloat(values[0], 64)
//pos[index].lat, _ = strconv.ParseFloat(values[1], 64)
//fmt.Println(pos[index].lon)
pos[index].Lat, _ = strconv.ParseFloat(values[1], 64)
pos[index].Speed = 0
}
return pos, err
}
func readAll(filePath string) ([]byte, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
///Users/mac/Downloads/Jt808-master/vendor/jt808/consts/const.go
package Jtconsts // 定义包名
const ( //
Msg_Terminal_Register int = 0x0100 //终端注册
Msg_Terminal_Logout int = 0x0003 //终端注销
Msg_Terminal_Auth int = 0x0102 //终端鉴权
Msg_Terminal_Gps_Up int = 0x0200 //位置信息汇报
Msg_Terminal_Gps_Batch_Up int = 0x0704 //批量数据上传
Msg_Terminal_Heartbeat int = 0x0002 //批量数据上传
Msg_Terminal_Identifer byte = 0x7e // 标识符
// 平台通用应答
Cmd_Common_Resp int = 0x8001
// 终端注册应答
Cmd_Terminal_Tegister_Resp int = 0x8100
// 设置终端参数
Cmd_Terminal_Param_Settings int = 0X8103
// 查询终端参数
Cmd_Terminal_Param_Query int = 0x8104
)
// 定义一个结构体,并改变结构体的成员
package model
import (
"Jt808/utils"
"bytes"
)
type TerminalGpsBody struct {
warn_mark int
status int
lat float64
lon float64
high int
speed int
heading int
timestr string
}
func (t *TerminalGpsBody) SetLat(lat float64) *TerminalGpsBody {
t.lat = lat
return t
}
func (t *TerminalGpsBody) SetLon(lon float64) *TerminalGpsBody {
t.lon = lon
return t
}
func (t *TerminalGpsBody) SetWarnMark(warn int) *TerminalGpsBody {
t.warn_mark = warn
return t
}
func (t *TerminalGpsBody) SetStatus(status int) *TerminalGpsBody {
t.status = status
return t
}
func (t *TerminalGpsBody) SetHigh(hight int) *TerminalGpsBody {
t.high = hight
return t
}
func (t *TerminalGpsBody) SetHeading(heading int) *TerminalGpsBody {
t.heading = heading
return t
}
func (t *TerminalGpsBody) SetSpeed(speed int) *TerminalGpsBody {
t.speed = speed
return t
}
func (t *TerminalGpsBody) SetTime(timestr string) *TerminalGpsBody {
t.timestr = timestr
return t
}
// 结构体的方法
func (t *TerminalGpsBody) GetBytes() []byte {
var buff = bytes.Buffer{}
if len(t.timestr) < 12 {
return buff.Bytes()
}
buff.Write(utils.IntTo4byte(t.warn_mark))
buff.Write(utils.IntTo4byte(t.status))
buff.Write(utils.IntTo4byte(int(t.lat * 1000000)))
buff.Write(utils.IntTo4byte(int(t.lon * 1000000)))
buff.Write(utils.IntTo2byte(t.high))
buff.Write(utils.IntTo2byte(t.speed))
buff.Write(utils.IntTo2byte(t.heading))
buff.Write(utils.EncodeCBCDFromString(t.timestr))
return buff.Bytes()
}
、、、、
package model
import (
"encoding/hex"
"testing"
)
func TestTerminalGpsBody_GetBytes(t *testing.T) {
dev := &TerminalGpsBody{} // 声明一个结构体对象实体
dev.SetTime("170515162830")
t.Error(hex.EncodeToString(dev.GetBytes()))
}