Skip to content

Commit

Permalink
Technical debt (#259)
Browse files Browse the repository at this point in the history
* technical debt
  • Loading branch information
e154 authored Jan 25, 2024
1 parent 741f49a commit 1e91e3d
Show file tree
Hide file tree
Showing 237 changed files with 7,085 additions and 2,496 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ test_system:
go test -v ./tests/plugins/cgminer
go test -v ./tests/plugins/email
go test -v ./tests/plugins/messagebird
#go test -v ./tests/plugins/modbus_rtu
#go test -v ./tests/plugins/modbus_tcp
go test -v ./tests/plugins/modbus_rtu
go test -v ./tests/plugins/modbus_tcp
go test -v ./tests/plugins/moon
go test -v ./tests/plugins/node
go test -v ./tests/plugins/scene
Expand Down
4 changes: 2 additions & 2 deletions Makefile.local
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ test_system:
go test -v ./tests/plugins/cgminer
go test -v ./tests/plugins/email
go test -v ./tests/plugins/messagebird
#go test -v ./tests/plugins/modbus_rtu
#go test -v ./tests/plugins/modbus_tcp
go test -v ./tests/plugins/modbus_rtu
go test -v ./tests/plugins/modbus_tcp
go test -v ./tests/plugins/moon
go test -v ./tests/plugins/node
go test -v ./tests/plugins/scene
Expand Down
6 changes: 3 additions & 3 deletions adaptors/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type IAction interface {
GetById(ctx context.Context, id int64) (metric *m.Action, err error)
Update(ctx context.Context, ver *m.Action) error
Delete(ctx context.Context, deviceId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.Action, total int64, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, ids *[]uint64) (list []*m.Action, total int64, err error)
Search(ctx context.Context, query string, limit, offset int) (list []*m.Action, total int64, err error)
fromDb(dbVer *db.Action) (ver *m.Action)
toDb(ver *m.Action) (dbVer *db.Action)
Expand Down Expand Up @@ -97,9 +97,9 @@ func (n *Action) Delete(ctx context.Context, deviceId int64) (err error) {
}

// List ...
func (n *Action) List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.Action, total int64, err error) {
func (n *Action) List(ctx context.Context, limit, offset int64, orderBy, sort string, ids *[]uint64) (list []*m.Action, total int64, err error) {
var dbList []*db.Action
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort); err != nil {
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, ids); err != nil {
return
}

Expand Down
6 changes: 3 additions & 3 deletions adaptors/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ICondition interface {
GetById(ctx context.Context, id int64) (metric *m.Condition, err error)
Update(ctx context.Context, ver *m.Condition) error
Delete(ctx context.Context, deviceId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.Condition, total int64, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, ids *[]uint64) (list []*m.Condition, total int64, err error)
Search(ctx context.Context, query string, limit, offset int) (list []*m.Condition, total int64, err error)
fromDb(dbVer *db.Condition) (ver *m.Condition)
toDb(ver *m.Condition) (dbVer *db.Condition)
Expand Down Expand Up @@ -94,9 +94,9 @@ func (n *Condition) Delete(ctx context.Context, deviceId int64) (err error) {
}

// List ...
func (n *Condition) List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.Condition, total int64, err error) {
func (n *Condition) List(ctx context.Context, limit, offset int64, orderBy, sort string, ids *[]uint64) (list []*m.Condition, total int64, err error) {
var dbList []*db.Condition
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort); err != nil {
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, ids); err != nil {
return
}

Expand Down
4 changes: 4 additions & 0 deletions adaptors/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ func (n *Entity) fromDb(dbVer *db.Entity) (ver *m.Entity) {
data := map[string]interface{}{}
_ = json.Unmarshal(dbVer.Storage[0].Attributes, &data)
_, _ = ver.Attributes.Deserialize(data)
storageAdaptor := GetEntityStorageAdaptor(n.db)
for _, store := range dbVer.Storage {
ver.Storage = append(ver.Storage, storageAdaptor.fromDb(store))
}
}

return
Expand Down
18 changes: 10 additions & 8 deletions adaptors/entity_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import (
type IEntityStorage interface {
Add(ctx context.Context, ver *m.EntityStorage) (id int64, err error)
GetLastByEntityId(ctx context.Context, entityId common.EntityId) (ver *m.EntityStorage, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, entityIds []common.EntityId, startDate, endDate *time.Time) (list []*m.EntityStorage, total int64, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string,
entityIds []common.EntityId,
startDate, endDate *time.Time) (list []*m.EntityStorage, total int64, err error)
DeleteOldest(ctx context.Context, days int) (err error)
fromDb(dbVer db.EntityStorage) (ver *m.EntityStorage)
toDb(ver *m.EntityStorage) (dbVer db.EntityStorage)
fromDb(dbVer *db.EntityStorage) (ver *m.EntityStorage)
toDb(ver *m.EntityStorage) (dbVer *db.EntityStorage)
}

// EntityStorage ...
Expand All @@ -62,7 +64,7 @@ func (n *EntityStorage) Add(ctx context.Context, ver *m.EntityStorage) (id int64

// GetLastByEntityId ...
func (n *EntityStorage) GetLastByEntityId(ctx context.Context, entityId common.EntityId) (ver *m.EntityStorage, err error) {
var dbVer db.EntityStorage
var dbVer *db.EntityStorage
if dbVer, err = n.table.GetLastByEntityId(ctx, entityId); err != nil {
return
}
Expand All @@ -72,7 +74,7 @@ func (n *EntityStorage) GetLastByEntityId(ctx context.Context, entityId common.E

// ListByEntityId ...
func (n *EntityStorage) List(ctx context.Context, limit, offset int64, orderBy, sort string, entityIds []common.EntityId, startDate, endDate *time.Time) (list []*m.EntityStorage, total int64, err error) {
var dbList []db.EntityStorage
var dbList []*db.EntityStorage
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, entityIds, startDate, endDate); err != nil {
return
}
Expand All @@ -90,7 +92,7 @@ func (n *EntityStorage) DeleteOldest(ctx context.Context, days int) (err error)
return
}

func (n *EntityStorage) fromDb(dbVer db.EntityStorage) (ver *m.EntityStorage) {
func (n *EntityStorage) fromDb(dbVer *db.EntityStorage) (ver *m.EntityStorage) {
ver = &m.EntityStorage{
Id: dbVer.Id,
EntityId: dbVer.EntityId,
Expand All @@ -106,8 +108,8 @@ func (n *EntityStorage) fromDb(dbVer db.EntityStorage) (ver *m.EntityStorage) {
return
}

func (n *EntityStorage) toDb(ver *m.EntityStorage) (dbVer db.EntityStorage) {
dbVer = db.EntityStorage{
func (n *EntityStorage) toDb(ver *m.EntityStorage) (dbVer *db.EntityStorage) {
dbVer = &db.EntityStorage{
Id: ver.Id,
EntityId: ver.EntityId,
State: ver.State,
Expand Down
6 changes: 3 additions & 3 deletions adaptors/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type IScript interface {
GetByName(ctx context.Context, name string) (script *m.Script, err error)
Update(ctx context.Context, script *m.Script) (err error)
Delete(ctx context.Context, scriptId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string) (list []*m.Script, total int64, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string, ids *[]uint64) (list []*m.Script, total int64, err error)
Search(ctx context.Context, query string, limit, offset int64) (list []*m.Script, total int64, err error)
Statistic(ctx context.Context) (statistic *m.ScriptsStatistic, err error)
fromDb(dbScript *db.Script) (script *m.Script, err error)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (n *Script) Delete(ctx context.Context, scriptId int64) (err error) {
}

// List ...
func (n *Script) List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string) (list []*m.Script, total int64, err error) {
func (n *Script) List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string, ids *[]uint64) (list []*m.Script, total int64, err error) {

if sort == "" {
sort = "id"
Expand All @@ -114,7 +114,7 @@ func (n *Script) List(ctx context.Context, limit, offset int64, orderBy, sort st
}

var dbList []*db.Script
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, query); err != nil {
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, query, ids); err != nil {
return
}

Expand Down
6 changes: 3 additions & 3 deletions adaptors/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ITrigger interface {
Update(ctx context.Context, ver *m.UpdateTrigger) error
Delete(ctx context.Context, deviceId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool) (list []*m.Trigger, total int64, err error)
ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool) (list []*m.Trigger, total int64, err error)
ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool, ids *[]uint64) (list []*m.Trigger, total int64, err error)
Search(ctx context.Context, query string, limit, offset int) (list []*m.Trigger, total int64, err error)
Enable(ctx context.Context, id int64) (err error)
Disable(ctx context.Context, id int64) (err error)
Expand Down Expand Up @@ -184,9 +184,9 @@ func (n *Trigger) List(ctx context.Context, limit, offset int64, orderBy, sort s
}

// ListPlain ...
func (n *Trigger) ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool) (list []*m.Trigger, total int64, err error) {
func (n *Trigger) ListPlain(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool, ids *[]uint64) (list []*m.Trigger, total int64, err error) {
var dbList []*db.Trigger
if dbList, total, err = n.table.ListPlain(ctx, int(limit), int(offset), orderBy, sort, onlyEnabled); err != nil {
if dbList, total, err = n.table.ListPlain(ctx, int(limit), int(offset), orderBy, sort, onlyEnabled, ids); err != nil {
return
}

Expand Down
16 changes: 10 additions & 6 deletions adaptors/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"time"
"unicode/utf8"

"github.com/e154/smart-home/common/apperr"
"gorm.io/gorm"

"github.com/e154/smart-home/common"
"github.com/e154/smart-home/common/apperr"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
)

// IUser ...
Expand Down Expand Up @@ -229,18 +229,18 @@ func (n *User) SignIn(ctx context.Context, u *m.User, ipv4 string) (err error) {

// update time
lastT := u.CurrentSignInAt
currentT := time.Now()
now := time.Now()

u.LastSignInAt = lastT
u.CurrentSignInAt = &currentT
u.CurrentSignInAt = &now

// update ipv4
lastIp := u.CurrentSignInIp
currentIp := ipv4
u.LastSignInIp = lastIp
u.CurrentSignInIp = currentIp

u.UpdateHistory(currentT, currentIp)
u.UpdateHistory(now, currentIp)

dbUser := n.toDb(u)
err = n.table.Update(ctx, dbUser)
Expand Down Expand Up @@ -320,7 +320,7 @@ func (n *User) fromDb(dbUser *db.User) (user *m.User) {
}
}

// history
// deserialize history
user.History = make([]*m.UserHistory, 0)
data, _ := dbUser.History.MarshalJSON()
_ = json.Unmarshal(data, &user.History)
Expand Down Expand Up @@ -373,5 +373,9 @@ func (n *User) toDb(user *m.User) (dbUser *db.User) {
_ = dbUser.UserId.Scan(user.UserId.Int64)
}

// serialize history
b, _ := json.Marshal(user.History)
_ = dbUser.History.UnmarshalJSON(b)

return
}
16 changes: 9 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (a *Api) Start() (err error) {
Skipper: middleware.DefaultSkipper,
Limit: "128M",
}))
a.echo.Use(controllers.NewMiddlewareContextValue)
a.echo.Use(middleware.Recover())

if a.cfg.Debug {
Expand Down Expand Up @@ -120,7 +121,6 @@ func (a *Api) Start() (err error) {

a.eventBus.Publish("system/services/api", events.EventServiceStarted{Service: "Api"})

//return group.Wait()
return nil
}

Expand Down Expand Up @@ -154,9 +154,6 @@ func (a *Api) registerHandlers() {
a.echo.GET("/api.swagger3.yaml", contentHandler)
}

// base api
//a.echo.Any("/v1/*", echo.WrapHandler(grpcHandlerFunc(a.grpcServer, mux)))

wrapper := stub.ServerInterfaceWrapper{
Handler: a.controllers,
}
Expand Down Expand Up @@ -312,7 +309,6 @@ func (a *Api) registerHandlers() {

// static files
a.echo.GET("/", echo.WrapHandler(a.controllers.Index(publicAssets.F)))
//a.echo.Any("/v1/image/upload", a.echoFilter.Auth(a.controllers.ImageServiceMuxUploadImage)) //Auth
a.echo.GET("/public/*", echo.WrapHandler(http.StripPrefix("/", http.FileServer(http.FS(publicAssets.F)))))
fileServer := http.FileServer(http.Dir("./data/file_storage"))
a.echo.Any("/upload/*", echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -327,13 +323,19 @@ func (a *Api) registerHandlers() {
staticServer.ServeHTTP(w, r)
})))
snapshotServer := http.FileServer(http.Dir("./snapshots"))
//a.echo.Any("/v1/backup/upload", a.echo
//Filter.Auth(a.controllers.BackupServiceMuxUploadBackup)) //Auth
a.echo.GET("/snapshots/*", a.echoFilter.Auth(echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.RequestURI = strings.ReplaceAll(r.RequestURI, "/snapshots/", "/")
r.URL, _ = r.URL.Parse(r.RequestURI)
snapshotServer.ServeHTTP(w, r)
}))))
// webdav
webdav := echo.WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//r.RequestURI = strings.ReplaceAll(r.RequestURI, "/webdav/", "/")
//r.URL, _ = r.URL.Parse(r.RequestURI)
a.controllers.Webdav(w, r)
}))
a.echo.Any("/webdav", webdav)
a.echo.Any("/webdav/*", webdav)

// media
a.echo.Any("/stream/:entity_id/channel/:channel/mse", a.echoFilter.Auth(a.controllers.StreamMSE)) //Auth
Expand Down
24 changes: 20 additions & 4 deletions api/api.swagger3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ paths:
- $ref: '#/components/parameters/listSort'
- $ref: '#/components/parameters/listPage'
- $ref: '#/components/parameters/listLimit'
- $ref: '#/components/parameters/ids'
responses:
200:
description: A successful response.
Expand Down Expand Up @@ -700,6 +701,7 @@ paths:
- $ref: '#/components/parameters/listSort'
- $ref: '#/components/parameters/listPage'
- $ref: '#/components/parameters/listLimit'
- $ref: '#/components/parameters/ids'
responses:
200:
description: A successful response.
Expand Down Expand Up @@ -1879,7 +1881,7 @@ paths:
- $ref: '#/components/parameters/listLimit'
- $ref: '#/components/parameters/startDate'
- $ref: '#/components/parameters/endDate'
- $ref: '#/components/parameters/entityId'
- $ref: '#/components/parameters/entityIds'
responses:
200:
description: A successful response.
Expand Down Expand Up @@ -2948,6 +2950,7 @@ paths:
- $ref: '#/components/parameters/listSort'
- $ref: '#/components/parameters/listPage'
- $ref: '#/components/parameters/listLimit'
- $ref: '#/components/parameters/ids'
- $ref: '#/components/parameters/query'
responses:
200:
Expand Down Expand Up @@ -3438,6 +3441,7 @@ paths:
- $ref: '#/components/parameters/listSort'
- $ref: '#/components/parameters/listPage'
- $ref: '#/components/parameters/listLimit'
- $ref: '#/components/parameters/ids'
responses:
200:
description: A successful response.
Expand Down Expand Up @@ -4287,6 +4291,16 @@ components:
type: integer
format: uint64
description: The number of results returned on a page
ids:
name: ids[]
in: query
required: false
schema:
type: array
items:
type: integer
format: uint64
description: The number of results returned on a page
startDate:
name: startDate
in: query
Expand All @@ -4301,12 +4315,14 @@ components:
schema:
type: string
format: date-time
entityId:
name: entityId
entityIds:
name: entityId[]
in: query
required: false
schema:
type: string
type: array
items:
type: string
query:
name: query
in: query
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c ControllerAction) ActionServiceGetActionById(ctx echo.Context, id int64)
func (c ControllerAction) ActionServiceGetActionList(ctx echo.Context, params stub.ActionServiceGetActionListParams) error {

pagination := c.Pagination(params.Page, params.Limit, params.Sort)
items, total, err := c.endpoint.Action.GetList(ctx.Request().Context(), pagination)
items, total, err := c.endpoint.Action.GetList(ctx.Request().Context(), pagination, params.Ids)
if err != nil {
return c.ERROR(ctx, err)
}
Expand Down
5 changes: 4 additions & 1 deletion api/controllers/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func (c ControllerBackup) BackupServiceUploadBackup(ctx echo.Context, _ stub.Bac
return c.ERROR(ctx, apperr.ErrInvalidRequest)
}

list, errs := c.endpoint.Backup.Upload(r.Context(), form.File)
list, errs, err := c.endpoint.Backup.Upload(r.Context(), form.File)
if err != nil {
return c.ERROR(ctx, err)
}

var resultBackups = make([]interface{}, 0)

Expand Down
Loading

0 comments on commit 1e91e3d

Please sign in to comment.