Skip to content

Commit

Permalink
Merge pull request #260 from e154/master
Browse files Browse the repository at this point in the history
Docs
  • Loading branch information
e154 authored Jan 25, 2024
2 parents 5d834c4 + 6b0b6f7 commit e73b011
Show file tree
Hide file tree
Showing 287 changed files with 8,301 additions and 3,357 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ jobs:
id: system-code
run: make test_system

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: trying to build
id: build-all
run: make build_server build_cli build_structure build_common_structure build_archive docker_image
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ vendor/*
snapshots/*
!snapshots/.gitignore
bee.json
/api/server/golang-swaggerui-example/
/cmd/pingmq/pingmq
._*
cli-*
server-*
build/public/*
build/admin/*
smart-home-common.tar.gz
/coverage.out
7 changes: 3 additions & 4 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 All @@ -71,8 +71,7 @@ test_system:

test:
@echo MARK: unit tests
go test $(go list ./... | grep -v /tests/)
go test -race $(go list ./... | grep -v /tests/)
go test -v $(shell go list ./... | grep -v /tmp | grep -v /tests) -timeout 60s -race -covermode=atomic -coverprofile=coverage.out

install_linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.55.2
Expand Down
7 changes: 3 additions & 4 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 All @@ -73,8 +73,7 @@ test_system:

test:
@echo MARK: unit tests
go test $(go list ./... | grep -v /tests/)
go test -race $(go list ./... | grep -v /tests/)
go test -v $(shell go list ./... | grep -v /tmp | grep -v /tests) -timeout 60s -race -covermode=atomic -coverprofile=coverage.out

install_linter:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.55.2
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
3 changes: 2 additions & 1 deletion adaptors/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ package adaptors
import (
"context"

"gorm.io/gorm"

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

// IPermission ...
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
Loading

0 comments on commit e73b011

Please sign in to comment.