提交 7945bb15 编写于 作者: J Jason

小改动

上级 633a827d
......@@ -111,7 +111,11 @@ func (c *Channel) accept() {
func (c *Channel) receive(conn net.Conn) {
client := newConnection(conn)
client.channel = c
c.storeLink(client)
//TODO 未开启注册,则直接保存
if !c.Register.Enable {
c.storeLink(client)
}
buf := make([]byte, 1024)
for client.conn != nil {
......@@ -162,7 +166,9 @@ func (c *Channel) receivePacket() {
client.channel = c
//根据ID保存
c.storeLink(client)
if !c.Register.Enable {
c.storeLink(client)
}
//根据地址保存,收到UDP包之后,方便索引
c.packetIndexes.Store(key, client)
......
......@@ -5,6 +5,9 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/asdine/storm/v3/q"
"github.com/zgwit/dtu-admin/storage"
"github.com/zgwit/dtu-admin/types"
"log"
"net"
"regexp"
......@@ -12,7 +15,7 @@ import (
)
type Link struct {
ID int64
ID int
Error string
Serial string
......@@ -26,7 +29,6 @@ type Link struct {
lastTime time.Time
channel *Channel
}
func (l *Link) checkRegister(buf []byte) error {
......@@ -45,19 +47,37 @@ func (l *Link) checkRegister(buf []byte) error {
}
}
////TODO 更新数据库中 serial online
//db := storage.DB("link")
//var link types.Link
//err := db.Select(q.Eq("Channel", l.channel.ID), q.Eq("Serial", serial)).First(&link)
//
//storage.DB("link").Update(&types.Link{
// ID: l.ID,
// Name: "",
// Serial: "",
// Addr: "",
// Channel: 0,
// Online: time.Time{},
//})
//配置序列号
l.Serial = serial
//查找数据库同通道,同序列号链接,更新数据库中 serial online
db := storage.DB("link")
var link types.Link
err := db.Select(q.Eq("Channel", l.channel.ID), q.Eq("Serial", serial)).First(&link)
if err == nil {
//更新客户端地址,
err = storage.DB("link").Update(&types.Link{
ID: link.ID,
Addr: l.RemoteAddr.String(),
Online: time.Now(),
})
if err != nil {
log.Println(err)
}
} else {
link = types.Link{
Serial: serial,
Addr: l.RemoteAddr.String(),
Channel: l.channel.ID,
Online: time.Now(),
Created: time.Now(),
}
err = storage.DB("link").Save(&link)
if err != nil {
log.Println(err)
}
l.ID = link.ID
}
return nil
}
......@@ -67,7 +87,7 @@ func (l *Link) onData(buf []byte) {
l.lastTime = time.Now()
//检查注册包
if l.channel.Register.Enable && l.Serial != "" {
if l.channel.Register.Enable && l.Serial == "" {
err := l.checkRegister(buf)
if err != nil {
log.Println(err)
......@@ -82,7 +102,7 @@ func (l *Link) onData(buf []byte) {
hb := l.channel.HeartBeat
//检查心跳包, 判断上次收发时间,是否已经过去心跳间隔
if hb.Enable && time.Now().Sub(l.lastTime) > time.Second * time.Duration(hb.Interval) {
if hb.Enable && time.Now().Sub(l.lastTime) > time.Second*time.Duration(hb.Interval) {
var b []byte
if hb.IsHex {
var e error
......
......@@ -26,7 +26,7 @@ require (
github.com/swaggo/swag v1.6.7
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
golang.org/x/net v0.0.0-20200822124328-c89045814202
golang.org/x/sys v0.0.0-20200831180312-196b9ba8737a // indirect
golang.org/x/tools v0.0.0-20200828161849-5deb26317202 // indirect
google.golang.org/protobuf v1.25.0 // indirect
......
......@@ -11,24 +11,26 @@
<thead>
<tr>
<th>ID</th>
<th>通道ID</th>
<th>名称</th>
<th>序列号</th>
<th>地址</th>
<th>状态</th>
<th>创建时间</th>
<th>上线时间</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of basicTable.data">
<td>{{ data.id }}</td>
<td>{{ data.channel }}</td>
<td>{{ data.name }}</td>
<td>{{ data.serial }}</td>
<td>{{ data.addr}}</td>
<td>
</td>
<td>{{ data.created | amDateFormat:'YYYY-MM-DD HH:mm:ss' }}</td>
<td>{{ data.online | amDateFormat:'YYYY-MM-DD HH:mm:ss' }}</td>
<td>
<a>
<i nz-icon nzType="delete"></i>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册