From dc8f02223d125daeabde4d58e05efb52282bcc15 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 29 Sep 2020 10:39:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/model/model.go | 53 +----- internal/model/parser.go | 75 -------- web/api/model.go | 366 +++++++++++++++++++++++++++++++++++++++ web/api/router.go | 60 ++++++- 4 files changed, 425 insertions(+), 129 deletions(-) delete mode 100644 internal/model/parser.go create mode 100644 web/api/model.go diff --git a/internal/model/model.go b/internal/model/model.go index 377ae82..87235d8 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -1,59 +1,18 @@ package model import ( - "git.zgwit.com/zgwit/iot-admin/interfaces" - "git.zgwit.com/zgwit/iot-admin/types" "github.com/robertkrimen/otto" ) -type _link struct { - linker interfaces.Linker - Protocol string -} - -type _variable struct { - Link *_link - Type types.DataType - Addr string - Default string - Writable bool //可写,用于输出(如开关) - - //TODO 采样:无、定时、轮询 - Cron string - - Children map[string]_variable -} - -type _batchResult struct { - Offset int - Variable string //_variable path -} - -type _batch struct { - Link string - Type string - Addr string - Size int - Cron string - - Results []_batchResult -} - -type _job struct { - Cron string - Script string //javascript -} - -type _strategy struct { - Script string //javascript -} type Instance struct { - Links map[string]_link + //Links map[string]_link Variables map[string]_variable - Batches map[string]_batch - Jobs map[string]_job - Strategies map[string]_strategy + //Batches map[string]_batch + //Jobs map[string]_job + //Strategies map[string]_strategy vm *otto.Otto } + + diff --git a/internal/model/parser.go b/internal/model/parser.go deleted file mode 100644 index c14a116..0000000 --- a/internal/model/parser.go +++ /dev/null @@ -1,75 +0,0 @@ -package model - -type _base struct { - Name string `json:"name"` - Description string `json:"description"` -} - -type ModelLink struct { - _base - Protocol string `json:"protocol"` -} - -type ModelVariable struct { - _base - Link string `json:"link"` - Type string `json:"type"` - Addr string `json:"addr"` - Default string `json:"default"` - Writable bool `json:"writable"` //可写,用于输出(如开关) - - //TODO 采样:无、定时、轮询 - Cron string `json:"cron"` - - Children []ModelVariable `json:"children"` -} - -type ModelBatchResult struct { - Offset int `json:"offset"` - Variable string `json:"variable"` //ModelVariable path -} - -type ModelBatch struct { - _base - Link string `json:"link"` - Type string `json:"type"` - Addr string `json:"addr"` - Size int `json:"size"` - Cron string `json:"cron"` - - Results []ModelBatchResult `json:"results"` -} - -type ModelJob struct { - _base - Cron string `json:"cron"` - Script string `json:"script"` //javascript -} - -type ModelStrategy struct { - _base - Script string `json:"script"` //javascript -} - -type Model struct { - _base - - Version string `json:"version"` - H5 string `json:"h5"` - - Links []ModelLink `json:"links"` - Variables []ModelVariable `json:"variables"` - Batches []ModelBatch `json:"batches"` - Jobs []ModelJob `json:"jobs"` - Strategies []ModelStrategy `json:"strategies"` -} - -func Import(json string) error { - //TODO parser model, import - return nil -} - -func Export(id int) string { - //TODO ge - return "" -} diff --git a/web/api/model.go b/web/api/model.go new file mode 100644 index 0000000..c93a384 --- /dev/null +++ b/web/api/model.go @@ -0,0 +1,366 @@ +package api + +import ( + "git.zgwit.com/zgwit/iot-admin/internal/db" + "git.zgwit.com/zgwit/iot-admin/types" + "github.com/gin-gonic/gin" + "time" +) + +type ModelBase struct { + Name string `json:"name"` + Description string `json:"description"` +} + +type ModelTunnel struct { + ModelBase + Protocol string `json:"protocol"` + ProtocolOpts string `json:"protocol_opts"` + PollingEnable bool `json:"polling_enable"` //轮询 + PollingInterval int `json:"polling_interval"` //轮询间隔 +} + +type ModelVariable struct { + ModelBase + Tunnel string `json:"tunnel"` + Type string `json:"type"` + Addr string `json:"addr"` + Default string `json:"default"` + Writable bool `json:"writable"` //可写,用于输出(如开关) + + //采样:无、定时、轮询 + Cron string `json:"cron"` + PollingEnable bool `json:"polling_enable"` //轮询 + PollingTimes int `json:"polling_times"` +} + +type ModelBatch struct { + ModelBase + Tunnel string `json:"tunnel"` + Type string `json:"type"` + Addr string `json:"addr"` + Size int `json:"size"` + + //采样:无、定时、轮询 + Cron string `json:"cron"` + PollingEnable bool `json:"polling_enable"` //轮询 + PollingTimes int `json:"polling_times"` + + //结果解析 + Results []types.ModelBatchResult `json:"results"` +} + +type ModelJob struct { + ModelBase + Cron string `json:"cron"` + Script string `json:"script"` //javascript +} + +type ModelStrategy struct { + ModelBase + Script string `json:"script"` //javascript +} + +type Model struct { + Name string `json:"name"` + Description string `json:"description"` + Version string `json:"version"` + H5 string `json:"h5"` + Polling bool `json:"polling"` //轮询 + + Tunnels []ModelTunnel `json:"tunnels"` + Variables []ModelVariable `json:"variables"` + Batches []ModelBatch `json:"batches"` + Jobs []ModelJob `json:"jobs"` + Strategies []ModelStrategy `json:"strategies"` +} + +func modelImport(ctx *gin.Context) { + var model Model + + err := ctx.ShouldBind(&model) + if err != nil { + replyError(ctx, err) + return + } + + //TODO 导入模型 + m := types.Model{ + Name: model.Name, + Description: model.Description, + Version: model.Version, + H5: model.H5, + Polling: model.Polling, + CreatedAt: time.Now(), + } + + modelDB := db.DB("model") + err = modelDB.Save(&m) + if err != nil { + replyError(ctx, err) + return + } + + //创建通道 + tunnelIds := make(map[string]int) + tunnelDB := modelDB.From("tunnel") + for _, t := range model.Tunnels { + tunnel := types.ModelTunnel{ + ModelBase: types.ModelBase{ + ModelId: m.Id, + Name: t.Name, + Description: t.Description, + CreatedAt: time.Now(), + }, + Protocol: t.Protocol, + ProtocolOpts: t.ProtocolOpts, + PollingEnable: t.PollingEnable, + PollingInterval: t.PollingInterval, + } + err = tunnelDB.Save(&tunnel) + if err != nil { + replyError(ctx, err) + return + } + tunnelIds[tunnel.Name] = tunnel.Id + } + + //创建变量 + variableDB := modelDB.From("variable") + for _, v := range model.Variables { + variable := types.ModelVariable{ + ModelBase: types.ModelBase{ + ModelId: m.Id, + Name: v.Name, + Description: v.Description, + CreatedAt: time.Now(), + }, + TunnelId: tunnelIds[v.Tunnel], + Type: v.Type, + Addr: v.Addr, + Default: v.Default, + Writable: v.Writable, + Cron: v.Cron, + PollingEnable: v.PollingEnable, + PollingTimes: v.PollingTimes, + } + err = variableDB.Save(&variable) + if err != nil { + replyError(ctx, err) + return + } + } + + //创建批量 + batchDB := modelDB.From("batch") + for _, v := range model.Batches { + batch := types.ModelBatch{ + ModelBase: types.ModelBase{ + ModelId: m.Id, + Name: v.Name, + Description: v.Description, + CreatedAt: time.Now(), + }, + TunnelId: tunnelIds[v.Tunnel], + Type: v.Type, + Addr: v.Addr, + Size: v.Size, + Cron: v.Cron, + PollingEnable: v.PollingEnable, + PollingTimes: v.PollingTimes, + Results: nil, + } + err = batchDB.Save(&batch) + if err != nil { + replyError(ctx, err) + return + } + } + + //创建任务 + jobDB := modelDB.From("job") + for _, v := range model.Jobs { + job := types.ModelJob{ + ModelBase: types.ModelBase{ + ModelId: m.Id, + Name: v.Name, + Description: v.Description, + CreatedAt: time.Now(), + }, + Cron: v.Cron, + Script: v.Script, + } + err = jobDB.Save(&job) + if err != nil { + replyError(ctx, err) + return + } + } + + //创建策略 + strategyDB := modelDB.From("strategy") + for _, v := range model.Strategies { + job := types.ModelStrategy{ + ModelBase: types.ModelBase{ + ModelId: m.Id, + Name: v.Name, + Description: v.Description, + CreatedAt: time.Now(), + }, + Script: v.Script, + } + err = strategyDB.Save(&job) + if err != nil { + replyError(ctx, err) + return + } + } + + replyOk(ctx, m) +} + +func modelExport(ctx *gin.Context) { + var pid paramId + if err := ctx.BindUri(&pid); err != nil { + replyError(ctx, err) + return + } + + var model types.Model + modelDB := db.DB("model") + err := modelDB.One("Id", pid.Id, &model) + if err != nil { + replyError(ctx, err) + return + } + + m := Model{ + Name: model.Name, + Description: model.Description, + Version: model.Version, + H5: model.H5, + Polling: model.Polling, + Tunnels: make([]ModelTunnel, 0), + Variables: make([]ModelVariable, 0), + Batches: make([]ModelBatch, 0), + Jobs: make([]ModelJob, 0), + Strategies: make([]ModelStrategy, 0), + } + + //读取通道 + tunnelIds := make(map[int]string) + tunnelDB := modelDB.From("tunnel") + var tunnels []types.ModelTunnel + err = tunnelDB.Find("ModelId", model.Id, &tunnels) + if err != nil { + replyError(ctx, err) + return + } + for _, v := range tunnels { + tunnel := ModelTunnel{ + ModelBase: ModelBase{ + Name: v.Name, + Description: v.Description, + }, + Protocol: v.Protocol, + ProtocolOpts: v.ProtocolOpts, + PollingEnable: v.PollingEnable, + PollingInterval: v.PollingInterval, + } + m.Tunnels = append(m.Tunnels, tunnel) + tunnelIds[v.Id] = v.Name + } + + //读取变量 + variableDB := modelDB.From("variable") + var variables []types.ModelVariable + err = variableDB.Find("ModelId", model.Id, &variables) + if err != nil { + replyError(ctx, err) + return + } + for _, v := range variables { + variable := ModelVariable{ + ModelBase: ModelBase{ + Name: v.Name, + Description: v.Description, + }, + Tunnel: tunnelIds[v.TunnelId], + Type: v.Type, + Addr: v.Addr, + Default: v.Default, + Writable: v.Writable, + Cron: v.Cron, + PollingEnable: v.PollingEnable, + PollingTimes: v.PollingTimes, + } + m.Variables = append(m.Variables, variable) + } + + //读取批量 + batchDB := modelDB.From("batch") + var batches []types.ModelBatch + err = batchDB.Find("ModelId", model.Id, &batches) + if err != nil { + replyError(ctx, err) + return + } + for _, v := range batches { + batch := ModelBatch{ + ModelBase: ModelBase{ + Name: v.Name, + Description: v.Description, + }, + Tunnel: tunnelIds[v.TunnelId], + Type: v.Type, + Addr: v.Addr, + Size: v.Size, + Cron: v.Cron, + PollingEnable: v.PollingEnable, + PollingTimes: v.PollingTimes, + Results: v.Results, + } + m.Batches = append(m.Batches, batch) + } + + //读取任务 + jobDB := modelDB.From("job") + var jobs []types.ModelJob + err = jobDB.Find("ModelId", model.Id, &jobs) + if err != nil { + replyError(ctx, err) + return + } + for _, v := range jobs { + job := ModelJob{ + ModelBase: ModelBase{ + Name: v.Name, + Description: v.Description, + }, + Cron: v.Cron, + Script: v.Script, + } + m.Jobs = append(m.Jobs, job) + } + + //读取策略 + strategyDB := modelDB.From("strategy") + var strategies []types.ModelStrategy + err = strategyDB.Find("ModelId", model.Id, &strategies) + if err != nil { + replyError(ctx, err) + return + } + for _, v := range strategies { + strategy := ModelStrategy{ + ModelBase: ModelBase{ + Name: v.Name, + Description: v.Description, + }, + Script: v.Script, + } + m.Strategies = append(m.Strategies, strategy) + } + + replyOk(ctx, m) +} diff --git a/web/api/router.go b/web/api/router.go index 786a5b3..0588f0b 100644 --- a/web/api/router.go +++ b/web/api/router.go @@ -60,14 +60,14 @@ func RegisterRoutes(app *gin.RouterGroup) { //TODO 转移至子目录,并使用中间件,检查session及权限 app.POST("/channels", channels) - app.POST("/core", channelCreate) - app.DELETE("/core/:id", channelDelete) - app.PUT("/core/:id", channelModify) - app.GET("/core/:id", channelGet) - app.GET("/core/:id/start", channelStart) - app.GET("/core/:id/stop", channelStop) + app.POST("/channel", channelCreate) + app.DELETE("/channel/:id", channelDelete) + app.PUT("/channel/:id", channelModify) + app.GET("/channel/:id", channelGet) + app.GET("/channel/:id/start", channelStart) + app.GET("/channel/:id/stop", channelStop) - //app.POST("/core/:id/links") + //app.POST("/channel/:id/links") //连接管理 app.POST("/links", links) @@ -82,6 +82,52 @@ func RegisterRoutes(app *gin.RouterGroup) { app.PUT("/plugin/:id", pluginModify) app.GET("/plugin/:id", pluginGet) + //模型管理 + app.POST("/models", nop) + app.POST("/model", nop) + app.DELETE("/model/:id", nop) + app.PUT("/model/:id", nop) + app.GET("/model/:id", nop) + + app.POST("/model/:id/tunnels", nop) + app.POST("/model/:id/variables", nop) + app.POST("/model/:id/batches", nop) + app.POST("/model/:id/jobs", nop) + app.POST("/model/:id/strategies", nop) + + app.POST("/model-import", modelImport) + app.GET("/model-export/:id", modelExport) + + app.POST("/tunnels", nop) + app.POST("/tunnel", nop) + app.DELETE("/tunnel/:id", nop) + app.PUT("/tunnel/:id", nop) + app.GET("/tunnel/:id", nop) + + app.POST("/variables", nop) + app.POST("/variable", nop) + app.DELETE("/variable/:id", nop) + app.PUT("/variable/:id", nop) + app.GET("/variable/:id", nop) + + app.POST("/batches", nop) + app.POST("/batch", nop) + app.DELETE("/batch/:id", nop) + app.PUT("/batch/:id", nop) + app.GET("/batch/:id", nop) + + app.POST("/jobs", nop) + app.POST("/job", nop) + app.DELETE("/job/:id", nop) + app.PUT("/job/:id", nop) + app.GET("/job/:id", nop) + + app.POST("/strategies", nop) + app.POST("/strategy", nop) + app.DELETE("/strategy/:id", nop) + app.PUT("/strategy/:id", nop) + app.GET("/strategy/:id", nop) + } func replyOk(ctx *gin.Context, data interface{}) { -- GitLab