package service import ( "context" "encoding/json" "fmt" "git.hpds.cc/Component/logging" "hpds-iot-web/config" "hpds-iot-web/internal/proto" "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: param := make(map[string]string) param["path"] = req.Path header := make(map[string]string) header["Content-Type"] = "application/json" res, err := utils.HttpDo("http://192.168.22.151:8099/api/directory/list", "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: param := make(map[string]string) param["path"] = req.Path header := make(map[string]string) header["Content-Type"] = "application/json" res, err := utils.HttpDo("http://192.168.22.151:8099/api/directory/info", "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 }