Skip to content

Commit

Permalink
[fix] segment marker error. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeyanss committed Jun 23, 2023
1 parent 7723c51 commit 0fd389b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 96 deletions.
102 changes: 102 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package api

import (
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"

"github.com/fakeyanss/jt808-server-go/internal/config"
"github.com/fakeyanss/jt808-server-go/internal/protocol/model"
"github.com/fakeyanss/jt808-server-go/internal/server"
"github.com/fakeyanss/jt808-server-go/internal/storage"
)

func Run(serv *server.TCPServer, cfg *config.Config) {
// web server structure
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
cache := storage.GetDeviceCache()
geoCache := storage.GetGeoCache()

router.GET("/device", func(c *gin.Context) {
c.JSON(http.StatusOK, cache.ListDevice())
})

router.GET("/device/:phone/geo", func(c *gin.Context) {
phone := c.Param("phone")

device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}

res := make(map[string]any)
err = mapstructure.Decode(device, &res)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
gis, err := geoCache.GetGeoLatestByPhone(phone)
if err != nil {
return
}
res["gis"] = gis

c.JSON(http.StatusOK, res)
})

router.GET("/device/:phone/params", func(c *gin.Context) {
phone := c.Param("phone")
device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
session, err := storage.GetSession(device.SessionID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
header := model.GenMsgHeader(device, 0x8104, session.GetNextSerialNum())
msg := model.Msg8104{
Header: header,
}
serv.Send(session.ID, &msg)
// todo: read channel from process 0104 msg
})

router.PUT("/device/:phone/params", func(c *gin.Context) {
phone := c.Param("phone")
params := model.DeviceParams{}
if err := c.ShouldBind(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
session, err := storage.GetSession(device.SessionID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
header := model.GenMsgHeader(device, 0x8103, session.GetNextSerialNum())
msg := model.Msg8103{
Header: header,
Parameters: &params,
}
serv.Send(session.ID, &msg)
})

httpAddr := ":" + cfg.Server.Port.HTTPPort

log.Debug().Msgf("Listening and serving HTTP on :%s", cfg.Server.Port.HTTPPort)
err := router.Run(httpAddr)
if err != nil {
log.Error().Err(err).Str("addr", httpAddr).Msg("Fail to run gin router")
os.Exit(1)
}
}
8 changes: 4 additions & 4 deletions internal/config/asset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/protocol/model/msg_0800.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func (m *Msg0800) GetHeader() *MsgHeader {
return m.Header
}

func (m *Msg0800) GenOutgoing(incoming JT808Msg) error {
func (m *Msg0800) GenOutgoing(_ JT808Msg) error {
return nil
}
2 changes: 1 addition & 1 deletion internal/protocol/msg_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (mp *JT808MsgProcessor) Process(ctx context.Context, pkt *model.PacketData)
}

// process segment packet
if !pkt.SegCompleted {
if pkt.Header.IsFragmented() && !pkt.SegCompleted {
return processSegmentPacket(ctx, pkt)
}

Expand Down
92 changes: 2 additions & 90 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package main
import (
"flag"
"fmt"
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/mitchellh/mapstructure"
"github.com/rs/zerolog/log"

"github.com/fakeyanss/jt808-server-go/internal/api"
"github.com/fakeyanss/jt808-server-go/internal/config"
"github.com/fakeyanss/jt808-server-go/internal/protocol/model"
"github.com/fakeyanss/jt808-server-go/internal/server"
"github.com/fakeyanss/jt808-server-go/internal/storage"
"github.com/fakeyanss/jt808-server-go/pkg/logger"
"github.com/fakeyanss/jt808-server-go/pkg/routines"
)
Expand Down Expand Up @@ -50,91 +46,7 @@ func main() {
}
routines.GoSafe(func() { serv.Start() })

// web server structure
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
cache := storage.GetDeviceCache()
geoCache := storage.GetGeoCache()

router.GET("/device", func(c *gin.Context) {
c.JSON(http.StatusOK, cache.ListDevice())
})

router.GET("/device/:phone/geo", func(c *gin.Context) {
phone := c.Param("phone")

device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}

res := make(map[string]any)
err = mapstructure.Decode(device, &res)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
gis, err := geoCache.GetGeoLatestByPhone(phone)
if err != nil {
return
}
res["gis"] = gis

c.JSON(http.StatusOK, res)
})

router.GET("/device/:phone/params", func(c *gin.Context) {
phone := c.Param("phone")
device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
session, err := storage.GetSession(device.SessionID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
header := model.GenMsgHeader(device, 0x8104, session.GetNextSerialNum())
msg := model.Msg8104{
Header: header,
}
serv.Send(session.ID, &msg)
// todo: read channel from process 0104 msg
})

router.PUT("/device/:phone/params", func(c *gin.Context) {
phone := c.Param("phone")
params := model.DeviceParams{}
if err := c.ShouldBind(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
device, err := cache.GetDeviceByPhone(phone)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
return
}
session, err := storage.GetSession(device.SessionID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
}
header := model.GenMsgHeader(device, 0x8103, session.GetNextSerialNum())
msg := model.Msg8103{
Header: header,
Parameters: &params,
}
serv.Send(session.ID, &msg)
})

httpAddr := ":" + cfg.Server.Port.HTTPPort
routines.GoSafe(func() {
log.Debug().Msgf("Listening and serving HTTP on :%s", cfg.Server.Port.HTTPPort)
err = router.Run(httpAddr)
if err != nil {
log.Error().Err(err).Str("addr", httpAddr).Msg("Fail to run gin router")
os.Exit(1)
}
})
routines.GoSafe(func() { api.Run(serv, cfg) })

select {} // block here
}

0 comments on commit 0fd389b

Please sign in to comment.