hpds_jkw_web/internal/service/edge.go

125 lines
3.1 KiB
Go

package service
import (
"context"
"encoding/json"
"fmt"
"git.hpds.cc/Component/logging"
"hpds-iot-web/config"
"hpds-iot-web/internal/proto"
"hpds-iot-web/model"
"hpds-iot-web/pkg/utils"
"net/http"
"xorm.io/xorm"
)
type EdgeService interface {
GetEdgeList(ctx context.Context, req proto.EdgeDatasetRequest) (rsp *proto.BaseResponse, err error)
GetEdgeInfo(ctx context.Context, req proto.EdgeDatasetRequest) (rsp *proto.BaseResponse, err error)
}
func NewEdgeService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) EdgeService {
return &repo{
AppConfig: cfg,
engine: engine,
logger: logger,
}
}
func (rp *repo) GetEdgeList(ctx context.Context, req proto.EdgeDatasetRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var (
h bool
)
node := new(model.Node)
h, err = rp.engine.ID(req.NodeId).Get(node)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("设备未在线")
goto ReturnPoint
}
param := make(map[string]interface{})
param["path"] = req.Path
param["page"] = int(req.Page)
param["pageSize"] = int(req.Size)
header := make(map[string]string)
header["Content-Type"] = "application/json"
res, err := utils.HttpDo(fmt.Sprintf("http://%s:8099/api/directory/list", node.LocalIP), "POST", param, header)
if err != nil {
goto ReturnPoint
}
err = json.Unmarshal(res, &rsp)
if err != nil {
goto ReturnPoint
}
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}
func (rp *repo) GetEdgeInfo(ctx context.Context, req proto.EdgeDatasetRequest) (rsp *proto.BaseResponse, err error) {
rsp = new(proto.BaseResponse)
select {
case <-ctx.Done():
err = fmt.Errorf("超时/取消")
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Message = "超时/取消"
rsp.Err = ctx.Err()
return rsp, ctx.Err()
default:
var (
h bool
)
node := new(model.Node)
h, err = rp.engine.ID(req.NodeId).Get(node)
if err != nil {
goto ReturnPoint
}
if !h {
err = fmt.Errorf("设备未在线")
goto ReturnPoint
}
param := make(map[string]interface{})
param["path"] = req.Path
header := make(map[string]string)
header["Content-Type"] = "application/json"
res, err := utils.HttpDo(fmt.Sprintf("http://%s:8099/api/directory/info", node.LocalIP), "POST", param, header)
if err != nil {
goto ReturnPoint
}
err = json.Unmarshal(res, &rsp)
if err != nil {
goto ReturnPoint
}
return rsp, err
}
ReturnPoint:
if err != nil {
rsp.Code = http.StatusInternalServerError
rsp.Status = http.StatusText(http.StatusInternalServerError)
rsp.Err = err
rsp.Message = "失败"
}
return rsp, err
}