mirror of
https://github.com/m5stack/StackChan.git
synced 2026-04-27 19:12:40 +00:00
server code
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
// =================================================================================
|
||||
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||
// =================================================================================
|
||||
|
||||
package device
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
// =================================================================================
|
||||
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||
// =================================================================================
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"stackChan/api/device"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewV1() device.IDeviceV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stackChan/api/device/v1"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model/do"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
|
||||
insertId, err := dao.Device.Ctx(ctx).Data(do.Device{
|
||||
Mac: req.Mac,
|
||||
Name: req.Name,
|
||||
}).InsertAndGetId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = &v1.CreateRes{
|
||||
Id: insertId,
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model"
|
||||
|
||||
"stackChan/api/device/v1"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) GetDeviceInfo(ctx context.Context, req *v1.GetDeviceInfoReq) (res *v1.GetDeviceInfoRes, err error) {
|
||||
var info model.DeviceInfo
|
||||
err = dao.Device.Ctx(ctx).WherePri(req.Mac).Scan(&info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = (*v1.GetDeviceInfoRes)(&info)
|
||||
return res, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stackChan/api/device/v1"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model/entity"
|
||||
"stackChan/internal/web_socket"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) GetRandomDevice(ctx context.Context, req *v1.GetRandomDeviceReq) (res *v1.GetRandomDeviceRes, err error) {
|
||||
|
||||
// Obtain the list of online StackChan mac addresses (excluding the current user) from the websocket layer.
|
||||
macList := web_socket.GetRandomStackChanDevice(req.Mac, 6)
|
||||
|
||||
if len(macList) == 0 {
|
||||
res = (*v1.GetRandomDeviceRes)(&[]entity.Device{})
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Query device information based on the Mac list
|
||||
list := make([]entity.Device, 0, len(macList))
|
||||
err = dao.Device.
|
||||
Ctx(ctx).
|
||||
WhereIn("mac", macList).
|
||||
Scan(&list)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = (*v1.GetRandomDeviceRes)(&list)
|
||||
return res, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model/do"
|
||||
|
||||
"stackChan/api/device/v1"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
|
||||
_, err = dao.Device.Ctx(ctx).Data(do.Device{
|
||||
Name: req.Name,
|
||||
}).WherePri(req.Mac).Update()
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
||||
SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
package device
|
||||
|
||||
import (
|
||||
"context"
|
||||
"stackChan/internal/dao"
|
||||
"stackChan/internal/model/do"
|
||||
|
||||
"stackChan/api/device/v1"
|
||||
)
|
||||
|
||||
func (c *ControllerV1) UpdateDeviceInfo(ctx context.Context, req *v1.UpdateDeviceInfoReq) (res *v1.UpdateDeviceInfoRes, err error) {
|
||||
doDevice := do.Device{}
|
||||
if req.Name != "" {
|
||||
doDevice.Name = req.Name
|
||||
}
|
||||
_, err = dao.Device.Ctx(ctx).Data(doDevice).WherePri(req.Mac).Update()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := v1.UpdateDeviceInfoRes("Update successful")
|
||||
return &response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user