提交 28d38829 编写于 作者: J Jason

修改了好多

上级 d5d0d6c7
......@@ -32,7 +32,7 @@ func NewChannel(channel *types.Channel) *Channel {
}
func (c *Channel) Open() error {
if c.IsServer {
if c.Net.IsServer {
return c.Listen()
} else {
return c.Dial()
......@@ -40,7 +40,7 @@ func (c *Channel) Open() error {
}
func (c *Channel) Dial() error {
conn, err := net.Dial(c.Net, c.Addr)
conn, err := net.Dial(c.Net.Type, c.Net.Addr)
if err != nil {
return err
}
......@@ -54,16 +54,16 @@ func (c *Channel) Dial() error {
func (c *Channel) Listen() error {
var err error
switch c.Net {
switch c.Net.Type {
case "tcp", "tcp4", "tcp6", "unix":
c.listener, err = net.Listen(c.Net, c.Addr)
c.listener, err = net.Listen(c.Net.Type, c.Net.Addr)
if err != nil {
return err
}
go c.accept()
case "udp", "udp4", "udp6", "unixgram":
c.packetConn, err = net.ListenPacket(c.Net, c.Addr)
c.packetConn, err = net.ListenPacket(c.Net.Type, c.Net.Addr)
if err != nil {
return err
......
......@@ -55,7 +55,7 @@ func CreateChannel(c *types.Channel) (*Channel, error) {
return startChannel(c)
}
func GetChannel(id int64) (*Channel, error) {
func GetChannel(id int) (*Channel, error) {
v, ok := channels.Load(id)
if !ok {
return nil, errors.New("通道不存在")
......@@ -63,7 +63,7 @@ func GetChannel(id int64) (*Channel, error) {
return v.(*Channel), nil
}
func DeleteChannel(id int64) error {
func DeleteChannel(id int) error {
v, ok := channels.Load(id)
if !ok {
return errors.New("通道不存在")
......
<form nz-form nzLayout="horizontal" [formGroup]="validateForm" (ngSubmit)="submit()">
<nz-form-item>
<nz-form-label>名称</nz-form-label>
<nz-form-control nzErrorTip="请输入名称">
<input nz-input formControlName="name" placeholder="名称" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>序列号</nz-form-label>
<nz-form-control nzErrorTip="请输入序列号">
<input nz-input formControlName="serial" placeholder="序列号" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>网络类型</nz-form-label>
<nz-form-control nzErrorTip="请输入类型">
<input nz-input formControlName="net" placeholder="下拉" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>地址</nz-form-label>
<nz-form-control nzErrorTip="请输入地址">
<input nz-input formControlName="addr" placeholder=":1843" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>服务端</nz-form-label>
<nz-form-control nzErrorTip="请勾选">
<nz-switch formControlName="is_server"></nz-switch>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>心跳</nz-form-label>
<nz-form-control nzErrorTip="请勾选">
<label nz-checkbox formControlName="heart_beat.enable">
<span>启用</span>
</label>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>注册包</nz-form-label>
<nz-form-control nzErrorTip="请勾选">
<label nz-checkbox formControlName="register.enable">
<span>启用</span>
</label>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control>
<button nz-button nzType="primary">保存</button>
</nz-form-control>
</nz-form-item>
</form>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="4">
名称
</div>
<div nz-col nzSpan="20">
<input nz-input [(ngModel)]="channel.name" placeholder="名称"/>
</div>
</div>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="4">
网络
</div>
<div nz-col nzSpan="20">
<nz-input-group style="display: flex">
<nz-select [(ngModel)]="channel.net.is_server">
<nz-option nzLabel="服务端" [nzValue]="true"></nz-option>
<nz-option nzLabel="客户端" [nzValue]="false"></nz-option>
</nz-select>
<nz-select [(ngModel)]="channel.net.type" style="width:80px;">
<nz-option nzLabel="TCP" nzValue="tcp"></nz-option>
<nz-option nzLabel="UDP" nzValue="udp"></nz-option>
</nz-select>
<input nz-input [(ngModel)]="channel.net.addr" placeholder=":1843" style="flex: 1;">
</nz-input-group>
</div>
</div>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="4">
禁用
</div>
<div nz-col nzSpan="20">
<nz-switch [(ngModel)]="channel.disabled"></nz-switch>
</div>
</div>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="4">
注册包
</div>
<div nz-col nzSpan="20">
<nz-switch [(ngModel)]="channel.register.enable"></nz-switch>
</div>
</div>
<div *ngIf="channel.register.enable">
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="6" nzOffset="2">
正则表达式
</div>
<div nz-col nzSpan="16">
<input nz-input [(ngModel)]="channel.register.regex"/>
</div>
</div>
</div>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="4">
心跳包
</div>
<div nz-col nzSpan="20">
<nz-switch [(ngModel)]="channel.heart_beat.enable"></nz-switch>
</div>
</div>
<div *ngIf="channel.heart_beat.enable">
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="6" nzOffset="2">
间隔(秒)
</div>
<div nz-col nzSpan="16">
<nz-input-number [(ngModel)]="channel.heart_beat.interval" [nzMin]="5" [nzMax]="60"
[nzStep]="1"></nz-input-number>
</div>
</div>
<div class="item" nz-row nzAlign="middle">
<div nz-col nzSpan="6" nzOffset="2">
内容(十六进制)
</div>
<div nz-col nzSpan="16">
<input nz-input [(ngModel)]="channel.heart_beat.content"/>
</div>
</div>
</div>
<button nz-button nzType="primary" (click)="submit()">保存</button>
......@@ -12,40 +12,29 @@ export class ChannelEditComponent implements OnInit {
@Input() channel: any = {};
validateForm!: FormGroup;
constructor(private as: ApiService, private fb: FormBuilder, private drawerRef: NzDrawerRef<string>) {
constructor(private as: ApiService, private drawerRef: NzDrawerRef<string>) {
}
ngOnInit(): void {
this.initForm({});
this.initChannel({});
if (this.channel.id) {
this.as.get('channel/' + this.channel.id).subscribe(res => {
this.channel = res.data;
this.initForm(this.channel);
this.initChannel(this.channel);
});
}
}
submit(): void {
for (const i in this.validateForm.controls) {
this.validateForm.controls[i].markAsDirty();
this.validateForm.controls[i].updateValueAndValidity();
}
if (!this.validateForm.valid) {
return;
}
if (this.channel.id) {
this.as.put('channel/' + this.channel.id, this.validateForm.value).subscribe(res => {
this.as.put('channel/' + this.channel.id, this.channel).subscribe(res => {
console.log(res);
// TODO 修改成功
this.drawerRef.close(res.data);
});
} else {
this.as.post('channel', this.validateForm.value).subscribe(res => {
this.as.post('channel', this.channel).subscribe(res => {
console.log(res);
// TODO 保存成功
this.drawerRef.close(res.data);
......@@ -53,25 +42,11 @@ export class ChannelEditComponent implements OnInit {
}
}
initForm(item): void {
if (!item.register)
item.register = {};
if (!item.heart_beat)
item.heart_beat = {};
initChannel(item): void {
item.net = item.net || { is_server: true, type: 'tcp', addr: ':1843'};
item.register = item.register || {};
item.heart_beat = item.heart_beat || {};
this.validateForm = this.fb.group({
name: [item.name, [Validators.required]],
// tags: [item.tags],
serial: [item.serial],
net: [item.net, [Validators.required]],
addr: [item.addr, [Validators.required]],
is_server: [item.is_server],
disabled: [item.disabled],
'register.enable': [item.register.enable],
'register.regex': [item.register.regex],
'heart_beat.enable': [item.heart_beat.enable],
'heart_beat.interval': [item.heart_beat.interval],
'heart_beat.content': [item.heart_beat.content],
});
this.channel = item;
}
}
<app-toolbar>
<nz-button-group>
<nz-space>
<nz-space-item>
<button nz-button (click)="load()">
<i nz-icon nzType="reload"></i>
刷新
</button>
</nz-space-item>
<nz-space-item>
<button nz-button (click)="edit()">
<i nz-icon nzType="plus"></i>
创建
</button>
</nz-button-group>
</app-toolbar>
</nz-space-item>
</nz-space>
<nz-table #basicTable [nzData]="channels">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>标签</th>
<th>序号</th>
<th>网络</th>
<th>地址</th>
<th>状态</th>
<th>创建时间</th>
<th></th>
</tr>
......@@ -28,13 +29,13 @@
<td>{{ data.id }}</td>
<td>{{ data.name }}</td>
<td>{{ data.tags }}</td>
<td>{{ data.serial }}</td>
<td>{{ data.net }}</td>
<td>{{ data.addr }}</td>
<td>{{ data.created }}</td>
<td>{{ data.net.is_server ? '服务端':'客户端' }} {{data.net.type}} {{data.net.addr}}</td>
<td>
{{data.disabled ? '禁用' : ''}}
<a>启动/停止</a>
<nz-divider nzType="vertical"></nz-divider>
</td>
<td>{{ data.created | amDateFormat:'YYYY-MM-DD HH:mm:ss' }}</td>
<td>
<a>
<i nz-icon nzType="delete"></i>
</a>
......
......@@ -32,7 +32,7 @@ export class ChannelComponent implements OnInit {
this.drawer.create({
nzTitle: c ? '编辑' : '创建',
nzMaskClosable: false,
nzWidth: 400,
nzWidth: 500,
nzContent: ChannelEditComponent,
nzContentParams: {
channel: c || {}
......
......@@ -14,8 +14,8 @@ import {
NzCheckboxModule, NzDividerModule, NzDrawerModule,
NzFormModule,
NzIconModule,
NzInputModule,
NzModalModule, NzPopconfirmModule, NzSwitchModule,
NzInputModule, NzInputNumberModule,
NzModalModule, NzPopconfirmModule, NzSelectModule, NzSwitchModule,
NzTableModule,
NzToolTipModule
} from 'ng-zorro-antd';
......@@ -31,6 +31,7 @@ import {ChannelDetailComponent} from './channel-detail/channel-detail.component'
import {LinkDetailComponent} from './link-detail/link-detail.component';
import {UserEditComponent} from './user-edit/user-edit.component';
import {LinkMonitorComponent} from './link-monitor/link-monitor.component';
import {NzSpaceModule} from "ng-zorro-antd/space";
@NgModule({
......@@ -64,6 +65,9 @@ import {LinkMonitorComponent} from './link-monitor/link-monitor.component';
IconsProviderModule,
NzDividerModule,
NzDrawerModule,
NzSelectModule,
NzSpaceModule,
NzInputNumberModule,
],
bootstrap: [MainComponent]
})
......
......@@ -2,15 +2,23 @@ package types
import "time"
type NetConf struct {
Type string `json:"type"`
Addr string `json:"addr"`
IsServer bool `json:"is_server"`
Timeout int `json:"timeout"` //TODO 改为秒
}
type RegisterConf struct {
Enable bool
Length int
Regex string
Enable bool `json:"enable"`
Length int `json:"length"`
Regex string `json:"regex"`
}
type HeartBeatConf struct {
Enable bool
Content []byte
Enable bool `json:"enable"`
Interval int `json:"interval"` //TODO 改为秒
Content []byte `json:"content"`
}
type Channel struct {
......@@ -18,24 +26,9 @@ type Channel struct {
Name string `json:"name"`
Tags []string `json:"tags"`
Serial string `storm:"index" json:"serial"`
Net string `json:"net"`
Addr string `json:"addr"`
IsServer bool `json:"is_server"`
Timeout int `json:"timeout"` //TODO 改为秒
Register struct {
Enable bool `json:"enable"`
Length int `json:"length"`
Regex string `json:"regex"`
} `json:"register"`
HeartBeat struct {
Enable bool `json:"enable"`
Interval int `json:"interval"` //TODO 改为秒
Content []byte `json:"content"`
} `json:"heart_beat"`
Net NetConf `json:"net"`
Register RegisterConf `json:"register"`
HeartBeat HeartBeatConf `json:"heart_beat"`
Disabled bool `json:"disabled"`
Created time.Time `json:"created"`
......
......@@ -7,7 +7,8 @@ import (
"github.com/zgwit/dtu-admin/types"
)
func channelAll(ctx *gin.Context) {
func channels(ctx *gin.Context) {
var cs []types.Channel
err := storage.DB("channel").All(&cs)
if err != nil {
......@@ -17,11 +18,6 @@ func channelAll(ctx *gin.Context) {
replyOk(ctx, cs)
}
func channels(ctx *gin.Context) {
cs := dtu.Channels()
replyOk(ctx, cs)
}
func channelCreate(ctx *gin.Context) {
var channel types.Channel
if err := ctx.ShouldBindJSON(&channel); err != nil {
......@@ -29,14 +25,15 @@ func channelCreate(ctx *gin.Context) {
return
}
//创建并启动
c, err := dtu.CreateChannel(&channel)
err := storage.DB("channel").Save(&channel)
if err != nil {
replyError(ctx, err)
return
}
replyOk(ctx, c)
//TODO 启动服务
replyOk(ctx, channel)
}
func channelDelete(ctx *gin.Context) {
......@@ -45,17 +42,36 @@ func channelDelete(ctx *gin.Context) {
replyError(ctx, err)
return
}
err := dtu.DeleteChannel(pid.Id)
err := storage.DB("channel").DeleteStruct(&types.Channel{ID: pid.Id})
if err != nil {
replyError(ctx, err)
return
}
//TODO 删除服务
replyOk(ctx, nil)
}
func channelModify(ctx *gin.Context) {
var channel types.Channel
if err := ctx.ShouldBindJSON(&channel); err != nil {
replyError(ctx, err)
return
}
//TODO 不能全部字段更新,应该先取值,修改,再存入
err := storage.DB("channel").Update(&channel)
if err != nil {
replyError(ctx, err)
return
}
//TODO 重新启动服务
replyOk(ctx, channel)
}
func channelGet(ctx *gin.Context) {
......@@ -64,13 +80,15 @@ func channelGet(ctx *gin.Context) {
replyError(ctx, err)
return
}
c, err := dtu.GetChannel(pid.Id)
var channel types.Channel
err := storage.DB("channel").One("ID", pid.Id, &channel)
if err != nil {
replyError(ctx, err)
return
}
replyOk(ctx, c)
replyOk(ctx, channel)
}
func channelStart(ctx *gin.Context) {
......
......@@ -17,12 +17,12 @@ type paramSearch struct {
}
type paramId struct {
Id int64 `uri:"id"`
Id int `uri:"id"`
}
type paramId2 struct {
Id int64 `uri:"id"`
Id2 int64 `uri:"id2"`
Id int `uri:"id"`
Id2 int `uri:"id2"`
}
func RegisterRoutes(app *gin.RouterGroup) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册