336 lines
11 KiB
Go
336 lines
11 KiB
Go
package equipment
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/tiger1103/gfast/v3/api/hwy/client"
|
|
"github.com/tiger1103/gfast/v3/api/hwy/sign"
|
|
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
|
"github.com/tiger1103/gfast/v3/library/liberr"
|
|
)
|
|
|
|
type EquipmentApi struct{}
|
|
|
|
// 从数据库中获取逆变器列表
|
|
type EquipmentListQueryReq struct {
|
|
g.Meta `path:"/equipment/query" method:"get" tags:"禾望云相关" summary:"从数据库中获取逆变器列表【实时数据不一样准确】"`
|
|
PageNum int `json:"pageNum" dc:"分页数" v:"required"`
|
|
PageSize int `json:"pageSize" dc:"分页大小" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) GetEquipmentListFromDB(ctx context.Context, req *EquipmentListQueryReq) (res *EquipmentListQueryRes, err error) {
|
|
res = new(EquipmentListQueryRes)
|
|
err = g.Try(ctx, func(ctx context.Context) {
|
|
// 从数据库中查询逆变器列表
|
|
m := dao.Equipment.Ctx(ctx).WithAll()
|
|
|
|
res.Total, err = m.Count()
|
|
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
|
|
|
if req.PageNum == 0 {
|
|
req.PageNum = 1
|
|
}
|
|
|
|
res.CurrentPage = req.PageNum
|
|
if req.PageSize == 0 {
|
|
req.PageSize = 10
|
|
}
|
|
|
|
err = m.Page(req.PageNum, req.PageSize).Scan(&res.List)
|
|
liberr.ErrIsNil(ctx, err, "获取逆变器列表失败")
|
|
})
|
|
|
|
return res, err
|
|
}
|
|
|
|
// 禾望云逆变器列表请求
|
|
type EquipmentListReq struct {
|
|
g.Meta `path:"/equipment/list" method:"get" tags:"禾望云相关" summary:"从平台获取逆变器列表【实时数据准确】"`
|
|
PageNum int `json:"pageNum" dc:"分页数" v:"required"`
|
|
PageSize int `json:"pageSize" dc:"分页大小" v:"required"`
|
|
PlantId int `json:"plantId" dc:"电站ID" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) GetEquipmentListAndSave(ctx context.Context, req *EquipmentListReq) (res *EquipmentListRes, err error) {
|
|
res = new(EquipmentListRes)
|
|
data := make(map[string]string)
|
|
data["pageIndex"] = strconv.Itoa(req.PageNum)
|
|
data["pageSize"] = strconv.Itoa(req.PageSize)
|
|
data["plantId"] = strconv.Itoa(req.PlantId)
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentListByPlantId", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
// 处理响应并存储数据
|
|
model := g.Model("equipment")
|
|
for _, record := range res.Result.Records {
|
|
_, err = model.Data(map[string]interface{}{
|
|
"plantId": req.PlantId,
|
|
"divertorId": record.DivertorId,
|
|
"divertorName": record.DivertorName,
|
|
"equipmentName": record.EquipmentName,
|
|
"equipmentPn": record.EquipmentPn,
|
|
"equipmentSn": record.EquipmentSn,
|
|
"id": record.Id,
|
|
"kwp": record.Kwp,
|
|
"monKwh": record.MonKwh,
|
|
"nowKw": record.NowKw,
|
|
"paramDcacCode": record.ParamDcacCode,
|
|
"paramDcdcCode": record.ParamDcdcCode,
|
|
"powerPlantId": record.PowerPlantId, // 确保这个字段已经是字符串,或者在数据库中正确处理类型转换
|
|
"powerPlantName": record.PowerPlantName,
|
|
"ratedPower": record.RatedPower,
|
|
"softDcacCode": record.SoftDcacCode,
|
|
"softDcdcCode": record.SoftDcdcCode,
|
|
"status": record.Status,
|
|
"sumKwh": record.SumKwh,
|
|
"todayKwh": record.TodayKwh,
|
|
"updateTime": record.UpdateTime, // 确保转换为适合数据库的日期时间格式
|
|
"userId": record.UserId,
|
|
"userName": record.UserName,
|
|
"yearKwh": record.YearKwh,
|
|
}).Save()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器详情
|
|
type EquipmentDetailReq struct {
|
|
g.Meta `path:"/equipment/detail" method:"get" tags:"禾望云相关" summary:"获取逆变器详情"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentDetail(ctx context.Context, req *EquipmentDetailReq) (res *EquipmentDetailRes, err error) {
|
|
res = new(EquipmentDetailRes)
|
|
data := make(map[string]string)
|
|
data["id"] = req.Id
|
|
data["sn"] = req.Sn
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentDetail", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
err = json.Unmarshal(bytes, &res)
|
|
|
|
var Address struct {
|
|
PlantAddress string `json:"plantAddress"`
|
|
}
|
|
|
|
name := res.Result.PowerPlantName
|
|
g.Model("plant").Fields("address AS plantAddress").Where("name = ?", name).Scan(&Address)
|
|
|
|
res.PlantAddress = Address.PlantAddress
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器实时数据
|
|
type EquipmentRealDataReq struct {
|
|
g.Meta `path:"/equipment/real/data" method:"get" tags:"禾望云相关" summary:"获取逆变器实时数据"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentRealData(ctx context.Context, req *EquipmentRealDataReq) (res *EquipmentRealDataRes, err error) {
|
|
res = new(EquipmentRealDataRes)
|
|
data := make(map[string]string)
|
|
data["id"] = req.Id
|
|
data["sn"] = req.Sn
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentData", data)
|
|
g.Dump(string(bytes))
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器历史数据
|
|
type EquipmentHistoryReq struct {
|
|
g.Meta `path:"/equipment/history" method:"get" tags:"禾望云相关" summary:"获取逆变器历史数据"`
|
|
Time string `json:"time" dc:"日期字符 yyyy-MM-dd" v:"required"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentHistory(ctx context.Context, req *EquipmentHistoryReq) (*EquipmentHistoryRes, error) {
|
|
res := new(EquipmentHistoryRes)
|
|
data := map[string]string{
|
|
"id": req.Id,
|
|
"sn": req.Sn,
|
|
"time": req.Time,
|
|
}
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentHistoryData", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
err = json.Unmarshal(bytes, &res)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
requiredKeys := map[string]bool{
|
|
"ACActivePower": true,
|
|
"ACReactivePower": true,
|
|
"DCInputPower": true,
|
|
"internalTemperature": true,
|
|
"gridFrequency": true,
|
|
"dayElectricity": true,
|
|
"inverterEfficiency": true,
|
|
}
|
|
|
|
for i := range res.Result {
|
|
var filteredParams []struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
Unit string `json:"unit"`
|
|
Value interface{} `json:"value"`
|
|
}
|
|
for _, param := range res.Result[i].ParamList {
|
|
if _, ok := requiredKeys[param.Key]; ok {
|
|
filteredParams = append(filteredParams, param)
|
|
}
|
|
}
|
|
res.Result[i].ParamList = filteredParams
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器日统计数据
|
|
type EquipmentStaticDayReq struct {
|
|
g.Meta `path:"/equipment/static/day" method:"get" tags:"禾望云相关" summary:"获取逆变器日统计数据"`
|
|
StartTime string `json:"startTime" dc:"开始日期字符 yyyy-MM-dd" v:"required"`
|
|
EndTime string `json:"endTime" dc:"开始日期字符 yyyy-MM-dd" v:"required"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentStaticDay(ctx context.Context, req *EquipmentStaticDayReq) (res *EquipmentStaticDayRes, err error) {
|
|
res = new(EquipmentStaticDayRes)
|
|
data := make(map[string]string)
|
|
data["id"] = req.Id
|
|
data["sn"] = req.Sn
|
|
data["startTime"] = req.StartTime
|
|
data["endTime"] = req.EndTime
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentDayStatisticsData", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器月统计数据
|
|
type EquipmentStaticMonthReq struct {
|
|
g.Meta `path:"/equipment/static/month" method:"get" tags:"禾望云相关" summary:"获取逆变器月统计数据"`
|
|
StartTime string `json:"startTime" dc:"开始日期字符 yyyy-MM" v:"required"`
|
|
EndTime string `json:"endTime" dc:"开始日期字符 yyyy-MM" v:"required"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentStaticMonth(ctx context.Context, req *EquipmentStaticMonthReq) (res *EquipmentStaticMonthRes, err error) {
|
|
res = new(EquipmentStaticMonthRes)
|
|
data := make(map[string]string)
|
|
data["id"] = req.Id
|
|
data["sn"] = req.Sn
|
|
data["startTime"] = req.StartTime
|
|
data["endTime"] = req.EndTime
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentMonthStatisticsData", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器年统计数据
|
|
type EquipmentStaticYearReq struct {
|
|
g.Meta `path:"/equipment/static/year" method:"get" tags:"禾望云相关" summary:"获取逆变器年统计数据"`
|
|
StartTime string `json:"startTime" dc:"开始日期字符 yyyy" v:"required"`
|
|
EndTime string `json:"endTime" dc:"开始日期字符 yyyy" v:"required"`
|
|
Id string `json:"id" dc:"逆变器ID" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentStaticYear(ctx context.Context, req *EquipmentStaticYearReq) (res *EquipmentStaticYearRes, err error) {
|
|
res = new(EquipmentStaticYearRes)
|
|
data := make(map[string]string)
|
|
data["id"] = req.Id
|
|
data["sn"] = req.Sn
|
|
data["startTime"] = req.StartTime
|
|
data["endTime"] = req.EndTime
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentYearStatisticsData", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器报警列表数据
|
|
type EquipmentAlarmListReq struct {
|
|
g.Meta `path:"/equipment/alarm/list" method:"get" tags:"禾望云相关" summary:"获取逆变器报警数据列表"`
|
|
StartTime string `json:"startTime" dc:"开始日期字符 yyyy-MM-dd" v:"required"`
|
|
EndTime string `json:"endTime" dc:"开始日期字符 yyyy-MM-dd" v:"required"`
|
|
Pn string `json:"pn" dc:"逆变器PN" v:"required"`
|
|
Sn string `json:"sn" dc:"逆变器SN" v:"required"`
|
|
}
|
|
|
|
func (e EquipmentApi) EquipmentAlarmList(ctx context.Context, req *EquipmentAlarmListReq) (res *EquipmentAlarmListRes, err error) {
|
|
res = new(EquipmentAlarmListRes)
|
|
data := make(map[string]string)
|
|
data["pn"] = req.Pn
|
|
data["sn"] = req.Sn
|
|
data["startTime"] = req.StartTime
|
|
data["endTime"] = req.EndTime
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Get("/openApi/equipment/getEquipmentFaultData", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|
|
|
|
// 获取逆变器报警相关数据
|
|
type EquipmentAlarmDetailDataReq struct {
|
|
g.Meta `path:"/equipment/alarm/detail" method:"get" tags:"禾望云相关" summary:"获取逆变器报警数据详情"`
|
|
AlarmId string `json:"alarmId" dc:"报警ID" v:"required"`
|
|
}
|
|
|
|
// 获取设备报警详情数据
|
|
func (e EquipmentApi) EquipmentAlarmDetailData(ctx context.Context, req *EquipmentAlarmDetailDataReq) (res *EquipmentAlarmDetailDataRes, err error) {
|
|
res = new(EquipmentAlarmDetailDataRes)
|
|
data := make(map[string]string)
|
|
data["alarmId"] = req.AlarmId
|
|
sign.SignByRSA(data)
|
|
err, bytes := client.Post("openApi/equipment/getAlarmDetailDataById", data)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = json.Unmarshal(bytes, &res)
|
|
return res, nil
|
|
}
|