提交 e52b4520 编写于 作者: J Jason

将link保存到数据库中,记录 通道ID,方便查询状态,操作等

上级 3212e368
...@@ -3,10 +3,12 @@ package dtu ...@@ -3,10 +3,12 @@ package dtu
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/zgwit/dtu-admin/storage"
"github.com/zgwit/dtu-admin/types" "github.com/zgwit/dtu-admin/types"
"log" "log"
"net" "net"
"sync" "sync"
"time"
) )
...@@ -16,13 +18,12 @@ type Channel struct { ...@@ -16,13 +18,12 @@ type Channel struct {
Error string Error string
listener net.Listener listener net.Listener
packetConn net.PacketConn packetConn net.PacketConn
packetIndexes sync.Map
links sync.Map packetIndexes sync.Map //<Link>
//自增ID links sync.Map
increment int64
} }
func NewChannel(channel *types.Channel) *Channel { func NewChannel(channel *types.Channel) *Channel {
...@@ -110,7 +111,7 @@ func (c *Channel) accept() { ...@@ -110,7 +111,7 @@ func (c *Channel) accept() {
func (c *Channel) receive(conn net.Conn) { func (c *Channel) receive(conn net.Conn) {
client := newConnection(conn) client := newConnection(conn)
client.channel = c client.channel = c
c.storeConnection(client) c.storeLink(client)
buf := make([]byte, 1024) buf := make([]byte, 1024)
for client.conn != nil { for client.conn != nil {
...@@ -126,9 +127,15 @@ func (c *Channel) receive(conn net.Conn) { ...@@ -126,9 +127,15 @@ func (c *Channel) receive(conn net.Conn) {
} }
func (c *Channel) storeConnection(conn *Link) { func (c *Channel) storeLink(conn *Link) {
c.increment++
conn.ID = c.increment lnk := types.Link{
Addr: conn.RemoteAddr.String(),
Channel: c.ID,
Created: time.Now(),
}
storage.DB("link").Save(&lnk)
//根据ID保存 //根据ID保存
c.links.Store(c.ID, conn) c.links.Store(c.ID, conn)
...@@ -155,7 +162,7 @@ func (c *Channel) receivePacket() { ...@@ -155,7 +162,7 @@ func (c *Channel) receivePacket() {
client.channel = c client.channel = c
//根据ID保存 //根据ID保存
c.storeConnection(client) c.storeLink(client)
//根据地址保存,收到UDP包之后,方便索引 //根据地址保存,收到UDP包之后,方便索引
c.packetIndexes.Store(key, client) c.packetIndexes.Store(key, client)
......
...@@ -45,8 +45,7 @@ func (l *Link) checkRegister(buf []byte) error { ...@@ -45,8 +45,7 @@ func (l *Link) checkRegister(buf []byte) error {
} }
} }
//按序号保存索引,供外部使用 //TODO 更新数据库中 serial
connections.Store(serial, l)
return nil return nil
} }
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
var channels sync.Map var channels sync.Map
var connections sync.Map
func Channels() []*Channel { func Channels() []*Channel {
cs := make([]*Channel, 0) cs := make([]*Channel, 0)
......
import { Component, OnInit } from '@angular/core'; import {Component, Input, OnInit} from '@angular/core';
@Component({ @Component({
selector: 'app-link-detail', selector: 'app-link-detail',
...@@ -7,7 +7,10 @@ import { Component, OnInit } from '@angular/core'; ...@@ -7,7 +7,10 @@ import { Component, OnInit } from '@angular/core';
}) })
export class LinkDetailComponent implements OnInit { export class LinkDetailComponent implements OnInit {
constructor() { } @Input() link: any = {};
constructor() {
}
ngOnInit(): void { ngOnInit(): void {
} }
......
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-link-edit',
templateUrl: './link-edit.component.html',
styleUrls: ['./link-edit.component.scss']
})
export class LinkEditComponent implements OnInit {
@Input() link: any = {};
constructor() { }
ngOnInit(): void {
}
}
<p>link works!</p> <nz-space>
<nz-space-item>
<button nz-button (click)="load()">
<i nz-icon nzType="reload"></i>
刷新
</button>
</nz-space-item>
</nz-space>
<nz-table #basicTable [nzData]="links">
<thead>
<tr>
<th>ID</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.name }}</td>
<td>{{ data.serial }}</td>
<td>{{ data.addr}}</td>
<td>
</td>
<td>{{ data.created | amDateFormat:'YYYY-MM-DD HH:mm:ss' }}</td>
<td>
<a>
<i nz-icon nzType="delete"></i>
</a>
<nz-divider nzType="vertical"></nz-divider>
<a (click)="edit(data)">
<i nz-icon nzType="edit"></i>
</a>
</td>
</tr>
</tbody>
</nz-table>
import { Component, OnInit } from '@angular/core'; import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {ApiService} from "../../api.service";
import {NzDrawerService, NzModalService} from "ng-zorro-antd";
import {LinkEditComponent} from "../link-edit/link-edit.component";
import {LinkDetailComponent} from "../link-detail/link-detail.component";
@Component({ @Component({
selector: 'app-link', selector: 'app-link',
...@@ -6,10 +10,43 @@ import { Component, OnInit } from '@angular/core'; ...@@ -6,10 +10,43 @@ import { Component, OnInit } from '@angular/core';
styleUrls: ['./link.component.scss'] styleUrls: ['./link.component.scss']
}) })
export class LinkComponent implements OnInit { export class LinkComponent implements OnInit {
links: [];
constructor() { } constructor(private as: ApiService, private drawer: NzDrawerService) {
}
ngOnInit(): void { ngOnInit(): void {
this.load();
}
load(): void {
this.as.get('links').subscribe(res => {
if (res.ok) {
this.links = res.data;
}
});
} }
edit(l?): void {
this.drawer.create({
nzTitle: l ? '编辑' : '创建',
nzMaskClosable: false,
nzWidth: 500,
nzContent: LinkEditComponent,
nzContentParams: {
link: l || {}
}
});
}
detail(l): void {
this.drawer.create({
nzTitle: '详情',
// nzWidth: 400,
nzContent: LinkDetailComponent,
nzContentParams: {
link: l
}
});
}
} }
...@@ -32,12 +32,13 @@ import {LinkDetailComponent} from './link-detail/link-detail.component'; ...@@ -32,12 +32,13 @@ import {LinkDetailComponent} from './link-detail/link-detail.component';
import {UserEditComponent} from './user-edit/user-edit.component'; import {UserEditComponent} from './user-edit/user-edit.component';
import {LinkMonitorComponent} from './link-monitor/link-monitor.component'; import {LinkMonitorComponent} from './link-monitor/link-monitor.component';
import {NzSpaceModule} from "ng-zorro-antd/space"; import {NzSpaceModule} from "ng-zorro-antd/space";
import {LinkEditComponent} from "./link-edit/link-edit.component";
@NgModule({ @NgModule({
declarations: [MainComponent, DashComponent, declarations: [MainComponent, DashComponent,
ChannelComponent, ChannelDetailComponent, ChannelEditComponent, ChannelComponent, ChannelDetailComponent, ChannelEditComponent,
LinkComponent, LinkDetailComponent, LinkMonitorComponent, LinkComponent, LinkDetailComponent, LinkMonitorComponent, LinkEditComponent,
UserComponent, UserEditComponent, UserComponent, UserEditComponent,
PasswordComponent], PasswordComponent],
imports: [ imports: [
......
package types
import (
"time"
)
type Link struct {
ID int `storm:"increment" json:"id"`
Name string `json:"name"`
Serial string `json:"serial" storm:"index"`
Addr string `json:"addr"`
Channel int `json:"channel"`
Created time.Time `json:"created"`
}
package api
import (
"github.com/gin-gonic/gin"
"github.com/zgwit/dtu-admin/storage"
"github.com/zgwit/dtu-admin/types"
"log"
)
func links(ctx *gin.Context) {
var cs []types.Link
err := storage.DB("link").All(&cs)
if err != nil {
replyError(ctx, err)
return
}
replyOk(ctx, cs)
}
func linkDelete(ctx *gin.Context) {
var pid paramId
if err := ctx.BindUri(&pid); err != nil {
replyError(ctx, err)
return
}
err := storage.DB("link").DeleteStruct(&types.Link{ID: pid.Id})
if err != nil {
replyError(ctx, err)
return
}
//TODO 删除服务
replyOk(ctx, nil)
}
func linkModify(ctx *gin.Context) {
var link types.Link
if err := ctx.ShouldBindJSON(&link); err != nil {
replyError(ctx, err)
return
}
log.Println("update", link)
//TODO 不能全部字段更新,应该先取值,修改,再存入
err := storage.DB("link").Update(&link)
if err != nil {
replyError(ctx, err)
return
}
//TODO 重新启动服务
replyOk(ctx, link)
}
func linkGet(ctx *gin.Context) {
var pid paramId
if err := ctx.BindUri(&pid); err != nil {
replyError(ctx, err)
return
}
var link types.Link
err := storage.DB("link").One("ID", pid.Id, &link)
if err != nil {
replyError(ctx, err)
return
}
replyOk(ctx, link)
}
...@@ -57,11 +57,18 @@ func RegisterRoutes(app *gin.RouterGroup) { ...@@ -57,11 +57,18 @@ func RegisterRoutes(app *gin.RouterGroup) {
app.GET("/channel/:id/start", channelStart) app.GET("/channel/:id/start", channelStart)
app.GET("/channel/:id/stop", channelStop) app.GET("/channel/:id/stop", channelStop)
app.GET("/channel/:id/connections") app.GET("/channel/:id/links")
app.POST("/channel/:id/connections") app.POST("/channel/:id/links")
app.DELETE("/channel/:id/connection/:id2") //关闭连接 app.DELETE("/channel/:id/link/:id2") //关闭连接
app.GET("/channel/:id/connection/:id2/statistic") app.GET("/channel/:id/link/:id2/statistic")
app.GET("/channel/:id/connection/:id2/pipe") //转Websocket透传 app.GET("/channel/:id/link/:id2/pipe") //转Websocket透传
app.GET("/links", links)
app.DELETE("/link/:id", linkDelete)
app.PUT("/link/:id", linkModify)
app.GET("/link/:id", linkGet)
//app.GET("/link/:id/watch", linkWatch)
//app.GET("/link/:id/stop", linkStop)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册